Index: trunk/autoquest-androidmonitor/src/main/java/de/ugoe/cs/autoquest/androidmonitor/AndroidMonitorLogFile.java
===================================================================
--- trunk/autoquest-androidmonitor/src/main/java/de/ugoe/cs/autoquest/androidmonitor/AndroidMonitorLogFile.java	(revision 1833)
+++ trunk/autoquest-androidmonitor/src/main/java/de/ugoe/cs/autoquest/androidmonitor/AndroidMonitorLogFile.java	(revision 1842)
@@ -25,12 +25,22 @@
 import org.xmlpull.v1.XmlSerializer;
 
+import android.app.Activity;
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
 import android.util.Log;
 import android.util.Xml;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.ImageView;
+import android.widget.TextView;
 
 /**
  * <p>
- * TODO comment
+ * Writes informations about the device, the application, the components of the activity and the
+ * events in a log file. File is stored in the space of the application. Path is normally internal
+ * storage: data/data/<package name of the application using the AndroidMonitor>/files/ e.g.
+ * data/data/de.ugoe.cs.autoquest.androidmonitor.testApp/files/
  * </p>
  * 
@@ -45,13 +55,20 @@
      * </p>
      */
-    private String name;
-    
-    /**
-     * <p>
-     * File representation to store monitored information. 
-     * </p>
-     */
-    private File file;
-    
+    private static String name;
+
+    /**
+     * <p>
+     * Name of the Application which is monitored.
+     * </p>
+     */
+    private static String appName;
+
+    /**
+     * <p>
+     * File representation to store monitored information.
+     * </p>
+     */
+    private static File file;
+
     /**
      * <p>
@@ -59,5 +76,5 @@
      * </p>
      */
