Skip to content

Commit

Permalink
* reworked leaderboard to load all FxA configuration from a fixed JSO…
Browse files Browse the repository at this point in the history
…N blob on a URI.

* added an asynctask to fetch the leaderboard configuration data
  • Loading branch information
crankycoder committed Jan 11, 2016
1 parent 8544424 commit 99ad711
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 54 deletions.
34 changes: 14 additions & 20 deletions android/build.gradle
@@ -1,7 +1,7 @@
// each of the version numbers must be 0-99
def versionMajor = 1
def versionMinor = 7 // minor feature releases
def versionPatch = 12 // This should be bumped for hot fixes
def versionMinor = 8 // minor feature releases
def versionPatch = 1 // This should be bumped for hot fixes

// Double check the versioning
for (versionPart in [versionPatch, versionMinor, versionMajor]) {
Expand Down Expand Up @@ -120,29 +120,23 @@ android {
// standard unlabelled tiles.
buildConfigField "boolean", "LABEL_MAP_TILES", "false"

buildConfigField "String", "FXA_APP_KEY", "\"86cd25bed2c63936\""
// buildConfigField "String", "FXA_APP_KEY", "\"86cd25bed2c63936\""
// buildConfigField "String", "FXA_OAUTH2_SERVER", "\"https://oauth-stable.dev.lcip.org\""
// buildConfigField "String", "FXA_PROFILE_SERVER", "\"https://stable.dev.lcip.org/profile\""
// buildConfigField "String", "FXA_APP_CALLBACK", "\"http://leaderboard-dev.jaredkerim.com\""

buildConfigField "String", "FXA_OAUTH2_SERVER", "\"https://oauth-stable.dev.lcip.org/v1\""
buildConfigField "String", "FXA_PROFILE_SERVER", "\"https://stable.dev.lcip.org/profile/v1\""

buildConfigField "String", "FXA_APP_CALLBACK", "\"http://leaderboard-dev.jaredkerim.com\""
buildConfigField "String", "LB_SUBMIT_URL", "\"http://leaderboard-dev.jaredkerim.com/api/v1/contributions/\""
// This is needed to generate the submission URL and the
// profile URL to view user stats
buildConfigField "String", "LB_BASE_URL", "\"http://cloudvm.jaredkerim.com\""

// This should go away and hang off the LB_BASE_URL
buildConfigField "String", "LB_SUBMIT_URL", "\"http://cloudvm.jaredkerim.com/api/v1/contributions/\""
}

release {
signingConfig signingConfigs.release

// TODO: this key will need to be reprovisioned for
// production releases
buildConfigField "String", "FXA_APP_KEY", "\"d0f6d2ed3c5fcc3b\""

buildConfigField "String", "FXA_OAUTH2_SERVER", "\"https://oauth.accounts.firefox.com/v1\""
buildConfigField "String", "FXA_PROFILE_SERVER", "\"https://profile.accounts.firefox.com/v1\""
// buildConfigField "String", "FXA_SIGNIN_URL", "\"https://accounts.firefox.com/signin\""

// TODO: update this callback for production
buildConfigField "String", "FXA_APP_CALLBACK", "\"http://ec2-52-1-93-147.compute-1.amazonaws.com/fxa/callback\""
buildConfigField "String", "LB_SUBMIT_URL", "\"http://leaderboard-dev.jaredkerim.com/api/v1/contributions/\""
}


Expand Down Expand Up @@ -255,8 +249,8 @@ dependencies {
compile "org.apache.james:apache-mime4j:0.6"

// Firefox Accounts library code
compile 'org.mozilla.accounts.fxa:fxa:0.9.17'
testCompile 'org.mozilla.accounts.fxa:fxa:0.9.17'
compile 'org.mozilla.accounts.fxa:fxa:0.10.2'
testCompile 'org.mozilla.accounts.fxa:fxa:0.10.2'

// org.apache.http.httpclient
compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
Expand Down
@@ -0,0 +1,115 @@
package org.mozilla.mozstumbler.client.subactivities;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.accounts.fxa.FxAGlobals;
import org.mozilla.accounts.fxa.LoggerUtil;
import org.mozilla.accounts.fxa.net.HTTPResponse;
import org.mozilla.accounts.fxa.net.HttpUtil;

import java.util.HashMap;
import java.util.Map;

import static org.mozilla.accounts.fxa.Intents.ACCESS_TOKEN_REFRESH;
import static org.mozilla.accounts.fxa.Intents.ACCESS_TOKEN_REFRESH_FAILURE;

/**
* Created by victorng on 2015-12-31.
*
*
* This class jsut calls GET on a URL, passes in a refresh token, an existing access token
* and the server processes
*
*/
public class FetchFxaConfiguration extends AsyncTask<Void, Void, JSONObject> {

// Most applications should use a refreshed access token on application startup.
// This will minimize the lifetime of any access token.

public static final String FXA_CONFIG_LOAD = "org.mozilla.accounts.fxa.config.load";
public static final String FXA_CONFIG_LOAD_FAILURE = "org.mozilla.accounts.fxa.config.load.failure";


private static final String LOG_TAG = LoggerUtil.makeLogTag(FetchFxaConfiguration.class);
private final String configuration_endpoint;
private final Context mContext;

public FetchFxaConfiguration(Context ctx, String cfg_url) {
mContext = ctx;
this.configuration_endpoint = cfg_url;
}

public String getFxaConfigEndpoint() {
return configuration_endpoint;
}

public AsyncTask<Void, Void, JSONObject> execute() {
return super.execute();
}


/*
This task requires no arguments.
*/
@Override
protected JSONObject doInBackground(Void... params) {
if (params.length != 0) {
Log.i(LOG_TAG, "Invalid number of arguments.");
return null;
}

HttpUtil httpUtil = new HttpUtil(System.getProperty("http.agent") + " " +
FxAGlobals.appName + "/" + FxAGlobals.appVersionName);

Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");

HTTPResponse resp = httpUtil.get(getFxaConfigEndpoint(), headers);

if (resp.isSuccessCode2XX()) {
try {
return new JSONObject(resp.body());
} catch (JSONException e) {
Log.e(LOG_TAG, "Error marshalling the FxA configuration JSON blob.");
return null;
}
} else {
Log.w(LOG_TAG, "FxA Configuration HTTP Status: " + resp.httpStatusCode());
return null;
}
}



@Override
protected void onPostExecute(JSONObject result) {
if (result == null) {
Intent intent = new Intent(FXA_CONFIG_LOAD_FAILURE);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
} else {
Intent intent = new Intent(FXA_CONFIG_LOAD);
intent.putExtra("json", result.toString());
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
}
}

public static void registerFxaIntents(Context ctx, BroadcastReceiver callbackReceiver) {
IntentFilter intentFilter = new IntentFilter();

intentFilter.addAction(FetchFxaConfiguration.FXA_CONFIG_LOAD);
intentFilter.addAction(FetchFxaConfiguration.FXA_CONFIG_LOAD_FAILURE);

LocalBroadcastManager.getInstance(ctx)
.registerReceiver(callbackReceiver, intentFilter);

}
}
Expand Up @@ -14,11 +14,12 @@
import android.webkit.WebViewClient;
import android.widget.Toast;

import org.mozilla.mozstumbler.BuildConfig;
import org.mozilla.mozstumbler.R;
import org.mozilla.mozstumbler.client.ClientPrefs;

public class LeaderboardActivity extends ActionBarActivity {
private static final String LEADERBOARD_URL = "https://location.services.mozilla.com/leaders";
private static final String LEADERBOARD_URL = BuildConfig.LB_BASE_URL;
private WebView mWebView;
private boolean mHasError;

Expand Down Expand Up @@ -67,15 +68,14 @@ public void onPageFinished(WebView webview, String url) {

setProgress(0);
ClientPrefs prefs = ClientPrefs.getInstance(getApplicationContext());
String nick = prefs.getNickname();
String url = LEADERBOARD_URL;
if (nick != null) {
url += "#" + nick;
} else {
// TODO Get server side to add this anchor
// https://github.com/mozilla/ichnaea/issues/327
url += "#leaderboard_start";
}
String url = LEADERBOARD_URL + "/?profile=" + getPrefs().getLeaderboardUID();

mWebView.loadUrl(url);
}


private ClientPrefs getPrefs() {
return ClientPrefs.getInstance(this);
}

}

0 comments on commit 99ad711

Please sign in to comment.