Permalink
Browse files

Android: Analytics - Set new session after being closed for 6 hours

  • Loading branch information...
zackhow committed Aug 25, 2018
1 parent 82f82a6 commit a26cf8febcaf8d8f8d44a44ae834614bf89de2a8
@@ -1,27 +1,18 @@
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;
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");
@@ -16,7 +16,6 @@
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.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 void onCreate(Bundle savedInstanceState)
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);
}
@@ -89,6 +89,20 @@ protected void onDestroy()
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()
{
@@ -72,6 +72,20 @@ protected void onDestroy()
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();
@@ -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 static void HandleInit(FragmentActivity parent)
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();
}
}
}

0 comments on commit a26cf8f

Please sign in to comment.