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