Home | History | Annotate | Download | only in facade
      1 /*
      2  * Copyright (C) 2016 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package com.googlecode.android_scripting.facade;
     18 
     19 import java.util.HashMap;
     20 
     21 import android.app.Service;
     22 import android.content.Context;
     23 import android.graphics.Point;
     24 import android.hardware.display.DisplayManager;
     25 import android.util.DisplayMetrics;
     26 import android.view.Display;
     27 
     28 import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
     29 import com.googlecode.android_scripting.rpc.Rpc;
     30 import com.googlecode.android_scripting.rpc.RpcDefault;
     31 import com.googlecode.android_scripting.rpc.RpcParameter;
     32 
     33 public 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