1 /* 2 * Copyright 2009, 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 package com.android.commands.monkey; 17 18 import android.hardware.display.DisplayManagerGlobal; 19 import android.os.Build; 20 import android.os.SystemClock; 21 import android.view.Display; 22 import android.util.DisplayMetrics; 23 24 import com.android.commands.monkey.MonkeySourceNetwork.CommandQueue; 25 import com.android.commands.monkey.MonkeySourceNetwork.MonkeyCommand; 26 import com.android.commands.monkey.MonkeySourceNetwork.MonkeyCommandReturn; 27 28 import java.lang.Integer; 29 import java.lang.Float; 30 import java.lang.Long; 31 import java.util.List; 32 import java.util.Map; 33 import java.util.Set; 34 import java.util.TreeMap; 35 36 public class MonkeySourceNetworkVars { 37 /** 38 * Interface to get the value of a var. 39 */ 40 private static interface VarGetter { 41 /** 42 * Get the value of the var. 43 * @returns the value of the var. 44 */ get()45 public String get(); 46 } 47 48 private static class StaticVarGetter implements VarGetter { 49 private final String value; 50 StaticVarGetter(String value)51 public StaticVarGetter(String value) { 52 this.value = value; 53 } 54 get()55 public String get() { 56 return value; 57 } 58 } 59 60 // Use a TreeMap to keep the keys sorted so they get displayed nicely in listvar 61 private static final Map<String, VarGetter> VAR_MAP = new TreeMap<String, VarGetter>(); 62 63 static { 64 VAR_MAP.put("build.board", new StaticVarGetter(Build.BOARD)); 65 VAR_MAP.put("build.brand", new StaticVarGetter(Build.BRAND)); 66 VAR_MAP.put("build.device", new StaticVarGetter(Build.DEVICE)); 67 VAR_MAP.put("build.display", new StaticVarGetter(Build.DISPLAY)); 68 VAR_MAP.put("build.fingerprint", new StaticVarGetter(Build.FINGERPRINT)); 69 VAR_MAP.put("build.host", new StaticVarGetter(Build.HOST)); 70 VAR_MAP.put("build.id", new StaticVarGetter(Build.ID)); 71 VAR_MAP.put("build.model", new StaticVarGetter(Build.MODEL)); 72 VAR_MAP.put("build.product", new StaticVarGetter(Build.PRODUCT)); 73 VAR_MAP.put("build.tags", new StaticVarGetter(Build.TAGS)); 74 VAR_MAP.put("build.brand", new StaticVarGetter(Long.toString(Build.TIME))); 75 VAR_MAP.put("build.type", new StaticVarGetter(Build.TYPE)); 76 VAR_MAP.put("build.user", new StaticVarGetter(Build.USER)); 77 VAR_MAP.put("build.cpu_abi", new StaticVarGetter(Build.CPU_ABI)); 78 VAR_MAP.put("build.manufacturer", new StaticVarGetter(Build.MANUFACTURER)); 79 VAR_MAP.put("build.version.incremental", new StaticVarGetter(Build.VERSION.INCREMENTAL)); 80 VAR_MAP.put("build.version.release", new StaticVarGetter(Build.VERSION.RELEASE_OR_CODENAME)); 81 VAR_MAP.put("build.version.sdk", new StaticVarGetter(Integer.toString(Build.VERSION.SDK_INT))); 82 VAR_MAP.put("build.version.codename", new StaticVarGetter(Build.VERSION.CODENAME)); 83 84 // Display 85 Display display = DisplayManagerGlobal.getInstance().getRealDisplay(Display.DEFAULT_DISPLAY); 86 VAR_MAP.put("display.width", new StaticVarGetter(Integer.toString(display.getWidth()))); 87 VAR_MAP.put("display.height", new StaticVarGetter(Integer.toString(display.getHeight()))); 88 89 DisplayMetrics dm = new DisplayMetrics(); 90 display.getMetrics(dm); 91 VAR_MAP.put("display.density", new StaticVarGetter(Float.toString(dm.density))); 92 93 // am. note that the current activity information isn't valid 94 // until the first activity gets launched after the monkey has 95 // been started. 96 VAR_MAP.put("am.current.package", new VarGetter() { 97 public String get() { 98 return Monkey.currentPackage; 99 } 100 }); 101 VAR_MAP.put("am.current.action", new VarGetter() { 102 public String get() { 103 if (Monkey.currentIntent == null) { 104 return null; 105 } 106 return Monkey.currentIntent.getAction(); 107 } 108 }); 109 VAR_MAP.put("am.current.comp.class", new VarGetter() { 110 public String get() { 111 if (Monkey.currentIntent == null) { 112 return null; 113 } 114 return Monkey.currentIntent.getComponent().getClassName(); 115 } 116 }); 117 VAR_MAP.put("am.current.comp.package", new VarGetter() { 118 public String get() { 119 if (Monkey.currentIntent == null) { 120 return null; 121 } 122 return Monkey.currentIntent.getComponent().getPackageName(); 123 } 124 }); 125 VAR_MAP.put("am.current.data", new VarGetter() { 126 public String get() { 127 if (Monkey.currentIntent == null) { 128 return null; 129 } 130 return Monkey.currentIntent.getDataString(); 131 } 132 }); 133 VAR_MAP.put("am.current.categories", new VarGetter() { 134 public String get() { 135 if (Monkey.currentIntent == null) { 136 return null; 137 } 138 StringBuffer sb = new StringBuffer(); 139 for (String cat : Monkey.currentIntent.getCategories()) { 140 sb.append(cat).append(" "); 141 } 142 return sb.toString(); 143 } 144 }); 145 146 // clock 147 VAR_MAP.put("clock.realtime", new VarGetter() { 148 public String get() { 149 return Long.toString(SystemClock.elapsedRealtime()); 150 } 151 }); 152 VAR_MAP.put("clock.uptime", new VarGetter() { 153 public String get() { 154 return Long.toString(SystemClock.uptimeMillis()); 155 } 156 }); 157 VAR_MAP.put("clock.millis", new VarGetter() { 158 public String get() { 159 return Long.toString(System.currentTimeMillis()); 160 } 161 }); 162 VAR_MAP.put("monkey.version", new VarGetter() { 163 public String get() { 164 return Integer.toString(MonkeySourceNetwork.MONKEY_NETWORK_VERSION); 165 } 166 }); 167 } 168 169 /** 170 * Command to list the "vars" that the monkey knows about. 171 */ 172 public static class ListVarCommand implements MonkeySourceNetwork.MonkeyCommand { 173 // listvar translateCommand(List<String> command, CommandQueue queue)174 public MonkeyCommandReturn translateCommand(List<String> command, 175 CommandQueue queue) { 176 Set<String> keys = VAR_MAP.keySet(); 177 StringBuffer sb = new StringBuffer(); 178 for (String key : keys) { 179 sb.append(key).append(" "); 180 } 181 return new MonkeyCommandReturn(true, sb.toString()); 182 } 183 } 184 185 /** 186 * Command to get the value of a var. 187 */ 188 public static class GetVarCommand implements MonkeyCommand { 189 // getvar varname translateCommand(List<String> command, CommandQueue queue)190 public MonkeyCommandReturn translateCommand(List<String> command, 191 CommandQueue queue) { 192 if (command.size() == 2) { 193 VarGetter getter = VAR_MAP.get(command.get(1)); 194 if (getter == null) { 195 return new MonkeyCommandReturn(false, "unknown var"); 196 } 197 return new MonkeyCommandReturn(true, getter.get()); 198 } 199 return MonkeySourceNetwork.EARG; 200 } 201 } 202 } 203