From e89a12d117afc7f1d42c8f460b5cb155ae97578c Mon Sep 17 00:00:00 2001 From: Christopher Guan Date: Sun, 17 Feb 2019 13:23:20 -0800 Subject: [PATCH] Subscribe to weekly steps in Home; Check if user has created account before + login flow --- .../personalbest/GetToKnowUActivity.java | 10 ++++---- .../android/personalbest/HomeFragment.java | 1 + .../android/personalbest/LoginActivity.java | 9 ++++++- .../android/personalbest/SharedPrefData.java | 24 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 +- 5 files changed, 40 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/android/personalbest/GetToKnowUActivity.java b/app/src/main/java/com/android/personalbest/GetToKnowUActivity.java index f5bb61e..f1fc43f 100644 --- a/app/src/main/java/com/android/personalbest/GetToKnowUActivity.java +++ b/app/src/main/java/com/android/personalbest/GetToKnowUActivity.java @@ -73,14 +73,16 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...); if (requestCode == RC_SIGN_IN) { - // The Task returned from this call is always completed, no need to attach - // a listener. + // The Task returned from this call is always completed, no need to attach a listener. Task task = GoogleSignIn.getSignedInAccountFromIntent(data); - //If sign in was successful then launchActivity if ( gSignInAndOut.handleSignInResult(task) == true ) { - // No longer need to launch activity, taken care of by finish button + // If the user logs in, already has an account, and tries to create a new account + // redirects them to their home page/the main activity + if (SharedPrefData.userSharedPrefExists(this)) { + launchActivity(); + } } } } diff --git a/app/src/main/java/com/android/personalbest/HomeFragment.java b/app/src/main/java/com/android/personalbest/HomeFragment.java index ed47777..cc4b366 100644 --- a/app/src/main/java/com/android/personalbest/HomeFragment.java +++ b/app/src/main/java/com/android/personalbest/HomeFragment.java @@ -206,6 +206,7 @@ protected void onPostExecute(String result) { @Override protected void onPreExecute() { + gFit.subscribeForWeeklySteps(); } @Override diff --git a/app/src/main/java/com/android/personalbest/LoginActivity.java b/app/src/main/java/com/android/personalbest/LoginActivity.java index 27a3ccd..15ac96f 100644 --- a/app/src/main/java/com/android/personalbest/LoginActivity.java +++ b/app/src/main/java/com/android/personalbest/LoginActivity.java @@ -48,7 +48,14 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) { Task task = GoogleSignIn.getSignedInAccountFromIntent(data); if ( gSignInAndOut.handleSignInResult(task) == true ) { - launchHomeScreenActivity(); + // Checks if users sign-in on their first use without creating an account first, + // redirects them to collect info such as their name and height + if (SharedPrefData.userSharedPrefExists(this.getApplicationContext())) { + launchHomeScreenActivity(); + } else { + launchGetToKnowYouActivity(); + } + } } } diff --git a/app/src/main/java/com/android/personalbest/SharedPrefData.java b/app/src/main/java/com/android/personalbest/SharedPrefData.java index 8da86c3..4fb478e 100644 --- a/app/src/main/java/com/android/personalbest/SharedPrefData.java +++ b/app/src/main/java/com/android/personalbest/SharedPrefData.java @@ -8,6 +8,7 @@ import com.google.android.gms.auth.api.signin.GoogleSignIn; import com.google.android.gms.auth.api.signin.GoogleSignInAccount; +import java.io.File; import java.util.Calendar; import java.util.Map; @@ -155,4 +156,27 @@ public static void setHeightIn(Context context, int heightIn) { editor.putInt("heightin", heightIn); editor.apply(); } + + + // Check if a logged in user has a SharedPreference file to indicate whether or not they + // created a Personal Best account; controls the login/create-account flow + public static boolean userSharedPrefExists(Context context) { + String accountId = getLoggedInUserId(context); + String SHARED_PREF_FILE_PATH = "/data/data/" + context.getPackageName() + "/shared_prefs/" + accountId + ".xml"; + + File f = new File(SHARED_PREF_FILE_PATH); + return f.exists() ? true : false; + } + + + // Helper method to print out all the key-values of a SharedPref file + public static void logAllKeyValues(Context context) { + String accountId = getLoggedInUserId(context); + SharedPreferences sharedPreferences = context.getSharedPreferences(accountId, Context.MODE_PRIVATE); + + Map allEntries = sharedPreferences.getAll(); + for (Map.Entry entry : allEntries.entrySet()) { + Log.d(TAG + " Content", entry.getKey() + ": " + entry.getValue().toString()); + } + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 471e71a..d05445b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -13,7 +13,7 @@ "Contacts permissions are needed for providing email completions." - MainActivity + Personal Best Home History