• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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.ide.eclipse.adt.internal.launch;
18 
19 import com.android.ddmlib.AndroidDebugBridge;
20 import com.android.ddmlib.IDevice;
21 import com.android.ide.eclipse.adt.internal.launch.DeviceChooserDialog.DeviceChooserResponse;
22 import com.android.sdklib.internal.avd.AvdInfo;
23 
24 import java.util.HashMap;
25 import java.util.Map;
26 
27 /**
28  * {@link DeviceChoiceCache} maps a launch configuration name to the device selected for use
29  * in that launch configuration by the {@link DeviceChooserDialog}.
30  */
31 public class DeviceChoiceCache {
32     private static final Map<String, String> sDeviceUsedForLaunch = new HashMap<String, String>();
33 
get(String launchConfigName)34     public static IDevice get(String launchConfigName) {
35         // obtain the cached entry
36         String deviceName = sDeviceUsedForLaunch.get(launchConfigName);
37         if (deviceName == null) {
38             return null;
39         }
40 
41         // verify that the device is still online
42         for (IDevice device : getOnlineDevices()) {
43             if (deviceName.equals(device.getAvdName()) ||
44                     deviceName.equals(device.getSerialNumber())) {
45                 return device;
46             }
47         }
48 
49         // remove from cache if device is not online anymore
50         sDeviceUsedForLaunch.remove(launchConfigName);
51 
52         return null;
53     }
54 
put(String launchConfigName, DeviceChooserResponse response)55     public static void put(String launchConfigName, DeviceChooserResponse response) {
56         if (!response.useDeviceForFutureLaunches()) {
57             return;
58         }
59 
60         AvdInfo avd = response.getAvdToLaunch();
61         String device = null;
62         if (avd != null) {
63             device = avd.getName();
64         } else {
65             device = response.getDeviceToUse().getSerialNumber();
66         }
67 
68         sDeviceUsedForLaunch.put(launchConfigName, device);
69     }
70 
getOnlineDevices()71     private static IDevice[] getOnlineDevices() {
72         AndroidDebugBridge bridge = AndroidDebugBridge.getBridge();
73         if (bridge != null) {
74             return bridge.getDevices();
75         } else {
76             return new IDevice[0];
77         }
78     }
79 }
80