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

This commit is contained in:
zackhow 2018-08-25 16:57:07 -04:00
parent 82f82a6b7d
commit a26cf8febc
5 changed files with 69 additions and 14 deletions

View File

@ -1,8 +1,6 @@
package org.dolphinemu.dolphinemu; package org.dolphinemu.dolphinemu;
import android.app.Application; import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import org.dolphinemu.dolphinemu.services.DirectoryInitializationService; import org.dolphinemu.dolphinemu.services.DirectoryInitializationService;
import org.dolphinemu.dolphinemu.utils.PermissionsHandler; import org.dolphinemu.dolphinemu.utils.PermissionsHandler;
@ -10,18 +8,11 @@ import org.dolphinemu.dolphinemu.utils.VolleyUtil;
public class DolphinApplication extends Application public class DolphinApplication extends Application
{ {
public static final String FIRST_OPEN = "FIRST_OPEN";
@Override @Override
public void onCreate() public void onCreate()
{ {
super.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()); VolleyUtil.init(getApplicationContext());
System.loadLibrary("main"); System.loadLibrary("main");

View File

@ -16,7 +16,6 @@ import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.Toast; import android.widget.Toast;
import org.dolphinemu.dolphinemu.DolphinApplication;
import org.dolphinemu.dolphinemu.NativeLibrary; import org.dolphinemu.dolphinemu.NativeLibrary;
import org.dolphinemu.dolphinemu.R; import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.activities.EmulationActivity; 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.services.DirectoryInitializationService.DirectoryInitializationState;
import org.dolphinemu.dolphinemu.utils.DirectoryStateReceiver; import org.dolphinemu.dolphinemu.utils.DirectoryStateReceiver;
import org.dolphinemu.dolphinemu.utils.Log; import org.dolphinemu.dolphinemu.utils.Log;
import org.dolphinemu.dolphinemu.utils.StartupHandler;
import java.io.File; import java.io.File;
import rx.functions.Action1;
public final class EmulationFragment extends Fragment implements SurfaceHolder.Callback public final class EmulationFragment extends Fragment implements SurfaceHolder.Callback
{ {
private static final String KEY_GAMEPATH = "gamepath"; 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); String gamePath = getArguments().getString(KEY_GAMEPATH);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 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(); SharedPreferences.Editor sPrefsEditor = preferences.edit();
sPrefsEditor.putBoolean(DolphinApplication.FIRST_OPEN, false); sPrefsEditor.putBoolean(StartupHandler.NEW_SESSION, false);
sPrefsEditor.apply(); sPrefsEditor.apply();
mEmulationState = new EmulationState(gamePath, getTemporaryStateFilePath(), firstOpen); mEmulationState = new EmulationState(gamePath, getTemporaryStateFilePath(), firstOpen);
} }

View File

@ -89,6 +89,20 @@ public final class MainActivity extends AppCompatActivity implements MainView
mPresenter.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. // TODO: Replace with a ButterKnife injection.
private void findViews() private void findViews()
{ {

View File

@ -72,6 +72,20 @@ public final class TvMainActivity extends FragmentActivity implements MainView
mPresenter.onDestroy(); mPresenter.onDestroy();
} }
@Override
protected void onStart()
{
super.onStart();
StartupHandler.checkSessionReset(this);
}
@Override
protected void onStop()
{
super.onStop();
StartupHandler.setSessionTime(this);
}
void setupUI() { void setupUI() {
final FragmentManager fragmentManager = getSupportFragmentManager(); final FragmentManager fragmentManager = getSupportFragmentManager();
mBrowseFragment = new BrowseSupportFragment(); mBrowseFragment = new BrowseSupportFragment();

View File

@ -1,14 +1,23 @@
package org.dolphinemu.dolphinemu.utils; package org.dolphinemu.dolphinemu.utils;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentActivity;
import android.text.TextUtils; import android.text.TextUtils;
import org.dolphinemu.dolphinemu.activities.EmulationActivity; import org.dolphinemu.dolphinemu.activities.EmulationActivity;
import java.util.Date;
public final class StartupHandler 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) public static void HandleInit(FragmentActivity parent)
{ {
// Ask the user to grant write permission if it's not already granted // Ask the user to grant write permission if it's not already granted
@ -31,4 +40,33 @@ public final class StartupHandler
parent.finish(); 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();
}
}
} }