Changeset 1728
- Timestamp:
- 09/05/14 13:40:28 (10 years ago)
- Location:
- trunk/autoquest-androidmonitor/src/de/ugoe/cs/autoquest/androidmonitor
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/autoquest-androidmonitor/src/de/ugoe/cs/autoquest/androidmonitor/Androidmonitor.java
r1725 r1728 23 23 24 24 String activityName; // name of the activity Class that starts a Tracker 25 26 //Handle XML 27 private XmlSerializer xmlSerializer = Xml.newSerializer(); // handle XML tracking 28 private StringWriter stringWriter = new StringWriter(); // used with XmlSerializer 29 30 //Log file 25 26 // Log file 31 27 private AndroidmonitorLogFile logFile; 32 33 28 34 29 /** … … 49 44 public void startMonitor(Activity activity) { 50 45 activityName = activity.getClass().getSimpleName(); 51 52 logFile = new AndroidmonitorLogFile(getAppLable(activity), activity.getFilesDir()); 46 47 logFile = new AndroidmonitorLogFile(getAppLable(activity), 48 activity.getFilesDir()); 53 49 54 50 addLogListenerToView(getRootView(activity)); 55 56 57 51 58 52 // tbd … … 102 96 103 97 /** 104 * generates the path of an view element105 *106 * @param view107 * @return path to the element108 */109 private String getViewPath(View view) {110 return getViewPath(view, null);111 }112 113 /**114 * generates the path of an view element115 *116 * @param view117 * @param path118 * @return path to the element119 */120 private String getViewPath(View view, String path) {121 if (path == null) {122 path = "";123 } else {124 path = view.getClass().getSimpleName() + "/" + path;125 }126 if (view.getParent() != null && (view.getParent() instanceof ViewGroup)) {127 return getViewPath((View) view.getParent(), path);128 } else {129 return path;130 }131 }132 133 /**134 98 * replace the listener of each view with a composite listener which 135 99 * collects several listeners for one view. … … 139 103 public void addLogListenerToView(View view) { 140 104 // traverse all views of the activity 105 logFile.addComponent(view, activityName); 141 106 if (view instanceof ViewGroup) { 142 107 ViewGroup group = (ViewGroup) view; … … 161 126 public void onClick(View v) { 162 127 128 logFile.addEvent(v); 129 163 130 // track information ... 164 Log.d("MyLog", 165 "activity:" + activityName + " id:" + v.getId() 166 + " element:" 167 + v.getClass().getSimpleName() + " path:" 168 + getViewPath(v) + " x:" + v.getX() + " y:" 169 + v.getY() + " time:" 170 + System.currentTimeMillis()); 171 String viewId = "" + v.getId(); 172 String x = "" + v.getX(); 173 String y = "" + v.getY(); 174 175 XmlSerializer serializer = Xml.newSerializer(); // handle 176 // XML 177 // tracking 178 StringWriter writer = new StringWriter(); 179 180 try { 181 serializer.setOutput(writer); 182 serializer.startTag("", "event"); 183 serializer.attribute("", "id", viewId); 184 serializer.startTag("", "param"); 185 serializer.attribute("", "value", x); 186 serializer.attribute("", "name", "X"); 187 serializer.endTag("", "param"); 188 serializer.startTag("", "param"); 189 serializer.attribute("", "value", y); 190 serializer.attribute("", "name", "Y"); 191 serializer.endTag("", "param"); 192 serializer.startTag("", "param"); 193 serializer.attribute("", "value", v.getClass() 194 .getSimpleName()); 195 serializer.attribute("", "name", v.getClass() 196 .getSimpleName()); 197 serializer.endTag("", "param"); 198 serializer.endTag("", "event"); 199 serializer.endDocument(); 200 Log.d("xml", writer.toString()); 201 } catch (Exception e) { 202 throw new RuntimeException(e); 203 } 131 // Log.d("MyLog", 132 // "activity:" + activityName + " id:" + v.getId() 133 // + " element:" 134 // + v.getClass().getSimpleName() + " x:" + v.getX() + " y:" 135 // + v.getY() + " time:" 136 // + System.currentTimeMillis()); 204 137 205 138 } … … 279 212 return retrievedListener; 280 213 } 281 214 282 215 /** 283 216 * get application name as defined in Package Name 284 * @param pContext package context; could also be an activity 217 * 218 * @param pContext 219 * package context; could also be an activity 285 220 * @return app name 286 221 */ 287 222 public String getAppLable(Context pContext) { 288 //source (2014-09-04): http://stackoverflow.com/questions/11229219/android-get-application-name-not-package-name 289 PackageManager lPackageManager = pContext.getPackageManager(); 290 ApplicationInfo lApplicationInfo = null; 291 try { 292 lApplicationInfo = lPackageManager.getApplicationInfo(pContext.getApplicationInfo().packageName, 0); 293 } catch (final NameNotFoundException e) { 294 } 295 return (String) (lApplicationInfo != null ? lPackageManager.getApplicationLabel(lApplicationInfo) : "Unknown"); 296 } 297 223 // source (2014-09-04): 224 // http://stackoverflow.com/questions/11229219/android-get-application-name-not-package-name 225 PackageManager lPackageManager = pContext.getPackageManager(); 226 ApplicationInfo lApplicationInfo = null; 227 try { 228 lApplicationInfo = lPackageManager.getApplicationInfo( 229 pContext.getApplicationInfo().packageName, 0); 230 } catch (final NameNotFoundException e) { 231 } 232 return (String) (lApplicationInfo != null ? lPackageManager 233 .getApplicationLabel(lApplicationInfo) : "Unknown"); 234 } 298 235 299 236 } -
trunk/autoquest-androidmonitor/src/de/ugoe/cs/autoquest/androidmonitor/AndroidmonitorLogFile.java
r1726 r1728 2 2 3 3 import java.io.File; 4 import java.io.FileNotFoundException; 4 5 import java.io.FileOutputStream; 6 import java.io.IOException; 5 7 import java.io.StringWriter; 6 8 … … 9 11 import android.util.Log; 10 12 import android.util.Xml; 13 import android.view.View; 14 import android.view.ViewGroup; 11 15 12 16 public class AndroidmonitorLogFile { 13 17 18 // TODO rename getDeviceInformation() and getAppInformation() to set ... use 19 // writeToFile for both 20 14 21 private String name; 22 private File file; 15 23 16 24 public AndroidmonitorLogFile(String appName, File dir) { … … 19 27 try { 20 28 // prove if file exists 21 Filefile = new File(dir, this.name);29 this.file = new File(dir, this.name); 22 30 /* 23 31 * if file does not exists write device and app information to a new … … 25 33 * file. 26 34 */ 27 if (true) { // !file.exists()35 if (true) { // !this.file.exists() 28 36 /* 29 37 * create log file. Using method openFileOutput() does not work … … 36 44 * another app directory as the own one. 37 45 */ 38 String string = getDeviceInformation() + getAppInformation(); 46 47 // TODO split document head information from 48 // getDeviceInformation. 49 50 String string = "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><session>" 51 + getDeviceInformation() + getAppInformation(); 39 52 try { 40 FileOutputStream outputStream = new FileOutputStream(file); 53 FileOutputStream outputStream = new FileOutputStream( 54 this.file); 41 55 outputStream.write(string.getBytes()); 42 56 outputStream.close(); 43 57 } catch (Exception e) { 44 Log.e(" file", "outputstream: " + e.getMessage());58 Log.e("this.file", "outputstream: " + e.getMessage()); 45 59 } 46 60 61 } else { 62 // tbd add activity information 47 63 } 48 64 } catch (Exception e) { … … 62 78 } 63 79 80 // should be set 64 81 private String getAppInformation() { 65 // app Name ... 82 // TODO create app information in the same manner as coded in 83 // getDeviceInformation 66 84 return ""; 67 85 } 68 86 87 /** 88 * Query device information. XML format. 89 * 90 * @return 91 */ 92 // should be set 69 93 private String getDeviceInformation() { 70 94 String deviceInformation = ""; 71 XmlSerializer serializer = Xml.newSerializer(); 95 XmlSerializer serializer = Xml.newSerializer(); 72 96 StringWriter writer = new StringWriter(); 73 97 try { 74 98 serializer.setOutput(writer); 75 serializer.startDocument("UTF-8", true);76 99 serializer.startTag("", "device"); 77 100 serializer.startTag("", "param"); … … 96 119 serializer.endTag("", "param"); 97 120 98 // tbdget resolution ...121 // TODO get resolution ... 99 122 100 123 serializer.endTag("", "device"); 101 124 serializer.endDocument(); 102 125 103 126 deviceInformation = writer.toString(); 104 127 105 } catch ( Exception e) {106 Log.e("xml", e.getMessage()); 128 } catch (IOException e) { 129 Log.e("xml", e.getMessage()); 107 130 } 108 131 … … 110 133 } 111 134 112 public void addComponent() { 113 // add component to file 114 } 115 116 public void addEvent() { 117 // add event to file 135 public void addComponent(View view, String activityName) { 136 XmlSerializer serializer = Xml.newSerializer(); 137 StringWriter writer = new StringWriter(); 138 // create HEX string 139 140 try { 141 serializer.setOutput(writer); 142 serializer.startTag("", "component"); 143 // TODO use hex string instead of integer number 144 serializer.attribute("", "hash", "" + view.hashCode()); 145 146 serializer.startTag("", "param"); 147 serializer.attribute("", "name", "id"); 148 serializer.attribute("", "value", "" + view.getId()); 149 serializer.endTag("", "param"); 150 151 serializer.startTag("", "param"); 152 serializer.attribute("", "name", "path"); 153 serializer.attribute("", "value", activityName + "/" 154 + getViewPath(view)); 155 serializer.endTag("", "param"); 156 157 serializer.startTag("", "param"); 158 serializer.attribute("", "name", "class"); 159 serializer.attribute("", "value", view.getClass().getName()); 160 serializer.endTag("", "param"); 161 162 serializer.startTag("", "param"); 163 serializer.attribute("", "name", "parent"); 164 // serializer.attribute("", "value", "" + 165 // view.getParent().hashCode()); 166 serializer.endTag("", "param"); 167 168 serializer.endTag("", "component"); 169 serializer.endDocument(); 170 171 writeToFile(writer.toString()); 172 173 } catch (IllegalArgumentException e) { 174 // TODO Auto-generated catch block 175 e.printStackTrace(); 176 Log.e("file", "outputstream: " + e.getMessage()); 177 } catch (IllegalStateException e) { 178 // TODO Auto-generated catch block 179 e.printStackTrace(); 180 Log.e("file", "outputstream: " + e.getMessage()); 181 } catch (IOException e) { 182 // TODO Auto-generated catch block 183 e.printStackTrace(); 184 Log.e("file", "outputstream: " + e.getMessage()); 185 } 186 187 } 188 189 public void addEvent(View view) { 190 191 String x = "" + view.getX(); 192 String y = "" + view.getY(); 193 194 XmlSerializer serializer = Xml.newSerializer(); 195 StringWriter writer = new StringWriter(); 196 197 try { 198 serializer.setOutput(writer); 199 200 serializer.startTag("", "event"); 201 serializer.attribute("", "type", view.getClass().getSimpleName()); 202 203 serializer.startTag("", "param"); 204 serializer.attribute("", "value", x); 205 serializer.attribute("", "name", "X"); 206 serializer.endTag("", "param"); 207 208 serializer.startTag("", "param"); 209 serializer.attribute("", "value", y); 210 serializer.attribute("", "name", "Y"); 211 serializer.endTag("", "param"); 212 213 serializer.startTag("", "param"); 214 serializer.attribute("", "value", "" + view.hashCode()); 215 serializer.attribute("", "name", "source"); 216 serializer.endTag("", "param"); 217 218 serializer.startTag("", "param"); 219 serializer.attribute("", "value", "" + System.currentTimeMillis()); 220 serializer.attribute("", "name", "timestamp"); 221 serializer.endTag("", "param"); 222 223 serializer.endTag("", "event"); 224 serializer.endDocument(); 225 226 writeToFile(writer.toString()); 227 } catch (IllegalArgumentException e) { 228 // TODO Auto-generated catch block 229 e.printStackTrace(); 230 Log.e("file", "outputstream: " + e.getMessage()); 231 } catch (IllegalStateException e) { 232 // TODO Auto-generated catch block 233 e.printStackTrace(); 234 Log.e("file", "outputstream: " + e.getMessage()); 235 } catch (IOException e) { 236 // TODO Auto-generated catch block 237 e.printStackTrace(); 238 Log.e("file", "outputstream: " + e.getMessage()); 239 } 240 } 241 242 private void writeToFile(String data) { 243 244 FileOutputStream outputStream; 245 try { 246 outputStream = new FileOutputStream(file, true); 247 outputStream.write(data.getBytes()); 248 outputStream.close(); 249 } catch (FileNotFoundException e) { 250 e.printStackTrace(); 251 Log.e("file", "outputstream: " + e.getMessage()); 252 } catch (IOException e) { 253 e.printStackTrace(); 254 Log.e("file", "outputstream: " + e.getMessage()); 255 } 256 257 } 258 259 /** 260 * generates the path of an view element 261 * 262 * @param view 263 * @return path to the element 264 */ 265 private String getViewPath(View view) { 266 return getViewPath(view, null); 267 } 268 269 /** 270 * generates the path of an view element 271 * 272 * @param view 273 * @param path 274 * @return path to the element 275 */ 276 private String getViewPath(View view, String path) { 277 if (path == null) { 278 path = ""; 279 } else { 280 path = view.getClass().getSimpleName() + "/" + path; 281 } 282 if (view.getParent() != null && (view.getParent() instanceof ViewGroup)) { 283 return getViewPath((View) view.getParent(), path); 284 } else { 285 return path; 286 } 118 287 } 119 288
Note: See TracChangeset
for help on using the changeset viewer.