Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.36 KB | None | 0 0
  1. public class TService extends Service {
  2. MediaRecorder recorder;
  3. File audiofile;
  4. String name, phonenumber;
  5. String audio_format;
  6. public String Audio_Type;
  7. int audioSource;
  8. Context context;
  9. private Handler handler;
  10. Timer timer;
  11. Boolean offHook = false, ringing = false;
  12. Toast toast;
  13. Boolean isOffHook = false;
  14. private boolean recordstarted = false;
  15.  
  16. private static final String ACTION_IN = "android.intent.action.PHONE_STATE";
  17. private static final String ACTION_OUT = "android.intent.action.NEW_OUTGOING_CALL";
  18. private CallBr br_call;
  19.  
  20.  
  21.  
  22.  
  23. @Override
  24. public IBinder onBind(Intent arg0) {
  25. // TODO Auto-generated method stub
  26. return null;
  27. }
  28.  
  29. @Override
  30. public void onDestroy() {
  31. Log.d("service", "destroy");
  32.  
  33. super.onDestroy();
  34. }
  35.  
  36. @Override
  37. public int onStartCommand(Intent intent, int flags, int startId) {
  38. // final String terminate =(String)
  39. // intent.getExtras().get("terminate");//
  40. // intent.getStringExtra("terminate");
  41. // Log.d("TAG", "service started");
  42. //
  43. // TelephonyManager telephony = (TelephonyManager)
  44. // getSystemService(Context.TELEPHONY_SERVICE); // TelephonyManager
  45. // // object
  46. // CustomPhoneStateListener customPhoneListener = new
  47. // CustomPhoneStateListener();
  48. // telephony.listen(customPhoneListener,
  49. // PhoneStateListener.LISTEN_CALL_STATE);
  50. // context = getApplicationContext();
  51.  
  52. final IntentFilter filter = new IntentFilter();
  53. filter.addAction(ACTION_OUT);
  54. filter.addAction(ACTION_IN);
  55. this.br_call = new CallBr();
  56. this.registerReceiver(this.br_call, filter);
  57.  
  58. // if(terminate != null) {
  59. // stopSelf();
  60. // }
  61. return START_NOT_STICKY;
  62. }
  63.  
  64. public class CallBr extends BroadcastReceiver {
  65. Bundle bundle;
  66. String state;
  67. String inCall, outCall;
  68. public boolean wasRinging = false;
  69.  
  70. @Override
  71. public void onReceive(Context context, Intent intent) {
  72.  
  73. if (intent.getAction().equals(ACTION_IN)) {
  74. if ((bundle = intent.getExtras()) != null) {
  75. state = bundle.getString(TelephonyManager.EXTRA_STATE);
  76. if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
  77. inCall = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
  78. wasRinging = true;
  79. Toast.makeText(context, "IN : " + inCall, Toast.LENGTH_LONG).show();
  80. } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
  81. if (wasRinging == true) {
  82.  
  83. Toast.makeText(context, "ANSWERED", Toast.LENGTH_LONG).show();
  84.  
  85. String out = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss").format(new Date());
  86. File sampleDir = new File(Environment.getExternalStorageDirectory(), "/TestRecordingDasa");
  87. if (!sampleDir.exists()) {
  88. sampleDir.mkdirs();
  89. }
  90. Log.d("TService", "onReceive: "+sampleDir);
  91. String file_name = "Outgoing";
  92. try {
  93. audiofile = File.createTempFile(file_name, ".wav", sampleDir);
  94. } catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. String path = Environment.getExternalStorageDirectory().getAbsolutePath();
  98.  
  99. recorder = new MediaRecorder();
  100. // recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
  101. // recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  102.  
  103. recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  104. recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
  105. recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
  106. recorder.setOutputFile(audiofile.getAbsolutePath());
  107. try {
  108. recorder.prepare();
  109. } catch (IllegalStateException e) {
  110. e.printStackTrace();
  111. } catch (IOException e) {
  112. e.printStackTrace();
  113. }
  114. recorder.start();
  115. recordstarted = true;
  116. }
  117. } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
  118. wasRinging = false;
  119. Toast.makeText(context, "REJECT || DISCO", Toast.LENGTH_LONG).show();
  120. if (recordstarted) {
  121. recorder.stop();
  122. recordstarted = false;
  123. }
  124. }
  125. }
  126. } else if (intent.getAction().equals(ACTION_OUT)) {
  127. if ((bundle = intent.getExtras()) != null) {
  128. outCall = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
  129. Toast.makeText(context, "OUT : " + outCall, Toast.LENGTH_LONG).show();
  130.  
  131. File sampleDir = new File(Environment.getExternalStorageDirectory(), "/TestRecordingDasa");
  132. if (!sampleDir.exists()) {
  133. sampleDir.mkdirs();
  134. }
  135. Log.d("TService", "onReceive: "+sampleDir);
  136. String file_name = "Record";
  137. try {
  138. audiofile = File.createTempFile(file_name, ".wav", sampleDir);
  139. } catch (IOException e) {
  140. e.printStackTrace();
  141. }
  142. String path = Environment.getExternalStorageDirectory().getAbsolutePath();
  143.  
  144. recorder = new MediaRecorder();
  145. // recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
  146. // recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  147.  
  148. recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  149. recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
  150. recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
  151. recorder.setOutputFile(audiofile.getAbsolutePath());
  152. try {
  153. recorder.prepare();
  154. } catch (IllegalStateException e) {
  155. e.printStackTrace();
  156. } catch (IOException e) {
  157. e.printStackTrace();
  158. }
  159. recorder.start();
  160. recordstarted = true;
  161.  
  162. }
  163. }
  164.  
  165. LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("callRecord"));
  166. }
  167. }
  168.  
  169. }
  170.  
  171. public class DeviceAdminDemo extends DeviceAdminReceiver {
  172. @Override
  173. public void onReceive(Context context, Intent intent) {
  174. super.onReceive(context, intent);
  175. }
  176.  
  177. public void onEnabled(Context context, Intent intent) {
  178. };
  179.  
  180. public void onDisabled(Context context, Intent intent) {
  181. };
  182. }
  183.  
  184. public class MainActivity extends Activity {
  185. private static final int REQUEST_CODE = 0;
  186. private DevicePolicyManager mDPM;
  187. private ComponentName mAdminName;
  188.  
  189. private static final int PERMISSION_REQUEST_CODE= 123;
  190.  
  191. @Override
  192. protected void onCreate(Bundle savedInstanceState) {
  193. super.onCreate(savedInstanceState);
  194. setContentView(R.layout.activity_main);
  195.  
  196. if (checkPermission()){
  197. Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
  198. }else{
  199. requestPermission();
  200. }
  201.  
  202. try {
  203. // Initiate DevicePolicyManager.
  204. mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
  205. mAdminName = new ComponentName(this, DeviceAdminDemo.class);
  206.  
  207. if (!mDPM.isAdminActive(mAdminName)) {
  208. Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
  209. intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
  210. intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Click on Activate button to secure your application.");
  211. startActivityForResult(intent, REQUEST_CODE);
  212. } else {
  213. // mDPM.lockNow();
  214. // Intent intent = new Intent(MainActivity.this,
  215. // TrackDeviceService.class);
  216. // startService(intent);
  217. }
  218. } catch (Exception e) {
  219. e.printStackTrace();
  220. }
  221.  
  222. LocalBroadcastManager.getInstance(MainActivity.this).registerReceiver(receiver, new IntentFilter("callRecord"));
  223. Intent intent = new Intent(MainActivity.this, TService.class);
  224. startService(intent);
  225. }
  226.  
  227.  
  228. private BroadcastReceiver receiver = new BroadcastReceiver() {
  229. @Override
  230. public void onReceive(Context context, Intent intent) {
  231. if (intent.getAction().equalsIgnoreCase("callRecord")) {
  232. Toast.makeText(MainActivity.this, "Call record", Toast.LENGTH_SHORT).show();
  233. }
  234. }
  235. };
  236.  
  237. private boolean checkPermission(){
  238. if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
  239. return true;
  240. }else{
  241. return false;
  242. }
  243. }
  244.  
  245. private void requestPermission(){
  246.  
  247. if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION)
  248. || ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.ACCESS_COARSE_LOCATION)
  249. || ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.CALL_PHONE)
  250. || ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.SEND_SMS)
  251. || ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.READ_CONTACTS)
  252. || ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
  253. || ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
  254. || ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_PHONE_STATE)
  255. || ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.PROCESS_OUTGOING_CALLS)
  256. || ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.RECORD_AUDIO)
  257. || ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CAPTURE_AUDIO_OUTPUT)){
  258.  
  259. Toast.makeText(MainActivity.this,"Allow Us pemissions. Please allow in App Settings for additional functionality.",Toast.LENGTH_LONG).show();
  260.  
  261. } else {
  262.  
  263. ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION,
  264. Manifest.permission.CALL_PHONE, Manifest.permission.SEND_SMS, Manifest.permission.READ_CONTACTS,
  265. Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE,
  266. Manifest.permission.PROCESS_OUTGOING_CALLS, Manifest.permission.RECORD_AUDIO, Manifest.permission.CAPTURE_AUDIO_OUTPUT}, PERMISSION_REQUEST_CODE);
  267. }
  268. }
  269.  
  270. @Override
  271. public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
  272. switch (requestCode) {
  273. case PERMISSION_REQUEST_CODE:
  274. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  275.  
  276. Toast.makeText(this, "Permission Granted, ", Toast.LENGTH_SHORT).show();
  277.  
  278. } else {
  279.  
  280. Toast.makeText(this, "Permission not Granted", Toast.LENGTH_SHORT).show();
  281.  
  282. }
  283. break;
  284. }
  285. }
  286.  
  287.  
  288.  
  289.  
  290. @Override
  291. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  292. super.onActivityResult(requestCode, resultCode, data);
  293.  
  294. if (REQUEST_CODE == requestCode) {
  295. Intent intent = new Intent(MainActivity.this, TService.class);
  296. startService(intent);
  297. }
  298. }
  299.  
  300.  
  301. @Override
  302. public void onResume() {
  303. LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter("callRecord"));
  304.  
  305. super.onResume();
  306. }
  307.  
  308. @Override
  309. public void onPause() {
  310. super.onPause();
  311. LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
  312.  
  313. }
  314.  
  315.  
  316. }
  317.  
  318. <?xml version="1.0" encoding="utf-8"?>
  319. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  320. package="com.example.practice.callrecorder">
  321.  
  322. <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT"/>
  323. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  324. <uses-permission android:name="android.permission.RECORD_AUDIO" />
  325. <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
  326. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  327. <uses-permission android:name="android.permission.STORAGE" />
  328. <uses-permission android:name="android.permission.CALL_PHONE"/>
  329. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  330. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  331. <uses-permission android:name="android.permission.SEND_SMS"/>
  332. <uses-permission android:name="android.permission.READ_CONTACTS"/>
  333.  
  334.  
  335.  
  336. <application
  337. android:allowBackup="true"
  338. android:icon="@mipmap/ic_launcher"
  339. android:label="@string/app_name"
  340. android:roundIcon="@mipmap/ic_launcher_round"
  341. android:supportsRtl="true"
  342. android:theme="@style/AppTheme">
  343. <activity android:name=".MainActivity">
  344. <intent-filter>
  345. <action android:name="android.intent.action.MAIN" />
  346.  
  347. <category android:name="android.intent.category.LAUNCHER" />
  348. </intent-filter>
  349. </activity>
  350.  
  351.  
  352. <receiver
  353. android:name=".DeviceAdminDemo"
  354. android:description="@string/app_name"
  355. android:label="@string/app_name"
  356. android:permission="android.permission.BIND_DEVICE_ADMIN">
  357.  
  358. <meta-data
  359. android:name="android.app.device_admin"
  360. android:resource="@xml/my_admin" />
  361.  
  362.  
  363. <intent-filter>
  364. <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
  365. <action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
  366. <action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" />
  367.  
  368. </intent-filter>
  369. </receiver>
  370.  
  371. <service
  372. android:name=".TService"
  373. android:enabled="true"
  374. android:exported="true">
  375.  
  376. </service>
  377. </application>
  378.  
  379. <device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
  380. <uses-policies>
  381. <force-lock />
  382. </uses-policies>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement