From 42ed17551b09c4ad0997a117f9f079769ccfc903 Mon Sep 17 00:00:00 2001 From: Bruno Martins Date: Tue, 1 Jan 2019 12:39:38 +0000 Subject: [PATCH] samsung: doze: Add AOD support to Ambient Display * This makes the always on display mode feature visible for those devices that explicitly set it as available via AOSP overlay. Should only be enabled on devices where the display has been tuned to be power efficient in DOZE and/or DOZE_SUSPEND states. Change-Id: If543936f9421dd7a6c0be594f7cb76afb227e34b --- doze/res/xml/gesture_panel.xml | 8 ++++++ .../settings/doze/SamsungDozeSettings.java | 27 ++++++++++++++++++- .../org/lineageos/settings/doze/Utils.java | 21 ++++++++++++++- 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/doze/res/xml/gesture_panel.xml b/doze/res/xml/gesture_panel.xml index 4665c3a..c281eb1 100644 --- a/doze/res/xml/gesture_panel.xml +++ b/doze/res/xml/gesture_panel.xml @@ -1,6 +1,7 @@ + + diff --git a/doze/src/org/lineageos/settings/doze/SamsungDozeSettings.java b/doze/src/org/lineageos/settings/doze/SamsungDozeSettings.java index 681ebd2..cdf6ac2 100644 --- a/doze/src/org/lineageos/settings/doze/SamsungDozeSettings.java +++ b/doze/src/org/lineageos/settings/doze/SamsungDozeSettings.java @@ -1,5 +1,6 @@ /* * Copyright (C) 2015 The CyanogenMod Project + * 2017-2019 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. @@ -38,6 +39,7 @@ public class SamsungDozeSettings extends PreferenceFragment private TextView mTextView; private View mSwitchBar; + private SwitchPreference mAlwaysOnDisplayPreference; private SwitchPreference mHandwavePreference; private SwitchPreference mPocketPreference; @@ -49,6 +51,10 @@ public class SamsungDozeSettings extends PreferenceFragment boolean dozeEnabled = Utils.isDozeEnabled(getActivity()); + mAlwaysOnDisplayPreference = findPreference(Utils.ALWAYS_ON_DISPLAY); + mAlwaysOnDisplayPreference.setEnabled(dozeEnabled); + mAlwaysOnDisplayPreference.setOnPreferenceChangeListener(this); + mHandwavePreference = findPreference(Utils.GESTURE_HAND_WAVE_KEY); mHandwavePreference.setEnabled(dozeEnabled); mHandwavePreference.setOnPreferenceChangeListener(this); @@ -56,6 +62,14 @@ public class SamsungDozeSettings extends PreferenceFragment mPocketPreference = findPreference(Utils.GESTURE_POCKET_KEY); mPocketPreference.setEnabled(dozeEnabled); mPocketPreference.setOnPreferenceChangeListener(this); + + // Hide AOD if not supported and set all its dependents otherwise + if (!Utils.alwaysOnDisplayAvailable(getActivity())) { + getPreferenceScreen().removePreference(mAlwaysOnDisplayPreference); + } else { + mHandwavePreference.setDependency(Utils.ALWAYS_ON_DISPLAY); + mPocketPreference.setDependency(Utils.ALWAYS_ON_DISPLAY); + } } @Override @@ -100,7 +114,12 @@ public class SamsungDozeSettings extends PreferenceFragment @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - Utils.enableGesture(getActivity(), preference.getKey(), (Boolean) newValue); + if (Utils.ALWAYS_ON_DISPLAY.equals(preference.getKey())) { + Utils.enableAlwaysOn(getActivity(), (Boolean) newValue); + } else { + Utils.enableGesture(getActivity(), preference.getKey(), (Boolean) newValue); + } + Utils.checkDozeService(getActivity()); return true; @@ -114,6 +133,12 @@ public class SamsungDozeSettings extends PreferenceFragment mTextView.setText(getString(isChecked ? R.string.switch_bar_on : R.string.switch_bar_off)); mSwitchBar.setActivated(isChecked); + if (!isChecked) { + Utils.enableAlwaysOn(getActivity(), false); + mAlwaysOnDisplayPreference.setChecked(false); + } + mAlwaysOnDisplayPreference.setEnabled(isChecked); + mHandwavePreference.setEnabled(isChecked); mPocketPreference.setEnabled(isChecked); } diff --git a/doze/src/org/lineageos/settings/doze/Utils.java b/doze/src/org/lineageos/settings/doze/Utils.java index ee22cc1..e5b7daa 100644 --- a/doze/src/org/lineageos/settings/doze/Utils.java +++ b/doze/src/org/lineageos/settings/doze/Utils.java @@ -17,10 +17,12 @@ package org.lineageos.settings.doze; +import static android.provider.Settings.Secure.DOZE_ALWAYS_ON; import static android.provider.Settings.Secure.DOZE_ENABLED; import android.content.Context; import android.content.Intent; +import android.hardware.display.AmbientDisplayConfiguration; import android.os.UserHandle; import android.provider.Settings; import android.util.Log; @@ -32,6 +34,8 @@ public final class Utils { private static final String TAG = "DozeUtils"; private static final boolean DEBUG = false; + protected static final String ALWAYS_ON_DISPLAY = "always_on_display"; + protected static final String GESTURE_HAND_WAVE_KEY = "gesture_hand_wave"; protected static final String GESTURE_POCKET_KEY = "gesture_pocket"; @@ -48,18 +52,33 @@ public final class Utils { } protected static void checkDozeService(Context context) { - if (isDozeEnabled(context) && isAnyGestureEnabled(context)) { + if (!isAlwaysOnEnabled(context) && + isDozeEnabled(context) && isAnyGestureEnabled(context)) { startService(context); } else { stopService(context); } } + protected static boolean alwaysOnDisplayAvailable(Context context) { + return new AmbientDisplayConfiguration(context).alwaysOnAvailable(); + } + + private static boolean isAlwaysOnEnabled(Context context) { + return Settings.Secure.getIntForUser(context.getContentResolver(), + DOZE_ALWAYS_ON, 1, UserHandle.USER_CURRENT) != 0; + } + protected static boolean isDozeEnabled(Context context) { return Settings.Secure.getInt(context.getContentResolver(), DOZE_ENABLED, 1) != 0; } + 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 enableDoze(Context context, boolean enable) { return Settings.Secure.putInt(context.getContentResolver(), DOZE_ENABLED, enable ? 1 : 0); -- 2.20.1