Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_internal / src / org / xwalk / core / internal / extension / api / XWalkDisplayManager.java
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.
4
5 package org.xwalk.core.internal.extension.api;
6
7 import android.os.Build;
8 import android.content.Context;
9 import android.view.Display;
10 import java.util.ArrayList;
11
12 /**
13  * A helper class to abstract the display manager for different Android build version.
14  */
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;
20
21     /**
22      * Return the singleton DisplayManager instance for different android build. 
23      *
24      * TODO(hmin): Need to make it thread-safe.
25      *
26      * @param context The given application context.
27      */
28     public static XWalkDisplayManager getInstance(Context context) {
29         if (mContext != null) {
30             // Would never be happened.
31             assert context.getApplicationContext() == mContext;
32         } else {
33             mContext = context.getApplicationContext();
34         }
35
36         if (mInstance == null) {
37             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
38                 mInstance = new DisplayManagerJBMR1(mContext);
39             } else {
40                 mInstance = new DisplayManagerNull();
41             }
42         }
43         return mInstance;
44     }
45
46     /**
47      * Get information about a logical display.
48      */
49     public abstract Display getDisplay(int displayId);
50
51     /**
52      * Get all currently valid logical displays, including the built-in display.
53      */
54     public abstract Display[] getDisplays();
55
56     /**
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.
60      */
61     public abstract Display[] getPresentationDisplays();
62
63     public void registerDisplayListener(DisplayListener listener) {
64         mListeners.add(listener);
65     }
66
67     public void unregisterDisplayListener(DisplayListener listener) {
68         mListeners.remove(listener);
69     }
70
71     protected void notifyDisplayAdded(int displayId) {
72         for (DisplayListener listener : mListeners)
73             listener.onDisplayAdded(displayId);
74     }
75
76     protected void notifyDisplayRemoved(int displayId) {
77         for (DisplayListener listener : mListeners)
78             listener.onDisplayRemoved(displayId);
79     }
80
81     protected void notifyDisplayChanged(int displayId) {
82         for (DisplayListener listener : mListeners)
83             listener.onDisplayChanged(displayId);
84     }
85
86
87     /**
88      * Listen for display arrival, removal and change event.
89      */
90     public interface DisplayListener {
91         /**
92          * Called whenever a logical display has been added to the system.
93          */
94         public void onDisplayAdded(int displayId);
95
96         /**
97          * Called whenever a logical display has been removed to the system.
98          */
99         public void onDisplayRemoved(int displayId);
100
101         /**
102          * Called whenever a logical display has been changed
103          */
104         public void onDisplayChanged(int displayId);
105     }
106 }