From cd4a323bb4906eec758d0106da959f2f728cfc2d Mon Sep 17 00:00:00 2001 From: davidevinavil Date: Wed, 22 Mar 2017 01:16:32 +0100 Subject: doze: DU-ify --- doze/AndroidManifest.xml | 6 +- doze/proguard.flags | 2 +- .../settings/doze/BootCompletedReceiver.java | 37 ------ .../com/cyanogenmod/settings/doze/DozeService.java | 97 -------------- .../cyanogenmod/settings/doze/DozeSettings.java | 148 --------------------- .../cyanogenmod/settings/doze/ProximitySensor.java | 91 ------------- .../com/cyanogenmod/settings/doze/TiltSensor.java | 87 ------------ doze/src/com/cyanogenmod/settings/doze/Utils.java | 92 ------------- .../du/settings/doze/BootCompletedReceiver.java | 37 ++++++ doze/src/com/du/settings/doze/DozeService.java | 97 ++++++++++++++ doze/src/com/du/settings/doze/DozeSettings.java | 148 +++++++++++++++++++++ doze/src/com/du/settings/doze/ProximitySensor.java | 91 +++++++++++++ doze/src/com/du/settings/doze/TiltSensor.java | 87 ++++++++++++ doze/src/com/du/settings/doze/Utils.java | 92 +++++++++++++ 14 files changed, 556 insertions(+), 556 deletions(-) delete mode 100644 doze/src/com/cyanogenmod/settings/doze/BootCompletedReceiver.java delete mode 100644 doze/src/com/cyanogenmod/settings/doze/DozeService.java delete mode 100644 doze/src/com/cyanogenmod/settings/doze/DozeSettings.java delete mode 100644 doze/src/com/cyanogenmod/settings/doze/ProximitySensor.java delete mode 100644 doze/src/com/cyanogenmod/settings/doze/TiltSensor.java delete mode 100644 doze/src/com/cyanogenmod/settings/doze/Utils.java create mode 100644 doze/src/com/du/settings/doze/BootCompletedReceiver.java create mode 100644 doze/src/com/du/settings/doze/DozeService.java create mode 100644 doze/src/com/du/settings/doze/DozeSettings.java create mode 100644 doze/src/com/du/settings/doze/ProximitySensor.java create mode 100644 doze/src/com/du/settings/doze/TiltSensor.java create mode 100644 doze/src/com/du/settings/doze/Utils.java (limited to 'doze') diff --git a/doze/AndroidManifest.xml b/doze/AndroidManifest.xml index 54783eb..3dfc322 100644 --- a/doze/AndroidManifest.xml +++ b/doze/AndroidManifest.xml @@ -1,6 +1,6 @@ @@ -16,14 +16,14 @@ android:label="ZukDoze" android:persistent="true"> - + - diff --git a/doze/proguard.flags b/doze/proguard.flags index a21e62f..e2dc71a 100644 --- a/doze/proguard.flags +++ b/doze/proguard.flags @@ -1,3 +1,3 @@ --keep class com.cyanogenmod.settings.doze.* { +-keep class com.du.settings.doze.* { *; } diff --git a/doze/src/com/cyanogenmod/settings/doze/BootCompletedReceiver.java b/doze/src/com/cyanogenmod/settings/doze/BootCompletedReceiver.java deleted file mode 100644 index e1b2b50..0000000 --- a/doze/src/com/cyanogenmod/settings/doze/BootCompletedReceiver.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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 = "ZukDoze"; - - @Override - public void onReceive(final Context context, Intent intent) { - if (Utils.isDozeEnabled(context) && Utils.sensorsEnabled(context)) { - if (DEBUG) Log.d(TAG, "Starting service"); - Utils.startService(context); - } - } - -} diff --git a/doze/src/com/cyanogenmod/settings/doze/DozeService.java b/doze/src/com/cyanogenmod/settings/doze/DozeService.java deleted file mode 100644 index 2ee79bf..0000000 --- a/doze/src/com/cyanogenmod/settings/doze/DozeService.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * 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.os.IBinder; -import android.util.Log; - -public class DozeService extends Service { - private static final String TAG = "DozeService"; - private static final boolean DEBUG = false; - - private ProximitySensor mProximitySensor; - private TiltSensor mTiltSensor; - - @Override - public void onCreate() { - if (DEBUG) Log.d(TAG, "Creating service"); - 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"); - return START_STICKY; - } - - @Override - public void onDestroy() { - if (DEBUG) Log.d(TAG, "Destroying service"); - super.onDestroy(); - this.unregisterReceiver(mScreenStateReceiver); - mProximitySensor.disable(); - mTiltSensor.disable(); - } - - @Override - public IBinder onBind(Intent intent) { - return null; - } - - private void onDisplayOn() { - if (DEBUG) Log.d(TAG, "Display on"); - 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 (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/DozeSettings.java b/doze/src/com/cyanogenmod/settings/doze/DozeSettings.java deleted file mode 100644 index 2e179bc..0000000 --- a/doze/src/com/cyanogenmod/settings/doze/DozeSettings.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * 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.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.os.Bundle; -import android.preference.Preference; -import android.preference.Preference.OnPreferenceChangeListener; -import android.preference.PreferenceActivity; -import android.preference.PreferenceScreen; -import android.preference.SwitchPreference; -import android.provider.Settings; -import android.view.Menu; -import android.view.MenuItem; - -public class DozeSettings extends PreferenceActivity implements OnPreferenceChangeListener { - - private Context mContext; - private SharedPreferences mPreferences; - - private SwitchPreference mAmbientDisplayPreference; - private SwitchPreference mPickUpPreference; - private SwitchPreference mHandwavePreference; - private SwitchPreference mPocketPreference; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - addPreferencesFromResource(R.xml.doze_settings); - mContext = getApplicationContext(); - boolean dozeEnabled = Utils.isDozeEnabled(mContext); - - // get shared preference - mPreferences = mContext.getSharedPreferences("doze_settings", Activity.MODE_PRIVATE); - if (savedInstanceState == null && !mPreferences.getBoolean("first_help_shown", false)) { - showHelp(); - } - - mAmbientDisplayPreference = - (SwitchPreference) findPreference(Utils.AMBIENT_DISPLAY_KEY); - // Read from DOZE_ENABLED secure setting - mAmbientDisplayPreference.setChecked(dozeEnabled); - mAmbientDisplayPreference.setOnPreferenceChangeListener(this); - - mPickUpPreference = - (SwitchPreference) findPreference(Utils.PICK_UP_KEY); - mPickUpPreference.setOnPreferenceChangeListener(this); - - mHandwavePreference = - (SwitchPreference) findPreference(Utils.GESTURE_HAND_WAVE_KEY); - mHandwavePreference.setOnPreferenceChangeListener(this); - - mPocketPreference = - (SwitchPreference) findPreference(Utils.GESTURE_POCKET_KEY); - mPocketPreference.setOnPreferenceChangeListener(this); - - final ActionBar actionBar = getActionBar(); - actionBar.setDisplayHomeAsUpEnabled(true); - } - - @Override - protected void onResume() { - super.onResume(); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == android.R.id.home) { - onBackPressed(); - return true; - } - return false; - } - - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - final String key = preference.getKey(); - final boolean value = (Boolean) newValue; - if (Utils.AMBIENT_DISPLAY_KEY.equals(key)) { - mAmbientDisplayPreference.setChecked(value); - Utils.enableDoze(value, mContext); - return true; - } else if (Utils.PICK_UP_KEY.equals(key)) { - mPickUpPreference.setChecked(value); - Utils.startService(mContext); - return true; - } else if (Utils.GESTURE_HAND_WAVE_KEY.equals(key)) { - mHandwavePreference.setChecked(value); - Utils.startService(mContext); - return true; - } else if (Utils.GESTURE_POCKET_KEY.equals(key)) { - mPocketPreference.setChecked(value); - Utils.startService(mContext); - return true; - } - return false; - } - - public 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.dlg_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 deleted file mode 100644 index 0f62b11..0000000 --- a/doze/src/com/cyanogenmod/settings/doze/ProximitySensor.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * 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.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"; - - private static final int POCKET_DELTA_NS = 1000 * 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); - } - - @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) - && !Utils.pocketGestureEnabled(mContext)) { - return delta < POCKET_DELTA_NS; - } else if (!Utils.handwaveGestureEnabled(mContext) - && Utils.pocketGestureEnabled(mContext)) { - return delta >= POCKET_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/com/cyanogenmod/settings/doze/TiltSensor.java b/doze/src/com/cyanogenmod/settings/doze/TiltSensor.java deleted file mode 100644 index 443a10d..0000000 --- a/doze/src/com/cyanogenmod/settings/doze/TiltSensor.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * 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.Context; -import android.hardware.Sensor; -import android.hardware.SensorEvent; -import android.hardware.SensorEventListener; -import android.hardware.SensorManager; -import android.os.PowerManager; -import android.os.PowerManager.WakeLock; -import android.os.SystemClock; -import android.util.Log; - -public class TiltSensor implements SensorEventListener { - - private static final boolean DEBUG = false; - private static final String TAG = "TiltSensor"; - - private static final int SENSOR_WAKELOCK_DURATION = 200; - private static final int BATCH_LATENCY_IN_MS = 100; - private static final int MIN_PULSE_INTERVAL_MS = 2500; - - private PowerManager mPowerManager; - private SensorManager mSensorManager; - private Sensor mSensor; - private WakeLock mSensorWakeLock; - private Context mContext; - - private long mEntryTimestamp; - - public TiltSensor(Context context) { - mContext = context; - mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); - mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); - mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_TILT_DETECTOR); - mSensorWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, - "SensorWakeLock"); - } - - @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 deleted file mode 100644 index 767cd96..0000000 --- a/doze/src/com/cyanogenmod/settings/doze/Utils.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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.ActivityManager; -import android.app.ActivityManager.RunningServiceInfo; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.os.UserHandle; -import android.preference.PreferenceManager; -import android.provider.Settings; -import android.util.Log; - -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 AMBIENT_DISPLAY_KEY = "ambient_display"; - protected static final String PICK_UP_KEY = "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.startService(new Intent(context, DozeService.class)); - } - - protected static void stopService(Context context) { - if (DEBUG) Log.d(TAG, "Stopping service"); - context.stopService(new Intent(context, DozeService.class)); - } - - protected static boolean isDozeEnabled(Context context) { - return Settings.Secure.getInt(context.getContentResolver(), - Settings.Secure.DOZE_ENABLED, 1) != 0; - } - - protected static boolean enableDoze(boolean enable, Context context) { - boolean dozeEnabled = Settings.Secure.putInt(context.getContentResolver(), - Settings.Secure.DOZE_ENABLED, enable ? 1 : 0); - if (enable) { - startService(context); - } else { - stopService(context); - } - return dozeEnabled; - } - - 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(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/com/du/settings/doze/BootCompletedReceiver.java b/doze/src/com/du/settings/doze/BootCompletedReceiver.java new file mode 100644 index 0000000..4e1e691 --- /dev/null +++ b/doze/src/com/du/settings/doze/BootCompletedReceiver.java @@ -0,0 +1,37 @@ +/* + * 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.du.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 = "ZukDoze"; + + @Override + public void onReceive(final Context context, Intent intent) { + if (Utils.isDozeEnabled(context) && Utils.sensorsEnabled(context)) { + if (DEBUG) Log.d(TAG, "Starting service"); + Utils.startService(context); + } + } + +} diff --git a/doze/src/com/du/settings/doze/DozeService.java b/doze/src/com/du/settings/doze/DozeService.java new file mode 100644 index 0000000..68cabca --- /dev/null +++ b/doze/src/com/du/settings/doze/DozeService.java @@ -0,0 +1,97 @@ +/* + * 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.du.settings.doze; + +import android.app.Service; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.IBinder; +import android.util.Log; + +public class DozeService extends Service { + private static final String TAG = "DozeService"; + private static final boolean DEBUG = false; + + private ProximitySensor mProximitySensor; + private TiltSensor mTiltSensor; + + @Override + public void onCreate() { + if (DEBUG) Log.d(TAG, "Creating service"); + 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"); + return START_STICKY; + } + + @Override + public void onDestroy() { + if (DEBUG) Log.d(TAG, "Destroying service"); + super.onDestroy(); + this.unregisterReceiver(mScreenStateReceiver); + mProximitySensor.disable(); + mTiltSensor.disable(); + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + private void onDisplayOn() { + if (DEBUG) Log.d(TAG, "Display on"); + 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 (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/du/settings/doze/DozeSettings.java b/doze/src/com/du/settings/doze/DozeSettings.java new file mode 100644 index 0000000..54b8287 --- /dev/null +++ b/doze/src/com/du/settings/doze/DozeSettings.java @@ -0,0 +1,148 @@ +/* + * 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.du.settings.doze; + +import android.app.ActionBar; +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.os.Bundle; +import android.preference.Preference; +import android.preference.Preference.OnPreferenceChangeListener; +import android.preference.PreferenceActivity; +import android.preference.PreferenceScreen; +import android.preference.SwitchPreference; +import android.provider.Settings; +import android.view.Menu; +import android.view.MenuItem; + +public class DozeSettings extends PreferenceActivity implements OnPreferenceChangeListener { + + private Context mContext; + private SharedPreferences mPreferences; + + private SwitchPreference mAmbientDisplayPreference; + private SwitchPreference mPickUpPreference; + private SwitchPreference mHandwavePreference; + private SwitchPreference mPocketPreference; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.doze_settings); + mContext = getApplicationContext(); + boolean dozeEnabled = Utils.isDozeEnabled(mContext); + + // get shared preference + mPreferences = mContext.getSharedPreferences("doze_settings", Activity.MODE_PRIVATE); + if (savedInstanceState == null && !mPreferences.getBoolean("first_help_shown", false)) { + showHelp(); + } + + mAmbientDisplayPreference = + (SwitchPreference) findPreference(Utils.AMBIENT_DISPLAY_KEY); + // Read from DOZE_ENABLED secure setting + mAmbientDisplayPreference.setChecked(dozeEnabled); + mAmbientDisplayPreference.setOnPreferenceChangeListener(this); + + mPickUpPreference = + (SwitchPreference) findPreference(Utils.PICK_UP_KEY); + mPickUpPreference.setOnPreferenceChangeListener(this); + + mHandwavePreference = + (SwitchPreference) findPreference(Utils.GESTURE_HAND_WAVE_KEY); + mHandwavePreference.setOnPreferenceChangeListener(this); + + mPocketPreference = + (SwitchPreference) findPreference(Utils.GESTURE_POCKET_KEY); + mPocketPreference.setOnPreferenceChangeListener(this); + + final ActionBar actionBar = getActionBar(); + actionBar.setDisplayHomeAsUpEnabled(true); + } + + @Override + protected void onResume() { + super.onResume(); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + onBackPressed(); + return true; + } + return false; + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + final String key = preference.getKey(); + final boolean value = (Boolean) newValue; + if (Utils.AMBIENT_DISPLAY_KEY.equals(key)) { + mAmbientDisplayPreference.setChecked(value); + Utils.enableDoze(value, mContext); + return true; + } else if (Utils.PICK_UP_KEY.equals(key)) { + mPickUpPreference.setChecked(value); + Utils.startService(mContext); + return true; + } else if (Utils.GESTURE_HAND_WAVE_KEY.equals(key)) { + mHandwavePreference.setChecked(value); + Utils.startService(mContext); + return true; + } else if (Utils.GESTURE_POCKET_KEY.equals(key)) { + mPocketPreference.setChecked(value); + Utils.startService(mContext); + return true; + } + return false; + } + + public 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.dlg_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/du/settings/doze/ProximitySensor.java b/doze/src/com/du/settings/doze/ProximitySensor.java new file mode 100644 index 0000000..9a07134 --- /dev/null +++ b/doze/src/com/du/settings/doze/ProximitySensor.java @@ -0,0 +1,91 @@ +/* + * 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.du.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"; + + private static final int POCKET_DELTA_NS = 1000 * 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); + } + + @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) + && !Utils.pocketGestureEnabled(mContext)) { + return delta < POCKET_DELTA_NS; + } else if (!Utils.handwaveGestureEnabled(mContext) + && Utils.pocketGestureEnabled(mContext)) { + return delta >= POCKET_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/com/du/settings/doze/TiltSensor.java b/doze/src/com/du/settings/doze/TiltSensor.java new file mode 100644 index 0000000..874f454 --- /dev/null +++ b/doze/src/com/du/settings/doze/TiltSensor.java @@ -0,0 +1,87 @@ +/* + * 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.du.settings.doze; + +import android.content.Context; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; +import android.os.PowerManager; +import android.os.PowerManager.WakeLock; +import android.os.SystemClock; +import android.util.Log; + +public class TiltSensor implements SensorEventListener { + + private static final boolean DEBUG = false; + private static final String TAG = "TiltSensor"; + + private static final int SENSOR_WAKELOCK_DURATION = 200; + private static final int BATCH_LATENCY_IN_MS = 100; + private static final int MIN_PULSE_INTERVAL_MS = 2500; + + private PowerManager mPowerManager; + private SensorManager mSensorManager; + private Sensor mSensor; + private WakeLock mSensorWakeLock; + private Context mContext; + + private long mEntryTimestamp; + + public TiltSensor(Context context) { + mContext = context; + mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); + mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); + mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_TILT_DETECTOR); + mSensorWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, + "SensorWakeLock"); + } + + @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/du/settings/doze/Utils.java b/doze/src/com/du/settings/doze/Utils.java new file mode 100644 index 0000000..0ad2e0c --- /dev/null +++ b/doze/src/com/du/settings/doze/Utils.java @@ -0,0 +1,92 @@ +/* + * 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.du.settings.doze; + +import android.app.ActivityManager; +import android.app.ActivityManager.RunningServiceInfo; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.UserHandle; +import android.preference.PreferenceManager; +import android.provider.Settings; +import android.util.Log; + +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 AMBIENT_DISPLAY_KEY = "ambient_display"; + protected static final String PICK_UP_KEY = "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.startService(new Intent(context, DozeService.class)); + } + + protected static void stopService(Context context) { + if (DEBUG) Log.d(TAG, "Stopping service"); + context.stopService(new Intent(context, DozeService.class)); + } + + protected static boolean isDozeEnabled(Context context) { + return Settings.Secure.getInt(context.getContentResolver(), + Settings.Secure.DOZE_ENABLED, 1) != 0; + } + + protected static boolean enableDoze(boolean enable, Context context) { + boolean dozeEnabled = Settings.Secure.putInt(context.getContentResolver(), + Settings.Secure.DOZE_ENABLED, enable ? 1 : 0); + if (enable) { + startService(context); + } else { + stopService(context); + } + return dozeEnabled; + } + + 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(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); + } +} -- cgit v1.2.3