Skip to content

Commit

Permalink
Code cleanup and Android linting
Browse files Browse the repository at this point in the history
  • Loading branch information
audaciouscode committed Jun 12, 2017
1 parent 30ab595 commit 0d7f08a
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 88 deletions.
6 changes: 2 additions & 4 deletions AndroidManifest.xml
@@ -1,7 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.northwestern.cbits.xsi"
android:versionCode="101"
android:versionName="1.0.1" >
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="edu.northwestern.cbits.xsi">
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
Expand Down
4 changes: 2 additions & 2 deletions res/values/strings.xml
Expand Up @@ -8,8 +8,8 @@
<string name="auth_success_message">You have successfully connected the app to your online account.</string>
<string name="auth_success_close">Continue&#8230;</string>

<string name="uri_foursquare_explore" formatted="false">https://api.foursquare.com/v2/venues/explore?client_id=%s&amp;client_secret=%s&amp;v=20130815&amp;ll=%f,%f&amp;sortByDistance=1&amp;query=%s</string>
<string name="uri_foursquare_venue_photo" formatted="false">https://api.foursquare.com/v2/venues/%s/photos?client_id=%s&amp;client_secret=%s&amp;v=20130815</string>
<string name="uri_foursquare_explore">https://api.foursquare.com/v2/venues/explore?client_id=%1$s&amp;client_secret=%2$s&amp;v=20130815&amp;ll=%3$f,%4$f&amp;sortByDistance=1&amp;query=%5$s</string>
<string name="uri_foursquare_venue_photo">https://api.foursquare.com/v2/venues/%1$s/photos?client_id=%2$s&amp;client_secret=%3$s&amp;v=20130815</string>

<string name="title_facebook_success">Facebook Login Successful!</string>
<string name="message_facebook_success">You have successfully logged in to Facebook and can now fetch data from the service.</string>
Expand Down
Expand Up @@ -41,7 +41,7 @@ protected void onResume()
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor e = prefs.edit();
e.remove(FacebookApi.TOKEN);
e.commit();
e.apply();

final FacebookLoginActivity me = this;

Expand Down Expand Up @@ -114,7 +114,7 @@ private void go(Session session)
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor e = prefs.edit();
e.putString(FacebookApi.TOKEN, token);
e.commit();
e.apply();

AlertDialog.Builder builder = new AlertDialog.Builder(this);

Expand Down
5 changes: 3 additions & 2 deletions src/edu/northwestern/cbits/xsi/logging/LogManager.java
Expand Up @@ -85,15 +85,16 @@ public String getUserId()
AccountManager manager = (AccountManager) this._context.getSystemService(Context.ACCOUNT_SERVICE);
Account[] list = manager.getAccountsByType("com.google");

if (list.length == 0)
if (list.length == 0) {
list = manager.getAccounts();
}

if (list.length > 0)
userId = list[0].name;

Editor e = prefs.edit();
e.putString("config_user_id", userId);
e.commit();
e.apply();
}

