From e5e7916b06fadb1ea06ab6bb2df9f264d4e8022e Mon Sep 17 00:00:00 2001 From: Al Sutton Date: Mon, 19 Aug 2019 13:42:16 +0100 Subject: Extract common display density configuraiton code We're currently importing the whole of SettingsLib into the Settings Backup Agent just to access a single static method. This seems to have contributed to a the regression which relates to b/139373401. This CL separates the 3 related methods out into their own build target so the amount of code included in SettingsBackupAgent is much smaller. A later CL will remove the original location after all usages have been redirected to this new location Bug: 139373401 Test: atest SettingsProviderTest SettingsLibRoboTests Change-Id: I36fbc21daddcf068491b58db01b66f540ac26cf7 --- packages/SettingsLib/Android.bp | 1 + .../SettingsLib/DisplayDensityUtils/Android.bp | 7 ++ .../DisplayDensityUtils/AndroidManifest.xml | 21 ++++++ .../display/DisplayDensityConfiguration.java | 88 ++++++++++++++++++++++ packages/SettingsProvider/Android.bp | 4 +- .../providers/settings/SettingsBackupAgent.java | 5 +- 6 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 packages/SettingsLib/DisplayDensityUtils/Android.bp create mode 100644 packages/SettingsLib/DisplayDensityUtils/AndroidManifest.xml create mode 100644 packages/SettingsLib/DisplayDensityUtils/src/com/android/settingslib/display/DisplayDensityConfiguration.java diff --git a/packages/SettingsLib/Android.bp b/packages/SettingsLib/Android.bp index fea1831b4c5f..9c8345dafbc8 100644 --- a/packages/SettingsLib/Android.bp +++ b/packages/SettingsLib/Android.bp @@ -25,6 +25,7 @@ android_library { "SettingsLibAdaptiveIcon", "SettingsLibRadioButtonPreference", "WifiTrackerLib", + "SettingsLibDisplayDensityUtils", ], // ANDROIDMK TRANSLATION ERROR: unsupported assignment to LOCAL_SHARED_JAVA_LIBRARIES diff --git a/packages/SettingsLib/DisplayDensityUtils/Android.bp b/packages/SettingsLib/DisplayDensityUtils/Android.bp new file mode 100644 index 000000000000..27d0cb5ad48c --- /dev/null +++ b/packages/SettingsLib/DisplayDensityUtils/Android.bp @@ -0,0 +1,7 @@ +android_library { + name: "SettingsLibDisplayDensityUtils", + + srcs: ["src/**/*.java"], + + min_sdk_version: "21", +} diff --git a/packages/SettingsLib/DisplayDensityUtils/AndroidManifest.xml b/packages/SettingsLib/DisplayDensityUtils/AndroidManifest.xml new file mode 100644 index 000000000000..0a4e2bb34409 --- /dev/null +++ b/packages/SettingsLib/DisplayDensityUtils/AndroidManifest.xml @@ -0,0 +1,21 @@ + + + + + + diff --git a/packages/SettingsLib/DisplayDensityUtils/src/com/android/settingslib/display/DisplayDensityConfiguration.java b/packages/SettingsLib/DisplayDensityUtils/src/com/android/settingslib/display/DisplayDensityConfiguration.java new file mode 100644 index 000000000000..284a9025de64 --- /dev/null +++ b/packages/SettingsLib/DisplayDensityUtils/src/com/android/settingslib/display/DisplayDensityConfiguration.java @@ -0,0 +1,88 @@ +/* + * 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.settingslib.display; + +import android.os.AsyncTask; +import android.os.RemoteException; +import android.os.UserHandle; +import android.util.Log; +import android.view.IWindowManager; +import android.view.WindowManagerGlobal; + +/** Utility methods for controlling the display density. */ +public class DisplayDensityConfiguration { + private static final String LOG_TAG = "DisplayDensityConfig"; + + /** + * Returns the default density for the specified display. + * + * @param displayId the identifier of the display + * @return the default density of the specified display, or {@code -1} if the display does not + * exist or the density could not be obtained + */ + static int getDefaultDisplayDensity(int displayId) { + try { + final IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); + return wm.getInitialDisplayDensity(displayId); + } catch (RemoteException exc) { + return -1; + } + } + + /** + * Asynchronously applies display density changes to the specified display. + * + *

The change will be applied to the user specified by the value of {@link + * UserHandle#myUserId()} at the time the method is called. + * + * @param displayId the identifier of the display to modify + */ + public static void clearForcedDisplayDensity(final int displayId) { + final int userId = UserHandle.myUserId(); + AsyncTask.execute( + () -> { + try { + final IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); + wm.clearForcedDisplayDensityForUser(displayId, userId); + } catch (RemoteException exc) { + Log.w(LOG_TAG, "Unable to clear forced display density setting"); + } + }); + } + + /** + * Asynchronously applies display density changes to the specified display. + * + *

The change will be applied to the user specified by the value of {@link + * UserHandle#myUserId()} at the time the method is called. + * + * @param displayId the identifier of the display to modify + * @param density the density to force for the specified display + */ + public static void setForcedDisplayDensity(final int displayId, final int density) { + final int userId = UserHandle.myUserId(); + AsyncTask.execute( + () -> { + try { + final IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); + wm.setForcedDisplayDensityForUser(displayId, density, userId); + } catch (RemoteException exc) { + Log.w(LOG_TAG, "Unable to save forced display density setting"); + } + }); + } +} diff --git a/packages/SettingsProvider/Android.bp b/packages/SettingsProvider/Android.bp index e54b847f167d..0144dd47b99b 100644 --- a/packages/SettingsProvider/Android.bp +++ b/packages/SettingsProvider/Android.bp @@ -11,7 +11,7 @@ android_app { ], static_libs: [ "junit", - "SettingsLib", + "SettingsLibDisplayDensityUtils", ], platform_apis: true, certificate: "platform", @@ -30,7 +30,7 @@ android_test { ], static_libs: [ "androidx.test.rules", - "SettingsLib", + "SettingsLibDisplayDensityUtils", ], libs: [ "android.test.base", diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java index 36e945fe30b6..50528a1f96e3 100644 --- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java +++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java @@ -45,7 +45,7 @@ import android.view.Display; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.ArrayUtils; import com.android.internal.widget.LockPatternUtils; -import com.android.settingslib.display.DisplayDensityUtils; +import com.android.settingslib.display.DisplayDensityConfiguration; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; @@ -1035,7 +1035,8 @@ public class SettingsBackupAgent extends BackupAgentHelper { if (previousDensity == null || previousDensity != newDensity) { // From nothing to something is a change. - DisplayDensityUtils.setForcedDisplayDensity(Display.DEFAULT_DISPLAY, newDensity); + DisplayDensityConfiguration.setForcedDisplayDensity( + Display.DEFAULT_DISPLAY, newDensity); } } -- cgit v1.2.3-59-g8ed1b