Browse Source

Add uploader service

tags/SensorLogger/0.1
Chris Smith 14 years ago
parent
commit
9343ea6ca7

+ 3
- 1
code/SensorLogger/AndroidManifest.xml View File

@@ -8,8 +8,10 @@
8 8
                 <category android:name="android.intent.category.LAUNCHER"/>
9 9
             </intent-filter>
10 10
         </activity>
11
-
12 11
         <service android:name=".SensorLoggerService" android:label="Sensor Logger Service"/>
12
+        <service android:name=".UploaderService" android:label="Sensor Uploader Service"/>
13 13
     </application>
14
+
14 15
     <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
16
+    <uses-permission android:name="android.permission.INTERNET"/>
15 17
 </manifest>

BIN
code/SensorLogger/dist/SensorLogger.apk View File


+ 15
- 2
code/SensorLogger/res/layout/main.xml View File

@@ -1,5 +1,5 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 3
     android:orientation="vertical"
4 4
     android:layout_width="fill_parent"
5 5
     android:layout_height="fill_parent">" 
@@ -8,4 +8,17 @@
8 8
         android:layout_width="fill_parent"
9 9
         android:layout_height="wrap_content"
10 10
         android:text="Hello Android from NetBeans"/>
11
-</LinearLayout>
11
+    <Button
12
+        android:id="@+id/start"
13
+        android:layout_width="wrap_content"
14
+        android:layout_height="wrap_content"
15
+        android:layout_below="@+id/text"
16
+        android:text="Start/Stop service"/>
17
+    <Button
18
+        android:id="@+id/upload"
19
+        android:layout_width="wrap_content"
20
+        android:layout_height="wrap_content"
21
+        android:layout_below="@+id/text"
22
+        android:layout_toRightOf="@+id/start"
23
+        android:text="Upload data"/>
24
+</RelativeLayout>

+ 25
- 31
code/SensorLogger/src/uk/co/md87/android/sensorlogger/MainActivity.java View File

@@ -6,31 +6,20 @@
6 6
 package uk.co.md87.android.sensorlogger;
7 7
 
8 8
 import android.app.Activity;
9
-import android.content.Context;
10 9
 import android.content.Intent;
11
-import android.hardware.Sensor;
12
-import android.hardware.SensorEvent;
13
-import android.hardware.SensorEventListener;
14
-import android.hardware.SensorManager;
15 10
 import android.os.Bundle;
16
-import android.telephony.TelephonyManager;
17
-import android.util.Log;
11
+import android.view.View;
12
+import android.view.View.OnClickListener;
13
+import android.widget.Button;
18 14
 import android.widget.TextView;
19
-import java.io.BufferedInputStream;
20
-import java.io.BufferedReader;
21
-import java.io.FileNotFoundException;
22
-import java.io.FileReader;
23
-import java.io.IOException;
24
-import java.io.InputStreamReader;
25
-import java.util.Arrays;
26 15
 
27 16
 /**
28 17
  *
29 18
  * @author chris
30 19
  */
