From 488a587dfd5b45a7dff0cc6003163f81da42bd06 Mon Sep 17 00:00:00 2001 From: Isaac Chen Date: Fri, 29 Jun 2018 00:03:49 +0800 Subject: [PATCH] msm8937-common: doze: Import from msm8996-common Signed-off-by: Isaac Chen --- doze/Android.mk | 26 ++- doze/AndroidManifest.xml | 31 +++- doze/proguard.flags | 7 +- doze/res/drawable/ic_settings_doze.xml | 51 ++++++ .../switchbar_background.xml} | 12 +- doze/res/layout/doze.xml | 28 ++++ doze/res/layout/switch_bar.xml | 47 ++++++ doze/res/values/colors.xml | 23 +++ doze/res/values/styles.xml | 30 ++++ doze/res/xml/doze_settings.xml | 49 ++++++ .../settings}/doze/BootCompletedReceiver.java | 16 +- .../settings}/doze/DozeService.java | 68 ++++---- .../settings/doze/DozeSettingsActivity.java | 48 ++++++ .../settings/doze/DozeSettingsFragment.java | 158 ++++++++++++++++++ .../settings/doze/ProximitySensor.java | 91 ++++++++++ .../settings/doze/TiltSensor.java} | 83 ++++----- .../com/cyanogenmod/settings/doze/Utils.java | 109 ++++++++++++ doze/src/org/lineageos/doze/TiltSensor.java | 48 ------ lineage.dependencies | 4 + product/display.mk | 2 +- 20 files changed, 791 insertions(+), 140 deletions(-) create mode 100644 doze/res/drawable/ic_settings_doze.xml rename doze/res/{values/strings.xml => drawable/switchbar_background.xml} (70%) create mode 100644 doze/res/layout/doze.xml create mode 100644 doze/res/layout/switch_bar.xml create mode 100644 doze/res/values/colors.xml create mode 100644 doze/res/values/styles.xml create mode 100644 doze/res/xml/doze_settings.xml rename doze/src/{org/lineageos => com/cyanogenmod/settings}/doze/BootCompletedReceiver.java (73%) rename doze/src/{org/lineageos => com/cyanogenmod/settings}/doze/DozeService.java (70%) create mode 100644 doze/src/com/cyanogenmod/settings/doze/DozeSettingsActivity.java create mode 100644 doze/src/com/cyanogenmod/settings/doze/DozeSettingsFragment.java create mode 100644 doze/src/com/cyanogenmod/settings/doze/ProximitySensor.java rename doze/src/{org/lineageos/doze/LineageSensor.java => com/cyanogenmod/settings/doze/TiltSensor.java} (54%) create mode 100644 doze/src/com/cyanogenmod/settings/doze/Utils.java delete mode 100644 doze/src/org/lineageos/doze/TiltSensor.java diff --git a/doze/Android.mk b/doze/Android.mk index 53f8284..095f7d8 100644 --- a/doze/Android.mk +++ b/doze/Android.mk @@ -1,17 +1,37 @@ -LOCAL_PATH := $(call my-dir) - +LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := $(call all-java-files-under, src) -LOCAL_PACKAGE_NAME := LineageDoze +LOCAL_PACKAGE_NAME := XiaomiDoze LOCAL_CERTIFICATE := platform LOCAL_PRIVILEGED_MODULE := true +LOCAL_STATIC_JAVA_LIBRARIES := \ + android-support-v4 \ + android-support-v13 \ + android-support-v7-recyclerview \ + android-support-v7-preference \ + android-support-v7-appcompat \ + android-support-v14-preference \ + org.cyanogenmod.platform.internal + +LOCAL_RESOURCE_DIR := \ + $(LOCAL_PATH)/res \ + $(LOCAL_PATH)/../../../../packages/resources/devicesettings/res \ + frameworks/support/v7/preference/res \ + frameworks/support/v14/preference/res \ + frameworks/support/v7/appcompat/res \ + frameworks/support/v7/recyclerview/res + +LOCAL_AAPT_FLAGS := --auto-add-overlay \ + --extra-packages android.support.v7.preference:android.support.v14.preference:android.support.v17.preference:android.support.v7.appcompat:android.support.v7.recyclerview LOCAL_PROGUARD_FLAG_FILES := proguard.flags +include frameworks/base/packages/SettingsLib/common.mk + include $(BUILD_PACKAGE) include $(call all-makefiles-under,$(LOCAL_PATH)) diff --git a/doze/AndroidManifest.xml b/doze/AndroidManifest.xml index 1adc806..5e31176 100644 --- a/doze/AndroidManifest.xml +++ b/doze/AndroidManifest.xml @@ -1,6 +1,7 @@ - @@ -24,15 +26,34 @@ + + + android:label="@string/device_settings_app_name" + android:persistent="true"> + - + + + + + + + + + + diff --git a/doze/proguard.flags b/doze/proguard.flags index df8c7f3..7f3d4c7 100644 --- a/doze/proguard.flags +++ b/doze/proguard.flags @@ -1,3 +1,8 @@ --keep class org.lineageos.doze.* { +-keepclasseswithmembers class * { + public (android.content.Context, android.util.AttributeSet); +} + +-keep class ** extends android.support.v14.preference.PreferenceFragment +-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 @@ + + + + + + + diff --git a/doze/res/values/strings.xml b/doze/res/drawable/switchbar_background.xml similarity index 70% rename from doze/res/values/strings.xml rename to doze/res/drawable/switchbar_background.xml index 0fea27f..45f1578 100644 --- a/doze/res/values/strings.xml +++ b/doze/res/drawable/switchbar_background.xml @@ -1,6 +1,5 @@ - - - LineageDoze - + + + + + diff --git a/doze/res/layout/doze.xml b/doze/res/layout/doze.xml new file mode 100644 index 0000000..941cdf6 --- /dev/null +++ b/doze/res/layout/doze.xml @@ -0,0 +1,28 @@ + + + + + + + + + diff --git a/doze/res/layout/switch_bar.xml b/doze/res/layout/switch_bar.xml new file mode 100644 index 0000000..d25948b --- /dev/null +++ b/doze/res/layout/switch_bar.xml @@ -0,0 +1,47 @@ + + + + + + + + + + diff --git a/doze/res/values/colors.xml b/doze/res/values/colors.xml new file mode 100644 index 0000000..d598598 --- /dev/null +++ b/doze/res/values/colors.xml @@ -0,0 +1,23 @@ + + + + #ff37474f + #ff7fcac3 + #ff37474F + diff --git a/doze/res/values/styles.xml b/doze/res/values/styles.xml new file mode 100644 index 0000000..f64a2b8 --- /dev/null +++ b/doze/res/values/styles.xml @@ -0,0 +1,30 @@ + + + + + + + + + diff --git a/doze/res/xml/doze_settings.xml b/doze/res/xml/doze_settings.xml new file mode 100644 index 0000000..005c6ae --- /dev/null +++ b/doze/res/xml/doze_settings.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + diff --git a/doze/src/org/lineageos/doze/BootCompletedReceiver.java b/doze/src/com/cyanogenmod/settings/doze/BootCompletedReceiver.java similarity index 73% rename from doze/src/org/lineageos/doze/BootCompletedReceiver.java rename to doze/src/com/cyanogenmod/settings/doze/BootCompletedReceiver.java index d30e007..891729b 100644 --- a/doze/src/org/lineageos/doze/BootCompletedReceiver.java +++ b/doze/src/com/cyanogenmod/settings/doze/BootCompletedReceiver.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2015-2016 The CyanogenMod Project +/* + * 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. @@ -13,7 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.lineageos.doze; + +package com.cyanogenmod.settings.doze; import android.content.BroadcastReceiver; import android.content.Context; @@ -23,11 +24,14 @@ import android.util.Log; public class BootCompletedReceiver extends BroadcastReceiver { private static final boolean DEBUG = false; - private static final String TAG = BootCompletedReceiver.class.getSimpleName(); + 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, DozeService.class)); + if (Utils.isDozeEnabled(context) && Utils.sensorsEnabled(context)) { + if (DEBUG) Log.d(TAG, "Starting service"); + Utils.startService(context); + } } + } diff --git a/doze/src/org/lineageos/doze/DozeService.java b/doze/src/com/cyanogenmod/settings/doze/DozeService.java similarity index 70% rename from doze/src/org/lineageos/doze/DozeService.java rename to doze/src/com/cyanogenmod/settings/doze/DozeService.java index c710654..2ee79bf 100644 --- a/doze/src/org/lineageos/doze/DozeService.java +++ b/doze/src/com/cyanogenmod/settings/doze/DozeService.java @@ -1,5 +1,5 @@ -/** - * Copyright (c) 2015-2016 The CyanogenMod Project +/* + * 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. @@ -13,7 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.lineageos.doze; + +package com.cyanogenmod.settings.doze; import android.app.Service; import android.content.BroadcastReceiver; @@ -21,42 +22,29 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.IBinder; -import android.provider.Settings; import android.util.Log; public class DozeService extends Service { - + private static final String TAG = "DozeService"; private static final boolean DEBUG = false; - private static final String TAG = DozeService.class.getSimpleName(); - private Context mContext; + private ProximitySensor mProximitySensor; private TiltSensor mTiltSensor; - 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(); - } - } - }; - @Override public void onCreate() { if (DEBUG) Log.d(TAG, "Creating service"); - super.onCreate(); - mContext = this; - mTiltSensor = new TiltSensor(mContext); + mProximitySensor = new ProximitySensor(this); + mTiltSensor = new TiltSensor(this); + + IntentFilter screenStateFilter = new IntentFilter(Intent.ACTION_SCREEN_ON); + screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF); + registerReceiver(mScreenStateReceiver, screenStateFilter); } @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; } @@ -64,6 +52,8 @@ public class DozeService extends Service { public void onDestroy() { if (DEBUG) Log.d(TAG, "Destroying service"); super.onDestroy(); + this.unregisterReceiver(mScreenStateReceiver); + mProximitySensor.disable(); mTiltSensor.disable(); } @@ -72,20 +62,36 @@ public class DozeService extends Service { return null; } - 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"); - mTiltSensor.disable(); + if (Utils.pickUpEnabled(this)) { + mTiltSensor.disable(); + } + if (Utils.handwaveGestureEnabled(this) || + Utils.pocketGestureEnabled(this)) { + mProximitySensor.disable(); + } } private void onDisplayOff() { if (DEBUG) Log.d(TAG, "Display off"); - if (isDozeEnabled()) { + if (Utils.pickUpEnabled(this)) { mTiltSensor.enable(); } + if (Utils.handwaveGestureEnabled(this) || + Utils.pocketGestureEnabled(this)) { + mProximitySensor.enable(); + } } + + private BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) { + onDisplayOn(); + } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) { + onDisplayOff(); + } + } + }; } diff --git a/doze/src/com/cyanogenmod/settings/doze/DozeSettingsActivity.java b/doze/src/com/cyanogenmod/settings/doze/DozeSettingsActivity.java new file mode 100644 index 0000000..7e3620a --- /dev/null +++ b/doze/src/com/cyanogenmod/settings/doze/DozeSettingsActivity.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2015-2016 The CyanogenMod Project + * 2017 The LineageOS 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.os.Bundle; +import android.view.MenuItem; + +import com.android.settingslib.drawer.SettingsDrawerActivity; + +public class DozeSettingsActivity extends SettingsDrawerActivity { + + private static final String TAG_DOZE = "doze"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.doze); + getActionBar().setDisplayHomeAsUpEnabled(true); + getFragmentManager().beginTransaction().replace(R.id.content_frame, + new DozeSettingsFragment(), TAG_DOZE).commit(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + onBackPressed(); + return true; + } + return super.onOptionsItemSelected(item); + } +} diff --git a/doze/src/com/cyanogenmod/settings/doze/DozeSettingsFragment.java b/doze/src/com/cyanogenmod/settings/doze/DozeSettingsFragment.java new file mode 100644 index 0000000..7167118 --- /dev/null +++ b/doze/src/com/cyanogenmod/settings/doze/DozeSettingsFragment.java @@ -0,0 +1,158 @@ +/* + * Copyright (C) 2015 The CyanogenMod Project + * 2017 The LineageOS 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.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.DialogFragment; +import android.content.Context; +import android.content.DialogInterface; +import android.content.SharedPreferences; +import android.database.ContentObserver; +import android.os.Bundle; +import android.os.Handler; +import android.support.v14.preference.PreferenceFragment; +import android.support.v14.preference.SwitchPreference; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceCategory; +import android.support.v7.preference.Preference.OnPreferenceChangeListener; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CompoundButton; +import android.widget.Switch; +import android.widget.TextView; + +public class DozeSettingsFragment extends PreferenceFragment implements OnPreferenceChangeListener, + CompoundButton.OnCheckedChangeListener { + + private TextView mTextView; + + private SwitchPreference mPickUpPreference; + private SwitchPreference mHandwavePreference; + private SwitchPreference mPocketPreference; + + @Override + public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { + addPreferencesFromResource(R.xml.doze_settings); + + SharedPreferences prefs = getActivity().getSharedPreferences("doze_settings", + Activity.MODE_PRIVATE); + if (savedInstanceState == null && !prefs.getBoolean("first_help_shown", false)) { + showHelp(); + } + + boolean dozeEnabled = Utils.isDozeEnabled(getActivity()); + + PreferenceCategory proximitySensorCategory = + (PreferenceCategory) getPreferenceScreen().findPreference(Utils.CATEG_PROX_SENSOR); + + mPickUpPreference = (SwitchPreference) findPreference(Utils.GESTURE_PICK_UP_KEY); + mPickUpPreference.setEnabled(dozeEnabled); + + mHandwavePreference = (SwitchPreference) findPreference(Utils.GESTURE_HAND_WAVE_KEY); + mHandwavePreference.setEnabled(dozeEnabled); + + mPocketPreference = (SwitchPreference) findPreference(Utils.GESTURE_POCKET_KEY); + mPocketPreference.setEnabled(dozeEnabled); + + // Hide proximity sensor related features if the device doesn't support them + if (!Utils.getProxCheckBeforePulse(getActivity())) { + getPreferenceScreen().removePreference(proximitySensorCategory); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + final View view = LayoutInflater.from(getContext()).inflate(R.layout.doze, container, false); + ((ViewGroup) view).addView(super.onCreateView(inflater, container, savedInstanceState)); + return view; + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + boolean dozeEnabled = Utils.isDozeEnabled(getActivity()); + + mTextView = (TextView) view.findViewById(R.id.switch_text); + mTextView.setText(getString(dozeEnabled ? + R.string.switch_bar_on : R.string.switch_bar_off)); + + View switchBar = view.findViewById(R.id.switch_bar); + Switch switchWidget = (Switch) switchBar.findViewById(android.R.id.switch_widget); + switchWidget.setChecked(dozeEnabled); + switchWidget.setOnCheckedChangeListener(this); + + switchBar.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + switchWidget.setChecked(!switchWidget.isChecked()); + } + }); + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + Utils.checkDozeService(getActivity()); + return true; + } + + @Override + public void onCheckedChanged(CompoundButton compoundButton, boolean b) { + Utils.enableDoze(b, getActivity()); + Utils.checkDozeService(getActivity()); + + mTextView.setText(getString(b ? R.string.switch_bar_on : R.string.switch_bar_off)); + + mPickUpPreference.setEnabled(b); + mHandwavePreference.setEnabled(b); + mPocketPreference.setEnabled(b); + } + + private static class HelpDialogFragment extends DialogFragment { + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + return new AlertDialog.Builder(getActivity()) + .setTitle(R.string.doze_settings_help_title) + .setMessage(R.string.doze_settings_help_text) + .setNegativeButton(R.string.dialog_ok, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }) + .create(); + } + + @Override + public void onCancel(DialogInterface dialog) { + getActivity().getSharedPreferences("doze_settings", Activity.MODE_PRIVATE) + .edit() + .putBoolean("first_help_shown", true) + .commit(); + } + } + + private void showHelp() { + HelpDialogFragment fragment = new HelpDialogFragment(); + fragment.show(getFragmentManager(), "help_dialog"); + } +} diff --git a/doze/src/com/cyanogenmod/settings/doze/ProximitySensor.java b/doze/src/com/cyanogenmod/settings/doze/ProximitySensor.java new file mode 100644 index 0000000..7d7d3f3 --- /dev/null +++ b/doze/src/com/cyanogenmod/settings/doze/ProximitySensor.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2015 The CyanogenMod Project + * Copyright (C) 2017 The LineageOS 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.Context; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; +import android.util.Log; + +public class ProximitySensor implements SensorEventListener { + + private static final boolean DEBUG = false; + private static final String TAG = "ProximitySensor"; + + // Maximum time for the hand to cover the sensor: 1s + private static final int HANDWAVE_MAX_DELTA_NS = 1000 * 1000 * 1000; + + // Minimum time until the device is considered to have been in the pocket: 2s + private static final int POCKET_MIN_DELTA_NS = 2000 * 1000 * 1000; + + private SensorManager mSensorManager; + private Sensor mSensor; + private Context mContext; + + private boolean mSawNear = false; + private long mInPocketTime = 0; + + public ProximitySensor(Context context) { + mContext = context; + mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); + mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY, false); + } + + @Override + public void onSensorChanged(SensorEvent event) { + boolean isNear = event.values[0] < mSensor.getMaximumRange(); + if (mSawNear && !isNear) { + if (shouldPulse(event.timestamp)) { + Utils.launchDozePulse(mContext); + } + } else { + mInPocketTime = event.timestamp; + } + mSawNear = isNear; + } + + private boolean shouldPulse(long timestamp) { + long delta = timestamp - mInPocketTime; + + if (Utils.handwaveGestureEnabled(mContext) && Utils.pocketGestureEnabled(mContext)) { + return true; + } else if (Utils.handwaveGestureEnabled(mContext)) { + return delta < HANDWAVE_MAX_DELTA_NS; + } else if (Utils.pocketGestureEnabled(mContext)) { + return delta >= POCKET_MIN_DELTA_NS; + } + return false; + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + /* Empty */ + } + + protected void enable() { + if (DEBUG) Log.d(TAG, "Enabling"); + mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL); + } + + protected void disable() { + if (DEBUG) Log.d(TAG, "Disabling"); + mSensorManager.unregisterListener(this, mSensor); + } +} diff --git a/doze/src/org/lineageos/doze/LineageSensor.java b/doze/src/com/cyanogenmod/settings/doze/TiltSensor.java similarity index 54% rename from doze/src/org/lineageos/doze/LineageSensor.java rename to doze/src/com/cyanogenmod/settings/doze/TiltSensor.java index c670d76..b3316e8 100644 --- a/doze/src/org/lineageos/doze/LineageSensor.java +++ b/doze/src/com/cyanogenmod/settings/doze/TiltSensor.java @@ -1,5 +1,6 @@ -/** - * Copyright (c) 2015-2016 The CyanogenMod Project +/* + * Copyright (c) 2015 The CyanogenMod Project + * Copyright (C) 2017 The LineageOS Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,65 +14,67 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.lineageos.doze; + +package com.cyanogenmod.settings.doze; 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.UserHandle; +import android.os.SystemClock; import android.util.Log; -public abstract class LineageSensor { +public class TiltSensor implements SensorEventListener { private static final boolean DEBUG = false; - private static final String TAG = LineageSensor.class.getSimpleName(); + private static final String TAG = "TiltSensor"; - private static final String DOZE_INTENT = "com.android.systemui.doze.pulse"; private static final int BATCH_LATENCY_IN_MS = 100; + private static final int MIN_PULSE_INTERVAL_MS = 2500; - private Context mContext; private SensorManager mSensorManager; private Sensor mSensor; + private Context mContext; - private SensorEventListener mSensorEventListener = new SensorEventListener() { - @Override - public void onSensorChanged(SensorEvent event) { - onSensorEvent(event); - } + private long mEntryTimestamp; - @Override - public void onAccuracyChanged(Sensor sensor, int accuracy) { - // Do nothing - } - }; - - LineageSensor(Context context, int type) { + public TiltSensor(Context context) { mContext = context; mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); - mSensor = mSensorManager.getDefaultSensor(type); + mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_TILT_DETECTOR); } - void launchDozePulse() { - Log.d(TAG, "Launch doze pulse"); - mContext.sendBroadcastAsUser(new Intent(DOZE_INTENT), - new UserHandle(UserHandle.USER_CURRENT)); - } - - void enable() { - if (DEBUG) Log.d(TAG, "Enabling"); - mSensorManager.registerListener(mSensorEventListener, mSensor, - SensorManager.SENSOR_DELAY_NORMAL, BATCH_LATENCY_IN_MS * 1000); - } - - void disable() { - if (DEBUG) Log.d(TAG, "Disabling"); - mSensorManager.unregisterListener(mSensorEventListener); - } - - void onSensorEvent(SensorEvent event) { + @Override + public void onSensorChanged(SensorEvent event) { if (DEBUG) Log.d(TAG, "Got sensor event: " + event.values[0]); + + long delta = SystemClock.elapsedRealtime() - mEntryTimestamp; + if (delta < MIN_PULSE_INTERVAL_MS) { + return; + } else { + mEntryTimestamp = SystemClock.elapsedRealtime(); + } + + if (event.values[0] == 1) { + Utils.launchDozePulse(mContext); + } + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + /* Empty */ + } + + protected void enable() { + if (DEBUG) Log.d(TAG, "Enabling"); + mSensorManager.registerListener(this, mSensor, + SensorManager.SENSOR_DELAY_NORMAL, BATCH_LATENCY_IN_MS * 1000); + mEntryTimestamp = SystemClock.elapsedRealtime(); + } + + protected void disable() { + if (DEBUG) Log.d(TAG, "Disabling"); + mSensorManager.unregisterListener(this, mSensor); } } diff --git a/doze/src/com/cyanogenmod/settings/doze/Utils.java b/doze/src/com/cyanogenmod/settings/doze/Utils.java new file mode 100644 index 0000000..3e55d44 --- /dev/null +++ b/doze/src/com/cyanogenmod/settings/doze/Utils.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2015 The CyanogenMod Project + * 2017 The LineageOS 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.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.UserHandle; +import android.support.v7.preference.PreferenceManager; +import android.provider.Settings; +import android.util.Log; + +import static android.provider.Settings.Secure.DOZE_ENABLED; + +public final class Utils { + + private static final String TAG = "DozeUtils"; + private static final boolean DEBUG = false; + + private static final String DOZE_INTENT = "com.android.systemui.doze.pulse"; + + protected static final String CATEG_PROX_SENSOR = "proximity_sensor"; + + protected static final String GESTURE_PICK_UP_KEY = "gesture_pick_up"; + protected static final String GESTURE_HAND_WAVE_KEY = "gesture_hand_wave"; + protected static final String GESTURE_POCKET_KEY = "gesture_pocket"; + + protected static void startService(Context context) { + if (DEBUG) Log.d(TAG, "Starting service"); + context.startServiceAsUser(new Intent(context, DozeService.class), + UserHandle.CURRENT); + } + + protected static void stopService(Context context) { + if (DEBUG) Log.d(TAG, "Stopping service"); + context.stopServiceAsUser(new Intent(context, DozeService.class), + UserHandle.CURRENT); + } + + protected static void checkDozeService(Context context) { + if (isDozeEnabled(context) && sensorsEnabled(context)) { + startService(context); + } else { + stopService(context); + } + } + + protected static boolean getProxCheckBeforePulse(Context context) { + try { + Context con = context.createPackageContext("com.android.systemui", 0); + int id = con.getResources().getIdentifier("doze_proximity_check_before_pulse", + "bool", "com.android.systemui"); + return con.getResources().getBoolean(id); + } catch (PackageManager.NameNotFoundException e) { + return false; + } + } + + protected static boolean isDozeEnabled(Context context) { + return Settings.Secure.getInt(context.getContentResolver(), + DOZE_ENABLED, 1) != 0; + } + + protected static boolean enableDoze(boolean enable, Context context) { + return Settings.Secure.putInt(context.getContentResolver(), + DOZE_ENABLED, enable ? 1 : 0); + } + + protected static void launchDozePulse(Context context) { + if (DEBUG) Log.d(TAG, "Launch doze pulse"); + context.sendBroadcastAsUser(new Intent(DOZE_INTENT), + new UserHandle(UserHandle.USER_CURRENT)); + } + + protected static boolean pickUpEnabled(Context context) { + return PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(GESTURE_PICK_UP_KEY, false); + } + + protected static boolean handwaveGestureEnabled(Context context) { + return PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(GESTURE_HAND_WAVE_KEY, false); + } + + protected static boolean pocketGestureEnabled(Context context) { + return PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(GESTURE_POCKET_KEY, false); + } + + protected static boolean sensorsEnabled(Context context) { + return pickUpEnabled(context) || handwaveGestureEnabled(context) + || pocketGestureEnabled(context); + } +} diff --git a/doze/src/org/lineageos/doze/TiltSensor.java b/doze/src/org/lineageos/doze/TiltSensor.java deleted file mode 100644 index 2353f63..0000000 --- a/doze/src/org/lineageos/doze/TiltSensor.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright (c) 2015-2016 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 org.lineageos.doze; - -import android.content.Context; -import android.hardware.Sensor; -import android.hardware.SensorEvent; -import android.os.SystemClock; -import android.util.Log; - -public class TiltSensor extends LineageSensor { - - private static final int MIN_PULSE_INTERVAL_MS = 2500; - - private long mEntryTimestamp; - - TiltSensor(Context context) { - super(context, Sensor.TYPE_TILT_DETECTOR); - } - - @Override - void onSensorEvent(SensorEvent event) { - super.onSensorEvent(event); - - if (SystemClock.elapsedRealtime() - mEntryTimestamp < MIN_PULSE_INTERVAL_MS) { - return; - } - - mEntryTimestamp = SystemClock.elapsedRealtime(); - - if (event.values[0] == 1) { - launchDozePulse(); - } - } -} diff --git a/lineage.dependencies b/lineage.dependencies index ca54c8a..b4b3f7a 100644 --- a/lineage.dependencies +++ b/lineage.dependencies @@ -3,6 +3,10 @@ "repository": "android_device_qcom_common", "target_path": "device/qcom/common" }, + { + "repository": "android_packages_resources_devicesettings", + "target_path": "packages/resources/devicesettings" + }, { "repository": "android_kernel_xiaomi_msm8937", "target_path": "kernel/xiaomi/msm8937" diff --git a/product/display.mk b/product/display.mk index 62d9aed..547b453 100644 --- a/product/display.mk +++ b/product/display.mk @@ -13,7 +13,7 @@ PRODUCT_AAPT_PREBUILT_DPI := hdpi # Ambient display PRODUCT_PACKAGES += \ - LineageDoze + XiaomiDoze # Display HAL PRODUCT_PACKAGES += \