Advertisement
nigatigga

heartb4 twin

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