189f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov/*
289f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov * Copyright (C) 2012 The Android Open Source Project
389f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov *
489f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov * Licensed under the Apache License, Version 2.0 (the "License");
589f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov * you may not use this file except in compliance with the License.
689f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov * You may obtain a copy of the License at
789f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov *
889f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov *      http://www.apache.org/licenses/LICENSE-2.0
989f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov *
1089f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov * Unless required by applicable law or agreed to in writing, software
1189f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov * distributed under the License is distributed on an "AS IS" BASIS,
1289f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1389f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov * See the License for the specific language governing permissions and
1489f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov * limitations under the License.
1589f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov */
1689f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov
1789f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganovpackage com.android.uiautomator.core;
1889f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov
1989f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganovimport android.app.ActivityManagerNative;
2089f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganovimport android.app.IActivityManager;
2189f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganovimport android.app.IActivityManager.ContentProviderHolder;
22cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhuimport android.app.UiAutomation;
23cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhuimport android.content.Context;
2489f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganovimport android.content.IContentProvider;
2589f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganovimport android.database.Cursor;
2689f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganovimport android.hardware.display.DisplayManagerGlobal;
2789f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganovimport android.os.Binder;
2889f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganovimport android.os.IBinder;
29cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhuimport android.os.IPowerManager;
3089f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganovimport android.os.RemoteException;
31cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhuimport android.os.ServiceManager;
3289f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganovimport android.os.UserHandle;
3389f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganovimport android.provider.Settings;
3489f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganovimport android.util.Log;
3589f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganovimport android.view.Display;
36cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhuimport android.view.IWindowManager;
3789f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov
38cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu/**
39cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu * @hide
40cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu */
41cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhupublic class ShellUiAutomatorBridge extends UiAutomatorBridge {
4289f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov
4389f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov    private static final String LOG_TAG = ShellUiAutomatorBridge.class.getSimpleName();
4489f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov
45cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu    public ShellUiAutomatorBridge(UiAutomation uiAutomation) {
4689f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov        super(uiAutomation);
4789f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov    }
4889f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov
4989f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov    public Display getDefaultDisplay() {
5089f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov        return DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY);
5189f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov    }
5289f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov
5389f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov    public long getSystemLongPressTime() {
5489f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov        // Read the long press timeout setting.
5589f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov        long longPressTimeout = 0;
5689f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov        try {
5789f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov            IContentProvider provider = null;
5889f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov            Cursor cursor = null;
5989f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov            IActivityManager activityManager = ActivityManagerNative.getDefault();
6089f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov            String providerName = Settings.Secure.CONTENT_URI.getAuthority();
6189f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov            IBinder token = new Binder();
6289f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov            try {
6389f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                ContentProviderHolder holder = activityManager.getContentProviderExternal(
6489f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                        providerName, UserHandle.USER_OWNER, token);
6589f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                if (holder == null) {
6689f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                    throw new IllegalStateException("Could not find provider: " + providerName);
6789f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                }
6889f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                provider = holder.provider;
6989f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                cursor = provider.query(null, Settings.Secure.CONTENT_URI,
7089f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                        new String[] {
7189f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                            Settings.Secure.VALUE
7289f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                        }, "name=?",
7389f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                        new String[] {
7489f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                            Settings.Secure.LONG_PRESS_TIMEOUT
7589f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                        }, null, null);
7689f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                if (cursor.moveToFirst()) {
7789f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                    longPressTimeout = cursor.getInt(0);
7889f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                }
7989f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov            } finally {
8089f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                if (cursor != null) {
8189f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                    cursor.close();
8289f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                }
8389f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                if (provider != null) {
8489f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                    activityManager.removeContentProviderExternal(providerName, token);
8589f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov                }
8689f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov            }
8789f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov        } catch (RemoteException e) {
8889f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov            String message = "Error reading long press timeout setting.";
8989f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov            Log.e(LOG_TAG, message, e);
9089f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov            throw new RuntimeException(message, e);
9189f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov        }
9289f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov        return longPressTimeout;
9389f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov    }
94cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu
95cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu    @Override
96cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu    public int getRotation() {
97cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu        IWindowManager wm =
98cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu                IWindowManager.Stub.asInterface(ServiceManager.getService(Context.WINDOW_SERVICE));
99cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu        int ret = -1;
100cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu        try {
101cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu            ret = wm.getRotation();
102cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu        } catch (RemoteException e) {
103cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu            Log.e(LOG_TAG, "Error getting screen rotation", e);
104cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu            throw new RuntimeException(e);
105cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu        }
106cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu        return ret;
107cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu    }
108cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu
109cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu    @Override
110cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu    public boolean isScreenOn() {
111cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu        IPowerManager pm =
112cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu                IPowerManager.Stub.asInterface(ServiceManager.getService(Context.POWER_SERVICE));
113cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu        boolean ret = false;
114cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu        try {
115cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu            ret = pm.isScreenOn();
116cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu        } catch (RemoteException e) {
117cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu            Log.e(LOG_TAG, "Error getting screen status", e);
118cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu            throw new RuntimeException(e);
119cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu        }
120cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu        return ret;
121cd08ab4b0d19e68cc1cf18396cb9076e828c1a63Guang Zhu    }
12289f6117cb1fbeab3770106cf54e05af1f597be81Svetoslav Ganov}
123