Просмотр исходного кода

Abstract configuration of ExceptionHandler

master
Chris Smith 14 лет назад
Родитель
Сommit
cfd63db665

+ 1
- 15
code/ActivityRecorder/src/uk/co/md87/android/activityrecorder/ActivityRecorderActivity.java Просмотреть файл

@@ -124,9 +124,7 @@ public class ActivityRecorderActivity extends Activity {
124 124
     public void onCreate(Bundle icicle) {
125 125
         super.onCreate(icicle);
126 126
 
127
-        Thread.setDefaultUncaughtExceptionHandler(
128
-                new ExceptionHandler("ActivityRecorder",
129
-                "http://chris.smith.name/android/upload", getVersionName(), getIMEI()));
127
+        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
130 128
 
131 129
         bindService(new Intent(this, RecorderService.class), connection, BIND_AUTO_CREATE);
132 130
 
@@ -185,18 +183,6 @@ public class ActivityRecorderActivity extends Activity {
185 183
         }
186 184
     }
187 185
 
188
-    public String getVersionName() {
189
-        try {
190
-            return getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
191
-        } catch (NameNotFoundException ex) {
192
-            return "Unknown";
193
-        }
194
-    }
195
-
196
-    public String getIMEI() {
197
-        return ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).getDeviceId();
198
-    }
199
-
200 186
     /** {@inheritDoc} */
201 187
     @Override
202 188
     protected void onStart() {

+ 30
- 0
code/Common/ExceptionHandler.java Просмотреть файл

@@ -22,6 +22,9 @@
22 22
 
23 23
 package uk.co.md87.android.common;
24 24
 
25
+import android.content.Context;
26
+import android.content.pm.PackageManager.NameNotFoundException;
27
+import android.telephony.TelephonyManager;
25 28
 import java.io.IOException;
26 29
 import java.io.PrintWriter;
27 30
 import java.io.StringWriter;
@@ -57,6 +60,33 @@ public class ExceptionHandler implements UncaughtExceptionHandler {
57 60
         this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
58 61
     }
59 62
 
63
+    public ExceptionHandler(Context context) {
64
+        this(getAppName(context), "http://chris.smith.name/android/upload",
65
+                getVersionName(context), getIMEI(context));
66
+    }
67
+
68
+    private static String getVersionName(final Context context) {
69
+        try {
70
+            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
71
+        } catch (NameNotFoundException ex) {
72
+            return "Unknown";
73
+        }
74
+    }
75
+
76
+    private static String getAppName(final Context context) {
77
+        try {
78
+            return context.getPackageManager().getApplicationLabel(context
79
+                    .getPackageManager().getPackageInfo(context.getPackageName(), 0)
80
+                    .applicationInfo).toString().replaceAll("[^A-Za-z]", "");
81
+        } catch (NameNotFoundException ex) {
82
+            return "Unknown";
83
+        }
84
+    }
85
+
86
+    private static String getIMEI(final Context context) {
87
+        return ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
88
+    }
89
+
60 90
     public void uncaughtException(Thread t, Throwable e) {
61 91
         String timestamp = String.valueOf(System.currentTimeMillis());
62 92
         final Writer result = new StringWriter();

Двоичные данные
code/ContextAnalyser/dist/ContextAnalyser.apk Просмотреть файл


+ 1
- 15
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/ContextAnalyserService.java Просмотреть файл

@@ -108,9 +108,7 @@ public class ContextAnalyserService extends Service {
108 108
     public void onCreate() {
109 109
         super.onCreate();
110 110
 
111
-        Thread.setDefaultUncaughtExceptionHandler(
112
-                new ExceptionHandler("ContextAnalyser",
113
-                "http://chris.smith.name/android/upload", getVersionName(), getIMEI()));
111
+        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
114 112
 
115 113
         locationMonitor = new LocationMonitorFactory().getMonitor(this);
116 114
 
@@ -126,18 +124,6 @@ public class ContextAnalyserService extends Service {
126 124
 
127 125
         FlurryAgent.onStartSession(this, "MKB8YES3C6CFB86PXYXK");
128 126
     }
129
-
130
-    public String getVersionName() {
131
-        try {
132
-            return getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
133
-        } catch (NameNotFoundException ex) {
134
-            return "Unknown";
135
-        }
136
-    }
137
-
138
-    public String getIMEI() {
139
-        return ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).getDeviceId();
140
-    }
141 127
     
142 128
     public void poll() {
143 129
         handler.postDelayed(scheduleRunnable, POLLING_DELAY);

+ 1
- 15
code/SensorLogger/src/uk/co/md87/android/sensorlogger/activities/IntroActivity.java Просмотреть файл

@@ -48,27 +48,13 @@ public class IntroActivity extends BoundActivity implements OnClickListener {
48 48
     public void onCreate(final Bundle icicle) {
49 49
         super.onCreate(icicle);
50 50
 
51
-        Thread.setDefaultUncaughtExceptionHandler(
52
-                new ExceptionHandler("SensorLogger",
53
-                "http://chris.smith.name/android/upload", getVersionName(), getIMEI()));
51
+        Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
54 52
 
55 53
         setContentView(R.layout.intro);
56 54
 
57 55
         ((Button) findViewById(R.id.introstart)).setOnClickListener(this);
58 56
     }
59 57
 
60
-    public String getVersionName() {
61
-        try {
62
-            return getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
63
-        } catch (NameNotFoundException ex) {
64
-            return "Unknown";
65
-        }
66
-    }
67
-
68
-    public String getIMEI() {
69
-        return ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).getDeviceId();
70
-    }
71
-
72 58
     /** {@inheritDoc} */
73 59
     @Override
74 60
     public void onClick(final View arg0) {

Загрузка…
Отмена
Сохранить