// Path: app/src/main/java/clock/socoolby/com/clock/utils/FuncUnit.java
// public class FuncUnit {
//     private final static String TAG = FuncUnit.class.getSimpleName();
// 
//     public static String getBoxPackageName() {
//         try {
//             PackageInfo packageInfo = ClockApplication.getContext().getPackageManager().getPackageInfo(ClockApplication.getContext().getPackageName(), 0);
//             return packageInfo.packageName;
//         } catch (PackageManager.NameNotFoundException e) {
//             e.printStackTrace();
//         }
//         return null;
//     }
// 
//     public static String getVersionName(String packageName) {
//         try {
//             PackageInfo packageInfo = ClockApplication.getContext().getPackageManager().getPackageInfo(packageName, 0);
//             return packageInfo.versionName;
//         } catch (PackageManager.NameNotFoundException e) {
//             return null;
//         }
//     }
// 
//     public static void openURL(Context c, String url) {
//         if (url == null)
//             return;
//         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
//         c.startActivity(intent);
//     }
// 
//     /**
//      * @param context
//      * @param className Activity Class Name
//      */
//     public static boolean isForeground(Context context, String className) {
//         if (context == null || TextUtils.isEmpty(className)) {
//             return false;
//         }
//         ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//         List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
//         if (list != null && list.size() > 0) {
//             ComponentName cpn = list.get(0).topActivity;
//             if (className.equals(cpn.getClassName())) {
//                 return true;
//             }
//         }
//         return false;
//     }
// }
// 
// Path: app/src/main/java/clock/socoolby/com/clock/utils/ScreenManager.java
// public class ScreenManager {
//     public static boolean isScreenOn() {
//         PowerManager pm = (PowerManager) ClockApplication.getContext().getSystemService(Activity.POWER_SERVICE);
//         return pm.isScreenOn();
//     }
// 
//     public static void turnScreenOff() {
//         PowerManager pm = (PowerManager) ClockApplication.getContext().getSystemService(Activity.POWER_SERVICE);
//         pm.goToSleep(SystemClock.uptimeMillis());
//     }
// 
//     public static void systemLock(Activity context) {
//         DevicePolicyManager policyManager;
//         ComponentName componentName;
//         policyManager = (DevicePolicyManager) context.getSystemService(Activity.DEVICE_POLICY_SERVICE);
// 
//         componentName = new ComponentName(context, ActivateAdmin.class);
// 
//         if (!policyManager.isAdminActive(componentName)) {
//             Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
//             intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
//             context.startActivity(intent);
//         }
//         if (policyManager.isAdminActive(componentName)) {
//             Window localWindow = context.getWindow();
//             WindowManager.LayoutParams localLayoutParams = localWindow.getAttributes();
//             localLayoutParams.screenBrightness = 1.0F;
//             localWindow.setAttributes(localLayoutParams);
//             policyManager.lockNow();
//         }
// 
//     }
// 
//     public static void systemUnLock() {
//         PowerManager pm = (PowerManager) ClockApplication.getContext().getSystemService(Activity.POWER_SERVICE);
//         PowerManager.WakeLock mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "SimpleTimer");
//         mWakelock.acquire();
//     }
// 
//     public static boolean isApplicationBroughtToBackground(final Context context) {
//         ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//         List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
//         if (!tasks.isEmpty()) {
//             ComponentName topActivity = tasks.get(0).topActivity;
//             if (!topActivity.getPackageName().equals(context.getPackageName())) {
//                 return true;
//             }
//         }
//         return false;
//     }
// }


import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.util.Log;
import clock.socoolby.com.clock.utils.FuncUnit;
import clock.socoolby.com.clock.utils.ScreenManager;
package clock.socoolby.com.clock;



/**
 * Alway zuo,never die.
 * Created by socoolby on 16/04/2017.
 */

public class ProximityService extends Service {
    private final static String TAG = ProximityService.class.getSimpleName();

