Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. public abstract class PhoneCallReceiver extends BroadcastReceiver {
  2. private static int lastState = TelephonyManager.CALL_STATE_IDLE;
  3. private static Date callStartTime;
  4. private static boolean isIncoming;
  5. private static String savedNumber;
  6.  
  7. @Override
  8. public void onReceive(Context context, Intent intent) {
  9. if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
  10. savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
  11. }
  12. else{
  13. String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
  14. String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
  15.  
  16.  
  17.  
  18. int state = 0;
  19. if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
  20. state = TelephonyManager.CALL_STATE_IDLE;
  21. }
  22. else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
  23. state = TelephonyManager.CALL_STATE_OFFHOOK;
  24. }
  25. else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
  26. state = TelephonyManager.CALL_STATE_RINGING;
  27. }
  28. onCallStateChanged(context, state, number);
  29. }
  30. }
  31.  
  32. protected void onIncomingCallStarted(Context ctx, String number, Date start){}
  33.  
  34. protected void onOutgoingCallStarted(Context ctx, String number, Date start){}
  35.  
  36. protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end){}
  37.  
  38. protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end){}
  39.  
  40. protected void onMissedCall(Context ctx, String number, Date start){}
  41.  
  42. public void onCallStateChanged(Context context, int state, String number) {
  43. if(lastState == state){
  44. return;
  45. }
  46. switch (state) {
  47. case TelephonyManager.CALL_STATE_RINGING:
  48. isIncoming = true;
  49. callStartTime = new Date();
  50. savedNumber = number;
  51. onIncomingCallStarted(context, number, callStartTime);
  52. break;
  53.  
  54. case TelephonyManager.CALL_STATE_OFFHOOK:
  55. if(lastState != TelephonyManager.CALL_STATE_RINGING){
  56. isIncoming = false;
  57. callStartTime = new Date();
  58. onOutgoingCallStarted(context, savedNumber, callStartTime);
  59. }
  60. break;
  61.  
  62. case TelephonyManager.CALL_STATE_IDLE:
  63. if(lastState == TelephonyManager.CALL_STATE_RINGING){
  64. onMissedCall(context, savedNumber, callStartTime);
  65. }
  66. else if(isIncoming){
  67. onIncomingCallEnded(context, savedNumber, callStartTime, new Date());
  68. }
  69. else{
  70. onOutgoingCallEnded(context, savedNumber, callStartTime, new Date());
  71. }
  72. break;
  73. }
  74. lastState = state;
  75. }
  76.  
  77. public class CallReciever extends PhoneCallReceiver {
  78. DatabaseHelper databaseHelper ;
  79. @Override
  80. protected void onIncomingCallEnded(final Context ctx, final String number, Date start, Date end) {
  81. SharedPreferences preferencesUnknown = PreferenceManager.getDefaultSharedPreferences(ctx);
  82. final SharedPreferences.Editor editorUnknown = preferencesUnknown.edit();
  83. boolean checkedUnknown = preferencesUnknown.getBoolean("checkedunknown", false);
  84. if (checkedUnknown) {
  85. boolean isUnknown = contactExists(ctx, number);
  86. if (isUnknown == false) {
  87. AlertDialog.Builder alerDialog = new AlertDialog.Builder(ctx);
  88. alerDialog.setIcon(R.drawable.calllogo);
  89. alerDialog.setTitle(number);
  90. alerDialog.setMessage("Save this number ?");
  91. alerDialog.setCancelable(false);
  92. alerDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
  93. @Override
  94. public void onClick(DialogInterface dialog, int which) {
  95. Intent intent = new Intent(ctx, CategoryPhoneActivity.class);
  96. intent.putExtra("Phone", number);
  97. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  98. ctx.startActivity(intent);
  99. }
  100. });
  101. alerDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
  102. @Override
  103. public void onClick(DialogInterface dialog, int which) {
  104. dialog.cancel();
  105. }
  106. });
  107. AlertDialog aldialog = alerDialog.create();
  108. aldialog.setCanceledOnTouchOutside(false);
  109. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  110. aldialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY); } else {
  111. aldialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
  112. }
  113. aldialog.show(); } else {
  114. Toast.makeText(ctx, "Number is already saved", Toast.LENGTH_SHORT).show(); }
  115. }else{
  116. AlertDialog.Builder alerDialog = new AlertDialog.Builder(ctx);
  117. alerDialog.setTitle(number);
  118. alerDialog.setMessage("Save this number ?");
  119. alerDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
  120. @Override
  121. public void onClick(DialogInterface dialog, int which) {
  122. Intent intent = new Intent(ctx, CategoryPhoneActivity.class);
  123. intent.putExtra("Phone", number);
  124. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  125. ctx.startActivity(intent);
  126. }
  127. });
  128. AlertDialog aldialog = alerDialog.create();
  129. aldialog.setCanceledOnTouchOutside(false);
  130. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  131. aldialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY); } else {
  132. aldialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
  133. }
  134. aldialog.show(); }
  135. }
  136.  
  137. private void requestCallLogPermission() {
  138. if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CALL_LOG)){
  139. new AlertDialog.Builder(this).setTitle("Permission Needed")
  140. .setMessage("This permission is needed by App to work properly !")
  141. .setPositiveButton("OK", new DialogInterface.OnClickListener() {
  142. @Override
  143. public void onClick(DialogInterface dialog, int which) {
  144. ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.READ_CALL_LOG},
  145. CALL_LOG_PERMISSION);
  146. }
  147. }).setCancelable(false)
  148. .create().show();
  149. }else {
  150. ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.READ_CALL_LOG},
  151. CALL_LOG_PERMISSION);
  152. }
  153. }
  154.  
  155.  
  156. @Override
  157. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  158. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  159. switch(requestCode){
  160. case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE:
  161. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
  162.  
  163. }else {
  164. return;
  165. }
  166. case CALL_LOG_PERMISSION:
  167. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  168. Toast.makeText(this, "Call Log Permission Granted !", Toast.LENGTH_SHORT).show();
  169. }
  170. }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement