-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
172 additions
and
39 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
53 changes: 53 additions & 0 deletions
53
samples/PhoneHomeTest/src/com/example/phonehometest/example/ExampleEligibilityChecker.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,53 @@ | ||
package com.example.phonehometest.example; | ||
|
||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.util.EntityUtils; | ||
import org.json.JSONObject; | ||
|
||
import android.content.Context; | ||
import android.net.http.AndroidHttpClient; | ||
import android.os.AsyncTask; | ||
|
||
public final class ExampleEligibilityChecker { | ||
// matches with the example backend provided | ||
private static final String HTTP_ENDPOINT = Utils.HTTP_BASE + "/eligibility"; | ||
|
||
private final Context context; | ||
private final AndroidHttpClient httpClient = AndroidHttpClient.newInstance("PhoneHomeTest"); | ||
|
||
public ExampleEligibilityChecker(Context context) { | ||
this.context = context; | ||
} | ||
|
||
public interface EligibilityCallback { | ||
public void handleEligibilty(boolean isEligible); | ||
} | ||
|
||
public void checkEligibility(final EligibilityCallback eligibilityCallback) { | ||
new AsyncTask<Void, Void, Boolean>() { | ||
@Override | ||
protected Boolean doInBackground(Void... params) { | ||
HttpGet httpGet = new HttpGet(HTTP_ENDPOINT); | ||
httpGet.setHeaders(Utils.getAndroidHeaders(context)); | ||
|
||
HttpResponse response; | ||
try { | ||
response = httpClient.execute(httpGet); | ||
String json = EntityUtils.toString(response.getEntity(), "UTF-8"); | ||
|
||
JSONObject obj = new JSONObject(json); | ||
return obj.getBoolean("is_eligible"); | ||
|
||
} catch (Exception ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(final Boolean isEligible) { | ||
eligibilityCallback.handleEligibilty(isEligible); | ||
} | ||
}.execute(); | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
samples/PhoneHomeTest/src/com/example/phonehometest/example/Utils.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,53 @@ | ||
package com.example.phonehometest.example; | ||
|
||
import org.apache.http.Header; | ||
import org.apache.http.message.BasicHeader; | ||
|
||
import android.content.Context; | ||
import android.content.pm.PackageInfo; | ||
import android.content.pm.PackageManager; | ||
import android.content.pm.PackageManager.NameNotFoundException; | ||
import android.os.Build; | ||
|
||
public final class Utils { | ||
// this points at localhost on whatever machine the meulator's running on; obviously change this to suit your needs | ||
static final String HTTP_BASE = "http://10.0.2.2"; | ||
|
||
private Utils() {} | ||
|
||
public static class AndroidInfo { | ||
public final String model; | ||
public final int sdkVersion; | ||
public final int appVersion; | ||
|
||
private AndroidInfo(final String model, final int sdkVersion, final int appVersion) { | ||
this.model = model; | ||
this.sdkVersion = sdkVersion; | ||
this.appVersion = appVersion; | ||
} | ||
} | ||
|
||
public static AndroidInfo getAndroidInfo(Context context) { | ||
int versionCode = -1; | ||
try { | ||
PackageManager manager = context.getPackageManager(); | ||
// FIXME point this at your own package! | ||
PackageInfo info = manager.getPackageInfo("com.example.phonehometest", 0); | ||
versionCode = info.versionCode; | ||
} catch (NameNotFoundException nnf) { | ||
throw new RuntimeException("Couldn't get package versionCode!", nnf); | ||
} | ||
|
||
return new AndroidInfo(Build.MODEL, Build.VERSION.SDK_INT, versionCode); | ||
} | ||
|
||
static Header[] getAndroidHeaders(Context context) { | ||
// see backend example for how these are used to associate log events with users | ||
AndroidInfo androidInfo = getAndroidInfo(context); | ||
return new Header[] { | ||
new BasicHeader("X-Android-Model", androidInfo.model), | ||
new BasicHeader("X-Android-Sdk", Integer.toString(androidInfo.sdkVersion)), | ||
new BasicHeader("X-Android-AppVersion", Integer.toString(androidInfo.appVersion)) | ||
}; | ||
} | ||
} |