SHARE
TWEET

Untitled

a guest Aug 12th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.muzaffar.myApps.App;
  2.  
  3. import android.app.AlarmManager;
  4. import android.app.PendingIntent;
  5. import android.app.Service;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.os.AsyncTask;
  9. import android.os.IBinder;
  10. import android.os.SystemClock;
  11. import android.widget.Toast;
  12.  
  13. import com.muzaffar.myApps.Lib.PhoneInfo;
  14.  
  15. import java.util.concurrent.TimeUnit;
  16.  
  17. public class myAppsServices extends Service {
  18.  
  19.     private static final int FIRST_RUN_TIMEOUT_MILISEC = 5 * 1000;
  20.     private static final int SERVICE_STARTER_INTERVAL_MILISEC = 1 * 1000;
  21.     private static final int SERVICE_TASK_TIMEOUT_SEC = 10;
  22.     private final int REQUEST_CODE = 1;
  23.  
  24.     private AlarmManager serviceReStarterAlarmManager = null;
  25.     private MyTask asyncTask = null;
  26.  
  27.     @Override
  28.     public IBinder onBind(Intent intent) {
  29.         return null;
  30.     }
  31.  
  32.     @Override
  33.     public void onCreate() {
  34.         super.onCreate();
  35.  
  36.         // Start of timeout-autostarter for our service (watchdog)
  37.         startServiceReStarter();
  38.  
  39.         // Start performing service task
  40.         serviceTask();
  41.  
  42.         /*Toast.makeText(this, "Service Started!", Toast.LENGTH_LONG).show();*/
  43.     }
  44.  
  45.     /***
  46.      *       _____                     _
  47.      *      / ____|                   (_)
  48.      *     | (___    ___  _ __ __   __ _   ___  ___  ___
  49.      *      ___   / _ | '__|  / /| | / __|/ _ / __|
  50.      *      ____) ||  __/| |     V / | || (__|  __/__
  51.      *     |_____/  ___||_|     _/  |_| ___|___||___/
  52.      *
  53.      *      http://patorjk.com/software/taag/#p=display&h=1&v=0&c=c&f=Big&t=Shared%20Pref
  54.      */
  55.  
  56.     @Override
  57.     public int onStartCommand(Intent intent, int flags, int startId) {
  58.         Toast.makeText(getApplicationContext(), "Services Has Been Started!", Toast.LENGTH_SHORT).show();
  59.  
  60.  
  61.         return START_STICKY;
  62.     }
  63.  
  64.     /***
  65.      *      _____                 _     _  __ _  _  _  __  __
  66.      *     |  __                | |   | |/ /(_)| || ||  /  |
  67.      *     | |  | |  ___   _ __  | |_  | ' /  _ | || ||   / |  ___
  68.      *     | |  | | / _  | '_  | __| |  <  | || || || |/| | / _
  69.      *     | |__| || (_) || | | || |_  | .  | || || || |  | ||  __/
  70.      *     |_____/  ___/ |_| |_| __| |_|_|_||_||_||_|  |_| ___|
  71.      *
  72.      *
  73.      */
  74.  
  75.     private void StopPerformingServiceTask() {
  76.         asyncTask.cancel(true);
  77.     }
  78.  
  79.     @Override
  80.     public void onDestroy() {
  81.         // performs when user or system kill our service
  82.         /*Toast.makeText(getApplicationContext(),"Services Has Been Destroyed!",Toast.LENGTH_SHORT).show();*/
  83.         StopPerformingServiceTask();
  84.     }
  85.  
  86.     private void serviceTask() {
  87.         asyncTask = new MyTask();
  88.         asyncTask.execute();
  89.     }
  90.  
  91.     class MyTask extends AsyncTask<Void, Void, Void> {
  92.         @Override
  93.         protected Void doInBackground(Void... params) {
  94.             try {
  95.                 for (;;) {
  96.                     TimeUnit.SECONDS.sleep(SERVICE_TASK_TIMEOUT_SEC);
  97.  
  98.                     // check does performing of the task need
  99.                     if(isCancelled()) {
  100.                         break;
  101.                     }
  102.  
  103.                     // Initiating of onProgressUpdate callback that has access to UI
  104.                     publishProgress();
  105.                 }
  106.  
  107.             } catch (InterruptedException e) {
  108.                 e.printStackTrace();
  109.             }
  110.             return null;
  111.         }
  112.  
  113.         @Override
  114.         protected void onProgressUpdate(Void... progress) {
  115.             super.onProgressUpdate(progress);
  116.             //Toast.makeText(getApplicationContext(), "Please dont kill me, Im tired dying...T.T", Toast.LENGTH_LONG).show();
  117.         }
  118.     }
  119.  
  120.     // We should to register our service in AlarmManager service
  121.     // for performing periodical starting of our service by the system
  122.     private void startServiceReStarter() {
  123.         Intent intent = new Intent(this, ServiceStarter.class);
  124.         PendingIntent pendingIntent = PendingIntent.getBroadcast(this, this.REQUEST_CODE, intent, 0);
  125.  
  126.         if (pendingIntent == null) {
  127.             /*Toast.makeText(this, "Some problems with creating of PendingIntent", Toast.LENGTH_LONG).show();*/
  128.         } else {
  129.             if (serviceReStarterAlarmManager == null) {
  130.                 serviceReStarterAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  131.                 serviceReStarterAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,
  132.                         SystemClock.elapsedRealtime() + FIRST_RUN_TIMEOUT_MILISEC,
  133.                         SERVICE_STARTER_INTERVAL_MILISEC, pendingIntent);
  134.             }
  135.         }
  136.     }
  137. }
  138.    
  139. package com.muzaffar.myApps.App;
  140.  
  141. import android.app.ActivityManager;
  142. import android.app.admin.DevicePolicyManager;
  143. import android.content.ComponentName;
  144. import android.content.Context;
  145. import android.content.Intent;
  146. import android.content.pm.ActivityInfo;
  147. import android.graphics.Color;
  148. import android.os.Bundle;
  149. import android.support.v7.app.AppCompatActivity;
  150. import android.util.Log;
  151. import android.view.View;
  152. import android.widget.Button;
  153. import android.widget.EditText;
  154. import android.widget.TextView;
  155. import android.widget.Toast;
  156.  
  157. import com.muzaffar.spycare.App.SpycareServices;
  158. import com.muzaffar.spycare.App.Util;
  159. import com.muzaffar.spycare.R;
  160. import com.muzaffar.spycare.Receiver.DeviceAdmin;
  161.  
  162. /**
  163.  * Created by OligoCoco on 11/2/2016.
  164.  */
  165.  
  166. public class Main_Activity extends AppCompatActivity implements View.OnClickListener {
  167.  
  168.     //Default PIN
  169.     public static final String DEFAULT_PIN = "1234";
  170.  
  171.     //Import Instance Shared Pref
  172.     Util myPref=new Util(Main_Activity.this);
  173.  
  174.     //ImportButton
  175.     Button btn_setting, btn_Stealth, btn_Test_Page;
  176.     public static Button btn_device_manager;
  177.     TextView txt_Start_Stop;
  178.  
  179.     private boolean isRunning = false;
  180.     private EditText SecretCode;
  181.     String device_admin;
  182.  
  183.     //Enable Device Admin
  184.     private ComponentName deviceAdmin;
  185.     private DevicePolicyManager devicePolicyManager;
  186.     private static final int REQUEST_CODE_ENABLE_ADMIN = 1;
  187.  
  188.     @Override
  189.     protected void onCreate(Bundle savedInstanceState) {
  190.         super.onCreate(savedInstanceState);
  191.         setContentView(R.layout.activity_main);
  192.  
  193.         /*Initialize*/
  194.         deviceAdmin = new ComponentName(getApplicationContext(), DeviceAdmin.class);
  195.         devicePolicyManager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
  196.  
  197.         /*Initialize Button*/
  198.         txt_Start_Stop = (TextView) findViewById(R.id.app_start_stop);
  199.             /*Return default status of txt_Start_Stop*/
  200.             showServiceStatus();
  201.         btn_setting = (Button) findViewById(R.id.app_setting);
  202.         btn_setting.setOnClickListener(this);
  203.         btn_device_manager = (Button) findViewById(R.id.app_device_manager);
  204.             device_admin = myPref.getSavedShared("device_admin");
  205.             if(device_admin==null || device_admin.equals("false") || device_admin.equals("")){
  206.                 btn_device_manager.setText(R.string.app_device_manager);
  207.                 btn_device_manager.setClickable(true);
  208.                 btn_device_manager.setEnabled(true);
  209.             }else{
  210.                 btn_device_manager.setText(R.string.app_device_manager_activated);
  211.                 btn_device_manager.setClickable(false);
  212.                 btn_device_manager.setEnabled(false);
  213.             }
  214.         btn_device_manager.setOnClickListener(this);
  215.         btn_Stealth = (Button) findViewById(R.id.app_start_stealth);
  216.         btn_Stealth.setOnClickListener(this);
  217.         SecretCode = (EditText) findViewById(R.id.app_secret_code);
  218.         SecretCode.setTextColor(Color.LTGRAY);
  219.         SecretCode.setText(myPref.getSavedShared("Secret_Code"));
  220.  
  221.         //Start Services
  222.         startMonitoring();
  223.  
  224.         //Request on potret
  225.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  226.     }
  227.  
  228.     @Override
  229.     public void onStop(){
  230.         super.onStop();
  231.         myPref.saveToPref("Secret_Code",SecretCode.getText().toString());
  232.         final int flags = DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY;
  233.         if (devicePolicyManager.isAdminActive(deviceAdmin)) {
  234.             devicePolicyManager.resetPassword(myPref.getSavedShared("Secret_Code"), flags);
  235.         }
  236.     }
  237.  
  238.  
  239.     /***
  240.      *      _____                _              __  __
  241.      *     |  __               (_)            |  /  |
  242.      *     | |  | |  ___ __   __ _   ___  ___  |   / |  __ _  _ __    __ _   __ _   ___  _ __
  243.      *     | |  | | / _ \  / /| | / __|/ _  | |/| | / _` || '_   / _` | / _` | / _ | '__|
  244.      *     | |__| ||  __/  V / | || (__|  __/ | |  | || (_| || | | || (_| || (_| ||  __/| |
  245.      *     |_____/  ___|  _/  |_| ___|___| |_|  |_| __,_||_| |_| __,_| __, | ___||_|
  246.      *                                                                        __/ |
  247.      *                                                                       |___/
  248.      */
  249.  
  250.  
  251.     /*Device Manager*/
  252.     private void lock() {
  253.         devicePolicyManager.setPasswordQuality(deviceAdmin, DevicePolicyManager.PASSWORD_QUALITY_NUMERIC);
  254.         devicePolicyManager.setPasswordMinimumLength(deviceAdmin, 4);
  255.         final int flags = DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY;
  256.         devicePolicyManager.resetPassword(myPref.getSavedShared("Secret_Code"), flags);
  257.  
  258.     }
  259.  
  260.     @Override
  261.     protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
  262.         super.onActivityResult(requestCode, resultCode, data);
  263.  
  264.         if (requestCode == REQUEST_CODE_ENABLE_ADMIN && resultCode == RESULT_OK) {
  265.             btn_device_manager.setText(this.getString(R.string.app_device_manager_activated));
  266.             btn_device_manager.setClickable(false);
  267.             btn_device_manager.setEnabled(false);
  268.             lock();
  269.         }else{
  270.             btn_device_manager.setText(this.getString(R.string.app_device_manager));
  271.             btn_device_manager.setClickable(true);
  272.             btn_device_manager.setEnabled(true);
  273.         }
  274.     }
  275.  
  276.     /***
  277.      *       _____             _                      __  __        _    _                 _
  278.      *      / ____|           | |                    |  /  |      | |  | |               | |
  279.      *     | |     _   _  ___ | |_  ___   _ __ ___   |   / |  ___ | |_ | |__    ___    __| |
  280.      *     | |    | | | |/ __|| __|/ _  | '_ ` _   | |/| | / _ | __|| '_   / _   / _` |
  281.      *     | |____| |_| |__ | |_| (_) || | | | | | | |  | ||  __/| |_ | | | || (_) || (_| |
  282.      *      _____|__,_||___/ __|___/ |_| |_| |_| |_|  |_| ___| __||_| |_| ___/  __,_|
  283.      *
  284.      *
  285.      */
  286.  
  287.     /*Function to listen which button has been clicked and response correspondingly*/
  288.     @Override
  289.     public void onClick(View v) {
  290.         switch (v.getId()) {
  291.             case R.id.app_device_manager:
  292.                 defaultPIN();
  293.                 if (devicePolicyManager.isAdminActive(deviceAdmin)) {
  294.                     //If device admin is active
  295.  
  296.                 } else {
  297.                     // Launch the activity to have the user enable our admin.
  298.                     Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
  299.                     intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdmin);
  300.                     startActivityForResult(intent, REQUEST_CODE_ENABLE_ADMIN);
  301.                     myPref.saveToPref("device_admin", "true");
  302.                 }
  303.                 break;
  304.             case R.id.app_setting:
  305.                 defaultPIN();
  306.                 Intent a = new Intent(getApplicationContext(), Setting_Activity.class);
  307.                 startActivity(a);
  308.                 break;
  309.             case R.id.app_start_stealth:
  310.                 defaultPIN();
  311.                 Intent b = new Intent(Intent.ACTION_MAIN);
  312.                 b.addCategory(Intent.CATEGORY_HOME);
  313.                 b.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  314.                 startActivity(b);
  315.                 break;
  316.         }
  317.     }
  318.  
  319.     /*Function used to start the monitoring process*/
  320.     private void startMonitoring() {
  321.         //startService(new Intent(this, SpycareServices.class));
  322.         startService(new Intent(this, SpycareServices.class));
  323.     }
  324.  
  325.     /*Function used to stop the monitoring process*/
  326.     private void stopMonitoring() {
  327.         //stopService(new Intent(this, SpycareServices.class));
  328.         stopService(new Intent(this, SpycareServices.class));
  329.     }
  330.  
  331.     /*Function to change button status*/
  332.     private void showServiceStatus(){
  333.         // Show the current service state
  334.         /*if(isMyServiceRunning(SpycareServices.class,getApplicationContext())){*/
  335.         if(isMyServiceRunning(SpycareServices.class,getApplicationContext())){
  336.             txt_Start_Stop.setText(this.getString(R.string.stop_label));
  337.             txt_Start_Stop.setTextColor(Color.BLUE);
  338.             isRunning = true;
  339.         }else{
  340.             txt_Start_Stop.setText(this.getString(R.string.start_label));
  341.             txt_Start_Stop.setTextColor(Color.RED);
  342.             isRunning = false;
  343.             //If service not running
  344.             startMonitoring();
  345.             //Return
  346.             Intent a = new Intent(getApplicationContext(), Main_Activity.class);
  347.             startActivity(a);
  348.         }
  349.     }
  350.  
  351.     /*Function to check if certain services is running*/
  352.     private boolean isMyServiceRunning(Class<?> serviceClass,Context context) {
  353.         ActivityManager manager = (ActivityManager)context. getSystemService(Context.ACTIVITY_SERVICE);
  354.         for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
  355.             if (serviceClass.getName().equals(service.service.getClassName())) {
  356.                 return true;
  357.             }
  358.         }
  359.         return false;
  360.     }
  361.  
  362.     /*Function to place default PIN*/
  363.     public void defaultPIN(){
  364.         String compA = myPref.getSavedShared("Secret_Code");
  365.         if(compA.isEmpty()){
  366.             Toast.makeText(getApplicationContext(), "PIN Cannot be empty, Applying default PIN "+DEFAULT_PIN, Toast.LENGTH_SHORT).show();
  367.             myPref.saveToPref("Secret_Code",DEFAULT_PIN);
  368.  
  369.             //Set DMP Pass
  370.             final int flags = DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY;
  371.             if (devicePolicyManager.isAdminActive(deviceAdmin)) {
  372.                 devicePolicyManager.resetPassword(DEFAULT_PIN, flags);
  373.             }
  374.  
  375.             SecretCode.setText(myPref.getSavedShared("Secret_Code"));
  376.  
  377.         }
  378.     }
  379.  
  380. }
  381.    
  382. @Override
  383. public int onStartCommand(Intent intent, int flags, int startId) {
  384.     Toast.makeText(getApplicationContext(), "Services Has Been Started!", Toast.LENGTH_SHORT).show();
  385.  
  386.  
  387.     return START_STICKY;
  388. }
  389.    
  390. @Override
  391. public int onStartCommand(Intent intent, int flags, int startId) {
  392. Log.i("LocalService", "Received start id " + startId + ": " + intent);
  393. // We want this service to continue running until it is explicitly
  394. // stopped, so return sticky.
  395. return START_STICKY;
  396. }
  397.    
  398. <service ...
  399.          android:process=":separate" >
  400.     ...
  401. </service>
RAW Paste Data
Top