Advertisement
Guest User

Untitled

a guest
Oct 25th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 KB | None | 0 0
  1.  
  2.  
  3. import android.app.admin.DeviceAdminReceiver;
  4. import android.app.admin.DevicePolicyManager;
  5. import android.content.ComponentName;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.hardware.Camera;
  10. import android.os.AsyncTask;
  11. import android.os.Bundle;
  12. import android.os.Handler;
  13. import android.os.ResultReceiver;
  14. import android.preference.PreferenceManager;
  15. import android.util.Log;
  16. import android.widget.Toast;
  17.  
  18. import bd.org.rain.lostphonefinder.util.GmailSender;
  19. import static android.content.ContentValues.TAG;
  20. import static android.content.Context.MODE_PRIVATE;
  21.  
  22.  
  23. public class AdminReceiver extends DeviceAdminReceiver {
  24.  
  25. private boolean mRecording;
  26. private boolean mHandlingEvent;
  27.  
  28. public static String getPreference(Context context, String key) {
  29. String sharedPrefFile = "bd.org.rain.lostphonefinder";
  30. SharedPreferences prefs = context.getSharedPreferences(sharedPrefFile, MODE_PRIVATE);
  31. return prefs.getString(key, null);
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38. Context applicationContext = ActivityMain.getContextOfApplication();
  39. SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
  40. String recipient = mPreferences.getString("email", "null");
  41. String sharedLocation = mPreferences.getString("sharedLocation", "null");
  42. // String recipient = mPreferences.getString(getString(R.string.str_email_recipient), "null");
  43.  
  44.  
  45.  
  46. String myEmailString, passString, sendToEmailString, subjectString, textBody;
  47.  
  48.  
  49. @Override
  50. public void onEnabled(Context ctxt, Intent intent) {
  51. ComponentName cn=new ComponentName(ctxt, AdminReceiver.class);
  52. DevicePolicyManager mgr=
  53. (DevicePolicyManager)ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
  54.  
  55. mgr.setPasswordQuality(cn,
  56. DevicePolicyManager.PASSWORD_QUALITY_NUMERIC);
  57.  
  58. //password attempt delay didn't work here
  59.  
  60. onPasswordChanged(ctxt, intent);
  61. }
  62.  
  63.  
  64. @Override
  65. public void onPasswordChanged(Context ctxt, Intent intent) {
  66. DevicePolicyManager mgr=
  67. (DevicePolicyManager)ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
  68. int msgId;
  69.  
  70. if (mgr.isActivePasswordSufficient()) {
  71. msgId=R.string.compliant;
  72. }
  73. else {
  74. msgId=R.string.not_compliant;
  75. }
  76.  
  77. // Toast.makeText(ctxt, msgId, Toast.LENGTH_LONG).show();
  78. }
  79.  
  80.  
  81. @Override
  82. public void onPasswordFailed(Context ctxt, Intent intent) {
  83.  
  84. DevicePolicyManager policyManager = (DevicePolicyManager)ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
  85.  
  86.  
  87. if(policyManager != null){
  88. int attempts = policyManager.getCurrentFailedPasswordAttempts();
  89.  
  90. Log.d(TAG, "Failed attempts = ====================================== " + attempts);
  91.  
  92. if (attempts >= 3) {
  93. //actions
  94. }
  95.  
  96.  
  97. String lat = getPreference(applicationContext, applicationContext.getString(R.string.str_lat));
  98. String lng = getPreference(applicationContext, applicationContext.getString(R.string.str_lng));
  99. String time = getPreference(applicationContext, applicationContext.getString(R.string.str_time));
  100.  
  101.  
  102. startRecording();
  103.  
  104. final Handler handler = new Handler();
  105. handler.postDelayed(new Runnable() {
  106. @Override
  107. public void run() {
  108. // Do something after 5s = 5000ms
  109. stopRecording();
  110. }
  111. }, 5000);
  112.  
  113.  
  114. /**/
  115.  
  116. // SEND MAIL
  117. final SendEmailTask sendEmailTask = new SendEmailTask();
  118. myEmailString = "my.lost.phone.finder@gmail.com";
  119. passString = "123456abc@";
  120. recipient = recipient;
  121. // sendToEmailString = recipient;
  122. subjectString = "Have you lost your phone?";
  123.  
  124.  
  125. textBody = "Dear User!" +
  126. "\nWe found some fishy activities with your phone. Hope you have your phone." +
  127. "\n\nNumber of failed password attempts: " + attempts +
  128. "\n\nLocation:\n" +
  129. "\n\nLocation on map: " + sharedLocation +
  130. "\n\nLat: " + lat +
  131. "\nLng: " + lng +
  132. "\nTime: " +time +
  133. "\n\nBest wishes,\nFindDevice Team";
  134.  
  135.  
  136. sendEmailTask.execute();
  137. //
  138.  
  139. }else{
  140. Log.d(TAG, "didnt get attempt ====================================== ");
  141. }
  142.  
  143. /*// SEND MAIL
  144. final SendEmailTask sendEmailTask = new SendEmailTask();
  145. myEmailString = "my.lost.phone.finder@gmail.com";
  146. passString = "123456abc@";
  147. sendToEmailString = "nabilatajrin@gmail.com";
  148. subjectString = "Have you lost your phone?ff";
  149. textString = "Dear User!\nSome fishy activities happen with your phone.\nHope you have your phone.\n\nNumber of failed password attempts: " + attempts + "\n\nBest wishes\nFindDevice Team";
  150.  
  151. sendEmailTask.execute();
  152. //*/
  153.  
  154. Log.d(TAG, "onPasswordFailed: FAILED ======================================");
  155. }
  156.  
  157.  
  158. @Override
  159. public void onPasswordSucceeded(Context ctxt, Intent intent) {
  160. /*Toast.makeText(ctxt, R.string.password_success, Toast.LENGTH_LONG)
  161. .show();*/
  162.  
  163.  
  164. Log.d(TAG, "onPasswordSuccess: SUCCESS ====================================");
  165.  
  166. //password attempt delay didn't work here
  167.  
  168. }
  169.  
  170.  
  171.  
  172. ///////////////////////////////////
  173.  
  174.  
  175. private void startRecording() {
  176. if (!mHandlingEvent) {
  177. mHandlingEvent = true;
  178. ResultReceiver receiver = new ResultReceiver(new Handler()) {
  179. @Override
  180. protected void onReceiveResult(int resultCode, Bundle resultData) {
  181. handleStartRecordingResult(resultCode, resultData);
  182. mHandlingEvent = false;
  183. }
  184. };
  185.  
  186. CameraService.startToStartRecording(this,
  187. Camera.CameraInfo.CAMERA_FACING_FRONT, receiver);
  188.  
  189.  
  190. }
  191. }
  192.  
  193. private void stopRecording() {
  194. if (!mHandlingEvent) {
  195. mHandlingEvent = true;
  196. ResultReceiver receiver = new ResultReceiver(new Handler()) {
  197. @Override
  198. protected void onReceiveResult(int resultCode, Bundle resultData) {
  199. handleStopRecordingResult(resultCode, resultData);
  200. mHandlingEvent = false;
  201. }
  202. };
  203. CameraService.startToStopRecording(this, receiver);
  204. }
  205. }
  206.  
  207.  
  208. private void handleStartRecordingResult(int resultCode, Bundle resultData) {
  209. if (resultCode == CameraService.RECORD_RESULT_OK) {
  210. // Toast.makeText(this, "Start recording...", Toast.LENGTH_SHORT).show();
  211. } else {
  212. // Toast.makeText(this, "Start recording failed...", Toast.LENGTH_SHORT).show();
  213. }
  214. }
  215.  
  216. private void handleStopRecordingResult(int resultCode, Bundle resultData) {
  217. if (resultCode == CameraService.RECORD_RESULT_OK) {
  218. String videoPath = resultData.getString(CameraService.VIDEO_PATH);
  219. // Toast.makeText(this, "Record succeed, file saved in " + videoPath,
  220. // Toast.LENGTH_LONG).show();
  221. } else if (resultCode == CameraService.RECORD_RESULT_UNSTOPPABLE) {
  222. // Toast.makeText(this, "Stop recording failed...", Toast.LENGTH_SHORT).show();
  223. } else {
  224. // Toast.makeText(this, "Recording failed...", Toast.LENGTH_SHORT).show();
  225. }
  226. }
  227.  
  228. ///////////////////////////////////
  229.  
  230.  
  231.  
  232. //class to send email
  233. class SendEmailTask extends AsyncTask<Void, Void, Void> {
  234.  
  235. @Override
  236. protected void onPreExecute() {
  237. super.onPreExecute();
  238. Log.i("Email sending", "sending start");
  239. }
  240.  
  241.  
  242. @Override
  243. protected Void doInBackground(Void... params) {
  244. try {
  245. GmailSender sender = new GmailSender(myEmailString, passString);
  246. //subject, body, sender, to
  247. sender.sendMail(subjectString,
  248. textBody,
  249. myEmailString,
  250. recipient);
  251.  
  252. Log.i("Email sending", "send");
  253. } catch (Exception e) {
  254. Log.i("Email sending", "cannot send");
  255. e.printStackTrace();
  256. }
  257. return null;
  258. }
  259.  
  260.  
  261. @Override
  262. protected void onPostExecute(Void result) {
  263. super.onPostExecute(result);
  264. }
  265. }
  266.  
  267.  
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement