DisplayManager.java revision bd6e1500aedc5461e832f69e76341bff0e55fa2b
1/* 2 * Copyright (C) 2012 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 17package android.hardware.display; 18 19import android.content.Context; 20import android.os.Handler; 21import android.util.SparseArray; 22import android.view.CompatibilityInfoHolder; 23import android.view.Display; 24 25/** 26 * Manages the properties of attached displays. 27 * <p> 28 * Get an instance of this class by calling 29 * {@link android.content.Context#getSystemService(java.lang.String) 30 * Context.getSystemService()} with the argument 31 * {@link android.content.Context#DISPLAY_SERVICE}. 32 * </p> 33 */ 34public final class DisplayManager { 35 private static final String TAG = "DisplayManager"; 36 private static final boolean DEBUG = false; 37 38 private final Context mContext; 39 private final DisplayManagerGlobal mGlobal; 40 41 private final Object mLock = new Object(); 42 private final SparseArray<Display> mDisplays = new SparseArray<Display>(); 43 44 /** @hide */ 45 public DisplayManager(Context context) { 46 mContext = context; 47 mGlobal = DisplayManagerGlobal.getInstance(); 48 } 49 50 /** 51 * Gets information about a logical display. 52 * 53 * The display metrics may be adjusted to provide compatibility 54 * for legacy applications. 55 * 56 * @param displayId The logical display id. 57 * @return The display object, or null if there is no valid display with the given id. 58 */ 59 public Display getDisplay(int displayId) { 60 synchronized (mLock) { 61 return getOrCreateDisplayLocked(displayId, false /*assumeValid*/); 62 } 63 } 64 65 /** 66 * Gets all currently valid logical displays. 67 * 68 * @return An array containing all displays. 69 */ 70 public Display[] getDisplays() { 71 int[] displayIds = mGlobal.getDisplayIds(); 72 int expectedCount = displayIds.length; 73 Display[] displays = new Display[expectedCount]; 74 synchronized (mLock) { 75 int actualCount = 0; 76 for (int i = 0; i < expectedCount; i++) { 77 Display display = getOrCreateDisplayLocked(displayIds[i], true /*assumeValid*/); 78 if (display != null) { 79 displays[actualCount++] = display; 80 } 81 } 82 if (actualCount != expectedCount) { 83 Display[] oldDisplays = displays; 84 displays = new Display[actualCount]; 85 System.arraycopy(oldDisplays, 0, displays, 0, actualCount); 86 } 87 } 88 return displays; 89 } 90 91 private Display getOrCreateDisplayLocked(int displayId, boolean assumeValid) { 92 Display display = mDisplays.get(displayId); 93 if (display == null) { 94 display = mGlobal.getCompatibleDisplay(displayId, 95 getCompatibilityInfoForDisplayLocked(displayId)); 96 if (display != null) { 97 mDisplays.put(displayId, display); 98 } 99 } else if (!assumeValid && !display.isValid()) { 100 display = null; 101 } 102 return display; 103 } 104 105 private CompatibilityInfoHolder getCompatibilityInfoForDisplayLocked(int displayId) { 106 CompatibilityInfoHolder cih = null; 107 if (displayId == Display.DEFAULT_DISPLAY) { 108 cih = mContext.getCompatibilityInfo(); 109 } 110 return cih; 111 } 112 113 /** 114 * Registers an display listener to receive notifications about when 115 * displays are added, removed or changed. 116 * 117 * @param listener The listener to register. 118 * @param handler The handler on which the listener should be invoked, or null 119 * if the listener should be invoked on the calling thread's looper. 120 * 121 * @see #unregisterDisplayListener 122 */ 123 public void registerDisplayListener(DisplayListener listener, Handler handler) { 124 mGlobal.registerDisplayListener(listener, handler); 125 } 126 127 /** 128 * Unregisters an input device listener. 129 * 130 * @param listener The listener to unregister. 131 * 132 * @see #registerDisplayListener 133 */ 134 public void unregisterDisplayListener(DisplayListener listener) { 135 mGlobal.unregisterDisplayListener(listener); 136 } 137 138 /** 139 * Listens for changes in available display devices. 140 */ 141 public interface DisplayListener { 142 /** 143 * Called whenever a logical display has been added to the system. 144 * Use {@link DisplayManager#getDisplay} to get more information about 145 * the display. 146 * 147 * @param displayId The id of the logical display that was added. 148 */ 149 void onDisplayAdded(int displayId); 150 151 /** 152 * Called whenever a logical display has been removed from the system. 153 * 154 * @param displayId The id of the logical display that was removed. 155 */ 156 void onDisplayRemoved(int displayId); 157 158 /** 159 * Called whenever the properties of a logical display have changed. 160 * 161 * @param displayId The id of the logical display that changed. 162 */ 163 void onDisplayChanged(int displayId); 164 } 165} 166