DisplayAdapter.java revision 4ed8fe75e1dde1a2b9576f3862aecc5a572c56b5
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 com.android.server.display;
18
19import android.content.Context;
20import android.os.Handler;
21
22import java.io.PrintWriter;
23
24/**
25 * A display adapter makes zero or more display devices available to the system
26 * and provides facilities for discovering when displays are connected or disconnected.
27 * <p>
28 * For now, all display adapters are registered in the system server but
29 * in principle it could be done from other processes.
30 * </p><p>
31 * Display adapters are guarded by the {@link DisplayManagerService.SyncRoot} lock.
32 * </p>
33 */
34abstract class DisplayAdapter {
35    private final DisplayManagerService.SyncRoot mSyncRoot;
36    private final Context mContext;
37    private final Handler mHandler;
38    private final Listener mListener;
39    private final String mName;
40
41    public static final int DISPLAY_DEVICE_EVENT_ADDED = 1;
42    public static final int DISPLAY_DEVICE_EVENT_CHANGED = 2;
43    public static final int DISPLAY_DEVICE_EVENT_REMOVED = 3;
44
45    public DisplayAdapter(DisplayManagerService.SyncRoot syncRoot,
46            Context context, Handler handler, Listener listener, String name) {
47        mSyncRoot = syncRoot;
48        mContext = context;
49        mHandler = handler;
50        mListener = listener;
51        mName = name;
52    }
53
54    /**
55     * Gets the object that the display adapter should synchronize on when handling
56     * calls that come in from outside of the display manager service.
57     */
58    public final DisplayManagerService.SyncRoot getSyncRoot() {
59        return mSyncRoot;
60    }
61
62    /**
63     * Gets the display adapter's context.
64     */
65    public final Context getContext() {
66        return mContext;
67    }
68
69    /**
70     * Gets a handler that the display adapter may use to post asynchronous messages.
71     */
72    public final Handler getHandler() {
73        return mHandler;
74    }
75
76    /**
77     * Gets the display adapter name for debugging purposes.
78     */
79    public final String getName() {
80        return mName;
81    }
82
83    /**
84     * Registers the display adapter with the display manager.
85     *
86     * The display adapter should register any built-in display devices as soon as possible.
87     * The boot process will wait for the default display to be registered.
88     * Other display devices can be registered dynamically later.
89     */
90    public void registerLocked() {
91    }
92
93    /**
94     * Dumps the local state of the display adapter.
95     */
96    public void dumpLocked(PrintWriter pw) {
97    }
98
99    /**
100     * Sends a display device event to the display adapter listener asynchronously.
101     */
102    protected final void sendDisplayDeviceEventLocked(
103            final DisplayDevice device, final int event) {
104        mHandler.post(new Runnable() {
105            @Override
106            public void run() {
107                mListener.onDisplayDeviceEvent(device, event);
108            }
109        });
110    }
111
112    /**
113     * Sends a request to perform traversals.
114     */
115    protected final void sendTraversalRequestLocked() {
116        mHandler.post(new Runnable() {
117            @Override
118            public void run() {
119                mListener.onTraversalRequested();
120            }
121        });
122    }
123
124    public interface Listener {
125        public void onDisplayDeviceEvent(DisplayDevice device, int event);
126        public void onTraversalRequested();
127    }
128}
129