1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.core.internal.extension.api;
7 import android.os.Build;
8 import android.content.Context;
9 import android.view.Display;
10 import java.util.ArrayList;
13 * A helper class to abstract the display manager for different Android build version.
15 public abstract class XWalkDisplayManager {
16 protected final ArrayList<DisplayListener> mListeners = new ArrayList<DisplayListener>();
17 private static XWalkDisplayManager mInstance;
18 // Hold the context of single and global application object of the current process.
19 private static Context mContext;
22 * Return the singleton DisplayManager instance for different android build.
24 * TODO(hmin): Need to make it thread-safe.
26 * @param context The given application context.
28 public static XWalkDisplayManager getInstance(Context context) {
29 if (mContext != null) {
30 // Would never be happened.
31 assert context.getApplicationContext() == mContext;
33 mContext = context.getApplicationContext();
36 if (mInstance == null) {
37 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
38 mInstance = new DisplayManagerJBMR1(mContext);
40 mInstance = new DisplayManagerNull();
47 * Get information about a logical display.
49 public abstract Display getDisplay(int displayId);
52 * Get all currently valid logical displays, including the built-in display.
54 public abstract Display[] getDisplays();
57 * Get all valid secondary displays, excluding the built-in display. The returned array
58 * is sorted for preference. The first display in the returned array is the most preferred
59 * display for presentation show.
61 public abstract Display[] getPresentationDisplays();
63 public void registerDisplayListener(DisplayListener listener) {
64 mListeners.add(listener);
67 public void unregisterDisplayListener(DisplayListener listener) {
68 mListeners.remove(listener);
71 protected void notifyDisplayAdded(int displayId) {
72 for (DisplayListener listener : mListeners)
73 listener.onDisplayAdded(displayId);
76 protected void notifyDisplayRemoved(int displayId) {
77 for (DisplayListener listener : mListeners)
78 listener.onDisplayRemoved(displayId);
81 protected void notifyDisplayChanged(int displayId) {
82 for (DisplayListener listener : mListeners)
83 listener.onDisplayChanged(displayId);
88 * Listen for display arrival, removal and change event.
90 public interface DisplayListener {
92 * Called whenever a logical display has been added to the system.
94 public void onDisplayAdded(int displayId);
97 * Called whenever a logical display has been removed to the system.
99 public void onDisplayRemoved(int displayId);
102 * Called whenever a logical display has been changed
104 public void onDisplayChanged(int displayId);