From 46d920caaa77cc79bfdd7d12a5bddf27d5840419 Mon Sep 17 00:00:00 2001 From: skykelsey Date: Fri, 11 Apr 2014 19:39:04 -0600 Subject: [PATCH] [#ANDROID-318]. Send Person, Device, and Sdk during conversation creation. Don't try to send them before. --- .../apptentive/android/sdk/Apptentive.java | 56 ++++++++++--------- .../sdk/model/ConversationTokenRequest.java | 8 +++ .../com/apptentive/android/sdk/model/Sdk.java | 2 + .../sdk/module/survey/SurveyManager.java | 36 +++++------- .../android/sdk/storage/DeviceManager.java | 4 -- .../android/sdk/util/Constants.java | 3 +- 6 files changed, 56 insertions(+), 53 deletions(-) diff --git a/apptentive-android-sdk/src/com/apptentive/android/sdk/Apptentive.java b/apptentive-android-sdk/src/com/apptentive/android/sdk/Apptentive.java index 911a8008e..05e57bb2e 100755 --- a/apptentive-android-sdk/src/com/apptentive/android/sdk/Apptentive.java +++ b/apptentive-android-sdk/src/com/apptentive/android/sdk/Apptentive.java @@ -775,35 +775,35 @@ public void stateChanged(NetworkInfo networkInfo) { asyncFetchAppConfiguration(context); SurveyManager.asyncFetchAndStoreSurveysIfCacheExpired(context); InteractionManager.asyncFetchAndStoreInteractions(context); - } - // TODO: Do this on a dedicated thread if it takes too long. Some HTC devices might take like 30 seconds I think. - // See if the device info has changed. - Device deviceInfo = DeviceManager.storeDeviceAndReturnDiff(context); - if (deviceInfo != null) { - Log.d("Device info was updated."); - Log.v(deviceInfo.toString()); - ApptentiveDatabase.getInstance(context).addPayload(deviceInfo); - } else { - Log.d("Device info was not updated."); - } + // TODO: Do this on a dedicated thread if it takes too long. Some HTC devices might take like 30 seconds I think. + // See if the device info has changed. + Device deviceInfo = DeviceManager.storeDeviceAndReturnDiff(context); + if (deviceInfo != null) { + Log.d("Device info was updated."); + Log.v(deviceInfo.toString()); + ApptentiveDatabase.getInstance(context).addPayload(deviceInfo); + } else { + Log.d("Device info was not updated."); + } - Sdk sdk = SdkManager.storeSdkAndReturnDiff(context); - if (sdk != null) { - Log.d("Sdk was updated."); - Log.v(sdk.toString()); - ApptentiveDatabase.getInstance(context).addPayload(sdk); - } else { - Log.d("Sdk was not updated."); - } + Sdk sdk = SdkManager.storeSdkAndReturnDiff(context); + if (sdk != null) { + Log.d("Sdk was updated."); + Log.v(sdk.toString()); + ApptentiveDatabase.getInstance(context).addPayload(sdk); + } else { + Log.d("Sdk was not updated."); + } - Person person = PersonManager.storePersonAndReturnDiff(context); - if (person != null) { - Log.d("Person was updated."); - Log.v(person.toString()); - ApptentiveDatabase.getInstance(context).addPayload(person); - } else { - Log.d("Person was not updated."); + Person person = PersonManager.storePersonAndReturnDiff(context); + if (person != null) { + Log.d("Person was updated."); + Log.v(person.toString()); + ApptentiveDatabase.getInstance(context).addPayload(person); + } else { + Log.d("Person was not updated."); + } } Log.d("Default Locale: %s", Locale.getDefault().toString()); @@ -850,6 +850,10 @@ public void uncaughtException(Thread thread, Throwable throwable) { private static void fetchConversationToken(Context context) { // Try to fetch a new one from the server. ConversationTokenRequest request = new ConversationTokenRequest(); + request.setSdk(SdkManager.storeSdkAndReturnDiff(context)); + request.setDevice(DeviceManager.storeDeviceAndReturnDiff(context)); + request.setPerson(PersonManager.storePersonAndReturnDiff(context)); + // TODO: Allow host app to send a user id, if available. ApptentiveHttpResponse response = ApptentiveClient.getConversationToken(request); if (response == null) { diff --git a/apptentive-android-sdk/src/com/apptentive/android/sdk/model/ConversationTokenRequest.java b/apptentive-android-sdk/src/com/apptentive/android/sdk/model/ConversationTokenRequest.java index 7e4e3e41c..ea9c4ff5e 100644 --- a/apptentive-android-sdk/src/com/apptentive/android/sdk/model/ConversationTokenRequest.java +++ b/apptentive-android-sdk/src/com/apptentive/android/sdk/model/ConversationTokenRequest.java @@ -27,6 +27,14 @@ public void setDevice(Device device) { } } + public void setSdk(Sdk sdk) { + try { + put(Sdk.KEY, sdk); + } catch (JSONException e) { + Log.e("Error adding %s to ConversationTokenRequest", Sdk.KEY); + } + } + public void setPerson(Person person) { try { put(Person.KEY, person); diff --git a/apptentive-android-sdk/src/com/apptentive/android/sdk/model/Sdk.java b/apptentive-android-sdk/src/com/apptentive/android/sdk/model/Sdk.java index bf9452c61..d43cc52cd 100644 --- a/apptentive-android-sdk/src/com/apptentive/android/sdk/model/Sdk.java +++ b/apptentive-android-sdk/src/com/apptentive/android/sdk/model/Sdk.java @@ -8,6 +8,8 @@ */ public class Sdk extends Payload { + public static final String KEY = "sdk"; + private static final String KEY_VERSION = "version"; private static final String KEY_PROGRAMMING_LANGUAGE = "programming_language"; private static final String KEY_AUTHOR_NAME = "author_name"; diff --git a/apptentive-android-sdk/src/com/apptentive/android/sdk/module/survey/SurveyManager.java b/apptentive-android-sdk/src/com/apptentive/android/sdk/module/survey/SurveyManager.java index de8999618..d2636c339 100644 --- a/apptentive-android-sdk/src/com/apptentive/android/sdk/module/survey/SurveyManager.java +++ b/apptentive-android-sdk/src/com/apptentive/android/sdk/module/survey/SurveyManager.java @@ -33,28 +33,22 @@ public class SurveyManager { public static void asyncFetchAndStoreSurveysIfCacheExpired(final Context context) { if (hasCacheExpired(context)) { - SharedPreferences prefs = context.getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE); - boolean deviceDataSent = prefs.getBoolean(Constants.PREF_KEY_DEVICE_DATA_SENT, false); Log.d("Survey cache has expired. Fetching new surveys."); - if (deviceDataSent) { // Don't allow survey fetches until Device info has been sent at least once. - Thread thread = new Thread() { - public void run() { - fetchAndStoreSurveys(context); - } - }; - Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() { - @Override - public void uncaughtException(Thread thread, Throwable throwable) { - Log.w("UncaughtException in SurveyManager.", throwable); - MetricModule.sendError(context.getApplicationContext(), throwable, null, null); - } - }; - thread.setUncaughtExceptionHandler(handler); - thread.setName("Apptentive-FetchSurveys"); - thread.start(); - } else { - Log.d("Can't fetch surveys because Device info has not been sent."); - } + Thread thread = new Thread() { + public void run() { + fetchAndStoreSurveys(context); + } + }; + Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() { + @Override + public void uncaughtException(Thread thread, Throwable throwable) { + Log.w("UncaughtException in SurveyManager.", throwable); + MetricModule.sendError(context.getApplicationContext(), throwable, null, null); + } + }; + thread.setUncaughtExceptionHandler(handler); + thread.setName("Apptentive-FetchSurveys"); + thread.start(); } else { Log.d("Survey cache has not expired. Using existing surveys."); } diff --git a/apptentive-android-sdk/src/com/apptentive/android/sdk/storage/DeviceManager.java b/apptentive-android-sdk/src/com/apptentive/android/sdk/storage/DeviceManager.java index 27eedf05b..ae000c657 100644 --- a/apptentive-android-sdk/src/com/apptentive/android/sdk/storage/DeviceManager.java +++ b/apptentive-android-sdk/src/com/apptentive/android/sdk/storage/DeviceManager.java @@ -13,7 +13,6 @@ import com.apptentive.android.sdk.GlobalInfo; import com.apptentive.android.sdk.model.CustomData; import com.apptentive.android.sdk.model.Device; -import com.apptentive.android.sdk.module.survey.SurveyManager; import com.apptentive.android.sdk.util.Constants; import com.apptentive.android.sdk.util.Reflection; import org.json.JSONException; @@ -353,8 +352,5 @@ private static CustomData chooseLatest(CustomData old, CustomData newer) { } public static void onSentDeviceInfo(Context appContext) { - SharedPreferences prefs = appContext.getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE); - prefs.edit().putBoolean(Constants.PREF_KEY_DEVICE_DATA_SENT, true).commit(); - SurveyManager.asyncFetchAndStoreSurveysIfCacheExpired(appContext); } } diff --git a/apptentive-android-sdk/src/com/apptentive/android/sdk/util/Constants.java b/apptentive-android-sdk/src/com/apptentive/android/sdk/util/Constants.java index 42c90e904..cd17597a5 100644 --- a/apptentive-android-sdk/src/com/apptentive/android/sdk/util/Constants.java +++ b/apptentive-android-sdk/src/com/apptentive/android/sdk/util/Constants.java @@ -29,8 +29,6 @@ public class Constants { public static final String PREF_KEY_DEVICE_INTEGRATION_CONFIG = "integrationConfig"; - public static final String PREF_KEY_DEVICE_DATA_SENT = "deviceDataSent"; // Keeps track of whether we have ever sent device data. - public static final String PREF_KEY_SDK = "sdk"; public static final String PREF_KEY_APP_RELEASE = "app_release"; public static final String PREF_KEY_PERSON = "person"; @@ -97,6 +95,7 @@ public class Constants { public static final String PREF_KEY_USER_ENTERED_EMAIL = "userEnteredEmail"; public static final String PREF_KEY_APP_VERSION_CODE = "app_version_code"; public static final String PREF_KEY_APP_VERSION_NAME = "app_version_name"; + public static final String PREF_KEY_DEVICE_DATA_SENT = "deviceDataSent"; // Keeps track of whether we have ever sent device data. /** * A list of mobile carrier network types as Strings.