Merge Screen Attention and Screen timeout Settings.

Bug: 155051311
Test: atest
Change-Id: I6f6ee52618eab629a41b28982be32ead5f9ba08d
This commit is contained in:
Yi Jiang
2020-09-10 14:26:51 -07:00
parent e94d2581f8
commit f5d1841c9d
17 changed files with 661 additions and 541 deletions

View File

@@ -1,66 +0,0 @@
/*
* 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
*
* 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.android.settings.display;
import android.content.Context;
import android.os.UserManager;
import androidx.preference.Preference;
import com.android.settings.bluetooth.RestrictionUtils;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.RestrictedSwitchPreference;
public class AdaptiveSleepDetailPreferenceController extends AdaptiveSleepPreferenceController {
private RestrictionUtils mRestrictionUtils;
public AdaptiveSleepDetailPreferenceController(Context context, String key,
RestrictionUtils restrictionUtils) {
super(context, key);
mRestrictionUtils = restrictionUtils;
}
public AdaptiveSleepDetailPreferenceController(Context context, String key) {
this(context, key, new RestrictionUtils());
}
@Override
@AvailabilityStatus
public int getAvailabilityStatus() {
return mContext.getResources().getBoolean(
com.android.internal.R.bool.config_adaptive_sleep_available)
? AVAILABLE
: UNSUPPORTED_ON_DEVICE;
}
@Override
public boolean isPublicSlice() {
return true;
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
final EnforcedAdmin enforcedAdmin = mRestrictionUtils.checkIfRestrictionEnforced(mContext,
UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT);
if (enforcedAdmin != null) {
((RestrictedSwitchPreference) preference).setDisabledByAdmin(enforcedAdmin);
} else {
preference.setEnabled(hasSufficientPermission(mContext.getPackageManager()));
}
}
}

View File

@@ -11,7 +11,7 @@
* 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
* limitations under the License.
*/
package com.android.settings.display;
@@ -20,44 +20,53 @@ import static com.android.settings.display.AdaptiveSleepPreferenceController.has
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.R;
public class AdaptiveSleepPermissionPreferenceController extends BasePreferenceController {
final static String PREF_NAME = "adaptive_sleep_permission";
private final Intent mIntent;
/**
* The controller of Screen attention's permission warning preference. The preference appears when
* the camera permission is missing for Screen Attention feature.
*/
public class AdaptiveSleepPermissionPreferenceController {
@VisibleForTesting
Preference mPreference;
private PackageManager mPackageManager;
public AdaptiveSleepPermissionPreferenceController(Context context, String key) {
super(context, key);
public AdaptiveSleepPermissionPreferenceController(Context context) {
final String packageName = context.getPackageManager().getAttentionServicePackageName();
mIntent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
mIntent.setData(Uri.parse("package:" + packageName));
}
@Override
@AvailabilityStatus
public int getAvailabilityStatus() {
return AVAILABLE_UNSEARCHABLE;
}
@Override
public boolean handlePreferenceTreeClick(Preference preference) {
if (TextUtils.equals(getPreferenceKey(), preference.getKey())) {
mContext.startActivity(mIntent);
mPackageManager = context.getPackageManager();
final Intent intent = new Intent(
android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + packageName));
mPreference = new Preference(context);
mPreference.setTitle(R.string.adaptive_sleep_title_no_permission);
mPreference.setSummary(R.string.adaptive_sleep_summary_no_permission);
mPreference.setIcon(R.drawable.ic_info_outline_24);
mPreference.setOnPreferenceClickListener(p -> {
context.startActivity(intent);
return true;
}
return super.handlePreferenceTreeClick(preference);
});
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
if (TextUtils.equals(getPreferenceKey(), preference.getKey())) {
preference.setVisible(!hasSufficientPermission(mContext.getPackageManager()));
/**
* Adds the controlled preference to the provided preference screen.
*/
public void addToScreen(PreferenceScreen screen) {
if (!hasSufficientPermission(mPackageManager)) {
screen.addPreference(mPreference);
}
}
/**
* Refreshes the visibility of the preference.
*/
public void updateVisibility() {
mPreference.setVisible(!hasSufficientPermission(mPackageManager));
}
}

View File

@@ -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;
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 isChecked() {
public AdaptiveSleepPreferenceController(Context context) {
this(context, new RestrictionUtils());
}
/**
* 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);
}
@VisibleForTesting
boolean isChecked() {
return hasSufficientPermission(mContext.getPackageManager()) && Settings.Secure.getInt(
mContext.getContentResolver(), SYSTEM_KEY, DEFAULT_VALUE) != DEFAULT_VALUE;
}
@Override
public boolean setChecked(boolean isChecked) {
Settings.Secure.putInt(mContext.getContentResolver(), SYSTEM_KEY,
isChecked ? 1 : DEFAULT_VALUE);
return true;
}
@Override
@AvailabilityStatus
public int getAvailabilityStatus() {
return isControllerAvailable(mContext);
}
@Override
public CharSequence getSummary() {
return mContext.getText(isChecked()
? R.string.adaptive_sleep_summary_on
: R.string.adaptive_sleep_summary_off);
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;
}
}
}

View File

@@ -1,77 +0,0 @@
/*
* 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
*
* 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.android.settings.display;
import static com.android.settings.homepage.contextualcards.slices.ContextualAdaptiveSleepSlice.PREF;
import static com.android.settings.homepage.contextualcards.slices.ContextualAdaptiveSleepSlice.PREF_KEY_INTERACTED;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.os.Bundle;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.search.SearchIndexable;
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class AdaptiveSleepSettings extends DashboardFragment {
private static final String TAG = "AdaptiveSleepSettings";
private Context mContext;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mContext = getContext();
Preference permissionPreference = findPreference(
AdaptiveSleepPermissionPreferenceController.PREF_NAME);
if (permissionPreference != null) {
permissionPreference.setVisible(false);
}
mContext.getSharedPreferences(PREF, Context.MODE_PRIVATE)
.edit()
.putBoolean(PREF_KEY_INTERACTED, true)
.apply();
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.adaptive_sleep_detail;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
public int getMetricsCategory() {
return SettingsEnums.SETTINGS_ADAPTIVE_SLEEP;
}
@Override
public int getHelpResource() {
return R.string.help_url_adaptive_sleep;
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.adaptive_sleep_detail);
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2020 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
*
* 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.android.settings.display;
import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
import static com.android.settings.display.ScreenTimeoutSettings.FALLBACK_SCREEN_TIMEOUT_VALUE;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtilsInternal;
/**
* The controller of {@link ScreenTimeoutSettings}.
*/
public class ScreenTimeoutPreferenceController extends BasePreferenceController {
public static String PREF_NAME = "screen_timeout";
public ScreenTimeoutPreferenceController(Context context, String key) {
super(context, key);
}
@Override
public int getAvailabilityStatus() {
return isDisableByAdmin() ? UNSUPPORTED_ON_DEVICE : AVAILABLE;
}
@Override
public CharSequence getSummary() {
if (isDisableByAdmin()) {
return mContext.getString(com.android.settings.R.string.disabled_by_policy_title);
} else {
final long currentTimeout = getCurrentScreenTimeout();
final CharSequence[] timeoutEntries = mContext.getResources().getStringArray(
R.array.screen_timeout_entries);
final CharSequence[] timeoutValues = mContext.getResources().getStringArray(
R.array.screen_timeout_values);
final CharSequence description = TimeoutPreferenceController.getTimeoutDescription(
currentTimeout, timeoutEntries, timeoutValues);
return mContext.getString(R.string.screen_timeout_summary, description);
}
}
private boolean isDisableByAdmin() {
final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
if (dpm != null) {
final RestrictedLockUtils.EnforcedAdmin admin =
RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
mContext, UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT,
UserHandle.myUserId());
return admin != null;
}
return false;
}
private long getCurrentScreenTimeout() {
return Settings.System.getLong(mContext.getContentResolver(),
SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE);
}
}

View File

@@ -0,0 +1,234 @@
/*
* Copyright (C) 2020 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
*
* 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.android.settings.display;
import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
import android.app.admin.DevicePolicyManager;
import android.app.settings.SettingsEnums;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.UserHandle;
import android.provider.Settings;
import android.util.Log;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.search.actionbar.SearchMenuController;
import com.android.settings.support.actionbar.HelpMenuController;
import com.android.settings.support.actionbar.HelpResourceProvider;
import com.android.settings.widget.RadioButtonPickerFragment;
import com.android.settings.widget.RadioButtonPreferenceWithExtraWidget;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.search.SearchIndexable;
import com.android.settingslib.search.SearchIndexableRaw;
import com.android.settingslib.widget.CandidateInfo;
import com.android.settingslib.widget.FooterPreference;
import java.util.ArrayList;
import java.util.List;
/**
* Fragment that is used to control screen timeout.
*/
@SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC)
public class ScreenTimeoutSettings extends RadioButtonPickerFragment implements
HelpResourceProvider {
private static final String TAG = "ScreenTimeout";
/** If there is no setting in the provider, use this. */
public static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000;
private CharSequence[] mInitialEntries;
private CharSequence[] mInitialValues;
private FooterPreference mPrivacyPreference;
private AdaptiveSleepPreferenceController mAdaptiveSleepController;
private AdaptiveSleepPermissionPreferenceController mAdaptiveSleepPermissionController;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
SearchMenuController.init(this /* host */);
HelpMenuController.init(this /* host */);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
mInitialEntries = getResources().getStringArray(R.array.screen_timeout_entries);
mInitialValues = getResources().getStringArray(R.array.screen_timeout_values);
mAdaptiveSleepController = new AdaptiveSleepPreferenceController(context);
mAdaptiveSleepPermissionController = new AdaptiveSleepPermissionPreferenceController(
context);
mPrivacyPreference = new FooterPreference(context);
mPrivacyPreference.setIcon(R.drawable.ic_privacy_shield_24dp);
mPrivacyPreference.setTitle(R.string.adaptive_sleep_privacy);
mPrivacyPreference.setSelectable(false);
mPrivacyPreference.setLayoutResource(R.layout.preference_footer);
}
@Override
protected List<? extends CandidateInfo> getCandidates() {
final Context context = getContext();
final List<CandidateInfo> candidates = new ArrayList<>();
final long maxTimeout = getMaxScreenTimeout(context);
for (int i = 0; i < mInitialValues.length; ++i) {
if (Long.parseLong(mInitialValues[i].toString()) <= maxTimeout) {
candidates.add(
new TimeoutCandidateInfo(mInitialEntries[i], mInitialValues[i].toString(),
true));
}
}
return candidates;
}
@Override
public void onStart() {
super.onStart();
mAdaptiveSleepPermissionController.updateVisibility();
}
@Override
public void updateCandidates() {
final String defaultKey = getDefaultKey();
final PreferenceScreen screen = getPreferenceScreen();
screen.removeAll();
final List<? extends CandidateInfo> candidateList = getCandidates();
if (candidateList == null) {
return;
}
for (CandidateInfo info : candidateList) {
RadioButtonPreferenceWithExtraWidget pref =
new RadioButtonPreferenceWithExtraWidget(getPrefContext());
bindPreference(pref, info.getKey(), info, defaultKey);
screen.addPreference(pref);
}
mAdaptiveSleepPermissionController.addToScreen(screen);
mAdaptiveSleepController.addToScreen(screen);
screen.addPreference(mPrivacyPreference);
}
@Override
protected String getDefaultKey() {
return getCurrentSystemScreenTimeout(getContext());
}
@Override
protected boolean setDefaultKey(String key) {
setCurrentSystemScreenTimeout(getContext(), key);
return true;
}
@Override
public int getMetricsCategory() {
return SettingsEnums.SCREEN_TIMEOUT;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.screen_timeout_settings;
}
@Override
public int getHelpResource() {
return R.string.help_url_adaptive_sleep;
}
private Long getMaxScreenTimeout(Context context) {
final DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
if (dpm == null) {
return Long.MAX_VALUE;
}
final RestrictedLockUtils.EnforcedAdmin admin =
RestrictedLockUtilsInternal.checkIfMaximumTimeToLockIsSet(context);
if (admin != null) {
return dpm.getMaximumTimeToLock(null /* admin */, UserHandle.myUserId());
}
return Long.MAX_VALUE;
}
private String getCurrentSystemScreenTimeout(Context context) {
if (context == null) {
return Long.toString(FALLBACK_SCREEN_TIMEOUT_VALUE);
} else {
return Long.toString(Settings.System.getLong(context.getContentResolver(),
SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE));
}
}
private void setCurrentSystemScreenTimeout(Context context, String key) {
try {
if (context != null) {
final long value = Long.parseLong(key);
Settings.System.putLong(context.getContentResolver(), SCREEN_OFF_TIMEOUT, value);
}
} catch (NumberFormatException e) {
Log.e(TAG, "could not persist screen timeout setting", e);
}
}
private static class TimeoutCandidateInfo extends CandidateInfo {
private final CharSequence mLabel;
private final String mKey;
TimeoutCandidateInfo(CharSequence label, String key, boolean enabled) {
super(enabled);
mLabel = label;
mKey = key;
}
@Override
public CharSequence loadLabel() {
return mLabel;
}
@Override
public Drawable loadIcon() {
return null;
}
@Override
public String getKey() {
return mKey;
}
}
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.screen_timeout_settings) {
public List<SearchIndexableRaw> getRawDataToIndex(Context context,
boolean enabled) {
final Resources res = context.getResources();
final SearchIndexableRaw data = new SearchIndexableRaw(context);
data.title = res.getString(R.string.adaptive_sleep_title);
data.key = AdaptiveSleepPreferenceController.PREFERENCE_KEY;
data.screenTitle = res.getString(R.string.screen_timeout_title);
data.keywords = res.getString(R.string.adaptive_sleep_title);
final List<SearchIndexableRaw> result = new ArrayList<>(1);
result.add(data);
return result;
}
};
}