1/*
2 * Copyright (C) 2017 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.googlecode.android_scripting.facade;
18
19import java.util.HashMap;
20
21import android.app.Service;
22import android.content.Context;
23import android.graphics.Point;
24import android.hardware.display.DisplayManager;
25import android.util.DisplayMetrics;
26import android.view.Display;
27
28import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
29import com.googlecode.android_scripting.rpc.Rpc;
30import com.googlecode.android_scripting.rpc.RpcDefault;
31import com.googlecode.android_scripting.rpc.RpcParameter;
32
33public class DisplayFacade extends RpcReceiver {
34
35    private final Service mService;
36    private final DisplayManager mDisplayManager;
37    private HashMap<Integer, Display> mDisplays;
38
39    public DisplayFacade(FacadeManager manager) {
40        super(manager);
41        mService = manager.getService();
42        mDisplayManager = (DisplayManager) mService.getSystemService(Context.DISPLAY_SERVICE);
43        updateDisplays(mDisplayManager.getDisplays());
44    }
45
46    private void updateDisplays(Display[] displays) {
47        if (mDisplays == null) {
48            mDisplays = new HashMap<Integer, Display>();
49        }
50        mDisplays.clear();
51        for(Display d : displays) {
52            mDisplays.put(d.getDisplayId(), d);
53        }
54    }
55
56    @Rpc(description = "Get a list of IDs of the logical displays connected."
57                     + "Also updates the cached displays.")
58    public Integer[] displayGetDisplays() {
59        Display[] displays = mDisplayManager.getDisplays();
60        updateDisplays(displays);
61        Integer[] results = new Integer[displays.length];
62        for(int i = 0; i < displays.length; i++) {
63            results[i] = displays[i].getDisplayId();
64        }
65        return results;
66    }
67
68    @Rpc(description = "Get the size of the specified display in pixels.")
69    public Point displayGetSize(
70            @RpcParameter(name = "displayId")
71            @RpcDefault(value = "0")
72            Integer displayId) {
73        Point outSize = new Point();
74        Display d = mDisplays.get(displayId);
75        d.getSize(outSize);
76        return outSize;
77    }
78
79    @Rpc(description = "Get the maximum screen size dimension that will happen.")
80    public Integer displayGetMaximumSizeDimension(
81            @RpcParameter(name = "displayId")
82            @RpcDefault(value = "0")
83            Integer displayId) {
84        Display d = mDisplays.get(displayId);
85        return d.getMaximumSizeDimension();
86    }
87
88    @Rpc(description = "Get display metrics based on the real size of this display.")
89    public DisplayMetrics displayGetRealMetrics(
90            @RpcParameter(name = "displayId")
91            @RpcDefault(value = "0")
92            Integer displayId) {
93        Display d = mDisplays.get(displayId);
94        DisplayMetrics outMetrics = new DisplayMetrics();
95        d.getRealMetrics(outMetrics);
96        return outMetrics;
97    }
98
99    @Override
100    public void shutdown() {
101    }
102}
103