    private SensorManager mSensorManager;
    private SensorEventListener mSensorListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            float[] its = sensorEvent.values;
            Log.d(TAG, String.format("its %f %f %f len:,%d", its[0], its[1], its[2], its.length));
            if (sensorEvent.sensor.getType() == Sensor.TYPE_PROXIMITY) {
// Path: app/src/main/java/clock/socoolby/com/clock/utils/FuncUnit.java
// public class FuncUnit {
//     private final static String TAG = FuncUnit.class.getSimpleName();
// 
//     public static String getBoxPackageName() {
//         try {
//             PackageInfo packageInfo = ClockApplication.getContext().getPackageManager().getPackageInfo(ClockApplication.getContext().getPackageName(), 0);
//             return packageInfo.packageName;
//         } catch (PackageManager.NameNotFoundException e) {
//             e.printStackTrace();
//         }
//         return null;
//     }
// 
//     public static String getVersionName(String packageName) {
//         try {
//             PackageInfo packageInfo = ClockApplication.getContext().getPackageManager().getPackageInfo(packageName, 0);
//             return packageInfo.versionName;
//         } catch (PackageManager.NameNotFoundException e) {
//             return null;
//         }
//     }
// 
//     public static void openURL(Context c, String url) {
//         if (url == null)
//             return;
//         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
//         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
//         c.startActivity(intent);
//     }
// 
//     /**
//      * @param context
//      * @param className Activity Class Name
//      */
//     public static boolean isForeground(Context context, String className) {
//         if (context == null || TextUtils.isEmpty(className)) {
//             return false;
//         }
//         ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//         List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(1);
//         if (list != null && list.size() > 0) {
//             ComponentName cpn = list.get(0).topActivity;
//             if (className.equals(cpn.getClassName())) {
//                 return true;
//             }
//         }
//         return false;
//     }
// }
// 
// Path: app/src/main/java/clock/socoolby/com/clock/utils/ScreenManager.java
// public class ScreenManager {
//     public static boolean isScreenOn() {
//         PowerManager pm = (PowerManager) ClockApplication.getContext().getSystemService(Activity.POWER_SERVICE);
//         return pm.isScreenOn();
//     }
// 
//     public static void turnScreenOff() {
//         PowerManager pm = (PowerManager) ClockApplication.getContext().getSystemService(Activity.POWER_SERVICE);
//         pm.goToSleep(SystemClock.uptimeMillis());
//     }
// 
//     public static void systemLock(Activity context) {
//         DevicePolicyManager policyManager;
//         ComponentName componentName;
//         policyManager = (DevicePolicyManager) context.getSystemService(Activity.DEVICE_POLICY_SERVICE);
// 
//         componentName = new ComponentName(context, ActivateAdmin.class);
// 
//         if (!policyManager.isAdminActive(componentName)) {
//             Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
//             intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
//             context.startActivity(intent);
//         }
//         if (policyManager.isAdminActive(componentName)) {
//             Window localWindow = context.getWindow();
//             WindowManager.LayoutParams localLayoutParams = localWindow.getAttributes();
//             localLayoutParams.screenBrightness = 1.0F;
//             localWindow.setAttributes(localLayoutParams);
//             policyManager.lockNow();
//         }
// 
//     }
// 
//     public static void systemUnLock() {
//         PowerManager pm = (PowerManager) ClockApplication.getContext().getSystemService(Activity.POWER_SERVICE);
//         PowerManager.WakeLock mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "SimpleTimer");
//         mWakelock.acquire();
//     }
// 
//     public static boolean isApplicationBroughtToBackground(final Context context) {
//         ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//         List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
//         if (!tasks.isEmpty()) {
//             ComponentName topActivity = tasks.get(0).topActivity;
//             if (!topActivity.getPackageName().equals(context.getPackageName())) {
//                 return true;
//             }
//         }
//         return false;
//     }
// }


// Path: app/src/main/java/clock/socoolby/com/clock/ProximityService.java
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.util.Log;
import clock.socoolby.com.clock.utils.FuncUnit;
import clock.socoolby.com.clock.utils.ScreenManager;

package clock.socoolby.com.clock;



/**
 * Alway zuo,never die.
 * Created by socoolby on 16/04/2017.
 */

public class ProximityService extends Service {
    private final static String TAG = ProximityService.class.getSimpleName();

    private SensorManager mSensorManager;
    private SensorEventListener mSensorListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            float[] its = sensorEvent.values;
            Log.d(TAG, String.format("its %f %f %f len:,%d", its[0], its[1], its[2], its.length));
            if (sensorEvent.sensor.getType() == Sensor.TYPE_PROXIMITY) {
                if (FuncUnit.isForeground(ClockApplication.getContext(), MainActivity.class.getName())) {