• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (C) 2011 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.chimpchat;
19 
20 import com.android.chimpchat.adb.AdbBackend;
21 import com.android.chimpchat.core.IChimpBackend;
22 import com.android.chimpchat.core.IChimpDevice;
23 
24 import java.util.Map;
25 import java.util.TreeMap;
26 
27 /**
28  * ChimpChat is a host-side library that provides an API for communication with
29  * an instance of Monkey on a device. This class provides an entry point to
30  * setting up communication with a device. Currently it only supports communciation
31  * over ADB, however.
32  */
33 public class ChimpChat {
34     private final IChimpBackend mBackend;
35     private static String sAdbLocation;
36 
ChimpChat(IChimpBackend backend)37     private ChimpChat(IChimpBackend backend) {
38         this.mBackend = backend;
39     }
40 
41     /**
42      * Generates a new instance of ChimpChat based on the options passed.
43      * @param options a map of settings for the new ChimpChat instance
44      * @return a new instance of ChimpChat or null if errors occur during creation
45      */
getInstance(Map<String, String> options)46     public static ChimpChat getInstance(Map<String, String> options) {
47         sAdbLocation = options.get("adbLocation");
48         IChimpBackend backend = createBackendByName(options.get("backend"));
49         if (backend == null) {
50             return null;
51         }
52         ChimpChat chimpchat = new ChimpChat(backend);
53         return chimpchat;
54     }
55 
56     /** Generates a new instance of ChimpChat using default settings
57      * @return a new instance of ChimpChat or null if errors occur during creation
58      */
getInstance()59     public static ChimpChat getInstance() {
60         Map<String, String> options = new TreeMap<String, String>();
61         options.put("backend", "adb");
62         return ChimpChat.getInstance(options);
63     }
64 
65 
66     /**
67      * Creates a specific backend by name.
68      *
69      * @param backendName the name of the backend to create
70      * @return the new backend, or null if none were found.
71      */
72 
createBackendByName(String backendName)73     private static IChimpBackend createBackendByName(String backendName) {
74         if ("adb".equals(backendName)) {
75             if (sAdbLocation == null) {
76                 return new AdbBackend();
77             } else {
78                 return new AdbBackend(sAdbLocation);
79             }
80         } else {
81             return null;
82         }
83     }
84 
85     /**
86      * Retrieves an instance of the device from the backend
87      * @param timeoutMs length of time to wait before timing out
88      * @param deviceId the id of the device you want to connect to
89      * @return an instance of the device
90      */
waitForConnection(long timeoutMs, String deviceId)91     public IChimpDevice waitForConnection(long timeoutMs, String deviceId){
92         return mBackend.waitForConnection(timeoutMs, deviceId);
93     }
94 
95     /**
96      * Retrieves an instance of the device from the backend.
97      * Defaults to the longest possible wait time and any available device.
98      * @return an instance of the device
99      */
waitForConnection()100     public IChimpDevice waitForConnection(){
101         return mBackend.waitForConnection(Integer.MAX_VALUE, ".*");
102     }
103 
104     /**
105      * Shutdown and clean up chimpchat.
106      */
shutdown()107     public void shutdown(){
108         mBackend.shutdown();
109     }
110 }
111