From a26cf8febcaf8d8f8d44a44ae834614bf89de2a8 Mon Sep 17 00:00:00 2001 From: zackhow Date: Sat, 25 Aug 2018 16:57:07 -0400 Subject: [PATCH] Android: Analytics - Set new session after being closed for 6 hours --- .../dolphinemu/DolphinApplication.java | 9 ----- .../fragments/EmulationFragment.java | 8 ++-- .../dolphinemu/ui/main/MainActivity.java | 14 +++++++ .../dolphinemu/ui/main/TvMainActivity.java | 14 +++++++ .../dolphinemu/utils/StartupHandler.java | 38 +++++++++++++++++++ 5 files changed, 69 insertions(+), 14 deletions(-) diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/DolphinApplication.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/DolphinApplication.java index 3fd6e2192b..251df3b117 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/DolphinApplication.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/DolphinApplication.java @@ -1,8 +1,6 @@ package org.dolphinemu.dolphinemu; import android.app.Application; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; import org.dolphinemu.dolphinemu.services.DirectoryInitializationService; import org.dolphinemu.dolphinemu.utils.PermissionsHandler; @@ -10,18 +8,11 @@ import org.dolphinemu.dolphinemu.utils.VolleyUtil; public class DolphinApplication extends Application { - public static final String FIRST_OPEN = "FIRST_OPEN"; @Override public void onCreate() { super.onCreate(); - // Passed at emulation start to trigger first open event. - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); - SharedPreferences.Editor sPrefsEditor = preferences.edit(); - sPrefsEditor.putBoolean(FIRST_OPEN, true); - sPrefsEditor.apply(); - VolleyUtil.init(getApplicationContext()); System.loadLibrary("main"); diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.java index 15e5980acc..2fb4ad8440 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/fragments/EmulationFragment.java @@ -16,7 +16,6 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.Toast; -import org.dolphinemu.dolphinemu.DolphinApplication; import org.dolphinemu.dolphinemu.NativeLibrary; import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.activities.EmulationActivity; @@ -25,11 +24,10 @@ import org.dolphinemu.dolphinemu.services.DirectoryInitializationService; import org.dolphinemu.dolphinemu.services.DirectoryInitializationService.DirectoryInitializationState; import org.dolphinemu.dolphinemu.utils.DirectoryStateReceiver; import org.dolphinemu.dolphinemu.utils.Log; +import org.dolphinemu.dolphinemu.utils.StartupHandler; import java.io.File; -import rx.functions.Action1; - public final class EmulationFragment extends Fragment implements SurfaceHolder.Callback { private static final String KEY_GAMEPATH = "gamepath"; @@ -86,9 +84,9 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C String gamePath = getArguments().getString(KEY_GAMEPATH); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); - boolean firstOpen = preferences.getBoolean(DolphinApplication.FIRST_OPEN, true); + boolean firstOpen = preferences.getBoolean(StartupHandler.NEW_SESSION, true); SharedPreferences.Editor sPrefsEditor = preferences.edit(); - sPrefsEditor.putBoolean(DolphinApplication.FIRST_OPEN, false); + sPrefsEditor.putBoolean(StartupHandler.NEW_SESSION, false); sPrefsEditor.apply(); mEmulationState = new EmulationState(gamePath, getTemporaryStateFilePath(), firstOpen); } diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java index a2d47444af..fe367276cf 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/MainActivity.java @@ -89,6 +89,20 @@ public final class MainActivity extends AppCompatActivity implements MainView mPresenter.onDestroy(); } + @Override + protected void onStart() + { + super.onStart(); + StartupHandler.checkSessionReset(this); + } + + @Override + protected void onStop() + { + super.onStop(); + StartupHandler.setSessionTime(this); + } + // TODO: Replace with a ButterKnife injection. private void findViews() { diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java index 1e3fb5ba8d..a6c25d67d3 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/ui/main/TvMainActivity.java @@ -72,6 +72,20 @@ public final class TvMainActivity extends FragmentActivity implements MainView mPresenter.onDestroy(); } + @Override + protected void onStart() + { + super.onStart(); + StartupHandler.checkSessionReset(this); + } + + @Override + protected void onStop() + { + super.onStop(); + StartupHandler.setSessionTime(this); + } + void setupUI() { final FragmentManager fragmentManager = getSupportFragmentManager(); mBrowseFragment = new BrowseSupportFragment(); diff --git a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/StartupHandler.java b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/StartupHandler.java index 232de7f347..22456d20e5 100644 --- a/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/StartupHandler.java +++ b/Source/Android/app/src/main/java/org/dolphinemu/dolphinemu/utils/StartupHandler.java @@ -1,14 +1,23 @@ package org.dolphinemu.dolphinemu.utils; +import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.v4.app.FragmentActivity; import android.text.TextUtils; import org.dolphinemu.dolphinemu.activities.EmulationActivity; +import java.util.Date; + public final class StartupHandler { + public static final String NEW_SESSION = "NEW_SESSION"; + public static final String LAST_CLOSED = "LAST_CLOSED"; + public static final Long SESSION_TIMEOUT = 21600000L; // 6 hours in milliseconds + public static void HandleInit(FragmentActivity parent) { // Ask the user to grant write permission if it's not already granted @@ -31,4 +40,33 @@ public final class StartupHandler parent.finish(); } } + + /** + * There isn't a good way to determine a new session. setSessionTime is called if the main + * activity goes into the background. + */ + public static void setSessionTime(Context context) + { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences.Editor sPrefsEditor = preferences.edit(); + sPrefsEditor.putLong(LAST_CLOSED, new Date(System.currentTimeMillis()).getTime()); + sPrefsEditor.apply(); + } + + /** + * Called to determine if we treat this activity start as a new session. + */ + public static void checkSessionReset(Context context) + { + Long currentTime = new Date(System.currentTimeMillis()).getTime(); + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + Long lastOpen = preferences.getLong(LAST_CLOSED, 0); + if (currentTime > (lastOpen + SESSION_TIMEOUT)) + { + // Passed at emulation start to trigger first open event. + SharedPreferences.Editor sPrefsEditor = preferences.edit(); + sPrefsEditor.putBoolean(NEW_SESSION, true); + sPrefsEditor.apply(); + } + } }