Advertisement
nigatigga

Untitled

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