Skip to content
This repository has been archived by the owner on Aug 15, 2022. It is now read-only.

Commit

Permalink
added lookup of provider name
Browse files Browse the repository at this point in the history
resolves #2
  • Loading branch information
sylvainemery committed Nov 17, 2014
1 parent 8f4dd4e commit f72d746
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions app/src/main/java/com/hackncheese/glassnetinfo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class MainActivity extends Activity {
private Slider.Indeterminate mIndSlider;

private GetExternalIPTask mExtTask;
private GetExternalIPInfoTask mExtInfoTask;

@Override
protected void onCreate(Bundle bundle) {
Expand Down Expand Up @@ -107,6 +108,11 @@ protected void onPause() {
mExtTask.cancel(true); // true = force interruption
}

// cancel the async task if it exists
if (mExtInfoTask != null) {
mExtInfoTask.cancel(true); // true = force interruption
}

super.onPause();
}

Expand Down Expand Up @@ -183,6 +189,33 @@ public String getExternalIpAddress() {

}

public String getExternalIpInfoAddress(String ip) {
OkHttpClient client = new OkHttpClient();
String NetworkProviderName = ip;

// don't wait more than 3 seconds total
client.setConnectTimeout(1000, TimeUnit.MILLISECONDS);
client.setWriteTimeout(1000, TimeUnit.MILLISECONDS);
client.setReadTimeout(1000, TimeUnit.MILLISECONDS);

Request request = new Request.Builder()
.url(String.format("http://ipinfo.io/%s/org", ip))
.build();

try {
Response response = client.newCall(request).execute();
String resp = response.body().string();
int idx = resp.indexOf(" ");
if (idx > 0) {
NetworkProviderName = resp.substring(idx, resp.length()).trim();
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
return NetworkProviderName;

}

private class GetExternalIPTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... p) {
Expand Down Expand Up @@ -210,6 +243,39 @@ protected void onPostExecute(String ip) {
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.playSoundEffect(Sounds.SUCCESS);

// get more info on the external IP
mExtInfoTask = new GetExternalIPInfoTask();
mExtInfoTask.execute(ip);

}
}
private class GetExternalIPInfoTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... ip) {
return getExternalIpInfoAddress(ip[0]);
}

protected void onPreExecute() {
// show progress bar
mIndSlider = mSlider.startIndeterminate();
}

protected void onPostExecute(String networkProviderName) {
// hide the progress bar
if (mIndSlider != null) {
mIndSlider.hide();
mIndSlider = null;
}
// add external ip to the list
ips.put("provider", networkProviderName);
// update the card info
updateCard();
// notify that the card UI must be redrawn
mCardAdapter.notifyDataSetChanged();
// play a nice sound
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.playSoundEffect(Sounds.SUCCESS);

}
}
}

0 comments on commit f72d746

Please sign in to comment.