diff --git a/parts/Android.bp b/parts/Android.bp new file mode 100644 index 0000000..adc8834 --- /dev/null +++ b/parts/Android.bp @@ -0,0 +1,24 @@ +// +// Copyright (C) 2017-2022 The LineageOS Project +// +// SPDX-License-Identifier: Apache-2.0 +// + +android_app { + name: "XiaomiParts", + + srcs: ["src/**/*.java"], + resource_dirs: ["res"], + certificate: "platform", + platform_apis: true, + privileged: true, + system_ext_specific: true, + + static_libs: [ + "org.lineageos.settings.resources", + ], + + optimize: { + proguard_flags_files: ["proguard.flags"], + }, +} diff --git a/parts/AndroidManifest.xml b/parts/AndroidManifest.xml new file mode 100644 index 0000000..9f88d3c --- /dev/null +++ b/parts/AndroidManifest.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/parts/proguard.flags b/parts/proguard.flags new file mode 100644 index 0000000..f6d7a5b --- /dev/null +++ b/parts/proguard.flags @@ -0,0 +1,3 @@ +-keep class org.lineageos.settings.doze.* { + *; +} diff --git a/parts/res/drawable/ic_hand.xml b/parts/res/drawable/ic_hand.xml new file mode 100644 index 0000000..5f56669 --- /dev/null +++ b/parts/res/drawable/ic_hand.xml @@ -0,0 +1,9 @@ + + + + diff --git a/parts/res/drawable/ic_pickup.xml b/parts/res/drawable/ic_pickup.xml new file mode 100644 index 0000000..a7d5eb7 --- /dev/null +++ b/parts/res/drawable/ic_pickup.xml @@ -0,0 +1,9 @@ + + + + diff --git a/parts/res/drawable/ic_pocket.xml b/parts/res/drawable/ic_pocket.xml new file mode 100644 index 0000000..44ae6e2 --- /dev/null +++ b/parts/res/drawable/ic_pocket.xml @@ -0,0 +1,9 @@ + + + + diff --git a/parts/res/xml/doze_settings.xml b/parts/res/xml/doze_settings.xml new file mode 100644 index 0000000..b28f7c1 --- /dev/null +++ b/parts/res/xml/doze_settings.xml @@ -0,0 +1,71 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/parts/src/org/lineageos/settings/BootCompletedReceiver.java b/parts/src/org/lineageos/settings/BootCompletedReceiver.java new file mode 100644 index 0000000..b74cdd6 --- /dev/null +++ b/parts/src/org/lineageos/settings/BootCompletedReceiver.java @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2015 The CyanogenMod Project + * 2017-2020 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 org.lineageos.settings; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +import org.lineageos.settings.doze.DozeUtils; + +public class BootCompletedReceiver extends BroadcastReceiver { + + private static final boolean DEBUG = false; + private static final String TAG = "XiaomiParts"; + + @Override + public void onReceive(final Context context, Intent intent) { + if (DEBUG) Log.d(TAG, "Received boot completed intent"); + DozeUtils.checkDozeService(context); + } +} diff --git a/parts/src/org/lineageos/settings/doze/DozeService.java b/parts/src/org/lineageos/settings/doze/DozeService.java new file mode 100644 index 0000000..503327b --- /dev/null +++ b/parts/src/org/lineageos/settings/doze/DozeService.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2015 The CyanogenMod Project + * 2017-2018 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 org.lineageos.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 PickupSensor mPickupSensor; + + @Override + public void onCreate() { + if (DEBUG) Log.d(TAG, "Creating service"); + mProximitySensor = new ProximitySensor(this); + mPickupSensor = new PickupSensor(this); + + IntentFilter screenStateFilter = new IntentFilter(); + screenStateFilter.addAction(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(); + mPickupSensor.disable(); + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + private void onDisplayOn() { + if (DEBUG) Log.d(TAG, "Display on"); + if (DozeUtils.isPickUpEnabled(this)) { + mPickupSensor.disable(); + } + if (DozeUtils.isHandwaveGestureEnabled(this) || + DozeUtils.isPocketGestureEnabled(this)) { + mProximitySensor.disable(); + } + } + + private void onDisplayOff() { + if (DEBUG) Log.d(TAG, "Display off"); + if (DozeUtils.isPickUpEnabled(this)) { + mPickupSensor.enable(); + } + if (DozeUtils.isHandwaveGestureEnabled(this) || + DozeUtils.isPocketGestureEnabled(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/parts/src/org/lineageos/settings/doze/DozeSettingsActivity.java b/parts/src/org/lineageos/settings/doze/DozeSettingsActivity.java new file mode 100644 index 0000000..213f0b4 --- /dev/null +++ b/parts/src/org/lineageos/settings/doze/DozeSettingsActivity.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2015-2016 The CyanogenMod Project + * 2017,2021 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 org.lineageos.settings.doze; + +import android.os.Bundle; + +import com.android.settingslib.collapsingtoolbar.CollapsingToolbarBaseActivity; +import com.android.settingslib.collapsingtoolbar.R; + +public class DozeSettingsActivity extends CollapsingToolbarBaseActivity { + + private static final String TAG_DOZE = "doze"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + getFragmentManager().beginTransaction().replace(R.id.content_frame, + new DozeSettingsFragment(), TAG_DOZE).commit(); + } +} diff --git a/parts/src/org/lineageos/settings/doze/DozeSettingsFragment.java b/parts/src/org/lineageos/settings/doze/DozeSettingsFragment.java new file mode 100644 index 0000000..9090978 --- /dev/null +++ b/parts/src/org/lineageos/settings/doze/DozeSettingsFragment.java @@ -0,0 +1,158 @@ +/* + * Copyright (C) 2015 The CyanogenMod Project + * 2017-2019,2021 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 org.lineageos.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.os.Bundle; +import android.os.Handler; +import android.widget.Switch; + +import androidx.preference.Preference; +import androidx.preference.PreferenceCategory; +import androidx.preference.PreferenceFragment; +import androidx.preference.SwitchPreference; + +import com.android.settingslib.widget.MainSwitchPreference; +import com.android.settingslib.widget.OnMainSwitchChangeListener; + +import org.lineageos.settings.R; + +public class DozeSettingsFragment extends PreferenceFragment implements + Preference.OnPreferenceChangeListener, OnMainSwitchChangeListener { + + private MainSwitchPreference mSwitchBar; + + private SwitchPreference mAlwaysOnDisplayPreference; + + private SwitchPreference mPickUpPreference; + private SwitchPreference mHandwavePreference; + private SwitchPreference mPocketPreference; + + private Handler mHandler = new Handler(); + + @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 = DozeUtils.isDozeEnabled(getActivity()); + + mSwitchBar = (MainSwitchPreference) findPreference(DozeUtils.DOZE_ENABLE); + mSwitchBar.addOnSwitchChangeListener(this); + mSwitchBar.setChecked(dozeEnabled); + + mAlwaysOnDisplayPreference = (SwitchPreference) findPreference(DozeUtils.ALWAYS_ON_DISPLAY); + mAlwaysOnDisplayPreference.setEnabled(dozeEnabled); + mAlwaysOnDisplayPreference.setChecked(DozeUtils.isAlwaysOnEnabled(getActivity())); + mAlwaysOnDisplayPreference.setOnPreferenceChangeListener(this); + + PreferenceCategory pickupSensorCategory = (PreferenceCategory) getPreferenceScreen(). + findPreference(DozeUtils.CATEG_PICKUP_SENSOR); + PreferenceCategory proximitySensorCategory = (PreferenceCategory) getPreferenceScreen(). + findPreference(DozeUtils.CATEG_PROX_SENSOR); + + mPickUpPreference = (SwitchPreference) findPreference(DozeUtils.GESTURE_PICK_UP_KEY); + mPickUpPreference.setEnabled(dozeEnabled); + mPickUpPreference.setOnPreferenceChangeListener(this); + + mHandwavePreference = (SwitchPreference) findPreference(DozeUtils.GESTURE_HAND_WAVE_KEY); + mHandwavePreference.setEnabled(dozeEnabled); + mHandwavePreference.setOnPreferenceChangeListener(this); + + mPocketPreference = (SwitchPreference) findPreference(DozeUtils.GESTURE_POCKET_KEY); + mPocketPreference.setEnabled(dozeEnabled); + mPocketPreference.setOnPreferenceChangeListener(this); + + // Hide proximity sensor related features if the device doesn't support them + if (!DozeUtils.getProxCheckBeforePulse(getActivity())) { + getPreferenceScreen().removePreference(proximitySensorCategory); + } + + // Hide AOD if not supported and set all its dependents otherwise + if (!DozeUtils.alwaysOnDisplayAvailable(getActivity())) { + getPreferenceScreen().removePreference(mAlwaysOnDisplayPreference); + } else { + pickupSensorCategory.setDependency(DozeUtils.ALWAYS_ON_DISPLAY); + proximitySensorCategory.setDependency(DozeUtils.ALWAYS_ON_DISPLAY); + } + } + + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + if (DozeUtils.ALWAYS_ON_DISPLAY.equals(preference.getKey())) { + DozeUtils.enableAlwaysOn(getActivity(), (Boolean) newValue); + } + + mHandler.post(() -> DozeUtils.checkDozeService(getActivity())); + + return true; + } + + @Override + public void onSwitchChanged(Switch switchView, boolean isChecked) { + DozeUtils.enableDoze(getActivity(), isChecked); + DozeUtils.checkDozeService(getActivity()); + + mSwitchBar.setChecked(isChecked); + + if (!isChecked) { + DozeUtils.enableAlwaysOn(getActivity(), false); + mAlwaysOnDisplayPreference.setChecked(false); + } + mAlwaysOnDisplayPreference.setEnabled(isChecked); + + mPickUpPreference.setEnabled(isChecked); + mHandwavePreference.setEnabled(isChecked); + mPocketPreference.setEnabled(isChecked); + } + + 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, (dialog, 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/parts/src/org/lineageos/settings/doze/DozeUtils.java b/parts/src/org/lineageos/settings/doze/DozeUtils.java new file mode 100644 index 0000000..db9f095 --- /dev/null +++ b/parts/src/org/lineageos/settings/doze/DozeUtils.java @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2015 The CyanogenMod Project + * 2017-2019,2021 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 org.lineageos.settings.doze; + +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.hardware.display.AmbientDisplayConfiguration; +import android.hardware.Sensor; +import android.hardware.SensorManager; +import android.os.UserHandle; +import android.provider.Settings; +import android.util.Log; + +import androidx.preference.PreferenceManager; + +import static android.provider.Settings.Secure.DOZE_ALWAYS_ON; +import static android.provider.Settings.Secure.DOZE_ENABLED; + +public final class DozeUtils { + + 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 DOZE_ENABLE = "doze_enable"; + + protected static final String ALWAYS_ON_DISPLAY = "always_on_display"; + + protected static final String CATEG_PICKUP_SENSOR = "pickup_sensor"; + 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"; + + public 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); + } + + public static void checkDozeService(Context context) { + if (isDozeEnabled(context) && !isAlwaysOnEnabled(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 enableDoze(Context context, boolean enable) { + return Settings.Secure.putInt(context.getContentResolver(), + DOZE_ENABLED, enable ? 1 : 0); + } + + public static boolean isDozeEnabled(Context context) { + return Settings.Secure.getInt(context.getContentResolver(), + DOZE_ENABLED, 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 enableAlwaysOn(Context context, boolean enable) { + return Settings.Secure.putIntForUser(context.getContentResolver(), + DOZE_ALWAYS_ON, enable ? 1 : 0, UserHandle.USER_CURRENT); + } + + protected static boolean isAlwaysOnEnabled(Context context) { + final boolean enabledByDefault = context.getResources() + .getBoolean(com.android.internal.R.bool.config_dozeAlwaysOnEnabled); + + return Settings.Secure.getIntForUser(context.getContentResolver(), + DOZE_ALWAYS_ON, alwaysOnDisplayAvailable(context) && enabledByDefault ? 1 : 0, + UserHandle.USER_CURRENT) != 0; + } + + protected static boolean alwaysOnDisplayAvailable(Context context) { + return new AmbientDisplayConfiguration(context).alwaysOnAvailable(); + } + + protected static boolean isGestureEnabled(Context context, String gesture) { + return PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean(gesture, false); + } + + protected static boolean isPickUpEnabled(Context context) { + return isGestureEnabled(context, GESTURE_PICK_UP_KEY); + } + + protected static boolean isHandwaveGestureEnabled(Context context) { + return isGestureEnabled(context, GESTURE_HAND_WAVE_KEY); + } + + protected static boolean isPocketGestureEnabled(Context context) { + return isGestureEnabled(context, GESTURE_POCKET_KEY); + } + + public static boolean sensorsEnabled(Context context) { + return isPickUpEnabled(context) || isHandwaveGestureEnabled(context) + || isPocketGestureEnabled(context); + } + + protected static Sensor getSensor(SensorManager sm, String type) { + for (Sensor sensor : sm.getSensorList(Sensor.TYPE_ALL)) { + if (type.equals(sensor.getStringType())) { + return sensor; + } + } + return null; + } +} diff --git a/parts/src/org/lineageos/settings/doze/PickupSensor.java b/parts/src/org/lineageos/settings/doze/PickupSensor.java new file mode 100644 index 0000000..02e4348 --- /dev/null +++ b/parts/src/org/lineageos/settings/doze/PickupSensor.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2015 The CyanogenMod Project + * 2017-2018 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 org.lineageos.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.SystemClock; +import android.util.Log; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +public class PickupSensor implements SensorEventListener { + + private static final boolean DEBUG = false; + private static final String TAG = "PickupSensor"; + + private static final int MIN_PULSE_INTERVAL_MS = 2500; + + private SensorManager mSensorManager; + private Sensor mSensor; + private Context mContext; + private ExecutorService mExecutorService; + + private long mEntryTimestamp; + + public PickupSensor(Context context) { + mContext = context; + mSensorManager = mContext.getSystemService(SensorManager.class); + mSensor = DozeUtils.getSensor(mSensorManager, "xiaomi.sensor.pickup"); + mExecutorService = Executors.newSingleThreadExecutor(); + } + + private Future submit(Runnable runnable) { + return mExecutorService.submit(runnable); + } + + @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; + } + + mEntryTimestamp = SystemClock.elapsedRealtime(); + + if (event.values[0] == 1) { + DozeUtils.launchDozePulse(mContext); + } + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + /* Empty */ + } + + protected void enable() { + if (DEBUG) Log.d(TAG, "Enabling"); + submit(() -> { + mSensorManager.registerListener(this, mSensor, + SensorManager.SENSOR_DELAY_NORMAL); + mEntryTimestamp = SystemClock.elapsedRealtime(); + }); + } + + protected void disable() { + if (DEBUG) Log.d(TAG, "Disabling"); + submit(() -> { + mSensorManager.unregisterListener(this, mSensor); + }); + } +} diff --git a/parts/src/org/lineageos/settings/doze/ProximitySensor.java b/parts/src/org/lineageos/settings/doze/ProximitySensor.java new file mode 100644 index 0000000..52bd0b5 --- /dev/null +++ b/parts/src/org/lineageos/settings/doze/ProximitySensor.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2015 The CyanogenMod Project + * 2017-2018 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 org.lineageos.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; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +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 ExecutorService mExecutorService; + + private boolean mSawNear = false; + private long mInPocketTime = 0; + + public ProximitySensor(Context context) { + mContext = context; + mSensorManager = mContext.getSystemService(SensorManager.class); + mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY, false); + mExecutorService = Executors.newSingleThreadExecutor(); + } + + private Future submit(Runnable runnable) { + return mExecutorService.submit(runnable); + } + + @Override + public void onSensorChanged(SensorEvent event) { + boolean isNear = event.values[0] < mSensor.getMaximumRange(); + if (mSawNear && !isNear) { + if (shouldPulse(event.timestamp)) { + DozeUtils.launchDozePulse(mContext); + } + } else { + mInPocketTime = event.timestamp; + } + mSawNear = isNear; + } + + private boolean shouldPulse(long timestamp) { + long delta = timestamp - mInPocketTime; + + if (DozeUtils.isHandwaveGestureEnabled(mContext) && + DozeUtils.isPocketGestureEnabled(mContext)) { + return true; + } else if (DozeUtils.isHandwaveGestureEnabled(mContext)) { + return delta < HANDWAVE_MAX_DELTA_NS; + } else if (DozeUtils.isPocketGestureEnabled(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"); + submit(() -> { + mSensorManager.registerListener(this, mSensor, + SensorManager.SENSOR_DELAY_NORMAL); + }); + } + + protected void disable() { + if (DEBUG) Log.d(TAG, "Disabling"); + submit(() -> { + mSensorManager.unregisterListener(this, mSensor); + }); + } +} diff --git a/sdm710.mk b/sdm710.mk index e3fbf81..524fb35 100644 --- a/sdm710.mk +++ b/sdm710.mk @@ -120,6 +120,10 @@ PRODUCT_PACKAGES += \ PRODUCT_COPY_FILES += \ $(LOCAL_PATH)/configs/component-overrides.xml:$(TARGET_COPY_OUT_VENDOR)/etc/sysconfig/component-overrides.xml +# Device-specific settings +PRODUCT_PACKAGES += \ + XiaomiParts + # Display PRODUCT_PACKAGES += \ android.hardware.graphics.mapper@2.0-impl-qti-display \