Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.admin.DeviceAdminReceiver;
- import android.app.admin.DevicePolicyManager;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.hardware.Camera;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.ResultReceiver;
- import android.preference.PreferenceManager;
- import android.util.Log;
- import android.widget.Toast;
- import bd.org.rain.lostphonefinder.util.GmailSender;
- import static android.content.ContentValues.TAG;
- import static android.content.Context.MODE_PRIVATE;
- public class AdminReceiver extends DeviceAdminReceiver {
- private boolean mRecording;
- private boolean mHandlingEvent;
- public static String getPreference(Context context, String key) {
- String sharedPrefFile = "bd.org.rain.lostphonefinder";
- SharedPreferences prefs = context.getSharedPreferences(sharedPrefFile, MODE_PRIVATE);
- return prefs.getString(key, null);
- }
- Context applicationContext = ActivityMain.getContextOfApplication();
- SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
- String recipient = mPreferences.getString("email", "null");
- String sharedLocation = mPreferences.getString("sharedLocation", "null");
- // String recipient = mPreferences.getString(getString(R.string.str_email_recipient), "null");
- String myEmailString, passString, sendToEmailString, subjectString, textBody;
- @Override
- public void onEnabled(Context ctxt, Intent intent) {
- ComponentName cn=new ComponentName(ctxt, AdminReceiver.class);
- DevicePolicyManager mgr=
- (DevicePolicyManager)ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
- mgr.setPasswordQuality(cn,
- DevicePolicyManager.PASSWORD_QUALITY_NUMERIC);
- //password attempt delay didn't work here
- onPasswordChanged(ctxt, intent);
- }
- @Override
- public void onPasswordChanged(Context ctxt, Intent intent) {
- DevicePolicyManager mgr=
- (DevicePolicyManager)ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
- int msgId;
- if (mgr.isActivePasswordSufficient()) {
- msgId=R.string.compliant;
- }
- else {
- msgId=R.string.not_compliant;
- }
- // Toast.makeText(ctxt, msgId, Toast.LENGTH_LONG).show();
- }
- @Override
- public void onPasswordFailed(Context ctxt, Intent intent) {
- DevicePolicyManager policyManager = (DevicePolicyManager)ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
- if(policyManager != null){
- int attempts = policyManager.getCurrentFailedPasswordAttempts();
- Log.d(TAG, "Failed attempts = ====================================== " + attempts);
- if (attempts >= 3) {
- //actions
- }
- String lat = getPreference(applicationContext, applicationContext.getString(R.string.str_lat));
- String lng = getPreference(applicationContext, applicationContext.getString(R.string.str_lng));
- String time = getPreference(applicationContext, applicationContext.getString(R.string.str_time));
- startRecording();
- final Handler handler = new Handler();
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- // Do something after 5s = 5000ms
- stopRecording();
- }
- }, 5000);
- /**/
- // SEND MAIL
- final SendEmailTask sendEmailTask = new SendEmailTask();
- myEmailString = "my.lost.phone.finder@gmail.com";
- passString = "123456abc@";
- recipient = recipient;
- // sendToEmailString = recipient;
- subjectString = "Have you lost your phone?";
- textBody = "Dear User!" +
- "\nWe found some fishy activities with your phone. Hope you have your phone." +
- "\n\nNumber of failed password attempts: " + attempts +
- "\n\nLocation:\n" +
- "\n\nLocation on map: " + sharedLocation +
- "\n\nLat: " + lat +
- "\nLng: " + lng +
- "\nTime: " +time +
- "\n\nBest wishes,\nFindDevice Team";
- sendEmailTask.execute();
- //
- }else{
- Log.d(TAG, "didnt get attempt ====================================== ");
- }
- /*// SEND MAIL
- final SendEmailTask sendEmailTask = new SendEmailTask();
- myEmailString = "my.lost.phone.finder@gmail.com";
- passString = "123456abc@";
- sendToEmailString = "nabilatajrin@gmail.com";
- subjectString = "Have you lost your phone?ff";
- 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";
- sendEmailTask.execute();
- //*/
- Log.d(TAG, "onPasswordFailed: FAILED ======================================");
- }
- @Override
- public void onPasswordSucceeded(Context ctxt, Intent intent) {
- /*Toast.makeText(ctxt, R.string.password_success, Toast.LENGTH_LONG)
- .show();*/
- Log.d(TAG, "onPasswordSuccess: SUCCESS ====================================");
- //password attempt delay didn't work here
- }
- ///////////////////////////////////
- private void startRecording() {
- if (!mHandlingEvent) {
- mHandlingEvent = true;
- ResultReceiver receiver = new ResultReceiver(new Handler()) {
- @Override
- protected void onReceiveResult(int resultCode, Bundle resultData) {
- handleStartRecordingResult(resultCode, resultData);
- mHandlingEvent = false;
- }
- };
- CameraService.startToStartRecording(this,
- Camera.CameraInfo.CAMERA_FACING_FRONT, receiver);
- }
- }
- private void stopRecording() {
- if (!mHandlingEvent) {
- mHandlingEvent = true;
- ResultReceiver receiver = new ResultReceiver(new Handler()) {
- @Override
- protected void onReceiveResult(int resultCode, Bundle resultData) {
- handleStopRecordingResult(resultCode, resultData);
- mHandlingEvent = false;
- }
- };
- CameraService.startToStopRecording(this, receiver);
- }
- }
- private void handleStartRecordingResult(int resultCode, Bundle resultData) {
- if (resultCode == CameraService.RECORD_RESULT_OK) {
- // Toast.makeText(this, "Start recording...", Toast.LENGTH_SHORT).show();
- } else {
- // Toast.makeText(this, "Start recording failed...", Toast.LENGTH_SHORT).show();
- }
- }
- private void handleStopRecordingResult(int resultCode, Bundle resultData) {
- if (resultCode == CameraService.RECORD_RESULT_OK) {
- String videoPath = resultData.getString(CameraService.VIDEO_PATH);
- // Toast.makeText(this, "Record succeed, file saved in " + videoPath,
- // Toast.LENGTH_LONG).show();
- } else if (resultCode == CameraService.RECORD_RESULT_UNSTOPPABLE) {
- // Toast.makeText(this, "Stop recording failed...", Toast.LENGTH_SHORT).show();
- } else {
- // Toast.makeText(this, "Recording failed...", Toast.LENGTH_SHORT).show();
- }
- }
- ///////////////////////////////////
- //class to send email
- class SendEmailTask extends AsyncTask<Void, Void, Void> {
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- Log.i("Email sending", "sending start");
- }
- @Override
- protected Void doInBackground(Void... params) {
- try {
- GmailSender sender = new GmailSender(myEmailString, passString);
- //subject, body, sender, to
- sender.sendMail(subjectString,
- textBody,
- myEmailString,
- recipient);
- Log.i("Email sending", "send");
- } catch (Exception e) {
- Log.i("Email sending", "cannot send");
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPostExecute(Void result) {
- super.onPostExecute(result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement