nigatigga

Untitled

Aug 25th, 2014
367
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.82 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.Set;
  15. import java.util.Timer;
  16. import java.util.TimerTask;
  17.  
  18. public class HeartBeat extends Service {
  19.     private static final String TAG = HeartBeat.class.getSimpleName();
  20.     public Timer TIMER;
  21.  
  22.     private static Set<AccessGranted> mAccessGrantedList = new HashSet<AccessGranted>();
  23.     private Set<String> mLockedApps = new HashSet<String>();
  24.     private long lastModified = 0;
  25.     private BroadcastReceiver mScreenStateReceiver;
  26.     private BroadcastReceiver mAccessGrantedReceiver;
  27.     private File mLockedAppsFile;
  28.  
  29.     @Override
  30.     public IBinder onBind(Intent arg0) {
  31.         return null;
  32.     }
  33.  
  34.  
  35.     @Override
  36.     public int onStartCommand(Intent intent, int flags, int startId) {
  37.         // Log.i("LocalService", "Received start id " + startId + ": " +
  38.         // intent);
  39.         // We want this service to continue running until it is explicitly
  40.         // stopped, so return sticky.
  41.         if (TIMER == null) {
  42.  
  43.             TIMER = new Timer(true);
  44.             TIMER.scheduleAtFixedRate(new LockAppsTimerTask(), 1000, 250);
  45.  
  46.             mScreenStateReceiver = new BroadcastReceiver() {
  47.  
  48.                 private boolean screenOff;
  49.  
  50.                 @Override
  51.                 public void onReceive(Context context, Intent intent) {
  52.                     if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
  53.                         screenOff = true;
  54.                     } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
  55.                         screenOff = false;
  56.                     }
  57.  
  58.                     if (screenOff) {
  59.                         Log.i(TAG, "Cancel Timer");
  60.                         TIMER.cancel();
  61.                     } else {
  62.                         Log.i(TAG, "Restart Timer");
  63.                         TIMER = new Timer(true);
  64.                         TIMER.scheduleAtFixedRate(new LockAppsTimerTask(), 1000, 250);
  65.                     }
  66.                 }
  67.             };
  68.  
  69.             IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
  70.             filter.addAction(Intent.ACTION_SCREEN_OFF);
  71.             registerReceiver(mScreenStateReceiver, filter);
  72.  
  73.             mAccessGrantedReceiver = new BroadcastReceiver() {
  74.  
  75.                 @Override
  76.                 public void onReceive(Context context, Intent intent) {
  77.                     String action = intent.getAction();
  78.                     String packageName = intent.getStringExtra("packageName");
  79.                     if (action.equals(Constants.ACTION_GRANT_ACCESS) && packageName != null) {
  80.                         AccessGranted ag = new AccessGranted(packageName);
  81.                         mAccessGrantedList.remove(ag);
  82.                         mAccessGrantedList.add(ag);
  83.                     }
  84.                 }
  85.             };
  86.  
  87.             IntentFilter filter2 = new IntentFilter(Constants.ACTION_GRANT_ACCESS);
  88.             registerReceiver(mAccessGrantedReceiver, filter2);
  89.         }
  90.         // this.stopSelf();
  91.         return START_STICKY;
  92.     }
  93.  
  94.     @Override
  95.     public void onDestroy() {
  96.         super.onDestroy();
  97.         Log.i(TAG, "onDestroy()");
  98.         TIMER.cancel();
  99.         TIMER = null;
  100.         unregisterReceiver(mScreenStateReceiver);
  101.         unregisterReceiver(mAccessGrantedReceiver);
  102.     }
  103.  
  104.  
  105.     private class LockAppsTimerTask extends TimerTask {
  106.  
  107.         @Override
  108.         public void run() {
  109.             ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
  110.  
  111.             try {
  112.                 //List<RecentTaskInfo> recentTasks = activityManager.getRecentTasks(1, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
  113.                 String packageName = "com.android.camera";
  114.                 if (isBlocked(packageName)) {
  115.                     Intent i = new Intent(getApplicationContext(), LockScreenActivity.class);
  116.                     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
  117.                     i.putExtra("packageName", packageName);
  118.                     startActivity(i);
  119.                 }
  120.  
  121.  
  122.             } catch (Exception e) {
  123.                 Log.e("Foreground App", e.getMessage(), e);
  124.             }
  125.         }
  126.  
  127.         private boolean isBlocked(String packageName) {
  128.             // Log.i("Foreground App", "SIZE => "+mAccessGrantedList.size());
  129.             if (packageName == "com.android.camera") {
  130.                 return true;
  131.             }
  132.  
  133.  
  134.             return true;
  135.         }
  136.  
  137.  
  138.     }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment