nigatigga

shiyat

Aug 29th, 2014
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.33 KB | None | 0 0
  1. package com.ibc.android.demo.appslist.app;
  2.  
  3. import android.app.ActivityManager;
  4. import android.app.AlarmManager;
  5. import android.app.PendingIntent;
  6. import android.app.Service;
  7. import android.content.BroadcastReceiver;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.IntentFilter;
  11. import android.content.SharedPreferences;
  12. import android.os.IBinder;
  13. import android.util.Log;
  14.  
  15. import java.io.File;
  16. import java.util.ArrayList;
  17. import java.util.HashSet;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import java.util.Timer;
  22. import java.util.TimerTask;
  23. public class HeartBeat extends Service {
  24.  
  25.  
  26.  
  27.     private static final String TAG = HeartBeat.class.getSimpleName();
  28.     public Timer TIMER;
  29.  
  30.  
  31.  
  32.     private static Set<AccessGranted> mAccessGrantedList = new HashSet<AccessGranted>();
  33.     private Set<String> mLockedApps = new HashSet<String>();
  34.     private long lastModified = 0;
  35.     private BroadcastReceiver mScreenStateReceiver;
  36.     private BroadcastReceiver mAccessGrantedReceiver;
  37.     private File mLockedAppsFile;
  38.     ArrayList<String> packagezList;
  39.     SharedPreferences sharedPrefs;
  40.     Map<String, ?> allEntries;
  41.     SharedPreferences sharedPrefsapp;
  42.  
  43.     String prefix;
  44.  
  45.  
  46.  
  47.  
  48.  
  49.     @Override
  50.     public IBinder onBind(Intent arg0) {
  51.         return null;
  52.     }
  53.  
  54.  
  55.     @Override
  56.     public int onStartCommand(Intent intent, int flags, int startId) {
  57.  
  58.  
  59.         //startService(new Intent(this, HeartBeat.class));
  60.         Intent ishintent = new Intent(this, HeartBeat.class);
  61.         PendingIntent pintent = PendingIntent.getService(this, 0, ishintent, 0);
  62.         AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
  63.         alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),1000, pintent);
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.         // Log.i("LocalService", "Received start id " + startId + ": " +
  77.         // intent);
  78.         // We want this service to continue running until it is explicitly
  79.         // stopped, so return sticky.
  80.  
  81.  
  82.             mScreenStateReceiver = new BroadcastReceiver() {
  83.  
  84.                 private boolean screenOff;
  85.  
  86.                 @Override
  87.                 public void onReceive(Context context, Intent intent) {
  88.                     Intent ishintent = new Intent(context, HeartBeat.class);
  89.                     PendingIntent pintent = PendingIntent.getService(context, 0, ishintent, 0);
  90.                     AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
  91.                     alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),1000, pintent);
  92.  
  93.  
  94.  
  95.  
  96.  
  97.                     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
  98.                         screenOff = true;
  99.                     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
  100.                         screenOff = false;
  101.                     }
  102.  
  103.                     if (screenOff) {
  104.                         //Log.i(TAG, "Cancel Timer");
  105.                         TIMER.cancel();
  106.                     } else {
  107.                        // Log.i(TAG, "Restart Timer");
  108.                         TIMER = new Timer(true);
  109.                         TIMER.scheduleAtFixedRate(new LockAppsTimerTask(), 1000, 250);
  110.                     }
  111.                 }
  112.             };
  113.  
  114.             IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
  115.             filter.addAction(Intent.ACTION_SCREEN_OFF);
  116.             registerReceiver(mScreenStateReceiver, filter);
  117.  
  118.             mAccessGrantedReceiver = new BroadcastReceiver() {
  119.  
  120.                 @Override
  121.                 public void onReceive(Context context, Intent intent) {
  122.  
  123.  
  124.                     Intent ishintent = new Intent(context, HeartBeat.class);
  125.                     PendingIntent pintent = PendingIntent.getService(context, 0, ishintent, 0);
  126.                     AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
  127.                     alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),1000, pintent);
  128.  
  129.                     String action = intent.getAction();
  130.                     String packageName = intent.getStringExtra("packageName");
  131.                     if (action.equals(Constants.ACTION_GRANT_ACCESS) && packageName != null) {
  132.                         AccessGranted ag = new AccessGranted(packageName);
  133.                         mAccessGrantedList.remove(ag);
  134.                         mAccessGrantedList.add(ag);
  135.                     }
  136.                 }
  137.             };
  138.  
  139.             IntentFilter filter2 = new IntentFilter(Constants.ACTION_GRANT_ACCESS);
  140.             registerReceiver(mAccessGrantedReceiver, filter2);
  141.  
  142.         // this.stopSelf();
  143.  
  144.  
  145.  
  146.         //startforeground goes here
  147.  
  148.  
  149.  
  150.         return START_STICKY;
  151.     }
  152.  
  153.     @Override
  154.     public void onDestroy() {
  155.         super.onDestroy();
  156.         //startService(new Intent(this, HeartBeat.class));
  157.  
  158.         Intent ishintent = new Intent(this, HeartBeat.class);
  159.         PendingIntent pintent = PendingIntent.getService(this, 0, ishintent, 0);
  160.         AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
  161.         alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),1000, pintent);
  162.     }
  163.  
  164.  
  165.  
  166.  
  167.     private class LockAppsTimerTask extends TimerTask {
  168.  
  169.  
  170.  
  171.  
  172.         @Override
  173.         public void run() {
  174.  
  175.  
  176.             sharedPrefs = getApplicationContext().getSharedPreferences(getApplicationContext().getPackageName(), Context.MODE_PRIVATE);
  177.             sharedPrefsapp = getApplicationContext().getSharedPreferences("appdb", Context.MODE_PRIVATE);
  178.             allEntries= null;
  179.              allEntries = sharedPrefsapp.getAll();
  180.  
  181.             //prefix = "m";
  182.             packagezList= null;
  183.  
  184.  
  185.             packagezList = new ArrayList<String>();
  186.  
  187.  
  188.  
  189.  
  190.             for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
  191.                     //Log.e("right key: ", entry.getKey() + "right value: " + entry.getValue().toString()  );
  192.                     packagezList.add(entry.getKey());
  193.  
  194.  
  195.  
  196.  
  197.  
  198.             }
  199.  
  200.  
  201. /*        for (Map.Entry<String, ?> entry : allEntries.entrySet())
  202.         {
  203.             //Check if the package name starts with the prefix.
  204.             if (entry.getKey().startsWith(prefix)) {
  205.                 //Add JUST the package name (trim off the prefix).
  206.                 packagezList.add(entry.getKey().substring(prefix.length()));
  207.             packagezList.add(entry.getKey());
  208.  
  209.             }
  210.         }*/
  211.  
  212.             for(Object object: packagezList){
  213.                 Log.e("YO!", (String) object);
  214.             }
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.             ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
  224.  
  225.             try {
  226.                 //List<RecentTaskInfo> recentTasks = activityManager.getRecentTasks(1, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
  227.                 ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  228.                 List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager
  229.                         .getRunningTasks(1);
  230.                 ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
  231.                 String activityOnTop = ar.topActivity.getPackageName();
  232.  
  233.                 // Log.e("activity on Top", "" + activityOnTop);
  234.                 //   Log.e(" My package name", "" + getApplicationContext().getPackageName());
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.                 //for (Object data : newArrayList) {
  244.  
  245.                 for(Object object: packagezList){
  246.  
  247. // Provide the packagename(s) of apps here, you want to show password activity
  248.                     if ((activityOnTop.contains((CharSequence) object)) &&
  249.                             (!activityOnTop.contains(getApplicationContext().getPackageName()
  250.                             ))) {  // you have to make this check even better
  251.  
  252.  
  253.                         Intent i = new Intent(getApplicationContext(), LockScreenActivity.class);
  254.                         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
  255.                         i.putExtra( "", "");
  256.                         startActivity(i);
  257.                     }
  258.  
  259.                 }
  260.  
  261.  
  262.             } catch (Exception e) {
  263.                // Log.e("Foreground App", e.getMessage(), e);
  264.             }
  265.         }
  266.  
  267.  
  268.  
  269.     }
  270.  
  271. }
Advertisement
Add Comment
Please, Sign In to add comment