summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/display/AdaptiveSleepPreferenceController.java
diff options
context:
space:
mode:
authorYi Jiang <eejiang@google.com>2020-09-10 14:26:51 -0700
committerYi Jiang <eejiang@google.com>2020-10-13 00:47:13 -0700
commitf5d1841c9d90fb72866a63d058ba5076478014a7 (patch)
treef384cbe9bd3930ee512e8645bde2fb23fbe88d65 /src/com/android/settings/display/AdaptiveSleepPreferenceController.java
parente94d2581f833cacd9576bc461ba65ec276cf5e17 (diff)
downloadpackages_apps_Settings-f5d1841c9d90fb72866a63d058ba5076478014a7.tar.gz
packages_apps_Settings-f5d1841c9d90fb72866a63d058ba5076478014a7.tar.bz2
packages_apps_Settings-f5d1841c9d90fb72866a63d058ba5076478014a7.zip
Merge Screen Attention and Screen timeout Settings.
Bug: 155051311 Test: atest Change-Id: I6f6ee52618eab629a41b28982be32ead5f9ba08d
Diffstat (limited to 'src/com/android/settings/display/AdaptiveSleepPreferenceController.java')
-rw-r--r--src/com/android/settings/display/AdaptiveSleepPreferenceController.java95
1 files changed, 62 insertions, 33 deletions
diff --git a/src/com/android/settings/display/AdaptiveSleepPreferenceController.java b/src/com/android/settings/display/AdaptiveSleepPreferenceController.java
index 9f5b49e47a..dfe6826195 100644
--- a/src/com/android/settings/display/AdaptiveSleepPreferenceController.java
+++ b/src/com/android/settings/display/AdaptiveSleepPreferenceController.java
@@ -1,66 +1,95 @@
/*
* Copyright (C) 2019 The Android Open Source Project
*
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
- * except in compliance with the License. You may obtain a copy of the License at
+ * 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.
+ * 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.android.settings.display;
-import static android.provider.Settings.Secure.ADAPTIVE_SLEEP;
+import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE;
+import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
+import android.os.UserManager;
import android.provider.Settings;
import android.service.attention.AttentionService;
import android.text.TextUtils;
+import androidx.preference.PreferenceScreen;
+
import com.android.settings.R;
-import com.android.settings.core.TogglePreferenceController;
+import com.android.settings.bluetooth.RestrictionUtils;
+import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
+import com.android.settingslib.RestrictedSwitchPreference;
+import com.google.common.annotations.VisibleForTesting;
-public class AdaptiveSleepPreferenceController extends TogglePreferenceController {
- public static final String PREF_NAME = "adaptive_sleep";
- private static final String SYSTEM_KEY = ADAPTIVE_SLEEP;
+/** The controller for Screen attention switch preference. */
+public class AdaptiveSleepPreferenceController {
+ public static final String PREFERENCE_KEY = "adaptive_sleep";
private static final int DEFAULT_VALUE = 0;
+ private RestrictionUtils mRestrictionUtils;
+ private PackageManager mPackageManager;
+ private Context mContext;
- public AdaptiveSleepPreferenceController(Context context, String key) {
- super(context, key);
- }
+ @VisibleForTesting
+ RestrictedSwitchPreference mPreference;
- @Override
- public boolean isChecked() {
- return hasSufficientPermission(mContext.getPackageManager()) && Settings.Secure.getInt(
- mContext.getContentResolver(), SYSTEM_KEY, DEFAULT_VALUE) != DEFAULT_VALUE;
+ public AdaptiveSleepPreferenceController(Context context, RestrictionUtils restrictionUtils) {
+ mContext = context;
+ mRestrictionUtils = restrictionUtils;
+ mPreference = new RestrictedSwitchPreference(context);
+ mPreference.setTitle(R.string.adaptive_sleep_title);
+ mPreference.setSummary(R.string.adaptive_sleep_description);
+ mPreference.setIcon(R.drawable.empty_icon);
+ mPreference.setChecked(isChecked());
+ mPreference.setKey(PREFERENCE_KEY);
+ mPreference.setOnPreferenceClickListener(preference -> {
+ final boolean isChecked = ((RestrictedSwitchPreference) preference).isChecked();
+ Settings.Secure.putInt(context.getContentResolver(),
+ Settings.Secure.ADAPTIVE_SLEEP, isChecked ? 1 : DEFAULT_VALUE);
+ return true;
+ });
+ mPackageManager = context.getPackageManager();
}
- @Override
- public boolean setChecked(boolean isChecked) {
- Settings.Secure.putInt(mContext.getContentResolver(), SYSTEM_KEY,
- isChecked ? 1 : DEFAULT_VALUE);
- return true;
+ public AdaptiveSleepPreferenceController(Context context) {
+ this(context, new RestrictionUtils());
}
- @Override
- @AvailabilityStatus
- public int getAvailabilityStatus() {
- return isControllerAvailable(mContext);
+ /**
+ * Adds the controlled preference to the provided preference screen.
+ */
+ public void addToScreen(PreferenceScreen screen) {
+ final EnforcedAdmin enforcedAdmin = mRestrictionUtils.checkIfRestrictionEnforced(mContext,
+ UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT);
+ if (enforcedAdmin != null) {
+ mPreference.setDisabledByAdmin(enforcedAdmin);
+ } else {
+ mPreference.setEnabled(hasSufficientPermission(mPackageManager));
+ }
+ screen.addPreference(mPreference);
}
- @Override
- public CharSequence getSummary() {
- return mContext.getText(isChecked()
- ? R.string.adaptive_sleep_summary_on
- : R.string.adaptive_sleep_summary_off);
+ @VisibleForTesting
+ boolean isChecked() {
+ return hasSufficientPermission(mContext.getPackageManager()) && Settings.Secure.getInt(
+ mContext.getContentResolver(), Settings.Secure.ADAPTIVE_SLEEP, DEFAULT_VALUE)
+ != DEFAULT_VALUE;
}
public static int isControllerAvailable(Context context) {
@@ -89,4 +118,4 @@ public class AdaptiveSleepPreferenceController extends TogglePreferenceControlle
return attentionPackage != null && packageManager.checkPermission(
Manifest.permission.CAMERA, attentionPackage) == PackageManager.PERMISSION_GRANTED;
}
-}
+} \ No newline at end of file