Skip to content

Commit

Permalink
[#ANDROID-318]. Send Person, Device, and Sdk during conversation crea…
Browse files Browse the repository at this point in the history
…tion. Don't try to send them before.
  • Loading branch information
skykelsey committed Apr 12, 2014
1 parent 7f1a4fe commit 46d920c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 46d920c

Please sign in to comment.