return userId;
Expand Down
4 changes: 2 additions & 2 deletions src/edu/northwestern/cbits/xsi/oauth/FitbitApi.java
Expand Up @@ -163,7 +163,7 @@ public static boolean refreshTokens(Context context, String accessKey, String re
writer.close();
os.close();

conn.connect();;
conn.connect();

InputStream in = null;

Expand Down Expand Up @@ -193,7 +193,7 @@ public static boolean refreshTokens(Context context, String accessKey, String re
e.putString(accessKey, payload.getString("access_token"));
e.putString(refreshKey, payload.getString("refresh_token"));
e.putLong(expiresKey, System.currentTimeMillis() + (payload.getLong("expires_in") * 1000));
e.commit();
e.apply();

return true;
} catch (IOException | JSONException e) {
Expand Down
6 changes: 3 additions & 3 deletions src/edu/northwestern/cbits/xsi/oauth/FoursquareApi.java
@@ -1,6 +1,7 @@
package edu.northwestern.cbits.xsi.oauth;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;

Expand Down Expand Up @@ -141,9 +142,8 @@ public static List<Place> searchPlaces(Context context, double latitude, double
FoursquareApi._exclude = new HashSet<>();

String[] excluded = context.getResources().getStringArray(R.array.foursquare_excluded_categories);

for (String exclude : excluded)
FoursquareApi._exclude.add(exclude);

Collections.addAll(FoursquareApi._exclude, excluded);
}

try
Expand Down
1 change: 0 additions & 1 deletion src/edu/northwestern/cbits/xsi/oauth/JawboneApi.java
Expand Up @@ -8,7 +8,6 @@

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.scheme.PlainSocketFactory;
Expand Down
12 changes: 6 additions & 6 deletions src/edu/northwestern/cbits/xsi/oauth/OAuthActivity.java
Expand Up @@ -158,7 +158,7 @@ else if (DefaultApi10a.class.isAssignableFrom(apiClass))
Editor e = prefs.edit();
e.putString("request_token_" + requester, token.getToken());
e.putString("request_secret_" + requester, token.getSecret());
e.commit();
e.apply();

Intent intent = new Intent(me, OAuthWebActivity.class);
intent.putExtra(OAuthActivity.LOG_URL, logUrl);
Expand Down Expand Up @@ -208,7 +208,7 @@ else if (DefaultApi10a.class.isAssignableFrom(apiClass))
e.putString("oauth_" + requester + "_secret", "");
e.putString("oauth_" + requester + "_token", access);

e.commit();
e.apply();

me.runOnUiThread(new Runnable()
{
Expand All @@ -231,7 +231,7 @@ else if ("jawbone".equals(requester))
e.putString("oauth_" + requester + "_secret", "");
e.putString("oauth_" + requester + "_token", access);

e.commit();
e.apply();

me.runOnUiThread(new Runnable()
{
Expand Down Expand Up @@ -419,7 +419,7 @@ public Token getAccessToken(Token requestToken, Verifier verifier)
e.putString("oauth_" + requester + "_access_token", accessToken.getToken());
e.putString("oauth_" + requester + "_refresh_token", accessToken.getSecret());
e.putLong("oauth_" + requester + "_expires", System.currentTimeMillis() + ((60 * 60) * 1000));
e.commit();
e.apply();

me.runOnUiThread(new Runnable() {
public void run() {
Expand All @@ -435,7 +435,7 @@ public void run() {
e.putString("oauth_" + requester + "_secret", accessToken.getSecret());
e.putString("oauth_" + requester + "_token", accessToken.getToken());

e.commit();
e.apply();

me.runOnUiThread(new Runnable() {
public void run() {
Expand Down Expand Up @@ -467,7 +467,7 @@ public void run()
e.putString("oauth_" + requester + "_secret", accessToken.getSecret());
e.putString("oauth_" + requester + "_token", accessToken.getToken());

e.commit();
e.apply();

me.runOnUiThread(new Runnable()
{
Expand Down
125 changes: 59 additions & 66 deletions src/edu/northwestern/cbits/xsi/oauth/iHealthApi.java
Expand Up @@ -48,14 +48,13 @@ public String getAccessTokenEndpoint()

public String getAuthorizationUrl(OAuthConfig oAuthConfig)
{
StringBuilder urlBuffer = new StringBuilder();
urlBuffer.append("https://api.ihealthlabs.com:8443/OpenApiV2/OAuthv2/userauthorization");
urlBuffer.append("?client_id=" + Keystore.get(iHealthApi.CONSUMER_KEY));
urlBuffer.append("&response_type=code");
urlBuffer.append("&redirect_uri=" + Keystore.get(iHealthApi.REDIRECT_URL));
urlBuffer.append("&APIName=OpenApiBG+OpenApiBP+OpenApiActivity+OpenApiSleep+OpenApiSpO2+OpenApiWeight");

return urlBuffer.toString();
String urlBuffer = "https://api.ihealthlabs.com:8443/OpenApiV2/OAuthv2/userauthorization" +
"?client_id=" + Keystore.get(iHealthApi.CONSUMER_KEY) +
"&response_type=code" +
"&redirect_uri=" + Keystore.get(iHealthApi.REDIRECT_URL) +
"&APIName=OpenApiBG+OpenApiBP+OpenApiActivity+OpenApiSleep+OpenApiSpO2+OpenApiWeight";

return urlBuffer;
}

public static JSONObject fetchActivity(Context context, long start, long end) throws IOException, JSONException
Expand All @@ -65,17 +64,16 @@ public static JSONObject fetchActivity(Context context, long start, long end) th
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String userId = prefs.getString("oauth_ihealth_user_id", "");

StringBuilder urlBuffer = new StringBuilder();
urlBuffer.append("https://api.ihealthlabs.com:8443/openapiv2/user/" + userId + "/activity.json/");
urlBuffer.append("?client_id=" + Keystore.get(iHealthApi.CONSUMER_KEY));
urlBuffer.append("&client_secret=" + Keystore.get(iHealthApi.CONSUMER_SECRET));
urlBuffer.append("&sc=" + Keystore.get(iHealthApi.ACTIVITY_SC));
urlBuffer.append("&sv=" + Keystore.get(iHealthApi.ACTIVITY_SV));
urlBuffer.append("&access_token=" + Keystore.get(iHealthApi.USER_TOKEN));
urlBuffer.append("&start_time=" + start);
urlBuffer.append("&end_time=" + end);
String urlBuffer = ("https://api.ihealthlabs.com:8443/openapiv2/user/" + userId + "/activity.json/") +
"?client_id=" + Keystore.get(iHealthApi.CONSUMER_KEY) +
"&client_secret=" + Keystore.get(iHealthApi.CONSUMER_SECRET) +
"&sc=" + Keystore.get(iHealthApi.ACTIVITY_SC) +
"&sv=" + Keystore.get(iHealthApi.ACTIVITY_SV) +
"&access_token=" + Keystore.get(iHealthApi.USER_TOKEN) +
"&start_time=" + start +
"&end_time=" + end;

BufferedReader in = new BufferedReader(new InputStreamReader(new URL(urlBuffer.toString()).openStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(new URL(urlBuffer).openStream()));

StringBuilder all = new StringBuilder();
String inputLine = null;
Expand Down Expand Up @@ -103,15 +101,14 @@ private static void refreshTokenAsNeeded(Context context) throws IOException, JS
{
String refreshToken = prefs.getString("oauth_ihealth_refresh_token", "");

StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append("https://api.ihealthlabs.com:8443/OpenApiV2/OAuthv2/userauthorization");
urlBuilder.append("?client_id=" + Keystore.get(iHealthApi.CONSUMER_KEY));
urlBuilder.append("&client_secret=" + Keystore.get(iHealthApi.CONSUMER_SECRET));
urlBuilder.append("&redirect_uri=" + Keystore.get(iHealthApi.REDIRECT_URL));
urlBuilder.append("&response_type=refresh_token");
urlBuilder.append("&refresh_token=" + refreshToken);
String urlBuilder = "https://api.ihealthlabs.com:8443/OpenApiV2/OAuthv2/userauthorization" +
"?client_id=" + Keystore.get(iHealthApi.CONSUMER_KEY) +
"&client_secret=" + Keystore.get(iHealthApi.CONSUMER_SECRET) +
"&redirect_uri=" + Keystore.get(iHealthApi.REDIRECT_URL) +
"&response_type=refresh_token" +
"&refresh_token=" + refreshToken;

BufferedReader in= new BufferedReader(new InputStreamReader(new URL(urlBuilder.toString()).openStream()));
BufferedReader in= new BufferedReader(new InputStreamReader(new URL(urlBuilder).openStream()));

StringBuilder all = new StringBuilder();
String inputLine = null;
Expand Down Expand Up @@ -139,7 +136,7 @@ private static void refreshTokenAsNeeded(Context context) throws IOException, JS
e.putString("oauth_ihealth_user_id", userId);
e.putLong("oauth_ihealth_expires", expires);

e.commit();
e.apply();
}
}

Expand All @@ -155,17 +152,16 @@ public static JSONObject fetchWeight(Context context, long start, long end) thro
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String userId = prefs.getString("oauth_ihealth_user_id", "");

StringBuilder urlBuffer = new StringBuilder();
urlBuffer.append("https://api.ihealthlabs.com:8443/openapiv2/user/" + userId + "/weight.json/");
urlBuffer.append("?client_id=" + Keystore.get(iHealthApi.CONSUMER_KEY));
urlBuffer.append("&client_secret=" + Keystore.get(iHealthApi.CONSUMER_SECRET));
urlBuffer.append("&sc=" + Keystore.get(iHealthApi.WEIGHT_SC));
urlBuffer.append("&sv=" + Keystore.get(iHealthApi.WEIGHT_SV));
urlBuffer.append("&access_token=" + Keystore.get(iHealthApi.USER_TOKEN));
urlBuffer.append("&start_time=" + start);
urlBuffer.append("&end_time=" + end);
String urlBuffer = ("https://api.ihealthlabs.com:8443/openapiv2/user/" + userId + "/weight.json/") +
"?client_id=" + Keystore.get(iHealthApi.CONSUMER_KEY) +
"&client_secret=" + Keystore.get(iHealthApi.CONSUMER_SECRET) +
"&sc=" + Keystore.get(iHealthApi.WEIGHT_SC) +
"&sv=" + Keystore.get(iHealthApi.WEIGHT_SV) +
"&access_token=" + Keystore.get(iHealthApi.USER_TOKEN) +
"&start_time=" + start +
"&end_time=" + end;

BufferedReader in = new BufferedReader(new InputStreamReader(new URL(urlBuffer.toString()).openStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(new URL(urlBuffer).openStream()));

StringBuilder all = new StringBuilder();
String inputLine = null;
Expand All @@ -188,17 +184,16 @@ public static JSONObject fetchBloodPressure(Context context, long start, long en
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String userId = prefs.getString("oauth_ihealth_user_id", "");

StringBuilder urlBuffer = new StringBuilder();
urlBuffer.append("https://api.ihealthlabs.com:8443/openapiv2/user/" + userId + "/bp.json/");
urlBuffer.append("?client_id=" + Keystore.get(iHealthApi.CONSUMER_KEY));
urlBuffer.append("&client_secret=" + Keystore.get(iHealthApi.CONSUMER_SECRET));
urlBuffer.append("&sc=" + Keystore.get(iHealthApi.PRESSURE_SC));
urlBuffer.append("&sv=" + Keystore.get(iHealthApi.PRESSURE_SV));
urlBuffer.append("&access_token=" + Keystore.get(iHealthApi.USER_TOKEN));
urlBuffer.append("&start_time=" + start);
urlBuffer.append("&end_time=" + end);
String urlBuffer = ("https://api.ihealthlabs.com:8443/openapiv2/user/" + userId + "/bp.json/") +
"?client_id=" + Keystore.get(iHealthApi.CONSUMER_KEY) +
"&client_secret=" + Keystore.get(iHealthApi.CONSUMER_SECRET) +
"&sc=" + Keystore.get(iHealthApi.PRESSURE_SC) +
"&sv=" + Keystore.get(iHealthApi.PRESSURE_SV) +
"&access_token=" + Keystore.get(iHealthApi.USER_TOKEN) +
"&start_time=" + start +
"&end_time=" + end;

BufferedReader in = new BufferedReader(new InputStreamReader(new URL(urlBuffer.toString()).openStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(new URL(urlBuffer).openStream()));

StringBuilder all = new StringBuilder();
String inputLine = null;
Expand Down Expand Up @@ -226,17 +221,16 @@ public static JSONObject fetchBloodGlucose(Context context, long start, long end
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String userId = prefs.getString("oauth_ihealth_user_id", "");

StringBuilder urlBuffer = new StringBuilder();
urlBuffer.append("https://api.ihealthlabs.com:8443/openapiv2/user/" + userId + "/weight.json/");
urlBuffer.append("?client_id=" + Keystore.get(iHealthApi.CONSUMER_KEY));
urlBuffer.append("&client_secret=" + Keystore.get(iHealthApi.CONSUMER_SECRET));
urlBuffer.append("&sc=" + Keystore.get(iHealthApi.GLUCOSE_SC));
urlBuffer.append("&sv=" + Keystore.get(iHealthApi.GLUCOSE_SV));
urlBuffer.append("&access_token=" + Keystore.get(iHealthApi.USER_TOKEN));
urlBuffer.append("&start_time=" + start);
urlBuffer.append("&end_time=" + end);
String urlBuffer = ("https://api.ihealthlabs.com:8443/openapiv2/user/" + userId + "/weight.json/") +
"?client_id=" + Keystore.get(iHealthApi.CONSUMER_KEY) +
"&client_secret=" + Keystore.get(iHealthApi.CONSUMER_SECRET) +
"&sc=" + Keystore.get(iHealthApi.GLUCOSE_SC) +
"&sv=" + Keystore.get(iHealthApi.GLUCOSE_SV) +
"&access_token=" + Keystore.get(iHealthApi.USER_TOKEN) +
"&start_time=" + start +
"&end_time=" + end;

BufferedReader in = new BufferedReader(new InputStreamReader(new URL(urlBuffer.toString()).openStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(new URL(urlBuffer).openStream()));

StringBuilder all = new StringBuilder();
String inputLine = null;
Expand All @@ -256,15 +250,14 @@ public static void completeLogin(Context context, String code) throws IOExceptio
{
long now = System.currentTimeMillis();

StringBuilder urlBuilder = new StringBuilder();
urlBuilder.append("https://api.ihealthlabs.com:8443/OpenApiV2/OAuthv2/userauthorization");
urlBuilder.append("?client_id=" + Keystore.get(iHealthApi.CONSUMER_KEY));
urlBuilder.append("&client_secret=" + Keystore.get(iHealthApi.CONSUMER_SECRET));
urlBuilder.append("&grant_type=authorization_code");
urlBuilder.append("&redirect_uri=" + Keystore.get(iHealthApi.REDIRECT_URL));
urlBuilder.append("&code=" + code);
String urlBuilder = "https://api.ihealthlabs.com:8443/OpenApiV2/OAuthv2/userauthorization" +
"?client_id=" + Keystore.get(iHealthApi.CONSUMER_KEY) +
"&client_secret=" + Keystore.get(iHealthApi.CONSUMER_SECRET) +
"&grant_type=authorization_code" +
"&redirect_uri=" + Keystore.get(iHealthApi.REDIRECT_URL) +
"&code=" + code;

BufferedReader in= new BufferedReader(new InputStreamReader(new URL(urlBuilder.toString()).openStream()));
BufferedReader in= new BufferedReader(new InputStreamReader(new URL(urlBuilder).openStream()));

StringBuilder all = new StringBuilder();
String inputLine = null;
Expand Down Expand Up @@ -294,6 +287,6 @@ public static void completeLogin(Context context, String code) throws IOExceptio
e.putString("oauth_ihealth_user_id", userId);
e.putLong("oauth_ihealth_expires", expires);

e.commit();
e.apply();
}
}

0 comments on commit 0d7f08a

Please sign in to comment.