Android R上系统设置里面定制一个系统休眠配置
客制需求里面有个需求要求定制一个系统休眠功能(休眠一定时长后自动关机),并且可以设置时间;
Index: packages/apps/Settings/src/com/android/settings/DisplaySettings.java
===================================================================
--- packages/apps/Settings/src/com/android/settings/DisplaySettings.java (revision 10444)
+++ packages/apps/Settings/src/com/android/settings/DisplaySettings.java (revision 10478)
@@ -31,6 +31,7 @@
import com.android.settings.display.TapToWakePreferenceController;
import com.android.settings.display.ThemePreferenceController;
import com.android.settings.display.TimeoutPreferenceController;
+import com.android.settings.display.SleepTimeoutPreferenceController;
import com.android.settings.display.VrDisplayPreferenceController;
import com.android.settings.display.AwColorTemperaturePreferenceController;
import com.android.settings.display.AwEnhanceModePreferenceController;
@@ -49,6 +50,7 @@
private static final String TAG = "DisplaySettings";
private static final String KEY_SCREEN_TIMEOUT = "screen_timeout";
+ private static final String KEY_SHUTDOWN_TIMEOUT = "shutdown_timeout";
@Override
public int getMetricsCategory() {
@@ -90,6 +92,7 @@
controllers.add(new ScreenSaverPreferenceController(context));
controllers.add(new TapToWakePreferenceController(context));
controllers.add(new TimeoutPreferenceController(context, KEY_SCREEN_TIMEOUT));
+ controllers.add(new SleepTimeoutPreferenceController(context, KEY_SHUTDOWN_TIMEOUT));
controllers.add(new VrDisplayPreferenceController(context));
controllers.add(new ShowOperatorNamePreferenceController(context));
controllers.add(new ThemePreferenceController(context));
Index: packages/apps/Settings/src/com/android/settings/display/SleepTimeoutPreferenceController.java
===================================================================
--- packages/apps/Settings/src/com/android/settings/display/SleepTimeoutPreferenceController.java (nonexistent)
+++ packages/apps/Settings/src/com/android/settings/display/SleepTimeoutPreferenceController.java (revision 10478)
@@ -0,0 +1,132 @@
+/*
+ * Copyright (C) 2016 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.SHUTDOWN_TIMEOUT;
+
+import android.app.admin.DevicePolicyManager;
+import android.content.Context;
+import android.os.UserHandle;
+import android.os.UserManager;
+import android.provider.Settings;
+import android.util.Log;
+
+import androidx.preference.Preference;
+
+import com.android.settings.R;
+import com.android.settings.core.PreferenceControllerMixin;
+import com.android.settingslib.RestrictedLockUtils;
+import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
+import com.android.settingslib.RestrictedLockUtilsInternal;
+import com.android.settingslib.core.AbstractPreferenceController;
+
+public class SleepTimeoutPreferenceController extends AbstractPreferenceController implements
+ PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
+
+ private static final String TAG = "SleepTimeoutPref";
+
+ /** If there is no setting in the provider, use this. */
+ public static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 3600000;
+
+ private final String mScreenTimeoutKey;
+
+ public SleepTimeoutPreferenceController(Context context, String key) {
+ super(context);
+ mScreenTimeoutKey = key;
+ }
+
+ @Override
+ public boolean isAvailable() {
+ return true;
+ }
+
+ @Override
+ public String getPreferenceKey() {
+ return mScreenTimeoutKey;
+ }
+
+ @Override
+ public void updateState(Preference preference) {
+ final TimeoutListPreference timeoutListPreference = (TimeoutListPreference) preference;
+ final long currentTimeout = Settings.System.getLong(mContext.getContentResolver(),
+ SHUTDOWN_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE);
+ Log.e(TAG, "updateState currentTimeout="+currentTimeout);
+ timeoutListPreference.setValue(String.valueOf(currentTimeout));
+ final DevicePolicyManager dpm =
+ (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
+ if (dpm != null) {
+ final RestrictedLockUtils.EnforcedAdmin admin =
+ RestrictedLockUtilsInternal.checkIfMaximumTimeToLockIsSet(mContext);
+ final long maxTimeout =
+ dpm.getMaximumTimeToLock(null /* admin */, UserHandle.myUserId());
+ timeoutListPreference.removeUnusableTimeouts(maxTimeout, admin);
+ }
+ updateTimeoutPreferenceDescription(timeoutListPreference,
+ Long.parseLong(timeoutListPreference.getValue()));
+
+ final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
+ mContext, UserManager.DISALLOW_CONFIG_SCREEN_TIMEOUT,
+ UserHandle.myUserId());
+ if (admin != null) {
+ timeoutListPreference.removeUnusableTimeouts(0/* disable all*/, admin);
+ }
+ }
+
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ try {
+ int value = Integer.parseInt((String) newValue);
+ Log.e(TAG, "onPreferenceChange currentTimeout="+value);
+ Settings.System.putInt(mContext.getContentResolver(), SHUTDOWN_TIMEOUT, value);
+ updateTimeoutPreferenceDescription((TimeoutListPreference) preference, value);
+ } catch (NumberFormatException e) {
+ Log.e(TAG, "could not persist screen timeout setting", e);
+ }
+ return true;
+ }
+
+ public static CharSequence getTimeoutDescription(
+ long currentTimeout, CharSequence[] entries, CharSequence[] values) {
+ if (currentTimeout < 0 || entries == null || values == null
+ || values.length != entries.length) {
+ return null;
+ }
+
+ for (int i = 0; i < values.length; i++) {
+ long timeout = Long.parseLong(values[i].toString());
+ if (currentTimeout == timeout) {
+ return entries[i];
+ }
+ }
+ return null;
+ }
+
+ private void updateTimeoutPreferenceDescription(TimeoutListPreference preference,
+ long currentTimeout) {
+ final CharSequence[] entries = preference.getEntries();
+ final CharSequence[] values = preference.getEntryValues();
+ final String summary;
+ if (preference.isDisabledByAdmin()) {
+ summary = mContext.getString(com.android.settings.R.string.disabled_by_policy_title);
+ } else {
+ final CharSequence timeoutDescription = getTimeoutDescription(
+ currentTimeout, entries, values);
+ summary = timeoutDescription == null
+ ? ""
+ : mContext.getString(R.string.screen_timeout_summary, timeoutDescription);
+ }
+ preference.setSummary(summary);
+ }
+
+}
Property changes on: packages/apps/Settings/src/com/android/settings/display/SleepTimeoutPreferenceController.java
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Index: packages/apps/Settings/res/values/strings.xml
===================================================================
--- packages/apps/Settings/res/values/strings.xml (revision 10444)
+++ packages/apps/Settings/res/values/strings.xml (revision 10478)
@@ -2879,6 +2879,9 @@
<!-- Sound & display settings screen, setting option name to change screen timeout -->
<string name="screen_timeout">Screen timeout</string>
+
+ <!-- Sound & display settings screen, setting option name to change Sleep timeout -->
+ <string name="shutdown_timeout">Shutdown timeout</string>
<!-- Sound & display settings screen, setting option name to change screen timeout [CHAR LIMIT=30] -->
<string name="screen_timeout_title">Screen turns off</string>
<!-- Sound & display settings screen, setting option summary to change screen timeout -->
@@ -7663,7 +7666,7 @@
<!-- List of synonyms for the display timeout (how long until the screen turns off) setting, used to match in settings search [CHAR LIMIT=NONE] -->
<string name="keywords_screen_timeout">screen, lock time, timeout, lockscreen</string>
-
+ <string name="keywords_shutdown_timeout">shutdown_timeout</string>
<!-- List of synonyms for Storage settings (everything related to storage on the device), used to match in settings search [CHAR LIMIT=NONE] -->
<string name="keywords_storage_settings">memory, cache, data, delete, clear, free, space</string>
Index: packages/apps/Settings/res/values/arrays.xml
===================================================================
--- packages/apps/Settings/res/values/arrays.xml (revision 10444)
+++ packages/apps/Settings/res/values/arrays.xml (revision 10478)
@@ -49,6 +49,8 @@
<item>10 minutes</item>
<item>30 minutes</item>
</string-array>
+
+
<!-- Do not translate. -->
<string-array name="screen_timeout_values" translatable="false">
@@ -67,6 +69,30 @@
<!-- Do not translate. -->
<item>1800000</item>
</string-array>
+
+ <string-array name="sleep_timeout_entries">
+ <item>15 minutes</item>
+ <item>30 minutes</item>
+ <item>45 minutes</item>
+ <item>60 minutes</item>
+ <item>120 minutes</item>
+ <item>180 minutes</item>
+ </string-array>
+
+ <string-array name="sleep_timeout_values" translatable="false">
+ <!-- Do not translate. -->
+ <item>900000</item>
+ <!-- Do not translate. -->
+ <item>1800000</item>
+ <!-- Do not translate. -->
+ <item>2700000</item>
+ <!-- Do not translate. -->
+ <item>3600000</item>
+ <!-- Do not translate. -->
+ <item>7200000</item>
+ <!-- Do not translate. -->
+ <item>10800000</item>
+ </string-array>
<!-- Display settings. The delay in inactivity before the dream is shown. These are shown in a list dialog. -->
<string-array name="dream_timeout_entries">
Index: packages/apps/Settings/res/xml/display_settings.xml
===================================================================
--- packages/apps/Settings/res/xml/display_settings.xml (revision 10444)
+++ packages/apps/Settings/res/xml/display_settings.xml (revision 10478)
@@ -70,6 +70,14 @@
android:entries="@array/screen_timeout_entries"
android:entryValues="@array/screen_timeout_values"
settings:keywords="@string/keywords_screen_timeout" />
+
+ <com.android.settings.display.TimeoutListPreference
+ android:key="shutdown_timeout"
+ android:title="@string/shutdown_timeout"
+ android:summary="@string/summary_placeholder"
+ android:entries="@array/sleep_timeout_entries"
+ android:entryValues="@array/sleep_timeout_values"
+ settings:keywords="@string/keywords_shutdown_timeout" />
<Preference
android:key="adaptive_sleep_entry"
&soc {
Index: frameworks/base/api/current.txt
===================================================================
--- frameworks/base/api/current.txt (revision 10444)
+++ frameworks/base/api/current.txt (revision 10478)
@@ -40816,6 +40816,7 @@
field public static final String SHOW_GTALK_SERVICE_STATUS = "SHOW_GTALK_SERVICE_STATUS";
field @Deprecated public static final String SHOW_PROCESSES = "show_processes";
field @Deprecated public static final String SHOW_WEB_SUGGESTIONS = "show_web_suggestions";
+ field public static final String SHUTDOWN_TIMEOUT = "shutdown_timeout";
field public static final String SOUND_EFFECTS_ENABLED = "sound_effects_enabled";
field @Deprecated public static final String STAY_ON_WHILE_PLUGGED_IN = "stay_on_while_plugged_in";
field public static final String TEXT_AUTO_CAPS = "auto_caps";
Index: frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java
===================================================================
--- frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java (revision 10444)
+++ frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java (revision 10478)
@@ -117,7 +117,12 @@
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
+import android.app.AlarmManager;
+import android.app.PendingIntent;
+import android.os.Build;
+import android.text.TextUtils;
+
/**
* The power manager service is responsible for coordinating power management
* functions on the device.
@@ -126,7 +131,7 @@
implements Watchdog.Monitor {
private static final String TAG = "PowerManagerService";
- private static final boolean DEBUG = false;
+ private static final boolean DEBUG = true;
private static final boolean DEBUG_SPEW = DEBUG && true;
// Message: Sent when a user activity timeout occurs to update the power state.
@@ -1172,6 +1177,11 @@
filter = new IntentFilter();
filter.addAction(Intent.ACTION_DOCK_EVENT);
mContext.registerReceiver(new DockReceiver(), filter, null, mHandler);
+
+ //by jcZeng start
+ mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
+ mAlarmHandler = new Handler();
+ //end
}
@VisibleForTesting
@@ -2009,6 +2019,16 @@
+ ", mBatteryLevel=" + mBatteryLevel);
}
+
+ //by jcZeng start
+ //cancel shutdown alarm when is plugged
+ if (mIsPowered) {
+ Slog.d(TAG,"jcZeng cancelShutdownTimerAlarm when plug");
+ mHandler.removeMessages(MSG_START_SHUTDOWN_ALARM);
+ cancelShutdownTimerAlarm();
+ }
+ //end
+
if (wasPowered != mIsPowered || oldPlugType != mPlugType) {
mDirty |= DIRTY_IS_POWERED;
@@ -2885,6 +2905,14 @@
mRequestWaitForNegativeProximity);
mRequestWaitForNegativeProximity = false;
+
+ //by jcZeng start
+ if (getWakefulnessLocked() == 0 && mWakeLockSummary == 0) {
+ Slog.d(TAG,"jcZeng startShutdownAlarmWhenSleep when mWakefulness == 0 && mWakeLockSummary == 0");
+ sendShutdownDelayMessage();
+ }
+ //end
+
if (DEBUG_SPEW) {
Slog.d(TAG, "updateDisplayPowerStateLocked: mDisplayReady=" + mDisplayReady
+ ", policy=" + mDisplayPowerRequest.policy
@@ -3074,6 +3102,8 @@
}
if (needDisplaySuspendBlocker && !mHoldingDisplaySuspendBlocker) {
mDisplaySuspendBlocker.acquire();
+ Slog.d(TAG,"jczeng needDisplaySuspendBlocker"+needDisplaySuspendBlocker);
+ cancelShutdownTimerAlarm();
mHoldingDisplaySuspendBlocker = true;
}
@@ -3105,11 +3135,70 @@
// Enable auto-suspend if needed.
if (autoSuspend && mDecoupleHalAutoSuspendModeFromDisplayConfig) {
+ Slog.d(TAG,"jczeng setHalAutoSuspendModeLocked");
setHalAutoSuspendModeLocked(true);
}
}
+ //by jczeng start
+ //The shutdown Alarm Timer
+
+ private AlarmManager mAlarmManager;
+
+ private boolean mIsStartAlarm;
+
+ private Handler mAlarmHandler;
+
+ public void startShutdownAlarmWhenSleep(){
+ if (mIsStartAlarm || mAlarmManager == null)return;
+ Slog.d(TAG,"jcZeng startShutdownAlarmWhenSleep ");
+ long TIME = 1 * 60 * 60 * 1000;
+ //long TIME = 10 * 60 * 1000;
+ long shutdownTimeOut = 0;
+ try {
+ shutdownTimeOut=Settings.System.getLong(mContext.getContentResolver(),Settings.System.SHUTDOWN_TIMEOUT,TIME);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ Slog.d(TAG,"jcZeng startShutdownAlarmWhenSleep shutdownTimeOut="+shutdownTimeOut);
+ // long time = 3 * 60 * 1000;
+ mAlarmManager.set(AlarmManager.RTC_WAKEUP,
+ System.currentTimeMillis() + shutdownTimeOut, null,mListener,mAlarmHandler);
+ mIsStartAlarm = true;
+ }
+
+ // Message: send start shutdown when sleep
+ private static final int MSG_START_SHUTDOWN_ALARM = 6;
+
+ public void sendShutdownDelayMessage(){
+ Message msg = mHandler.obtainMessage(MSG_START_SHUTDOWN_ALARM);
+ msg.setAsynchronous(true);
+ mHandler.sendMessageDelayed(msg, 3000);
+ }
+
/**
+ * cancel shutdown Alarm
+ */
+ public void cancelShutdownTimerAlarm(){
+ if (mAlarmManager == null || !mIsStartAlarm)return;
+ Thread th=Thread.currentThread();
+ Slog.d(TAG,"jcZeng cancelShutdownTimerAlarm Tread name:"+th.getName());
+ mAlarmManager.cancel(mListener);
+ mIsStartAlarm = false;
+ }
+
+
+ private AlarmManager.OnAlarmListener mListener = new AlarmManager.OnAlarmListener() {
+ @Override
+ public void onAlarm() {
+ Slog.d(TAG,"jczeng onAlarm callback shutdown");
+ mBinderService.wakeUp(SystemClock.uptimeMillis(), PowerManager.WAKE_REASON_UNKNOWN, "wakeUp", mContext.getOpPackageName());
+ mBinderService.shutdown(false,"",false);
+ }
+ };
+ //end
+
+ /**
* Return true if we must keep a suspend blocker active on behalf of the display.
* We do so if the screen is on or is in transition between states.
*/
@@ -4448,6 +4537,10 @@
case MSG_ATTENTIVE_TIMEOUT:
handleAttentiveTimeout();
break;
+ case MSG_START_SHUTDOWN_ALARM:
+ Slog.d(TAG,"jcZeng MSG_START_SHUTDOWN_ALARM");
+ startShutdownAlarmWhenSleep();
+ break;
}
return true;
Index: frameworks/base/core/java/android/provider/Settings.java
===================================================================
--- frameworks/base/core/java/android/provider/Settings.java (revision 10444)
+++ frameworks/base/core/java/android/provider/Settings.java (revision 10478)
@@ -3099,6 +3099,8 @@
// At one time in System, then Global, but now back in Secure
MOVED_TO_SECURE.add(Secure.INSTALL_NON_MARKET_APPS);
+ //addd SHUTDOWN_TIMEOUT
+ //MOVED_TO_SECURE.add(SHUTDOWN_TIMEOUT);
}
@UnsupportedAppUsage
@@ -3983,6 +3985,10 @@
* The screen backlight brightness between 0 and 255.
*/
public static final String SCREEN_BRIGHTNESS = "screen_brightness";
+ /**
+ * add shutdown_timeout
+ */
+ public static final String SHUTDOWN_TIMEOUT = "shutdown_timeout";
/**
* The screen backlight brightness between 0 and 255.
@@ -4825,10 +4831,13 @@
PUBLIC_SETTINGS.add(DIM_SCREEN);
PUBLIC_SETTINGS.add(SCREEN_OFF_TIMEOUT);
PUBLIC_SETTINGS.add(SCREEN_BRIGHTNESS);
+ //add SHUTDOWN_TIMEOUT
+ PUBLIC_SETTINGS.add(SHUTDOWN_TIMEOUT);
PUBLIC_SETTINGS.add(SCREEN_BRIGHTNESS_FLOAT);
PUBLIC_SETTINGS.add(SCREEN_BRIGHTNESS_FOR_VR);
PUBLIC_SETTINGS.add(SCREEN_BRIGHTNESS_FOR_VR_FLOAT);
PUBLIC_SETTINGS.add(SCREEN_BRIGHTNESS_MODE);
+
PUBLIC_SETTINGS.add(MODE_RINGER_STREAMS_AFFECTED);
PUBLIC_SETTINGS.add(MUTE_STREAMS_AFFECTED);
PUBLIC_SETTINGS.add(VIBRATE_ON);
Index: frameworks/base/non-updatable-api/current.txt
===================================================================
--- frameworks/base/non-updatable-api/current.txt (revision 10444)
+++ frameworks/base/non-updatable-api/current.txt (revision 10478)
@@ -38984,6 +38984,7 @@
field public static final String SHOW_GTALK_SERVICE_STATUS = "SHOW_GTALK_SERVICE_STATUS";
field @Deprecated public static final String SHOW_PROCESSES = "show_processes";
field @Deprecated public static final String SHOW_WEB_SUGGESTIONS = "show_web_suggestions";
+ field public static final String SHUTDOWN_TIMEOUT = "shutdown_timeout";
field public static final String SOUND_EFFECTS_ENABLED = "sound_effects_enabled";
field @Deprecated public static final String STAY_ON_WHILE_PLUGGED_IN = "stay_on_while_plugged_in";
field public static final String TEXT_AUTO_CAPS = "auto_caps";
1G_DDR_FOR_EEA_GMS := false