Переглянути джерело

Abstract configuration of ExceptionHandler

master
Chris Smith 14 роки тому
джерело
коміт
cfd63db665

+ 1
- 15
code/ActivityRecorder/src/uk/co/md87/android/activityrecorder/ActivityRecorderActivity.java Переглянути файл

124
     public void onCreate(Bundle icicle) {
124
     public void onCreate(Bundle icicle) {
125
         super.onCreate(icicle);
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
         bindService(new Intent(this, RecorderService.class), connection, BIND_AUTO_CREATE);
129
         bindService(new Intent(this, RecorderService.class), connection, BIND_AUTO_CREATE);
132
 
130
 
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
     /** {@inheritDoc} */
186
     /** {@inheritDoc} */
201
     @Override
187
     @Override
202
     protected void onStart() {
188
     protected void onStart() {

+ 30
- 0
code/Common/ExceptionHandler.java Переглянути файл

22
 
22
 
23
 package uk.co.md87.android.common;
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
 import java.io.IOException;
28
 import java.io.IOException;
26
 import java.io.PrintWriter;
29
 import java.io.PrintWriter;
27
 import java.io.StringWriter;
30
 import java.io.StringWriter;
57
         this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
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
     public void uncaughtException(Thread t, Throwable e) {
90
     public void uncaughtException(Thread t, Throwable e) {
61
         String timestamp = String.valueOf(System.currentTimeMillis());
91
         String timestamp = String.valueOf(System.currentTimeMillis());
62
         final Writer result = new StringWriter();
92
         final Writer result = new StringWriter();

BIN
code/ContextAnalyser/dist/ContextAnalyser.apk Переглянути файл


+ 1
- 15
code/ContextAnalyser/src/uk/co/md87/android/contextanalyser/ContextAnalyserService.java Переглянути файл

108
     public void onCreate() {
108
     public void onCreate() {
109
         super.onCreate();
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
         locationMonitor = new LocationMonitorFactory().getMonitor(this);
113
         locationMonitor = new LocationMonitorFactory().getMonitor(this);
116
 
114
 
126
 
124
 
127
         FlurryAgent.onStartSession(this, "MKB8YES3C6CFB86PXYXK");
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
     public void poll() {
128
     public void poll() {
143
         handler.postDelayed(scheduleRunnable, POLLING_DELAY);
129
         handler.postDelayed(scheduleRunnable, POLLING_DELAY);

+ 1
- 15
code/SensorLogger/src/uk/co/md87/android/sensorlogger/activities/IntroActivity.java Переглянути файл

48
     public void onCreate(final Bundle icicle) {
48
     public void onCreate(final Bundle icicle) {
49
         super.onCreate(icicle);
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
         setContentView(R.layout.intro);
53
         setContentView(R.layout.intro);
56
 
54
 
57
         ((Button) findViewById(R.id.introstart)).setOnClickListener(this);
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
     /** {@inheritDoc} */
58
     /** {@inheritDoc} */
73
     @Override
59
     @Override
74
     public void onClick(final View arg0) {
60
     public void onClick(final View arg0) {

Завантаження…
Відмінити
Зберегти