Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* reworked leaderboard to load all FxA configuration from a fixed JSO…
…N blob on a URI. * added an asynctask to fetch the leaderboard configuration data
- Loading branch information
1 parent
8544424
commit 99ad711
Showing
5 changed files
with
262 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...oid/src/main/java/org/mozilla/mozstumbler/client/subactivities/FetchFxaConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.