31
-public class MainActivity extends Activity {
20
+public class MainActivity extends Activity implements OnClickListener {
32 21
 
33
-    private static final String TAG = "MainActivity";
22
+    static final String VERSION = "0.1";
34 23
 
35 24
     /** {@inheritDoc} */
36 25
     @Override
@@ -41,25 +30,30 @@ public class MainActivity extends Activity {
41 30
 
42 31
         setContentView(R.layout.main);
43 32
 
44
-        int lines = 0;
33
+        ((Button) findViewById(R.id.start)).setOnClickListener(this);
34
+        ((Button) findViewById(R.id.upload)).setOnClickListener(this);
45 35
 
46
-        try {
47
-            String line;
48
-            BufferedReader reader =
49
-                new BufferedReader(new InputStreamReader(openFileInput("sensors.log")));
50 36
 
51
-            while ((line = reader.readLine()) != null) {
52
-                lines++;
37
+        ((TextView) findViewById(R.id.text)).setText("Welcome to sensor logger v"
38
+                + VERSION + "..."
39
+                + "\n\nThis application records any changes in your phone's "
40
+                + "accelerometer state to a text file, along with the timestamp "
41
+                + "that the change occured at.\n\n"
42
+                + "Hit the upload button to upload the data you've recorded so "
43
+                + "it can be analysed. Thanks.\n\n");
44
+    }
45
+
46
+    /** {@inheritDoc} */
47
+    @Override
48
+    public void onClick(final View view) {
49
+        if (view.getId() == R.id.start) {
50
+            if (!stopService(new Intent(this, SensorLoggerService.class))) {
51
+                startService(new Intent(this, SensorLoggerService.class));
53 52
             }
54
-        } catch (FileNotFoundException ex) {
55
-            lines = -1;
56
-        } catch (IOException ex) {
57
-            lines = -2;
53
+        } else if (view.getId() == R.id.upload) {
54
+            stopService(new Intent(this, SensorLoggerService.class));
55
+            startService(new Intent(this, UploaderService.class));
58 56
         }
59
-
60
-        ((TextView) findViewById(R.id.text)).setText(
61
-                ((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId()
62
-                + " -- " + getFileStreamPath("sensors.log") + " -- " + lines);
63 57
     }
64 58
 
65 59
 }

+ 2
- 0
code/SensorLogger/src/uk/co/md87/android/sensorlogger/R.java View File

@@ -11,7 +11,9 @@ public final class R {
11 11
     public static final class attr {
12 12
     }
13 13
     public static final class id {
14
+        public static final int start=0x7f040001;
14 15
         public static final int text=0x7f040000;
16
+        public static final int upload=0x7f040002;
15 17
     }
16 18
     public static final class layout {
17 19
         public static final int main=0x7f020000;

+ 15
- 4
code/SensorLogger/src/uk/co/md87/android/sensorlogger/SensorLoggerService.java View File

@@ -13,13 +13,12 @@ import android.hardware.SensorEvent;
13 13
 import android.hardware.SensorEventListener;
14 14
 import android.hardware.SensorManager;
15 15
 import android.os.IBinder;
16
-import android.util.Log;
17 16
 import android.widget.Toast;
17
+
18 18
 import java.io.FileNotFoundException;
19 19
 import java.io.FileOutputStream;
20 20
 import java.io.IOException;
21 21
 import java.io.OutputStreamWriter;
22
-import java.util.Arrays;
23 22
 
24 23
 /**
25 24
  *
@@ -42,7 +41,6 @@ public class SensorLoggerService extends Service {
42 41
                 writer.write(System.currentTimeMillis() + ":" +
43 42
                         event.values[0] + "," + event.values[1]
44 43
                         + "," + event.values[2] + "\n");
45
-                Toast.makeText(getApplicationContext(), "New values!", Toast.LENGTH_SHORT).show();
46 44
             } catch (IOException ex) {
47 45
 
48 46
             }
@@ -68,8 +66,12 @@ public class SensorLoggerService extends Service {
68 66
         try {
69 67
             stream = openFileOutput("sensors.log", MODE_APPEND | MODE_WORLD_READABLE);
70 68
             writer = new OutputStreamWriter(stream);
69
+            writer.write("Hello world");
70
+            writer.flush();
71 71
         } catch (FileNotFoundException ex) {
72 72
             return;
73
+        } catch (IOException ex) {
74
+            return;
73 75
         }
74 76
 
75 77
         manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
@@ -78,7 +80,16 @@ public class SensorLoggerService extends Service {
78 80
             manager.registerListener(accelListener, sensor, SensorManager.SENSOR_DELAY_FASTEST);
79 81
         }
80 82
 
81
-        Log.i(TAG, "Registered listeners!");
83
+        Toast.makeText(getApplicationContext(), "Sensor logger service started",
84
+                Toast.LENGTH_SHORT).show();
85
+    }
86
+
87
+    @Override
88
+    public void onDestroy() {
89
+        manager.unregisterListener(accelListener);
90
+
91
+        Toast.makeText(getApplicationContext(), "Sensor logger service destroyed",
92
+                Toast.LENGTH_SHORT).show();
82 93
     }
83 94
 
84 95
 }

+ 66
- 0
code/SensorLogger/src/uk/co/md87/android/sensorlogger/UploaderService.java View File

@@ -0,0 +1,66 @@
1
+/*
2
+ * To change this template, choose Tools | Templates
3
+ * and open the template in the editor.
4
+ */
5
+
6
+package uk.co.md87.android.sensorlogger;
7
+
8
+import android.app.Service;
9
+import android.content.Context;
10
+import android.content.Intent;
11
+import android.os.IBinder;
12
+import android.telephony.TelephonyManager;
13
+import android.util.Log;
14
+import android.widget.Toast;
15
+
16
+import java.io.File;
17
+import java.io.IOException;
18
+
19
+import org.apache.http.client.methods.HttpPost;
20
+import org.apache.http.entity.FileEntity;
21
+import org.apache.http.impl.client.DefaultHttpClient;
22
+import org.apache.http.params.HttpParams;
23
+
24
+/**
25
+ *
26
+ * @author chris
27
+ */
28
+public class UploaderService extends Service {
29
+
30
+    @Override
31
+    public void onStart(Intent intent, int startId) {
32
+        super.onStart(intent, startId);
33
+
34
+        final HttpPost post = new HttpPost("http://chris.smith.name/android/upload");
35
+        final File file = getFileStreamPath("sensors.log");
36
+        final FileEntity entity = new FileEntity(file, "text/plain");
37
+
38
+        post.setEntity(entity);
39
+        post.addHeader("x-application", "SensorLogger");
40
+        post.addHeader("x-version", MainActivity.VERSION);
41
+        post.addHeader("x-imei", ((TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId());
42
+
43
+        try {
44
+            int code = new DefaultHttpClient().execute(post).getStatusLine().getStatusCode();
45
+
46
+            Toast.makeText(getApplicationContext(), "Upload complete (" + code + ")",
47
+                Toast.LENGTH_SHORT).show();
48
+
49
+            file.delete();
50
+        } catch (IOException ex) {
51
+            Log.e("UploaderService", "Unable to upload sensor logs", ex);
52
+
53
+            Toast.makeText(getApplicationContext(), "Upload failed",
54
+                Toast.LENGTH_SHORT).show();
55
+        }
56
+
57
+        startService(new Intent(this, SensorLoggerService.class));
58
+        stopSelf();
59
+    }
60
+
61
+    @Override
62
+    public IBinder onBind(Intent arg0) {
63
+        return null;
64
+    }
65
+
66
+}

Loading…
Cancel
Save