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

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

fixed parentHash Problem in method addComponent

  • Property svn:mime-type set to text/plain
File size: 8.5 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
16public class AndroidmonitorLogFile {
17
18        // TODO rename getDeviceInformation() and getAppInformation() to set ... use
19        // writeToFile for both
20
21        private String name;
22        private File file;
23
24        public AndroidmonitorLogFile(String appName, File dir) {
25                this.name = "android_" + appName + "_LogFile.xml";
26
27                try {
28                        // prove if file exists
29                        this.file = new File(dir, this.name);
30                        /*
31                         * if file does not exists write device and app information to a new
32                         * file. Otherwise use existing file and add activity information to
33                         * file.
34                         */
35                        if (true) { // !this.file.exists()
36                                /*
37                                 * create log file. Using method openFileOutput() does not work
38                                 * for this project due to the reason that this method would try
39                                 * to create the file in the directory of the non-existing
40                                 * directory de.ugoe.cs.androidmonitor. This directory does not
41                                 * exist due to the reason that this project is a library and
42                                 * the file has to be stored in the directory of the running
43                                 * application. Furthermore it would not be possible to write in
44                                 * another app directory as the own one.
45                                 */
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();
52                                try {
53                                        FileOutputStream outputStream = new FileOutputStream(
54                                                        this.file);
55                                        outputStream.write(string.getBytes());
56                                        outputStream.close();
57                                } catch (Exception e) {
58                                        Log.e("this.file", "outputstream: " + e.getMessage());
59                                }
60
61                        } else {
62                                // TODO add activity information
63                        }
64                } catch (Exception e) {
65                        e.printStackTrace();
66                        Log.e("file", "file: " + e.getMessage());
67                }
68
69        }
70
71        /**
72         * get file name
73         *
74         * @return
75         */
76        public String getName() {
77                return this.name;
78        }
79
80        // should be set
81        private String getAppInformation() {
82                // TODO create app information in the same manner as coded in
83                // getDeviceInformation
84                return "";
85        }
86
87        /**
88         * Query device information. XML format.
89         *
90         * @return
91         */
92        // should be set
93        private String getDeviceInformation() {
94                String deviceInformation = "";
95                XmlSerializer serializer = Xml.newSerializer();
96                StringWriter writer = new StringWriter();
97                try {
98                        serializer.setOutput(writer);
99                        serializer.startTag("", "device");
100                        serializer.startTag("", "param");
101                        serializer.attribute("", "value", ""
102                                        + android.os.Build.VERSION.SDK_INT);
103                        serializer.attribute("", "name", "sdk_version");
104                        serializer.endTag("", "param");
105
106                        serializer.startTag("", "param");
107                        serializer.attribute("", "value", android.os.Build.DEVICE);
108                        serializer.attribute("", "name", "device");
109                        serializer.endTag("", "param");
110
111                        serializer.startTag("", "param");
112                        serializer.attribute("", "value", android.os.Build.MANUFACTURER);
113                        serializer.attribute("", "name", "manufacturer");
114                        serializer.endTag("", "param");
115
116                        serializer.startTag("", "param");
117                        serializer.attribute("", "value", android.os.Build.MODEL);
118                        serializer.attribute("", "name", "model");
119                        serializer.endTag("", "param");
120
121                        // TODO get resolution ...
122
123                        serializer.endTag("", "device");
124                        serializer.endDocument();
125
126                        deviceInformation = writer.toString();
127
128                } catch (IOException e) {
129                        Log.e("xml", e.getMessage());
130                }
131
132                return deviceInformation;
133        }
134
135        public void addComponent(View view, int parentHash, 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) + view.getClass().getSimpleName());
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                        // Problem in using view.getParent().hashCode():
165                        // http://developer.android.com/reference/android/view/View.html#getParent()
166                        // tells: "... parent is a ViewParent and not necessarily a View."
167                        // ViewParent does not have a method hashCode(). Solution is done
168                        // add parentHash as parameter to method addComponent() and
169                        // Androidmonitor-> addLogListenerToView.
170                        serializer.attribute("", "value", "" + parentHash);
171                        serializer.endTag("", "param");
172
173                        serializer.endTag("", "component");
174                        serializer.endDocument();
175
176                        writeToFile(writer.toString());
177
178                } catch (IllegalArgumentException e) {
179                        // TODO Auto-generated catch block
180                        e.printStackTrace();
181                        Log.e("file", "outputstream: " + e.getMessage());
182                } catch (IllegalStateException e) {
183                        // TODO Auto-generated catch block
184                        e.printStackTrace();
185                        Log.e("file", "outputstream: " + e.getMessage());
186                } catch (IOException e) {
187                        // TODO Auto-generated catch block
188                        e.printStackTrace();
189                        Log.e("file", "outputstream: " + e.getMessage());
190                }
191
192        }
193
194        public void addEvent(View view) {
195
196                String x = "" + view.getX();
197                String y = "" + view.getY();
198
199                XmlSerializer serializer = Xml.newSerializer();
200                StringWriter writer = new StringWriter();
201
202                try {
203                        serializer.setOutput(writer);
204
205                        serializer.startTag("", "event");
206                        serializer.attribute("", "type", view.getClass().getSimpleName());
207
208                        serializer.startTag("", "param");
209                        serializer.attribute("", "value", x);
210                        serializer.attribute("", "name", "X");
211                        serializer.endTag("", "param");
212
213                        serializer.startTag("", "param");
214                        serializer.attribute("", "value", y);
215                        serializer.attribute("", "name", "Y");
216                        serializer.endTag("", "param");
217
218                        serializer.startTag("", "param");
219                        serializer.attribute("", "value", "" + view.hashCode());
220                        serializer.attribute("", "name", "source");
221                        serializer.endTag("", "param");
222
223                        serializer.startTag("", "param");
224                        serializer.attribute("", "value", "" + System.currentTimeMillis());
225                        serializer.attribute("", "name", "timestamp");
226                        serializer.endTag("", "param");
227
228                        serializer.endTag("", "event");
229                        serializer.endDocument();
230
231                        writeToFile(writer.toString());
232                } catch (IllegalArgumentException e) {
233                        // TODO Auto-generated catch block
234                        e.printStackTrace();
235                        Log.e("file", "outputstream: " + e.getMessage());
236                } catch (IllegalStateException e) {
237                        // TODO Auto-generated catch block
238                        e.printStackTrace();
239                        Log.e("file", "outputstream: " + e.getMessage());
240                } catch (IOException e) {
241                        // TODO Auto-generated catch block
242                        e.printStackTrace();
243                        Log.e("file", "outputstream: " + e.getMessage());
244                }
245        }
246
247        private void writeToFile(String data) {
248
249                FileOutputStream outputStream;
250                try {
251                        outputStream = new FileOutputStream(file, true);
252                        outputStream.write(data.getBytes());
253                        outputStream.close();
254                } catch (FileNotFoundException e) {
255                        e.printStackTrace();
256                        Log.e("file", "outputstream: " + e.getMessage());
257                } catch (IOException e) {
258                        e.printStackTrace();
259                        Log.e("file", "outputstream: " + e.getMessage());
260                }
261
262        }
263
264        /**
265         * generates the path of an view element
266         *
267         * @param view
268         * @return path to the element
269         */
270        private String getViewPath(View view) {
271                return getViewPath(view, null);
272        }
273
274        /**
275         * generates the path of an view element
276         *
277         * @param view
278         * @param path
279         * @return path to the element
280         */
281        private String getViewPath(View view, String path) {
282                if (path == null) {
283                        path = "";
284                } else {
285                        path = view.getClass().getSimpleName() + "/" + path;
286                }
287                if (view.getParent() != null && (view.getParent() instanceof ViewGroup)) {
288                        return getViewPath((View) view.getParent(), path);
289                } else {
290                        return path;
291                }
292        }
293
294}
Note: See TracBrowser for help on using the repository browser.