source: trunk/autoquest-androidmonitor/src/de/ugoe/cs/autoquest/androidmonitor/AndroidmonitorLogFile.java @ 1802

Last change on this file since 1802 was 1802, checked in by funger, 10 years ago

bug fix: spelling mistake element sessions

  • Property svn:mime-type set to text/plain
File size: 10.2 KB
Line 
1package de.ugoe.cs.autoquest.androidmonitor;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.StringWriter;
8
9import org.xmlpull.v1.XmlSerializer;
10
11import android.util.Log;
12import android.util.Xml;
13import android.view.View;
14import android.view.ViewGroup;
15
16/**
17 *
18 * @author Florian Unger
19 * @version 1.0
20 */
21public class AndroidmonitorLogFile {
22
23        // TODO rename getDeviceInformation() and getAppInformation() to set ... use
24        // writeToFile for both
25
26        private String name;
27        private File file;
28
29        public AndroidmonitorLogFile(String appName, File dir) {
30                this.name = "androidLogFile_" + appName + System.currentTimeMillis() + ".log";
31
32                try {
33                        // prove if file exists
34                        this.file = new File(dir, this.name);
35                        /*
36                         * if file does not exists write device and app information to a new
37                         * file. Otherwise use existing file and add activity information to
38                         * file.
39                         */
40                        if (true) { // !this.file.exists()
41                                /*
42                                 * create log file. Using method openFileOutput() does not work
43                                 * for this project due to the reason that this method would try
44                                 * to create the file in the directory of the non-existing
45                                 * directory de.ugoe.cs.androidmonitor. This directory does not
46                                 * exist due to the reason that this project is a library and
47                                 * the file has to be stored in the directory of the running
48                                 * application. Furthermore it would not be possible to write in
49                                 * another app directory as the own one.
50                                 */
51
52                                // TODO split document head information from
53                                // getDeviceInformation.
54
55                                String string = "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><sessions>"
56                                                + getDeviceInformation() + getAppInformation();
57                                try {
58                                        FileOutputStream outputStream = new FileOutputStream(
59                                                        this.file);
60                                        outputStream.write(string.getBytes());
61                                        outputStream.close();
62                                } catch (Exception e) {
63                                        Log.e("this.file", "outputstream: " + e.getMessage());
64                                }
65
66                        } else {
67                                // TODO add activity information
68                        }
69                } catch (Exception e) {
70                        e.printStackTrace();
71                        Log.e("file", "file: " + e.getMessage());
72                }
73
74        }
75
76        /**
77         * <p>
78         * Get file name which is in use.
79         * </p>
80         *
81         * @return filename
82         *
83         */
84        public String getName() {
85                return this.name;
86        }
87
88        // should be set
89        private String getAppInformation() {
90                // TODO create app information in the same manner as coded in
91                // getDeviceInformation
92                return "";
93        }
94
95        /**
96         * <p>
97         * Query device information.
98         * </p>
99         *
100         * @return XML device information in XML format
101         */
102        // should be set
103        private String getDeviceInformation() {
104                String deviceInformation = "";
105                XmlSerializer serializer = Xml.newSerializer();
106                StringWriter writer = new StringWriter();
107                try {
108                        serializer.setOutput(writer);
109                        serializer.startTag("", "device");
110                        serializer.startTag("", "param");
111                        serializer.attribute("", "value", ""
112                                        + android.os.Build.VERSION.SDK_INT);
113                        serializer.attribute("", "name", "sdk_version");
114                        serializer.endTag("", "param");
115
116                        serializer.startTag("", "param");
117                        serializer.attribute("", "value", android.os.Build.DEVICE);
118                        serializer.attribute("", "name", "device");
119                        serializer.endTag("", "param");
120
121                        serializer.startTag("", "param");
122                        serializer.attribute("", "value", android.os.Build.MANUFACTURER);
123                        serializer.attribute("", "name", "manufacturer");
124                        serializer.endTag("", "param");
125
126                        serializer.startTag("", "param");
127                        serializer.attribute("", "value", android.os.Build.MODEL);
128                        serializer.attribute("", "name", "model");
129                        serializer.endTag("", "param");
130
131                        // TODO get resolution ...
132
133                        serializer.endTag("", "device");
134                        serializer.endDocument();
135
136                        deviceInformation = writer.toString();
137
138                } catch (IOException e) {
139                        Log.e("xml", e.getMessage());
140                }
141
142                return deviceInformation;
143        }
144
145        /**
146         * <p>
147         * Adds some information of an component of an activity (view) to the file.
148         * </p>
149         *
150         * @param view
151         *            view to be logged
152         * @param parentHash
153         *            hash of the parent view
154         * @param activityName
155         *            name of the activity that is analyzed
156         */
157        public void addComponent(View view, int parentHash, String activityName) {
158                XmlSerializer serializer = Xml.newSerializer();
159                StringWriter writer = new StringWriter();
160                // create HEX string
161
162                try {
163                        serializer.setOutput(writer);
164                        serializer.startTag("", "component");
165                        // TODO find a way in that the hash code is unique over time and
166                        // target
167                        /*
168                         * (non-Javadoc) view.getId() seems to be unique over time and
169                         * targets but there is a problem. In some cases there is no ID
170                         * (value: -1).
171                         */
172                        serializer.attribute("", "hash", "" + view.hashCode());
173
174                        serializer.startTag("", "param");
175                        serializer.attribute("", "name", "id");
176                        serializer.attribute("", "value", "" + view.getId());
177                        serializer.endTag("", "param");
178
179                        serializer.startTag("", "param");
180                        serializer.attribute("", "name", "path");
181                        serializer.attribute("", "value", activityName + "/"
182                                        + getViewPath(view) + view.getClass().getSimpleName());
183                        serializer.endTag("", "param");
184
185                        serializer.startTag("", "param");
186                        serializer.attribute("", "name", "class");
187                        serializer.attribute("", "value", view.getClass().getName());
188                        serializer.endTag("", "param");
189
190                        serializer.startTag("", "param");
191                        serializer.attribute("", "name", "parent");
192                        // Problem in using view.getParent().hashCode():
193                        // http://developer.android.com/reference/android/view/View.html#getParent()
194                        // tells: "... parent is a ViewParent and not necessarily a View."
195                        // ViewParent does not have a method hashCode(). Solution is done
196                        // add parentHash as parameter to method addComponent() and
197                        // Androidmonitor-> addLogListenerToView.
198                        serializer.attribute("", "value", "" + parentHash);
199                        serializer.endTag("", "param");
200
201                        // TODO add title e.g. android:text="Button"
202
203                        // TODO add ancestors @see:
204                        // de.ugoe.cs.autoquest.jfcmonitor.JFCComponent#getClassHierarchy()
205                       
206                        /* This solution leads to java.lang.OutOfMemory!
207                        serializer.startTag("", "ancestors");
208                       
209                        Class<? extends Object> classobject = view.getClass();
210                        while(classobject != null){
211                                serializer.startTag("", "ancestor");
212                                serializer.attribute("", "name", classobject.getName());
213                                serializer.endTag("", "ancestor");
214                        }
215                        serializer.endTag("", "ancestors");
216                        */
217
218                        serializer.endTag("", "component");
219                        serializer.endDocument();
220
221                        writeToFile(writer.toString());
222
223                } catch (IllegalArgumentException e) {
224                        // TODO Auto-generated catch block
225                        e.printStackTrace();
226                        Log.e("file", "outputstream: " + e.getMessage());
227                } catch (IllegalStateException e) {
228                        // TODO Auto-generated catch block
229                        e.printStackTrace();
230                        Log.e("file", "outputstream: " + e.getMessage());
231                } catch (IOException e) {
232                        // TODO Auto-generated catch block
233                        e.printStackTrace();
234                        Log.e("file", "outputstream: " + e.getMessage());
235                }
236
237        }
238
239        /**
240         * <p>
241         * Add an event to the log file
242         * </p>
243         *
244         * @param view
245         *            the calling view of the listener
246         * @param type
247         *            the type of listener e.g. onClick ...
248         */
249        public void addEvent(View view, String type) {
250
251                String x = "" + view.getX();
252                String y = "" + view.getY();
253
254                XmlSerializer serializer = Xml.newSerializer();
255                StringWriter writer = new StringWriter();
256
257                try {
258                        serializer.setOutput(writer);
259
260                        serializer.startTag("", "event");
261                        serializer.attribute("", "id", type);
262
263                        serializer.startTag("", "param");
264                        serializer.attribute("", "value", x);
265                        serializer.attribute("", "name", "X");
266                        serializer.endTag("", "param");
267
268                        serializer.startTag("", "param");
269                        serializer.attribute("", "value", y);
270                        serializer.attribute("", "name", "Y");
271                        serializer.endTag("", "param");
272
273                        serializer.startTag("", "param");
274                        serializer.attribute("", "value", "" + view.hashCode());
275                        serializer.attribute("", "name", "source");
276                        serializer.endTag("", "param");
277
278                        serializer.startTag("", "param");
279                        serializer.attribute("", "value", "" + System.currentTimeMillis());
280                        serializer.attribute("", "name", "timestamp");
281                        serializer.endTag("", "param");
282
283                        serializer.endTag("", "event");
284                        serializer.endDocument();
285
286                        writeToFile(writer.toString());
287                } catch (IllegalArgumentException e) {
288                        // TODO Auto-generated catch block
289                        e.printStackTrace();
290                        Log.e("file", "outputstream: " + e.getMessage());
291                } catch (IllegalStateException e) {
292                        // TODO Auto-generated catch block
293                        e.printStackTrace();
294                        Log.e("file", "outputstream: " + e.getMessage());
295                } catch (IOException e) {
296                        // TODO Auto-generated catch block
297                        e.printStackTrace();
298                        Log.e("file", "outputstream: " + e.getMessage());
299                }
300        }
301
302        /**
303         * <p>
304         * Writes given information to the file. e.g. previous produced XML
305         * statements.
306         * </p>
307         *
308         * @param data
309         *            content to add to the file
310         */
311        private void writeToFile(String data) {
312
313                FileOutputStream outputStream;
314                try {
315                        outputStream = new FileOutputStream(file, true);
316                        outputStream.write(data.getBytes());
317                        outputStream.close();
318                } catch (FileNotFoundException e) {
319                        e.printStackTrace();
320                        Log.e("file", "outputstream: " + e.getMessage());
321                } catch (IOException e) {
322                        e.printStackTrace();
323                        Log.e("file", "outputstream: " + e.getMessage());
324                }
325
326        }
327
328        /**
329         * <p>
330         * Generates the path of an view element.
331         * </p>
332         *
333         * @param view
334         * @return path path to the element
335         */
336        private String getViewPath(View view) {
337                return getViewPath(view, null);
338        }
339
340        /**
341         * <p>
342         * Generates the path of an view element.
343         * </p>
344         *
345         * @param view
346         * @param path
347         * @return path path to the element
348         */
349        private String getViewPath(View view, String path) {
350                if (path == null) {
351                        path = "";
352                } else {
353                        path = view.getClass().getSimpleName() + "/" + path;
354                }
355                if (view.getParent() != null && (view.getParent() instanceof ViewGroup)) {
356                        return getViewPath((View) view.getParent(), path);
357                } else {
358                        return path;
359                }
360        }
361
362}
Note: See TracBrowser for help on using the repository browser.