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

Commit b3f8dc12 authored by Piotr Wilczyński's avatar Piotr Wilczyński
Browse files

Set restored density to all displays

Bug: 349022353
Test: SettingsBackupAgentTest
Flag: EXEMPT bugfix
Change-Id: Iec565a5c3da70bee79c078b5fa292b2945213c50
parent 82da37e4
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -15,6 +15,4 @@ android_library {
    ],

    srcs: ["src/**/*.java"],

    min_sdk_version: "21",
}
+45 −0
Original line number Diff line number Diff line
@@ -16,13 +16,20 @@

package com.android.settingslib.display;

import android.annotation.NonNull;
import android.content.Context;
import android.hardware.display.DisplayManager;
import android.os.AsyncTask;
import android.os.RemoteException;
import android.os.UserHandle;
import android.util.Log;
import android.view.Display;
import android.view.DisplayInfo;
import android.view.IWindowManager;
import android.view.WindowManagerGlobal;

import java.util.function.Predicate;

/** Utility methods for controlling the display density. */
public class DisplayDensityConfiguration {
    private static final String LOG_TAG = "DisplayDensityConfig";
@@ -85,4 +92,42 @@ public class DisplayDensityConfiguration {
                    }
                });
    }

    /**
     * Asynchronously applies display density changes to all displays that satisfy the predicate.
     *
     * <p>The change will be applied to the user specified by the value of
     * {@link UserHandle#myUserId()} at the time the method is called.
     *
     * @param context The context
     * @param predicate Determines which displays to set the density to
     * @param density The density to force
     */
    public static void setForcedDisplayDensity(@NonNull Context context,
            @NonNull Predicate<DisplayInfo> predicate, final int density) {
        final int userId = UserHandle.myUserId();
        DisplayManager dm = context.getSystemService(DisplayManager.class);
        AsyncTask.execute(() -> {
            try {
                for (Display display : dm.getDisplays(
                        DisplayManager.DISPLAY_CATEGORY_ALL_INCLUDING_DISABLED)) {
                    int displayId = display.getDisplayId();
                    DisplayInfo info = new DisplayInfo();
                    if (!display.getDisplayInfo(info)) {
                        Log.w(LOG_TAG, "Unable to save forced display density setting "
                                + "for display " + displayId);
                        continue;
                    }
                    if (!predicate.test(info)) {
                        continue;
                    }

                    final IWindowManager wm = WindowManagerGlobal.getWindowManagerService();
                    wm.setForcedDisplayDensityForUser(displayId, density, userId);
                }
            } catch (RemoteException exc) {
                Log.w(LOG_TAG, "Unable to save forced display density setting");
            }
        });
    }
}
+3 −3
Original line number Diff line number Diff line
@@ -89,6 +89,7 @@ import java.io.OutputStream;
import java.time.DateTimeException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
@@ -97,7 +98,6 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.HashMap;
import java.util.zip.CRC32;

/**
@@ -1753,8 +1753,8 @@ public class SettingsBackupAgent extends BackupAgentHelper {

        if (previousDensity == null || previousDensity != newDensity) {
            // From nothing to something is a change.
            DisplayDensityConfiguration.setForcedDisplayDensity(
                    Display.DEFAULT_DISPLAY, newDensity);
            DisplayDensityConfiguration.setForcedDisplayDensity(getBaseContext(),
                    info -> info.type == Display.TYPE_INTERNAL, newDensity);
        }
    }