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