From 60b73517fe535e02c1571ba8200653b449746f37 Mon Sep 17 00:00:00 2001 From: Jamorham Date: Wed, 2 Nov 2016 15:42:54 +0000 Subject: [PATCH] StateException fix for ShareRest --- .../dexdrip/ShareModels/ShareRest.java | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/ShareModels/ShareRest.java b/app/src/main/java/com/eveningoutpost/dexdrip/ShareModels/ShareRest.java index 9a3234f554..146bb0516a 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/ShareModels/ShareRest.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/ShareModels/ShareRest.java @@ -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() {