aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Lower <devvortex@gmail.com>2016-04-24 22:58:35 +0100
committerdavidevinavil <davidevinavil@gmail.com>2017-01-21 18:30:58 +0100
commit68b7ddae6f2a75239b218141a28022c3eab99be5 (patch)
treef196fe5090867bafd4ee4a3469f533ea7e3a77dc
parent76e047ae8dbf02b54e662eb0590bc116a9067317 (diff)
z2_plus: Add doze package
Taken from yu lettuce Includes the following patches YUDoze: Add service for using proximity sensor for wake-up Author: Patrick Lower <devvortex@gmail.com> Date: Mon May 11 16:14:17 2015 -0400 YUDoze: Set gesture to disabled by default Author: Arne Coucheron <arco68@gmail.com> Date: Wed May 20 00:56:19 2015 +0200 YUDoze: Consolidate doze settings Author: Christopher R. Palmer <crpalmer@gmail.com> Date: Sat May 23 11:42:49 2015 -0700 YUDoze: fix defaults Author: Dave Daynard <nardholio@gmail.com> Date: Sat May 23 18:29:13 2015 -0400 YUDoze: Add back button to action bar Author: luca020400 <luca.stefani.ge1@gmail.com> Date: Sun May 24 15:47:17 2015 -0700 YUDoze: add proximity wake Author: Dave Daynard <nardholio@gmail.com> Date: Mon May 25 12:54:00 2015 -0400 YUDoze: restore proximity wake preference on boot Author: Dave Daynard <nardholio@gmail.com> Date: Sat Jul 4 13:44:53 2015 -0400 YUDozeService: Prevent a pm wakeup with a timestamp in the future Author: Brandon McAnsh <brandon.mcansh@gmail.com> Date: Fri Jul 24 15:40:45 2015 -0400 YUDozeService: properly fix future timestamp error Author: Patrick Lower <devvortex@gmail.com> Date: Wed Aug 5 10:51:03 2015 -0400 YUDoze: fix typo Author: Dave Daynard <nardholio@gmail.com> Date: Tue Sep 8 22:56:10 2015 -0400 YUDoze: Use ScreenType util from cmsdk Author: Michael Bestas <mikeioannina@gmail.com> Date: Sun Dec 13 02:51:09 2015 -0500 YUDOze: Remove summary for YUDoze Author: TheStrix <parthbhatia98@gmail.com> Date: Sun Dec 13 07:25:36 2015 -0500 Change-Id: Ia7e668622eecf4355fd55dc9cda7ca05a6494b93
-rwxr-xr-xdevice.mk4
-rw-r--r--doze/Android.mk19
-rw-r--r--doze/AndroidManifest.xml48
-rw-r--r--doze/proguard.flags3
-rw-r--r--doze/res/drawable/ic_settings_doze.xml51
-rw-r--r--doze/res/values/strings.xml35
-rw-r--r--doze/res/xml/gesture_panel.xml53
-rw-r--r--doze/src/com/cyanogenmod/settings/doze/BootCompletedReceiver.java35
-rw-r--r--doze/src/com/cyanogenmod/settings/doze/DozeSettings.java124
-rw-r--r--doze/src/com/cyanogenmod/settings/doze/XiaomiDozeService.java199
10 files changed, 569 insertions, 2 deletions
diff --git a/device.mk b/device.mk
index d38be5b..683abde 100755
--- a/device.mk
+++ b/device.mk
@@ -161,9 +161,9 @@ PRODUCT_PACKAGES += \
# Doze mode
PRODUCT_PACKAGES += \
- ZukDoze
+ XiaomiDoze
-# Fingerprint sensor
+# Fingerprint
PRODUCT_PACKAGES += \
fingerprintd
diff --git a/doze/Android.mk b/doze/Android.mk
new file mode 100644
index 0000000..6ef922e
--- /dev/null
+++ b/doze/Android.mk
@@ -0,0 +1,19 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_PACKAGE_NAME := XiaomiDoze
+LOCAL_CERTIFICATE := platform
+LOCAL_PRIVILEGED_MODULE := true
+
+LOCAL_STATIC_JAVA_LIBRARIES := \
+ org.cyanogenmod.platform.internal
+
+LOCAL_PROGUARD_FLAG_FILES := proguard.flags
+
+include $(BUILD_PACKAGE)
+
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/doze/AndroidManifest.xml b/doze/AndroidManifest.xml
new file mode 100644
index 0000000..c63a5b4
--- /dev/null
+++ b/doze/AndroidManifest.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.cyanogenmod.settings.doze"
+ android:versionCode="1"
+ android:versionName="1.0"
+ android:sharedUserId="android.uid.system">
+
+ <uses-permission android:name="android.permission.DEVICE_POWER" />
+ <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
+ <uses-permission android:name="android.permission.VIBRATE"/>
+ <uses-permission android:name="android.permission.WAKE_LOCK" />
+
+ <uses-sdk
+ android:minSdkVersion="21"
+ android:targetSdkVersion="21"/>
+
+ <application
+ android:label="XiaomiDoze"
+ android:persistent="true">
+
+ <receiver android:name="com.cyanogenmod.settings.doze.BootCompletedReceiver">
+ <intent-filter>
+ <action android:name="android.intent.action.BOOT_COMPLETED" />
+ <category android:name="android.intent.category.DEFAULT" />
+ </intent-filter>
+ </receiver>
+
+ <service android:name="com.cyanogenmod.settings.doze.XiaomiDozeService"
+ android:permission="XiaomiDozeService">
+ </service>
+
+ <activity
+ android:name=".DozeSettings"
+ android:label="@string/screen_gestures_panel_title"
+ android:theme="@android:style/Theme.Material.Settings">
+ <intent-filter>
+ <action android:name="com.android.settings.action.EXTRA_SETTINGS" />
+ </intent-filter>
+ <meta-data
+ android:name="com.android.settings.category"
+ android:value="com.android.settings.category.device" />
+ <meta-data
+ android:name="com.android.settings.icon"
+ android:resource="@drawable/ic_settings_doze" />
+ </activity>
+
+ </application>
+</manifest>
diff --git a/doze/proguard.flags b/doze/proguard.flags
new file mode 100644
index 0000000..a21e62f
--- /dev/null
+++ b/doze/proguard.flags
@@ -0,0 +1,3 @@
+-keep class com.cyanogenmod.settings.doze.* {
+ *;
+}
diff --git a/doze/res/drawable/ic_settings_doze.xml b/doze/res/drawable/ic_settings_doze.xml
new file mode 100644
index 0000000..8386024
--- /dev/null
+++ b/doze/res/drawable/ic_settings_doze.xml
@@ -0,0 +1,51 @@
+<!--
+Copyright (C) 2014 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="64dp"
+ android:height="64dp"
+ android:viewportWidth="64"
+ android:viewportHeight="64">
+
+ <group
+ android:translateY="-988.583">
+ <path
+ android:fillColor="@android:color/white"
+ android:pathData="M15.0133
+1051.24c-1.3615-0.2479-2.37425-1.2789-2.57968-2.6262-0.06554-0.4298-0.06571-55.8484-0.00018-56.28
+0.09875-0.65024 0.367851-1.20544 0.812983-1.6773 0.402211-0.42637
+0.920053-0.7356 1.49909-0.8952l0.299302-0.0825h16.845 16.845l0.291174
+0.0804c1.24697 0.34447 2.13039 1.32473 2.32019 2.57456 0.06515 0.42898 0.06541
+55.8484 0.0003 56.28-0.188067 1.2461-1.07608 2.2305-2.32306 2.5752l-0.288567
+0.08h-16.755c-13.5267 0-16.7958
+0-16.9665-0.034zm29.8065-31.1862v-19.23h-12.99-12.99v19.23 19.23h12.99
+12.99v-19.23zm-13.459
+11.687c-1.03547-0.2354-1.77837-1.1208-1.83292-2.1845l-0.01654-0.3225h1.87175c1.02946
+0 2.10645 0.01 2.3933 0.02l0.521553 0.02-0.01945 0.253c-0.02847 0.3705-0.09559
+0.6134-0.267159 0.9672-0.135477 0.2794-0.18522 0.3474-0.439626 0.6013-0.31197
+0.3114-0.571617 0.4707-0.976866 0.5995-0.269196 0.085-0.949163 0.111-1.23404
+0.046zm-9.04096-4.1842v-0.6872l1.10534-1.0831 1.10534-1.0831
+0.02483-1.2072c0.01366-0.664 0.02781-2.213 0.03145-3.4422 0.007-2.3727
+0.01776-2.5909 0.162279-3.3 0.234013-1.1483 0.861931-2.2993 1.7263-3.1643
+0.762874-0.7634 1.91951-1.4813 3.00204-1.8633 0.262572-0.093 0.512691-0.1813
+0.555819-0.1969l0.07842-0.029 0.01577-0.426c0.01284-0.3469 0.03018-0.4706
+0.09332-0.666 0.197394-0.6109 0.642609-1.0525 1.21018-1.2004 0.365688-0.095
+0.860153-0.043 1.2089 0.1282 0.234414 0.1149 0.589851 0.4783 0.725931 0.7422
+0.171483 0.3325 0.207933 0.491 0.225975 0.9825 0.01148 0.3126 0.02671 0.4425
+0.0519 0.4425 0.01961 0 0.2844 0.088 0.588423 0.1954 2.74734 0.9715 4.21726
+2.7794 4.71985 5.805 0.171696 1.0336 0.211308 1.7926 0.259098 4.9646l0.03345
+2.22 1.10911 1.095 1.1091 1.095-0.0014 0.6825-0.0014 0.6825h-9.57-9.57v-0.6872z" />
+ </group>
+</vector>
diff --git a/doze/res/values/strings.xml b/doze/res/values/strings.xml
new file mode 100644
index 0000000..79b3747
--- /dev/null
+++ b/doze/res/values/strings.xml
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright (C) 2015 The CyanogenMod Project
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+ http://www.apache.org/licenses/LICENSE-2.0
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+
+ <!-- Gesture shortcuts -->
+ <string name="screen_gestures_panel_title">Doze</string>
+
+ <string name="ambient_display_title">Ambient display</string>
+
+ <string name="ambient_display_enable_title">Ambient display</string>
+ <string name="ambient_display_enable_summary">Wake screen when you receive notifications</string>
+
+ <string name="hand_wave_gesture_title">Hand wave</string>
+ <string name="hand_wave_gesture_summary">Pulse notifications on hand wave</string>
+
+ <string name="pocket_gesture_title">Pocket</string>
+ <string name="pocket_gesture_summary">Pulse notifications on removal from pocket</string>
+
+ <string name="proximity_wake_title">Proximity wake</string>
+
+ <string name="proximity_wake_enable_title">Proximity wake</string>
+ <string name="proximity_wake_enable_summary">Wake device on hand wave</string>
+
+</resources>
diff --git a/doze/res/xml/gesture_panel.xml b/doze/res/xml/gesture_panel.xml
new file mode 100644
index 0000000..40a017c
--- /dev/null
+++ b/doze/res/xml/gesture_panel.xml
@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ Copyright (C) 2015 The CyanogenMod Project
+
+ Licensed under the Apache License, Version 2.0 (the "License"
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
+
+ <PreferenceCategory
+ android:key="ambient_display_key"
+ android:title="@string/ambient_display_title" >
+
+ <SwitchPreference
+ android:key="ambient_display_enable"
+ android:defaultValue="true"
+ android:title="@string/ambient_display_enable_title"
+ android:summary="@string/ambient_display_enable_summary" />
+
+ <SwitchPreference
+ android:key="gesture_hand_wave"
+ android:defaultValue="false"
+ android:title="@string/hand_wave_gesture_title"
+ android:summary="@string/hand_wave_gesture_summary" />
+
+ <SwitchPreference
+ android:key="gesture_pocket"
+ android:defaultValue="false"
+ android:title="@string/pocket_gesture_title"
+ android:summary="@string/pocket_gesture_summary" />
+ </PreferenceCategory>
+
+ <PreferenceCategory
+ android:key="proximity_wake_key"
+ android:title="@string/proximity_wake_title" >
+
+ <SwitchPreference
+ android:key="proximity_wake_enable"
+ android:defaultValue="false"
+ android:title="@string/proximity_wake_enable_title"
+ android:summary="@string/proximity_wake_enable_summary" />
+ </PreferenceCategory>
+
+</PreferenceScreen> \ No newline at end of file
diff --git a/doze/src/com/cyanogenmod/settings/doze/BootCompletedReceiver.java b/doze/src/com/cyanogenmod/settings/doze/BootCompletedReceiver.java
new file mode 100644
index 0000000..195ec2d
--- /dev/null
+++ b/doze/src/com/cyanogenmod/settings/doze/BootCompletedReceiver.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2015 The CyanogenMod Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.cyanogenmod.settings.doze;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+public class BootCompletedReceiver extends BroadcastReceiver {
+
+ private static final boolean DEBUG = false;
+ private static final String TAG = "XiaomiDoze";
+
+ @Override
+ public void onReceive(final Context context, Intent intent) {
+ if (DEBUG) Log.d(TAG, "Starting service");
+ context.startService(new Intent(context, XiaomiDozeService.class));
+ }
+
+}
diff --git a/doze/src/com/cyanogenmod/settings/doze/DozeSettings.java b/doze/src/com/cyanogenmod/settings/doze/DozeSettings.java
new file mode 100644
index 0000000..d0697db
--- /dev/null
+++ b/doze/src/com/cyanogenmod/settings/doze/DozeSettings.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2015 The CyanogenMod Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.cyanogenmod.settings.doze;
+
+import android.app.ActionBar;
+import android.os.Bundle;
+import android.preference.Preference;
+import android.preference.PreferenceActivity;
+import android.preference.SwitchPreference;
+import android.provider.Settings;
+import android.view.Menu;
+import android.view.MenuItem;
+
+import org.cyanogenmod.internal.util.ScreenType;
+
+public class DozeSettings extends PreferenceActivity {
+
+ private static final String KEY_AMBIENT_DISPLAY_ENABLE = "ambient_display_enable";
+ private static final String KEY_HAND_WAVE = "gesture_hand_wave";
+ private static final String KEY_GESTURE_POCKET = "gesture_pocket";
+ private static final String KEY_PROXIMITY_WAKE = "proximity_wake_enable";
+
+ private SwitchPreference mAmbientDisplayPreference;
+ private SwitchPreference mHandwavePreference;
+ private SwitchPreference mPocketPreference;
+ private SwitchPreference mProximityWakePreference;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ addPreferencesFromResource(R.xml.gesture_panel);
+ boolean dozeEnabled = isDozeEnabled();
+ mAmbientDisplayPreference =
+ (SwitchPreference) findPreference(KEY_AMBIENT_DISPLAY_ENABLE);
+ // Read from DOZE_ENABLED secure setting
+ mAmbientDisplayPreference.setChecked(dozeEnabled);
+ mAmbientDisplayPreference.setOnPreferenceChangeListener(mAmbientDisplayPrefListener);
+ mHandwavePreference =
+ (SwitchPreference) findPreference(KEY_HAND_WAVE);
+ mHandwavePreference.setEnabled(dozeEnabled);
+ mHandwavePreference.setOnPreferenceChangeListener(mProximityListener);
+ mPocketPreference =
+ (SwitchPreference) findPreference(KEY_GESTURE_POCKET);
+ mPocketPreference.setEnabled(dozeEnabled);
+ mProximityWakePreference =
+ (SwitchPreference) findPreference(KEY_PROXIMITY_WAKE);
+ mProximityWakePreference.setOnPreferenceChangeListener(mProximityListener);
+
+ final ActionBar actionBar = getActionBar();
+ actionBar.setDisplayHomeAsUpEnabled(true);
+ }
+
+ @Override
+ protected void onResume() {
+ super.onResume();
+
+ // If running on a phone, remove padding around the listview
+ if (!ScreenType.isTablet(this)) {
+ getListView().setPadding(0, 0, 0, 0);
+ }
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ if (item.getItemId() == android.R.id.home) {
+ onBackPressed();
+ return true;
+ }
+ return false;
+ }
+
+ private boolean enableDoze(boolean enable) {
+ return Settings.Secure.putInt(getContentResolver(),
+ Settings.Secure.DOZE_ENABLED, enable ? 1 : 0);
+ }
+
+ private boolean isDozeEnabled() {
+ return Settings.Secure.getInt(getContentResolver(),
+ Settings.Secure.DOZE_ENABLED, 1) != 0;
+ }
+
+ private Preference.OnPreferenceChangeListener mAmbientDisplayPrefListener =
+ new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ boolean enable = (boolean) newValue;
+ boolean ret = enableDoze(enable);
+ if (ret) {
+ mHandwavePreference.setEnabled(enable);
+ mPocketPreference.setEnabled(enable);
+ }
+ return ret;
+ }
+ };
+
+ private Preference.OnPreferenceChangeListener mProximityListener =
+ new Preference.OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ if ((boolean) newValue) {
+ if (preference.getKey().equals(KEY_HAND_WAVE)) {
+ mProximityWakePreference.setChecked(false);
+ } else if (preference.getKey().equals(KEY_PROXIMITY_WAKE)) {
+ mHandwavePreference.setChecked(false);
+ }
+ }
+ return true;
+ }
+ };
+}
diff --git a/doze/src/com/cyanogenmod/settings/doze/XiaomiDozeService.java b/doze/src/com/cyanogenmod/settings/doze/XiaomiDozeService.java
new file mode 100644
index 0000000..e10db8d
--- /dev/null
+++ b/doze/src/com/cyanogenmod/settings/doze/XiaomiDozeService.java
@@ -0,0 +1,199 @@
+/*
+ * Copyright (c) 2015 The CyanogenMod Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.cyanogenmod.settings.doze;
+
+import android.app.Service;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.content.SharedPreferences;
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorEventListener;
+import android.hardware.SensorManager;
+import android.os.IBinder;
+import android.os.PowerManager;
+import android.preference.PreferenceManager;
+import android.provider.Settings;
+import android.util.Log;
+
+import java.lang.System;
+import java.util.ArrayList;
+import java.util.concurrent.TimeUnit;
+import java.util.List;
+
+public class XiaomiDozeService extends Service {
+ private static final String TAG = "XiaomiDozeService";
+ private static final boolean DEBUG = false;
+
+ private static final String DOZE_INTENT = "com.android.systemui.doze.pulse";
+
+ private static final String GESTURE_HAND_WAVE_KEY = "gesture_hand_wave";
+ private static final String GESTURE_POCKET_KEY = "gesture_pocket";
+ private static final String PROXIMITY_WAKE_KEY = "proximity_wake_enable";
+
+ private static final int POCKET_DELTA_NS = 1000 * 1000 * 1000;
+
+ private Context mContext;
+ private OneplusProximitySensor mSensor;
+ private PowerManager mPowerManager;
+
+ private boolean mHandwaveGestureEnabled = false;
+ private boolean mPocketGestureEnabled = false;
+ private boolean mProximityWakeEnabled = false;
+
+ class OneplusProximitySensor implements SensorEventListener {
+ private SensorManager mSensorManager;
+ private Sensor mSensor;
+
+ private boolean mSawNear = false;
+ private long mInPocketTime = 0;
+
+ public OneplusProximitySensor(Context context) {
+ mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
+ mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
+ }
+
+ @Override
+ public void onSensorChanged(SensorEvent event) {
+ boolean isNear = event.values[0] < mSensor.getMaximumRange();
+ if (mSawNear && !isNear) {
+ if (shouldPulse(event.timestamp)) {
+ launchDozePulse();
+ }
+ } else {
+ mInPocketTime = event.timestamp;
+ }
+ mSawNear = isNear;
+ }
+
+ @Override
+ public void onAccuracyChanged(Sensor sensor, int accuracy) {
+ /* Empty */
+ }
+
+ private boolean shouldPulse(long timestamp) {
+ long delta = timestamp - mInPocketTime;
+
+ if (mHandwaveGestureEnabled && mPocketGestureEnabled) {
+ return true;
+ } else if (mProximityWakeEnabled && (delta < POCKET_DELTA_NS)) {
+ mPowerManager.wakeUp(TimeUnit.NANOSECONDS.toMillis(System.nanoTime()));
+ return false;
+ } else if (mHandwaveGestureEnabled && !mPocketGestureEnabled) {
+ return delta < POCKET_DELTA_NS;
+ } else if (!mHandwaveGestureEnabled && mPocketGestureEnabled) {
+ return delta >= POCKET_DELTA_NS;
+ }
+ return false;
+ }
+
+ public void testAndEnable() {
+ if ((isDozeEnabled() && (mHandwaveGestureEnabled || mPocketGestureEnabled)) ||
+ mProximityWakeEnabled) {
+ mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
+ }
+ }
+
+ public void disable() {
+ mSensorManager.unregisterListener(this, mSensor);
+ }
+ }
+
+ @Override
+ public void onCreate() {
+ if (DEBUG) Log.d(TAG, "XiaomiDozeService Started");
+ mContext = this;
+ mPowerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
+ mSensor = new OneplusProximitySensor(mContext);
+ SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
+ loadPreferences(sharedPrefs);
+ sharedPrefs.registerOnSharedPreferenceChangeListener(mPrefListener);
+ if (!isInteractive()) {
+ mSensor.testAndEnable();
+ }
+ }
+
+ @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ if (DEBUG) Log.d(TAG, "Starting service");
+ IntentFilter screenStateFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
+ screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
+ mContext.registerReceiver(mScreenStateReceiver, screenStateFilter);
+ return START_STICKY;
+ }
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ return null;
+ }
+
+ private void launchDozePulse() {
+ mContext.sendBroadcast(new Intent(DOZE_INTENT));
+ }
+
+ private boolean isInteractive() {
+ return mPowerManager.isInteractive();
+ }
+
+ private boolean isDozeEnabled() {
+ return Settings.Secure.getInt(mContext.getContentResolver(),
+ Settings.Secure.DOZE_ENABLED, 1) != 0;
+ }
+
+ private void onDisplayOn() {
+ if (DEBUG) Log.d(TAG, "Display on");
+ mSensor.disable();
+ }
+
+ private void onDisplayOff() {
+ if (DEBUG) Log.d(TAG, "Display off");
+ mSensor.testAndEnable();
+ }
+
+ private void loadPreferences(SharedPreferences sharedPreferences) {
+ mHandwaveGestureEnabled = sharedPreferences.getBoolean(GESTURE_HAND_WAVE_KEY, false);
+ mPocketGestureEnabled = sharedPreferences.getBoolean(GESTURE_POCKET_KEY, false);
+ mProximityWakeEnabled = sharedPreferences.getBoolean(PROXIMITY_WAKE_KEY, false);
+ }
+
+ private BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
+ onDisplayOff();
+ } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
+ onDisplayOn();
+ }
+ }
+ };
+
+ private SharedPreferences.OnSharedPreferenceChangeListener mPrefListener =
+ new SharedPreferences.OnSharedPreferenceChangeListener() {
+ @Override
+ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
+ if (GESTURE_HAND_WAVE_KEY.equals(key)) {
+ mHandwaveGestureEnabled = sharedPreferences.getBoolean(GESTURE_HAND_WAVE_KEY, false);
+ } else if (GESTURE_POCKET_KEY.equals(key)) {
+ mPocketGestureEnabled = sharedPreferences.getBoolean(GESTURE_POCKET_KEY, false);
+ } else if (PROXIMITY_WAKE_KEY.equals(key)) {
+ mProximityWakeEnabled = sharedPreferences.getBoolean(PROXIMITY_WAKE_KEY, false);
+ }
+ }
+ };
+}