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    // Called with SyncRoot lock held.
46    public DisplayAdapter(DisplayManagerService.SyncRoot syncRoot,
47            Context context, Handler handler, Listener listener, String name) {
48        mSyncRoot = syncRoot;
49        mContext = context;
50        mHandler = handler;
51        mListener = listener;
52        mName = name;
53    }
54
55    /**
56     * Gets the object that the display adapter should synchronize on when handling
57     * calls that come in from outside of the display manager service.
58     */
59    public final DisplayManagerService.SyncRoot getSyncRoot() {
60        return mSyncRoot;
61    }
62
63    /**
64     * Gets the display adapter's context.
65     */
66    public final Context getContext() {
67        return mContext;
68    }
69
70    /**
71     * Gets a handler that the display adapter may use to post asynchronous messages.
72     */
73    public final Handler getHandler() {
74        return mHandler;
75    }
76
77    /**
78     * Gets the display adapter name for debugging purposes.
79     */
80    public final String getName() {
81        return mName;
82    }
83
84    /**
85     * Registers the display adapter with the display manager.
86     *
87     * The display adapter should register any built-in display devices as soon as possible.
88     * The boot process will wait for the default display to be registered.
89     * Other display devices can be registered dynamically later.
90     */
91    public void registerLocked() {
92    }
93
94    /**
95     * Dumps the local state of the display adapter.
96     */
97    public void dumpLocked(PrintWriter pw) {
98    }
99
100    /**
101     * Sends a display device event to the display adapter listener asynchronously.
102     */
103    protected final void sendDisplayDeviceEventLocked(
104            final DisplayDevice device, final int event) {
105        mHandler.post(new Runnable() {
106            @Override
107            public void run() {
108                mListener.onDisplayDeviceEvent(device, event);
109            }
110        });
111    }
112
113    /**
114     * Sends a request to perform traversals.
115     */
116    protected final void sendTraversalRequestLocked() {
117        mHandler.post(new Runnable() {
118            @Override
119            public void run() {
120                mListener.onTraversalRequested();
121            }
122        });
123    }
124
125    public interface Listener {
126        public void onDisplayDeviceEvent(DisplayDevice device, int event);
127        public void onTraversalRequested();
128    }
129}
130