-    private List<Integer> currentLoggedComponents;
+    private static List<Integer> currentLoggedComponents;
 
     /**
@@ -66,59 +83,17 @@
      * Constructor. Creates a new AndroidmonitorLogFile.
      * </p>
-     *
+     * 
      * @param appName
-     *          Name of the calling application.
+     *            Name of the calling application.
      * @param dir
-     *          Folder to store the log file.
-     */
-    public AndroidMonitorLogFile(String appName, File dir) {
-        
-        currentLoggedComponents = new ArrayList<Integer>();
-        
-        this.name = "androidLogFile_" + appName + System.currentTimeMillis() + ".log";
-
-        try {
-                // prove if file exists
-                this.file = new File(dir, this.name);
-                /*
-                 * if file does not exists write device and app information to a new
-                 * file. Otherwise use existing file and add activity information to
-                 * file.
-                 */
-                // TODO prove if file exists and add activity information
-                if (true) { // !this.file.exists()
-                        /*
-                         * create log file. Using method openFileOutput() does not work
-                         * for this project due to the reason that this method would try
-                         * to create the file in the directory of the non-existing
-                         * directory de.ugoe.cs.androidmonitor. This directory does not
-                         * exist due to the reason that this project is a library and
-                         * the file has to be stored in the directory of the running
-                         * application. Furthermore it would not be possible to write in
-                         * another app directory as the own one.
-                         */
-
-                        String string = "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><sessions>";
-                        
-                        try {
-                                FileOutputStream outputStream = new FileOutputStream(
-                                                this.file);
-                                outputStream.write(string.getBytes());
-                                outputStream.close();
-                        } catch (Exception e) {
-                                Log.e("this.file", "outputstream: " + e.getMessage());
-                        }
-                        
-                        setDeviceInformation();
-                        setAppInformation();
-
-                } 
-                /*else {
-                        // TODO add activity information
-                }*/
-        } catch (Exception e) {
-                e.printStackTrace();
-                Log.e("file", "file: " + e.getMessage());
-        }
+     *            Folder to store the log file.
+     */
+    public AndroidMonitorLogFile(Activity activity) {
+
+        AndroidMonitorLogFile.currentLoggedComponents = new ArrayList<Integer>();
+
+        AndroidMonitorLogFile.appName = getAppLable(activity);
+
+        createFile(activity);
     }
 
@@ -128,9 +103,45 @@
      * Get file name which is in use.
      * </p>
-     *
+     * 
      * @return filename
      */
-    public String getFileName() {
-            return this.name;
+    public static String getFileName() {
+        return AndroidMonitorLogFile.name;
+    }
+
+    /**
+     * Get application name as defined in Package Name
+     * 
+     * @param pContext
+     *            package context; could also be an activity
+     * @return app name
+     */
+    private String getAppLable(Context pContext) {
+        // source:
+        // http://stackoverflow.com/questions/11229219/android-get-application-name-not-package-name
+        // (last call 2014-09-04)
+        PackageManager lPackageManager = pContext.getPackageManager();
+        ApplicationInfo lApplicationInfo = null;
+        try {
+            lApplicationInfo =
+                lPackageManager.getApplicationInfo(pContext.getApplicationInfo().packageName, 0);
+        }
+        catch (final NameNotFoundException e) {}
+        return (String) (lApplicationInfo != null ? lPackageManager
+            .getApplicationLabel(lApplicationInfo) : "Unknown");
+    }
+
+    /**
+     * 
+     * <p>
+     * Get package Name.
+     * </p>
+     * 
+     * @param pContext
+     *            package context; could also be an activity
+     * @return package name
+     */
+    private String getAppPackageName(Context pContext) {
+        return pContext.getPackageName();
     }
 
@@ -140,10 +151,44 @@
      * Writes information about the application to the log file.
      * </p>
-     *
-     */
-    private void setAppInformation() {
-            // TODO create app information in the same manner as coded in
-            // getDeviceInformation
-            
+     * 
+     * @param activity
+     * 
+     */
+    private void setAppInformation(Activity activity) {
+
+        XmlSerializer serializer = Xml.newSerializer();
+        StringWriter writer = new StringWriter();
+        try {
+            serializer.setOutput(writer);
+
+            serializer.startTag("", "application");
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "value", getAppPackageName(activity));
+            serializer.attribute("", "name", "package");
+            serializer.endTag("", "param");
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "value", getAppLable(activity));
+            serializer.attribute("", "name", "name");
+            serializer.endTag("", "param");
+
+            serializer.endTag("", "application");
+            serializer.endDocument();
+
+            writeToFile(writer.toString());
+        }
+        catch (IllegalArgumentException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
+        catch (IllegalStateException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
+        catch (IOException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
     }
 
@@ -155,53 +200,51 @@
      */
     private void setDeviceInformation() {
-           
-            XmlSerializer serializer = Xml.newSerializer();
-            StringWriter writer = new StringWriter();
-            try {
-                    serializer.setOutput(writer);
-                    serializer.startTag("", "device");
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "value", ""
-                                    + android.os.Build.VERSION.SDK_INT);
-                    serializer.attribute("", "name", "sdk_version");
-                    serializer.endTag("", "param");
-
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "value", android.os.Build.DEVICE);
-                    serializer.attribute("", "name", "device");
-                    serializer.endTag("", "param");
-
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "value", android.os.Build.MANUFACTURER);
-                    serializer.attribute("", "name", "manufacturer");
-                    serializer.endTag("", "param");
-
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "value", android.os.Build.MODEL);
-                    serializer.attribute("", "name", "model");
-                    serializer.endTag("", "param");
-
-                    // TODO get resolution ...
-
-                    serializer.endTag("", "device");
-                    serializer.endDocument();
-                    
-                    writeToFile(writer.toString());
-
-            } catch (IllegalArgumentException e) {
-                // TODO Auto-generated catch block
-                e.printStackTrace();
-                Log.e("file", "outputstream: " + e.getMessage());
-            } catch (IllegalStateException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                    Log.e("file", "outputstream: " + e.getMessage());
-            } catch (IOException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                    Log.e("file", "outputstream: " + e.getMessage());
-            }
-
-            
+
+        XmlSerializer serializer = Xml.newSerializer();
+        StringWriter writer = new StringWriter();
+        try {
+            serializer.setOutput(writer);
+            serializer.startTag("", "device");
+            serializer.startTag("", "param");
+            serializer.attribute("", "value", "" + android.os.Build.VERSION.SDK_INT);
+            serializer.attribute("", "name", "sdk_version");
+            serializer.endTag("", "param");
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "value", android.os.Build.DEVICE);
+            serializer.attribute("", "name", "device");
+            serializer.endTag("", "param");
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "value", android.os.Build.MANUFACTURER);
+            serializer.attribute("", "name", "manufacturer");
+            serializer.endTag("", "param");
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "value", android.os.Build.MODEL);
+            serializer.attribute("", "name", "model");
+            serializer.endTag("", "param");
+
+            // TODO get resolution ...
+
+            serializer.endTag("", "device");
+            serializer.endDocument();
+
+            writeToFile(writer.toString());
+
+        }
+        catch (IllegalArgumentException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
+        catch (IllegalStateException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
+        catch (IOException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
+
     }
 
@@ -219,79 +262,97 @@
      */
     public void addComponent(View view, int parentHash, String activityName) {
-            XmlSerializer serializer = Xml.newSerializer();
-            StringWriter writer = new StringWriter();
-            
-            try {
-                    serializer.setOutput(writer);
-                    serializer.startTag("", "component");
-                    // TODO find a way in that the hash code is unique over time and
-                    // target
-                    /*
-                     * (non-Javadoc) view.getId() seems to be unique over time and
-                     * targets but there is a problem. In some cases there is no ID
-                     * (value: -1).
-                     */
-                    serializer.attribute("", "hash", "" + view.hashCode());
-                    
-
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "name", "id");
-                    serializer.attribute("", "value", "" + view.getId());
-                    serializer.endTag("", "param");
-
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "name", "path");
-                    serializer.attribute("", "value", activityName + "/"
-                                    + getViewPath(view) + view.getClass().getSimpleName());
-                    serializer.endTag("", "param");
-
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "name", "class");
-                    serializer.attribute("", "value", view.getClass().getName());
-                    serializer.endTag("", "param");
-
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "name", "parent");
-                    // Problem in using view.getParent().hashCode():
-                    // http://developer.android.com/reference/android/view/View.html#getParent()
-                    // tells: "... parent is a ViewParent and not necessarily a View."
-                    // ViewParent does not have a method hashCode(). Solution is done
-                    // add parentHash as parameter to method addComponent() and
-                    // Androidmonitor-> addLogListenerToView.
-                    serializer.attribute("", "value", "" + parentHash);
-                    serializer.endTag("", "param");
-
-                    // TODO add title e.g. android:text="Button"
-
-                    serializer.startTag("", "ancestors");
-                    
-                    Class<? extends Object> classobject = view.getClass();
-                    
-                    while((classobject != null)){
-                            serializer.startTag("", "ancestor");
-                            serializer.attribute("", "name", classobject.getName());
-                            serializer.endTag("", "ancestor");
-                            classobject = classobject.getSuperclass();
-                    }
-                    serializer.endTag("", "ancestors");
-
-                    serializer.endTag("", "component");
-                    serializer.endDocument();
-
-                    writeToFile(writer.toString());
-
-            } catch (IllegalArgumentException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                    Log.e("file", "outputstream: " + e.getMessage());
-            } catch (IllegalStateException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                    Log.e("file", "outputstream: " + e.getMessage());
-            } catch (IOException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                    Log.e("file", "outputstream: " + e.getMessage());
+        XmlSerializer serializer = Xml.newSerializer();
+        StringWriter writer = new StringWriter();
+
+        try {
+            serializer.setOutput(writer);
+            serializer.startTag("", "component");
+            /*
+             * (non-Javadoc) TODO find a way in that the hash code is unique over time and target
+             * 
+             * view.getId() seems to be unique over time and targets but there is a problem. In some
+             * cases there is no ID (value: -1).
+             * 
+             * view.getId() is not unique in every case. E.g. in the application timerdroid the id
+             * of the list elements changes when calling home button.
+             */
+            serializer.attribute("", "hash", "" + view.hashCode());
+            currentLoggedComponents.add(view.hashCode());
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "name", "id");
+            serializer.attribute("", "value", "" + view.getId());
+            serializer.endTag("", "param");
+
+            if (view instanceof TextView) {
+                serializer.startTag("", "param");
+                serializer.attribute("", "name", "title");
+                TextView textView = (TextView) view;
+                serializer.attribute("", "value", "" + textView.getText());
+                serializer.endTag("", "param");
             }
+            //TODO in case of an image add file name
+            if (view instanceof ImageView) {
+                serializer.startTag("", "param");
+                serializer.attribute("", "name", "title");
+               
+                
+                serializer.attribute("", "value", "image:" );
+                serializer.endTag("", "param");
+            }
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "name", "path");
+            serializer.attribute("", "value", activityName + "/" + getViewPath(view) +
+                view.getClass().getSimpleName());
+            serializer.endTag("", "param");
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "name", "class");
+            serializer.attribute("", "value", view.getClass().getName());
+            serializer.endTag("", "param");
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "name", "parent");
+            /*
+             * (non-Javadoc) Problem in using view.getParent().hashCode():
+             * http://developer.android.com/reference/android/view/View.html#getParent() tells:
+             * "... parent is a ViewParent and not necessarily a View." ViewParent does not have a
+             * method hashCode(). Solution is done add parentHash as parameter to method
+             * addComponent() and Androidmonitor-> addLogListenerToView.
+             */
+            serializer.attribute("", "value", "" + parentHash);
+            serializer.endTag("", "param");
+
+            serializer.startTag("", "ancestors");
+
+            Class<? extends Object> classobject = view.getClass();
+
+            while ((classobject != null)) {
+                serializer.startTag("", "ancestor");
+                serializer.attribute("", "name", classobject.getName());
+                serializer.endTag("", "ancestor");
+                classobject = classobject.getSuperclass();
+            }
+            serializer.endTag("", "ancestors");
+
+            serializer.endTag("", "component");
+            serializer.endDocument();
+
+            writeToFile(writer.toString());
+
+        }
+        catch (IllegalArgumentException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
+        catch (IllegalStateException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
+        catch (IOException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
 
     }
@@ -303,54 +364,54 @@
      * 
      * @param hash
-     *                      hash value of the calling view of the listener
+     *            hash value of the calling view of the listener
      * @param type
-     *                      the type of listener e.g. textView ...
+     *            the type of listener e.g. textView ...
      * @param message
-     *                      message typed in
-     */
-    public void addEvent(int hash, String type, String message){
-            XmlSerializer serializer = Xml.newSerializer();
-            StringWriter writer = new StringWriter();
-
-            try {
-                    serializer.setOutput(writer);
-
-                    serializer.startTag("", "event");
-                    serializer.attribute("", "id", type);
-
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "value", "" + hash);
-                    serializer.attribute("", "name", "source");
-                    serializer.endTag("", "param");
-                    
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "value", message);
-                    serializer.attribute("", "name", "message");
-                    serializer.endTag("", "param");
-
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "value", "" + System.currentTimeMillis());
-                    serializer.attribute("", "name", "timestamp");
-                    serializer.endTag("", "param");
-
-                    serializer.endTag("", "event");
-                    serializer.endDocument();
-
-                    writeToFile(writer.toString());
-            } catch (IllegalArgumentException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                    Log.e("file", "outputstream: " + e.getMessage());
-            } catch (IllegalStateException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                    Log.e("file", "outputstream: " + e.getMessage());
-            } catch (IOException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                    Log.e("file", "outputstream: " + e.getMessage());
-            }
-    }
-    
+     *            message typed in
+     */
+    public void addEvent(int hash, String type, String message) {
+        XmlSerializer serializer = Xml.newSerializer();
+        StringWriter writer = new StringWriter();
+
+        try {
+            serializer.setOutput(writer);
+
+            serializer.startTag("", "event");
+            serializer.attribute("", "id", type);
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "value", "" + hash);
+            serializer.attribute("", "name", "source");
+            serializer.endTag("", "param");
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "value", message);
+            serializer.attribute("", "name", "message");
+            serializer.endTag("", "param");
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "value", "" + System.currentTimeMillis());
+            serializer.attribute("", "name", "timestamp");
+            serializer.endTag("", "param");
+
+            serializer.endTag("", "event");
+            serializer.endDocument();
+
+            writeToFile(writer.toString());
+        }
+        catch (IllegalArgumentException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
+        catch (IllegalStateException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
+        catch (IOException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
+    }
+
     /**
      * <p>
@@ -365,59 +426,134 @@
     public void addEvent(View view, String type) {
 
-            String x = "" + view.getX();
-            String y = "" + view.getY();
-
-            XmlSerializer serializer = Xml.newSerializer();
-            StringWriter writer = new StringWriter();
-
-            try {
-                    serializer.setOutput(writer);
-
-                    serializer.startTag("", "event");
-                    serializer.attribute("", "id", type);
-
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "value", x);
-                    serializer.attribute("", "name", "X");
-                    serializer.endTag("", "param");
-
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "value", y);
-                    serializer.attribute("", "name", "Y");
-                    serializer.endTag("", "param");
-
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "value", "" + view.hashCode());
-                    serializer.attribute("", "name", "source");
-                    serializer.endTag("", "param");
-
-                    serializer.startTag("", "param");
-                    serializer.attribute("", "value", "" + System.currentTimeMillis());
-                    serializer.attribute("", "name", "timestamp");
-                    serializer.endTag("", "param");
-
-                    serializer.endTag("", "event");
-                    serializer.endDocument();
-
-                    writeToFile(writer.toString());
-            } catch (IllegalArgumentException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                    Log.e("file", "outputstream: " + e.getMessage());
-            } catch (IllegalStateException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                    Log.e("file", "outputstream: " + e.getMessage());
-            } catch (IOException e) {
-                    // TODO Auto-generated catch block
-                    e.printStackTrace();
-                    Log.e("file", "outputstream: " + e.getMessage());
+        String x = "" + view.getX();
+        String y = "" + view.getY();
+
+        XmlSerializer serializer = Xml.newSerializer();
+        StringWriter writer = new StringWriter();
+
+        try {
+            serializer.setOutput(writer);
+
+            serializer.startTag("", "event");
+            serializer.attribute("", "id", type);
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "value", x);
+            serializer.attribute("", "name", "X");
+            serializer.endTag("", "param");
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "value", y);
+            serializer.attribute("", "name", "Y");
+            serializer.endTag("", "param");
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "value", "" + view.hashCode());
+            serializer.attribute("", "name", "source");
+            serializer.endTag("", "param");
+
+            serializer.startTag("", "param");
+            serializer.attribute("", "value", "" + System.currentTimeMillis());
+            serializer.attribute("", "name", "timestamp");
+            serializer.endTag("", "param");
+
+            serializer.endTag("", "event");
+            serializer.endDocument();
+
+            writeToFile(writer.toString());
+        }
+        catch (IllegalArgumentException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
+        catch (IllegalStateException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
+        catch (IOException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
+    }
+
+    /**
+     * 
+     * <p>
+     * Creates a new file to store information.
+     * </p>
+     * 
+     * @param activity
+     *            Calling application.
+     */
+    private void createFile(Activity activity) {
+
+        AndroidMonitorLogFile.name =
+            "androidLogFile_" + AndroidMonitorLogFile.appName + System.currentTimeMillis() + ".log";
+        try {
+            AndroidMonitorLogFile.file =
+                new File(activity.getFilesDir(), AndroidMonitorLogFile.name);
+            if (AndroidMonitorLogFile.file.exists() && !AndroidMonitorLogFile.file.isDirectory()) {
+                createFile(activity, 0);
             }
-    }
-
-    /**
-     * <p>
-     * Writes given information to the file. e.g. previous produced XML
-     * statements.
+            else {
+                writeHeaderToFile(activity);
+            }
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+            Log.e("AndroidMonitorLogFile.file", "file: " + e.getMessage());
+        }
+    }
+
+    /**
+     * 
+     * <p>
+     * Creates a new file to store information. Counts up if file exists.
+     * </p>
+     * 
+     * @param activity
+     *            Calling application.
+     * @param count
+     *            File number.
+     */
+    private void createFile(Activity activity, int count) {
+        AndroidMonitorLogFile.name =
+            "androidLogFile_" + count + "_" + AndroidMonitorLogFile.appName +
+                System.currentTimeMillis() + ".log";
+        try {
+            AndroidMonitorLogFile.file =
+                new File(activity.getFilesDir(), AndroidMonitorLogFile.name);
+            if (AndroidMonitorLogFile.file.exists() && !AndroidMonitorLogFile.file.isDirectory()) {
+                count++;
+                createFile(activity, count);
+            }
+            else {
+                writeHeaderToFile(activity);
+            }
+        }
+        catch (Exception e) {
+            e.printStackTrace();
+            Log.e("AndroidMonitorLogFile.file", "file: " + e.getMessage());
+        }
+    }
+
+    /**
+     * 
+     * <p>
+     * Writes XML head, device and application information to file.
+     * </p>
+     * 
+     * @param activity
+     *            Calling application.
+     */
+    private void writeHeaderToFile(Activity activity) {
+        writeToFile("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><sessions>");
+        setDeviceInformation();
+        setAppInformation(activity);
+    }
+
+    /**
+     * <p>
+     * Writes given information to the file. e.g. previous produced XML statements.
      * </p>
      * 
@@ -427,16 +563,18 @@
     private void writeToFile(String data) {
 
-            FileOutputStream outputStream;
-            try {
-                    outputStream = new FileOutputStream(file, true);
-                    outputStream.write(data.getBytes());
-                    outputStream.close();
-            } catch (FileNotFoundException e) {
-                    e.printStackTrace();
-                    Log.e("file", "outputstream: " + e.getMessage());
-            } catch (IOException e) {
-                    e.printStackTrace();
-                    Log.e("file", "outputstream: " + e.getMessage());
-            }
+        FileOutputStream outputStream;
+        try {
+            outputStream = new FileOutputStream(AndroidMonitorLogFile.file, true);
+            outputStream.write(data.getBytes());
+            outputStream.close();
+        }
+        catch (FileNotFoundException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
+        catch (IOException e) {
+            e.printStackTrace();
+            Log.e("file", "outputstream: " + e.getMessage());
+        }
 
     }
@@ -451,5 +589,5 @@
      */
     private String getViewPath(View view) {
-            return getViewPath(view, null);
+        return getViewPath(view, null);
     }
 
@@ -464,16 +602,18 @@
      */
     private String getViewPath(View view, String path) {
-            if (path == null) {
-                    path = "";
-            } else {
-                    path = view.getClass().getSimpleName() + "/" + path;
-            }
-            if (view.getParent() != null && (view.getParent() instanceof ViewGroup)) {
-                    return getViewPath((View) view.getParent(), path);
-            } else {
-                    return path;
-            }
-    }
-    
+        if (path == null) {
+            path = "";
+        }
+        else {
+            path = view.getClass().getSimpleName() + "/" + path;
+        }
+        if (view.getParent() != null && (view.getParent() instanceof ViewGroup)) {
+            return getViewPath((View) view.getParent(), path);
+        }
+        else {
+            return path;
+        }
+    }
+
     /**
      * 
@@ -481,13 +621,12 @@
      * Check whether a component is still written to log file.
      * </p>
-     *
+     * 
      * @param hashCode
-     *          hash code of the view
+     *            hash code of the view
      * @return
      */
-    public Boolean isComponentLogged(Integer hashCode){
-        return currentLoggedComponents.contains(hashCode);
-    }
-    
-    
+    public static Boolean isComponentLogged(Integer hashCode) {
+        return AndroidMonitorLogFile.currentLoggedComponents.contains(hashCode);
+    }
+
 }
