Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[telemetry] Save and read application state on a background thread. #540

Merged
merged 1 commit into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapbox.android.telemetry;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.SharedPreferences;
Expand All @@ -9,6 +10,8 @@
import androidx.annotation.RequiresApi;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;

public class AppStateUtils {
private static final String TAG = "AppStateUtils";
Expand Down Expand Up @@ -78,12 +81,20 @@ public String toString() {

public static String PREFERENCE_FILENAME = "mb_app_state_utils";
public static String KEY_LAST_KNOWN_ACTIVITY_STATE = "mb_telemetry_last_know_activity_state";

public static void saveActivityState(@NonNull Context context, @NonNull ActivityState state) {
final SharedPreferences preferences =
context.getSharedPreferences(PREFERENCE_FILENAME, Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = preferences.edit();
editor.putInt(KEY_LAST_KNOWN_ACTIVITY_STATE, state.getCode()).apply();
private static final ScheduledThreadPoolExecutor ioExecutor =
(ScheduledThreadPoolExecutor)Executors.newScheduledThreadPool(1);

public static void saveActivityState(@NonNull final Context context, @NonNull final ActivityState state) {
ioExecutor.execute(new Runnable() {
@SuppressLint("ApplySharedPref")
@Override
public void run() {
final SharedPreferences preferences =
context.getSharedPreferences(PREFERENCE_FILENAME, Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = preferences.edit();
editor.putInt(KEY_LAST_KNOWN_ACTIVITY_STATE, state.getCode()).commit();
}
});
}

public static ActivityState getLastKnownActivityState(@NonNull Context context) {
Expand Down Expand Up @@ -154,7 +165,6 @@ private static AppState getAppStateLollipopAndHigher(@NonNull Context context) {
AppState state = AppState.BACKGROUND;
List<ActivityManager.AppTask> tasks = activityManager.getAppTasks();
for (ActivityManager.AppTask task : tasks) {
//noinspection deprecation
if (task.getTaskInfo().id != -1) {
state = AppState.FOREGROUND;
}
Expand Down Expand Up @@ -196,11 +206,20 @@ && isActivityInactive(lastKnownActivityState)) {
return stateFromActivityManager;
}

public static AppState getAppState(@NonNull Context context) {
LogUtils.v(TAG, "Getting app state...");
final AppState state = arbitrage(getAppStateFromActivityManager(context),
getLastKnownActivityState(context));
LogUtils.v(TAG, "getAppState() returns " + state);
return state;
public interface GetAppStateCallback {
void onReady(AppState state);
}

public static void getAppState(@NonNull final Context context, final GetAppStateCallback callback) {
ioExecutor.execute(new Runnable() {
@Override
public void run() {
LogUtils.v(TAG, "Getting app state...");
final AppState state = arbitrage(getAppStateFromActivityManager(context),
getLastKnownActivityState(context));
LogUtils.v(TAG,"getAppState() returns " + state);
callback.onReady(state);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,21 @@ public void onReceive(Context context, Intent intent) {
}

LocationCollectionClient collectionClient = LocationCollectionClient.getInstance();
MapboxTelemetry telemetry = collectionClient.getTelemetry();
String sessionId = collectionClient.getSessionId();
List<Location> locations = result.getLocations();
for (Location location : locations) {
if (isThereAnyNaN(location) || isThereAnyInfinite(location)) {
continue;
final MapboxTelemetry telemetry = collectionClient.getTelemetry();
final String sessionId = collectionClient.getSessionId();
final List<Location> locations = result.getLocations();
AppStateUtils.getAppState(context, new AppStateUtils.GetAppStateCallback() {
@Override
public void onReady(AppStateUtils.AppState state) {
for (final Location location : locations) {
if (isThereAnyNaN(location) || isThereAnyInfinite(location)) {
continue;
}
telemetry.push(LocationMapper.create(location, state.toString(), sessionId));
}
}
final String appState = AppStateUtils.getAppState(context).toString();
telemetry.push(LocationMapper.create(location, appState, sessionId));
}
});

} catch (Throwable throwable) {
// TODO: log silent crash
Log.e(TAG, throwable.toString());
Expand Down