Home | History | Annotate | Download | only in core
      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 
     17 package com.android.uiautomator.core;
     18 
     19 import android.app.ActivityManager;
     20 import android.app.ContentProviderHolder;
     21 import android.app.IActivityManager;
     22 import android.app.UiAutomation;
     23 import android.content.ContentResolver;
     24 import android.content.Context;
     25 import android.content.IContentProvider;
     26 import android.database.Cursor;
     27 import android.hardware.display.DisplayManagerGlobal;
     28 import android.os.Binder;
     29 import android.os.IBinder;
     30 import android.os.IPowerManager;
     31 import android.os.RemoteException;
     32 import android.os.ServiceManager;
     33 import android.os.UserHandle;
     34 import android.provider.Settings;
     35 import android.util.Log;
     36 import android.view.Display;
     37 import android.view.IWindowManager;
     38 
     39 /**
     40  * @hide
     41  */
     42 public class ShellUiAutomatorBridge extends UiAutomatorBridge {
     43 
     44     private static final String LOG_TAG = ShellUiAutomatorBridge.class.getSimpleName();
     45 
     46     public ShellUiAutomatorBridge(UiAutomation uiAutomation) {
     47         super(uiAutomation);
     48     }
     49 
     50     public Display getDefaultDisplay() {
     51         return DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
     52     }
     53 
     54     public long getSystemLongPressTime() {
     55         // Read the long press timeout setting.
     56         long longPressTimeout = 0;
     57         try {
     58             IContentProvider provider = null;
     59             Cursor cursor = null;
     60             IActivityManager activityManager = ActivityManager.getService();
     61             String providerName = Settings.Secure.CONTENT_URI.getAuthority();
     62             IBinder token = new Binder();
     63             try {
     64                 ContentProviderHolder holder = activityManager.getContentProviderExternal(
     65                         providerName, UserHandle.USER_SYSTEM, token);
     66                 if (holder == null) {
     67                     throw new IllegalStateException("Could not find provider: " + providerName);
     68                 }
     69                 provider = holder.provider;
     70                 cursor = provider.query(null, Settings.Secure.CONTENT_URI,
     71                         new String[] {
     72                             Settings.Secure.VALUE
     73                         },
     74                         ContentResolver.createSqlQueryBundle(
     75                                 "name=?",
     76                                 new String[] { Settings.Secure.LONG_PRESS_TIMEOUT },
     77                                 null),
     78                         null);
     79                 if (cursor.moveToFirst()) {
     80                     longPressTimeout = cursor.getInt(0);
     81                 }
     82             } finally {
     83                 if (cursor != null) {
     84                     cursor.close();
     85                 }
     86                 if (provider != null) {
     87                     activityManager.removeContentProviderExternal(providerName, token);
     88                 }
     89             }
     90         } catch (RemoteException e) {
     91             String message = "Error reading long press timeout setting.";
     92             Log.e(LOG_TAG, message, e);
     93             throw new RuntimeException(message, e);
     94         }
     95         return longPressTimeout;
     96     }
     97 
     98     @Override
     99     public int getRotation() {
    100         IWindowManager wm =
    101                 IWindowManager.Stub.asInterface(ServiceManager.getService(Context.WINDOW_SERVICE));
    102         int ret = -1;
    103         try {
    104             ret = wm.getDefaultDisplayRotation();
    105         } catch (RemoteException e) {
    106             Log.e(LOG_TAG, "Error getting screen rotation", e);
    107             throw new RuntimeException(e);
    108         }
    109         return ret;
    110     }
    111 
    112     @Override
    113     public boolean isScreenOn() {
    114         IPowerManager pm =
    115                 IPowerManager.Stub.asInterface(ServiceManager.getService(Context.POWER_SERVICE));
    116         boolean ret = false;
    117         try {
    118             ret = pm.isInteractive();
    119         } catch (RemoteException e) {
    120             Log.e(LOG_TAG, "Error getting screen status", e);
    121             throw new RuntimeException(e);
    122         }
    123         return ret;
    124     }
    125 }
    126