Advertisement
nigatigga

Untitled

Aug 25th, 2014
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.39 KB | None | 0 0
  1. package com.alimuzaffar.android.childlock;
  2.  
  3. import android.app.ActivityManager;
  4. import android.app.Service;
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.IntentFilter;
  9. import android.os.IBinder;
  10. import android.util.Log;
  11.  
  12. import java.io.File;
  13. import java.util.HashSet;
  14. import java.util.List;
  15. import java.util.Set;
  16. import java.util.Timer;
  17. import java.util.TimerTask;
  18.  
  19. public class HeartBeat extends Service {
  20.     private static final String TAG = HeartBeat.class.getSimpleName();
  21.     public Timer TIMER;
  22.  
  23.    String CURRENT_PACKAGE_NAME;
  24.  
  25.  
  26.     private static Set<AccessGranted> mAccessGrantedList = new HashSet<AccessGranted>();
  27.     private Set<String> mLockedApps = new HashSet<String>();
  28.     private long lastModified = 0;
  29.     private BroadcastReceiver mScreenStateReceiver;
  30.     private BroadcastReceiver mAccessGrantedReceiver;
  31.     private File mLockedAppsFile;
  32.  
  33.     @Override
  34.     public IBinder onBind(Intent arg0) {
  35.         return null;
  36.     }
  37.  
  38.  
  39.     @Override
  40.     public int onStartCommand(Intent intent, int flags, int startId) {
  41.         // Log.i("LocalService", "Received start id " + startId + ": " +
  42.         // intent);
  43.         // We want this service to continue running until it is explicitly
  44.         // stopped, so return sticky.
  45.         if (TIMER == null) {
  46.  
  47.             TIMER = new Timer(true);
  48.             TIMER.scheduleAtFixedRate(new LockAppsTimerTask(), 1000, 250);
  49.  
  50.             mScreenStateReceiver = new BroadcastReceiver() {
  51.  
  52.                 private boolean screenOff;
  53.  
  54.                 @Override
  55.                 public void onReceive(Context context, Intent intent) {
  56.                     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
  57.                         screenOff = true;
  58.                     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
  59.                         screenOff = false;
  60.                     }
  61.  
  62.                     if (screenOff) {
  63.                         Log.i(TAG, "Cancel Timer");
  64.                         TIMER.cancel();
  65.                     } else {
  66.                         Log.i(TAG, "Restart Timer");
  67.                         TIMER = new Timer(true);
  68.                         TIMER.scheduleAtFixedRate(new LockAppsTimerTask(), 1000, 250);
  69.                     }
  70.                 }
  71.             };
  72.  
  73.             IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
  74.             filter.addAction(Intent.ACTION_SCREEN_OFF);
  75.             registerReceiver(mScreenStateReceiver, filter);
  76.  
  77.             mAccessGrantedReceiver = new BroadcastReceiver() {
  78.  
  79.                 @Override
  80.                 public void onReceive(Context context, Intent intent) {
  81.                     String action = intent.getAction();
  82.                     String packageName = intent.getStringExtra("packageName");
  83.                     if (action.equals(Constants.ACTION_GRANT_ACCESS) && packageName != null) {
  84.                         AccessGranted ag = new AccessGranted(packageName);
  85.                         mAccessGrantedList.remove(ag);
  86.                         mAccessGrantedList.add(ag);
  87.                     }
  88.                 }
  89.             };
  90.  
  91.             IntentFilter filter2 = new IntentFilter(Constants.ACTION_GRANT_ACCESS);
  92.             registerReceiver(mAccessGrantedReceiver, filter2);
  93.         }
  94.         // this.stopSelf();
  95.         return START_STICKY;
  96.     }
  97.  
  98.     @Override
  99.     public void onDestroy() {
  100.         super.onDestroy();
  101.         Log.i(TAG, "onDestroy()");
  102.         TIMER.cancel();
  103.         TIMER = null;
  104.         unregisterReceiver(mScreenStateReceiver);
  105.         unregisterReceiver(mAccessGrantedReceiver);
  106.     }
  107.  
  108.  
  109.     private class LockAppsTimerTask extends TimerTask {
  110.  
  111.         @Override
  112.         public void run() {
  113.             ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
  114.  
  115.             try {
  116.                 //List<RecentTaskInfo> recentTasks = activityManager.getRecentTasks(1, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
  117.                 String packageName = "com.android.camera";
  118.                 ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  119.                 List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager
  120.                         .getRunningTasks(1);
  121.                 ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
  122.                 String activityOnTop = ar.topActivity.getPackageName();
  123.  
  124.                 Log.e("activity on Top", "" + activityOnTop);
  125.                 Log.e(" My package name", "" + getApplicationContext().getPackageName());
  126.  
  127.  
  128.  
  129. // Provide the packagename(s) of apps here, you want to show password activity
  130.                 if (activityOnTop.contains(packageName)  // you can make this check even better
  131.                         && !activityOnTop.contains("com.spicycurryman.getdisciplined10.app.dev"
  132.                 )) {
  133.  
  134.                     Intent i = new Intent(getApplicationContext(), LockScreenActivity.class);
  135.                     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
  136.                     i.putExtra("packageName", packageName);
  137.                     startActivity(i);
  138.                 }
  139.  
  140.  
  141.             } catch (Exception e) {
  142.                 Log.e("Foreground App", e.getMessage(), e);
  143.             }
  144.         }
  145.  
  146.  
  147.  
  148.     }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement