Skip to content

Commit bb92c16

Browse files
committed
Merge pull request #160 from dougt/api_key
Add support for an API key
2 parents ae86f09 + fbd401a commit bb92c16

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

AndroidManifest.xml.in

+4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
android:name="com.google.android.maps.v2.API_KEY"
6767
android:value="MY_GOOGLE_API_KEY"/>
6868

69+
<meta-data
70+
android:name="org.mozilla.mozstumbler.API_KEY"
71+
android:value="MY_MOZILLA_API_KEY"/>
72+
6973
</application>
7074

7175
</manifest>

build.gradle

+7-2
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,14 @@ if (hasProperty('StoreFile')) {
6363
}
6464

6565
// Generate AndroidManifest.xml
66-
String myAPIKey = hasProperty('APIKey') ? APIKey : "FAKE_GOOGLE_API_KEY";
66+
String googleAPIKey = hasProperty('APIKey') ? APIKey : "FAKE_GOOGLE_API_KEY";
67+
String mozillaAPIKey = hasProperty('MozAPIKey') ? MozAPIKey : "FAKE_MOZILLA_API_KEY";
68+
6769
File manifest = file("./AndroidManifest.xml.in")
6870
String content = manifest.getText('UTF-8')
69-
content = content.replaceAll(/MY_GOOGLE_API_KEY/, myAPIKey)
71+
content = content.replaceAll(/MY_GOOGLE_API_KEY/, googleAPIKey)
72+
content = content.replaceAll(/MY_MOZILLA_API_KEY/, mozillaAPIKey)
73+
7074
manifest = file("./AndroidManifest.xml")
7175
manifest.write(content, 'UTF-8')
76+

src/org/mozilla/mozstumbler/Reporter.java

+26-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import android.content.Context;
44
import android.content.Intent;
55
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;
610
import android.util.Log;
711

812
import org.json.JSONArray;
@@ -30,11 +34,13 @@ class Reporter extends BroadcastReceiver {
3034
private static final int REPORTER_WINDOW = 3000; //ms
3135

3236
private static String MOZSTUMBLER_USER_AGENT_STRING;
37+
private static String MOZSTUMBLER_API_KEY_STRING;
3338

3439
private final Context mContext;
3540
private final Prefs mPrefs;
3641
private JSONArray mReports;
37-
private volatile long mLastUploadTime; //TODO why volatile
42+
private long mLastUploadTime;
43+
private URL mURL;
3844

3945
private String mWifiData;
4046
private long mWifiDataTime;
@@ -61,6 +67,23 @@ class Reporter extends BroadcastReceiver {
6167
mReports = new JSONArray();
6268
}
6369

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+
6487
resetData();
6588
mContext.registerReceiver(this, new IntentFilter(ScannerService.MESSAGE_TOPIC));
6689
}
@@ -169,8 +192,7 @@ public void run() {
169192
try {
170193
Log.d(LOGTAG, "sending results...");
171194

172-
URL url = new URL(LOCATION_URL);
173-
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
195+
HttpURLConnection urlConnection = (HttpURLConnection) mURL.openConnection();
174196
try {
175197
urlConnection.setDoOutput(true);
176198
urlConnection.setRequestProperty(USER_AGENT_HEADER, MOZSTUMBLER_USER_AGENT_STRING);
@@ -188,7 +210,7 @@ public void run() {
188210
out.write(bytes);
189211
out.flush();
190212

191-
Log.d(LOGTAG, "uploaded wrapperData: " + wrapperData + " to " + LOCATION_URL);
213+
Log.d(LOGTAG, "uploaded wrapperData: " + wrapperData + " to " + mURL.toString());
192214

193215
int code = urlConnection.getResponseCode();
194216
if (code>=200 && code <= 299) {

0 commit comments

Comments
 (0)