Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a8e4cd6c authored by wilsonshih's avatar wilsonshih Committed by Vadim Caen
Browse files

Support persist night mode for application(6/N)

Developer now can use UiModeManager setApplicationNightMode to set
the night mode and persist in framework. Next time when start a new
process with the same package and userId, the persisted configuration
will be overload to the process and activities.

Introduce PackageConfigPersister, used to presist configuration for
each package.

Reference: go/wm-config-persist

Bug: 73289295
Test: atest SplashscreenTests
Change-Id: I17025a61526492596705ffe58ce450c328ff9470
parent cf05d926
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -6659,6 +6659,7 @@ package android.app {
    method @NonNull public java.time.LocalTime getCustomNightModeEnd();
    method @NonNull public java.time.LocalTime getCustomNightModeStart();
    method public int getNightMode();
    method public void setApplicationNightMode(int);
    method public void setCustomNightModeEnd(@NonNull java.time.LocalTime);
    method public void setCustomNightModeStart(@NonNull java.time.LocalTime);
    method public void setNightMode(int);
+10 −0
Original line number Diff line number Diff line
@@ -61,6 +61,16 @@ interface IUiModeManager {
     */
    int getNightMode();

    /**
     * Sets the dark mode for the given application. This setting is persisted and will override the
     * system configuration for this application.
     *   1 - notnight mode
     *   2 - night mode
     *   3 - automatic mode switching
     * @throws RemoteException
     */
    void setApplicationNightMode(in int mode);

    /**
     * Tells if UI mode is locked or not.
     */
+42 −2
Original line number Diff line number Diff line
@@ -477,11 +477,13 @@ public class UiModeManager {
     * Changes to night mode take effect globally and will result in a configuration change
     * (and potentially an Activity lifecycle event) being applied to all running apps.
     * Developers interested in an app-local implementation of night mode should consider using
     * {@link android.support.v7.app.AppCompatDelegate#setDefaultNightMode(int)} to manage the
     * -night qualifier locally.
     * {@link #setApplicationNightMode(int)} to set and persist the -night qualifier locally or
     * {@link android.support.v7.app.AppCompatDelegate#setDefaultNightMode(int)} for the
     * backward compatible implementation.
     *
     * @param mode the night mode to set
     * @see #getNightMode()
     * @see #setApplicationNightMode(int)
     */
    public void setNightMode(@NightMode int mode) {
        if (mService != null) {
@@ -493,6 +495,44 @@ public class UiModeManager {
        }
    }

    /**
     * Sets and persist the night mode for this application.
     * <p>
     * The mode can be one of:
     * <ul>
     *   <li><em>{@link #MODE_NIGHT_NO}<em> sets the device into
     *       {@code notnight} mode</li>
     *   <li><em>{@link #MODE_NIGHT_YES}</em> sets the device into
     *       {@code night} mode</li>
     *   <li><em>{@link #MODE_NIGHT_CUSTOM}</em> automatically switches between
     *       {@code night} and {@code notnight} based on the custom time set (or default)</li>
     *   <li><em>{@link #MODE_NIGHT_AUTO}</em> automatically switches between
     *       {@code night} and {@code notnight} based on the device's current
     *       location and certain other sensors</li>
     * </ul>
     * <p>
     * Changes to night mode take effect locally and will result in a configuration change
     * (and potentially an Activity lifecycle event) being applied to this application. The mode
     * is persisted for this application until it is either modified by the application, the
     * user clears the data for the application, or this application is uninstalled.
     * <p>
     * Developers interested in a non-persistent app-local implementation of night mode should
     * consider using {@link android.support.v7.app.AppCompatDelegate#setDefaultNightMode(int)}
     * to manage the -night qualifier locally.
     *
     * @param mode the night mode to set
     * @see #setNightMode(int)
     */
    public void setApplicationNightMode(@NightMode int mode) {
        if (mService != null) {
            try {
                mService.setApplicationNightMode(mode);
            } catch (RemoteException e) {
                throw e.rethrowFromSystemServer();
            }
        }
    }

    /**
     * Returns the currently configured night mode.
     * <p>
+37 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server;
import static android.app.UiModeManager.DEFAULT_PRIORITY;
import static android.app.UiModeManager.MODE_NIGHT_AUTO;
import static android.app.UiModeManager.MODE_NIGHT_CUSTOM;
import static android.app.UiModeManager.MODE_NIGHT_NO;
import static android.app.UiModeManager.MODE_NIGHT_YES;
import static android.app.UiModeManager.PROJECTION_TYPE_AUTOMOTIVE;
import static android.app.UiModeManager.PROJECTION_TYPE_NONE;
@@ -82,6 +83,7 @@ import com.android.internal.util.DumpUtils;
import com.android.server.twilight.TwilightListener;
import com.android.server.twilight.TwilightManager;
import com.android.server.twilight.TwilightState;
import com.android.server.wm.ActivityTaskManagerInternal;
import com.android.server.wm.WindowManagerInternal;

import java.io.FileDescriptor;
@@ -158,6 +160,7 @@ final class UiModeManagerService extends SystemService {
    private NotificationManager mNotificationManager;
    private StatusBarManager mStatusBarManager;
    private WindowManagerInternal mWindowManager;
    private ActivityTaskManagerInternal mActivityTaskManager;
    private AlarmManager mAlarmManager;
    private PowerManager mPowerManager;

@@ -366,6 +369,7 @@ final class UiModeManagerService extends SystemService {
                mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                mWakeLock = mPowerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG);
                mWindowManager = LocalServices.getService(WindowManagerInternal.class);
                mActivityTaskManager = LocalServices.getService(ActivityTaskManagerInternal.class);
                mAlarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
                TwilightManager twilightManager = getLocalService(TwilightManager.class);
                if (twilightManager != null) mTwilightManager = twilightManager;
@@ -749,6 +753,39 @@ final class UiModeManagerService extends SystemService {
            }
        }

        @Override
        public void setApplicationNightMode(@UiModeManager.NightMode int mode)
                throws RemoteException {
            switch (mode) {
                case UiModeManager.MODE_NIGHT_NO:
                case UiModeManager.MODE_NIGHT_YES:
                case UiModeManager.MODE_NIGHT_AUTO:
                case UiModeManager.MODE_NIGHT_CUSTOM:
                    break;
                default:
                    throw new IllegalArgumentException("Unknown mode: " + mode);
            }
            final int configNightMode;
            switch (mode) {
                case MODE_NIGHT_YES:
                    configNightMode = Configuration.UI_MODE_NIGHT_YES;
                    break;
                case MODE_NIGHT_NO:
                    configNightMode = Configuration.UI_MODE_NIGHT_NO;
                    break;
                default:
                    configNightMode = Configuration.UI_MODE_NIGHT_UNDEFINED;
            }
            try {
                final ActivityTaskManagerInternal.PackageConfigurationUpdater updater =
                        mActivityTaskManager.createPackageConfigurationUpdater();
                updater.setNightMode(configNightMode);
                updater.commit();
            } catch (RemoteException e) {
                throw e;
            }
        }

        @Override
        public boolean isUiModeLocked() {
            synchronized (mLock) {
+1 −0
Original line number Diff line number Diff line
@@ -1755,6 +1755,7 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A
        if (_createTime > 0) {
            createTime = _createTime;
        }
        mAtmService.mPackageConfigPersister.updateConfigIfNeeded(this, mUserId, packageName);
    }

    /**
Loading