1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settingslib.display; 18 19 import android.os.AsyncTask; 20 import android.os.RemoteException; 21 import android.os.UserHandle; 22 import android.util.Log; 23 import android.view.IWindowManager; 24 import android.view.WindowManagerGlobal; 25 26 /** Utility methods for controlling the display density. */ 27 public class DisplayDensityConfiguration { 28 private static final String LOG_TAG = "DisplayDensityConfig"; 29 30 /** 31 * Returns the default density for the specified display. 32 * 33 * @param displayId the identifier of the display 34 * @return the default density of the specified display, or {@code -1} if the display does not 35 * exist or the density could not be obtained 36 */ getDefaultDisplayDensity(int displayId)37 static int getDefaultDisplayDensity(int displayId) { 38 try { 39 final IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); 40 return wm.getInitialDisplayDensity(displayId); 41 } catch (RemoteException exc) { 42 return -1; 43 } 44 } 45 46 /** 47 * Asynchronously applies display density changes to the specified display. 48 * 49 * <p>The change will be applied to the user specified by the value of {@link 50 * UserHandle#myUserId()} at the time the method is called. 51 * 52 * @param displayId the identifier of the display to modify 53 */ clearForcedDisplayDensity(final int displayId)54 public static void clearForcedDisplayDensity(final int displayId) { 55 final int userId = UserHandle.myUserId(); 56 AsyncTask.execute( 57 () -> { 58 try { 59 final IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); 60 wm.clearForcedDisplayDensityForUser(displayId, userId); 61 } catch (RemoteException exc) { 62 Log.w(LOG_TAG, "Unable to clear forced display density setting"); 63 } 64 }); 65 } 66 67 /** 68 * Asynchronously applies display density changes to the specified display. 69 * 70 * <p>The change will be applied to the user specified by the value of {@link 71 * UserHandle#myUserId()} at the time the method is called. 72 * 73 * @param displayId the identifier of the display to modify 74 * @param density the density to force for the specified display 75 */ setForcedDisplayDensity(final int displayId, final int density)76 public static void setForcedDisplayDensity(final int displayId, final int density) { 77 final int userId = UserHandle.myUserId(); 78 AsyncTask.execute( 79 () -> { 80 try { 81 final IWindowManager wm = WindowManagerGlobal.getWindowManagerService(); 82 wm.setForcedDisplayDensityForUser(displayId, density, userId); 83 } catch (RemoteException exc) { 84 Log.w(LOG_TAG, "Unable to save forced display density setting"); 85 } 86 }); 87 } 88 } 89