3
3
import android .content .Context ;
4
4
import android .content .Intent ;
5
5
import android .content .IntentFilter ;
6
+ import android .content .pm .ApplicationInfo ;
7
+ import android .content .pm .PackageInfo ;
8
+ import android .content .pm .PackageManager ;
9
+ import android .os .Bundle ;
6
10
import android .util .Log ;
7
11
8
12
import org .json .JSONArray ;
@@ -30,11 +34,13 @@ class Reporter extends BroadcastReceiver {
30
34
private static final int REPORTER_WINDOW = 3000 ; //ms
31
35
32
36
private static String MOZSTUMBLER_USER_AGENT_STRING ;
37
+ private static String MOZSTUMBLER_API_KEY_STRING ;
33
38
34
39
private final Context mContext ;
35
40
private final Prefs mPrefs ;
36
41
private JSONArray mReports ;
37
- private volatile long mLastUploadTime ; //TODO why volatile
42
+ private long mLastUploadTime ;
43
+ private URL mURL ;
38
44
39
45
private String mWifiData ;
40
46
private long mWifiDataTime ;
@@ -61,6 +67,23 @@ class Reporter extends BroadcastReceiver {
61
67
mReports = new JSONArray ();
62
68
}
63
69
70
+ try {
71
+ ApplicationInfo appi = context .getPackageManager ().getApplicationInfo (context .getPackageName (),
72
+ PackageManager .GET_META_DATA );
73
+ String apiKey = (String ) appi .metaData .get ("org.mozilla.mozstumbler.API_KEY" );
74
+ mURL = new URL (LOCATION_URL + "?key=" + apiKey );
75
+ } catch (Exception e ) {
76
+ Log .w (LOGTAG , "Not reporting with a valid API key." );
77
+ }
78
+
79
+ try {
80
+ if (mURL == null ) {
81
+ mURL = new URL (LOCATION_URL );
82
+ }
83
+ } catch (Exception f ) {
84
+ Log .e (LOGTAG , "Bad URL: " + LOCATION_URL );
85
+ }
86
+
64
87
resetData ();
65
88
mContext .registerReceiver (this , new IntentFilter (ScannerService .MESSAGE_TOPIC ));
66
89
}
@@ -169,8 +192,7 @@ public void run() {
169
192
try {
170
193
Log .d (LOGTAG , "sending results..." );
171
194
172
- URL url = new URL (LOCATION_URL );
173
- HttpURLConnection urlConnection = (HttpURLConnection ) url .openConnection ();
195
+ HttpURLConnection urlConnection = (HttpURLConnection ) mURL .openConnection ();
174
196
try {
175
197
urlConnection .setDoOutput (true );
176
198
urlConnection .setRequestProperty (USER_AGENT_HEADER , MOZSTUMBLER_USER_AGENT_STRING );
@@ -188,7 +210,7 @@ public void run() {
188
210
out .write (bytes );
189
211
out .flush ();
190
212
191
- Log .d (LOGTAG , "uploaded wrapperData: " + wrapperData + " to " + LOCATION_URL );
213
+ Log .d (LOGTAG , "uploaded wrapperData: " + wrapperData + " to " + mURL . toString () );
192
214
193
215
int code = urlConnection .getResponseCode ();
194
216
if (code >=200 && code <= 299 ) {
0 commit comments