Skip to content

Commit

Permalink
StateException fix for ShareRest
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Nov 2, 2016
1 parent 43e3a20 commit 60b7351
Showing 1 changed file with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,34 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
private static final String SHARE_BASE_URL = "https://share2.dexcom.com/ShareWebServices/Services/";
private SharedPreferences sharedPreferences;

public ShareRest (Context context, OkHttpClient okHttpClient) {
OkHttpClient httpClient = okHttpClient != null ? okHttpClient : getOkHttpClient();

if (httpClient == null) httpClient = getOkHttpClient(); // try again on failure
// if fails second time we've got big problems

Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SHARE_BASE_URL)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
dexcomShareApi = retrofit.create(DexcomShare.class);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
sessionId = sharedPreferences.getString("dexcom_share_session_id", null);
username = sharedPreferences.getString("dexcom_account_name", null);
password = sharedPreferences.getString("dexcom_account_password", null);
serialNumber = sharedPreferences.getString("share_key", null);
sharedPreferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
if ("".equals(sessionId)) // migrate previous empty sessionIds to null;
sessionId = null;
public ShareRest(Context context, OkHttpClient okHttpClient) {

try {
OkHttpClient httpClient = okHttpClient != null ? okHttpClient : getOkHttpClient();

if (httpClient == null) httpClient = getOkHttpClient(); // try again on failure
// if fails second time we've got big problems

Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(SHARE_BASE_URL)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
dexcomShareApi = retrofit.create(DexcomShare.class);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
sessionId = sharedPreferences.getString("dexcom_share_session_id", null);
username = sharedPreferences.getString("dexcom_account_name", null);
password = sharedPreferences.getString("dexcom_account_password", null);
serialNumber = sharedPreferences.getString("share_key", null);
sharedPreferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
if ("".equals(sessionId)) // migrate previous empty sessionIds to null;
sessionId = null;
} catch (IllegalStateException e) {
Log.wtf(TAG, "Illegal state exception: " + e);
}
}

public synchronized OkHttpClient getOkHttpClient() {
Expand Down

0 comments on commit 60b7351

Please sign in to comment.