Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. String MANUFACTURER_SAMSUNG = "SAMSUNG";
  4.  
  5.  
  6.  
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. }
  12. }
  13.  
  14. public class PhoneStateReceiver extends BroadcastReceiver {
  15.  
  16. public void onReceive(Context context, Intent intent) {
  17.  
  18. Log.d("in","phone state onreceive");
  19. try {
  20. System.out.println("Receiver start");
  21. String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
  22. String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
  23. if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
  24. Toast.makeText(context, "Incoming Call State", Toast.LENGTH_SHORT).show();
  25. Toast.makeText(context, "Ringing State Number is -" + incomingNumber, Toast.LENGTH_SHORT).show();
  26. Log.d("after","toasts");
  27. MediaSessionManager mediaSessionManager = (MediaSessionManager) context.getSystemService(Context.MEDIA_SESSION_SERVICE);
  28. Log.d("before","tryblock");
  29. try {
  30. List<MediaController> mediaControllerList = mediaSessionManager.getActiveSessions(new ComponentName(context, NotificationReceiverService.class));
  31. Log.d("medialistempty",mediaControllerList.isEmpty()+"");
  32. for (MediaController m : mediaControllerList) {
  33. Log.d("packagename:",m.getPackageName());
  34. if ("com.android.server.telecom".equals(m.getPackageName())) {
  35. m.dispatchMediaButtonEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
  36. Log.d("HEADSETHOOK sent", "to telecom server");
  37. break;
  38. }
  39. }
  40. } catch (SecurityException e) {
  41. Log.d("Permission error.","Access to notification not granted to the app.");
  42. Log.d("exc",e.toString());
  43. }
  44. TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  45.  
  46. try {
  47. if (tm == null) {
  48. // this will be easier for debugging later on
  49. throw new NullPointerException("tm == null");
  50. }
  51. // do reflection magic
  52. tm.getClass().getMethod("answerRingingCall").invoke(tm);
  53. } catch (Exception e) {}
  54. }
  55. if ((state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK))) {
  56. Toast.makeText(context, "Call Received State", Toast.LENGTH_SHORT).show();
  57. try {
  58. Intent callIntent = new Intent(Intent.ACTION_CALL);
  59. callIntent.setData(Uri.parse("tel:" + "611,,,1"));
  60. context.startActivity(callIntent);
  61. }catch(SecurityException e) {
  62. Log.d("myexcpetion","Security exception");
  63. }
  64. }
  65. if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
  66. Toast.makeText(context, "Call Idle State", Toast.LENGTH_SHORT).show();
  67. }
  68. }
  69. catch(Exception e){
  70. e.printStackTrace();
  71. }
  72. }
  73. }
  74.  
  75. public class NotificationReceiverService extends NotificationListenerService{
  76.  
  77. PhoneStateReceiver psr;
  78. public void onCreate(){
  79. super.onCreate();
  80. psr = new PhoneStateReceiver();
  81. IntentFilter filter = new IntentFilter();
  82. filter.addAction("diverse.technologies.autocallreceiver.NOTIFICATION_LISTENER_SERVICE_EXAMPLE");
  83. registerReceiver(psr,filter);
  84. }
  85. public void onDestroy(){
  86. super.onDestroy();
  87. unregisterReceiver(psr);
  88. }
  89.  
  90. public NotificationReceiverService() {}
  91. }
  92.  
  93. <?xml version="1.0" encoding="utf-8"?>
  94. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  95. package="diverse.technologies.autocallreceiver">
  96.  
  97. <uses-permission android:name="android.permission.WAKE_LOCK" />
  98. <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
  99. <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
  100. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  101. <uses-permission android:name="android.permission.CALL_PHONE" />
  102. <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/>
  103.  
  104.  
  105. <application
  106. android:allowBackup="true"
  107. android:icon="@mipmap/ic_launcher"
  108. android:label="@string/app_name"
  109. android:roundIcon="@mipmap/ic_launcher_round"
  110. android:supportsRtl="true"
  111. android:theme="@style/AppTheme">
  112. <activity android:name=".MainActivity">
  113. <intent-filter android:priority="100" >
  114. <action android:name="android.intent.action.MAIN" />
  115. <category android:name="android.intent.category.LAUNCHER" />
  116. <action android:name="android.intent.action.MAIN" />
  117. <action android:name="android.intent.action.DIAL" />
  118. <action android:name="android.intent.action.CALL_PRIVILEGED" />
  119. <data android:scheme="tel" />
  120.  
  121. </intent-filter>
  122. </activity>
  123.  
  124. <receiver android:name=".PhoneStateReceiver">
  125. <intent-filter>
  126. <action android:name="android.intent.action.PHONE_STATE" />
  127. </intent-filter>
  128. </receiver>
  129.  
  130. <service android:name=".NotificationReceiverService" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
  131. android:enabled="true" android:exported="true" >
  132. <intent-filter>
  133. <action android:name="android.service.notification.NotificationListenerService" />
  134. </intent-filter>
  135. </service>
  136.  
  137. </application>
  138.  
  139. </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement