From 8c29c85696012f7e6f3ed3da0b94424195917f64 Mon Sep 17 00:00:00 2001
From: Torsten Grote <t@grobox.de>
Date: Tue, 10 Oct 2017 15:11:24 -0300
Subject: [PATCH] Extend expiry and show a green snackbar about it once

---
 .../briar/android/BriarApplication.java       |  4 +--
 .../android/navdrawer/NavDrawerActivity.java  | 26 ++++++++++++-----
 .../navdrawer/NavDrawerController.java        |  4 ++-
 .../navdrawer/NavDrawerControllerImpl.java    | 28 +++++++++++--------
 briar-android/src/main/res/values/strings.xml |  1 +
 5 files changed, 42 insertions(+), 21 deletions(-)

diff --git a/briar-android/src/main/java/org/briarproject/briar/android/BriarApplication.java b/briar-android/src/main/java/org/briarproject/briar/android/BriarApplication.java
index c971899170..c2e2595f30 100644
--- a/briar-android/src/main/java/org/briarproject/briar/android/BriarApplication.java
+++ b/briar-android/src/main/java/org/briarproject/briar/android/BriarApplication.java
@@ -6,8 +6,8 @@ package org.briarproject.briar.android;
  */
 public interface BriarApplication {
 
-	// This build expires on 21 October 2017
-	long EXPIRY_DATE = 1508544000 * 1000L;
+	// This build expires on 31 December 2017
+	long EXPIRY_DATE = 1514761200 * 1000L;
 
 	AndroidComponent getApplicationComponent();
 
diff --git a/briar-android/src/main/java/org/briarproject/briar/android/navdrawer/NavDrawerActivity.java b/briar-android/src/main/java/org/briarproject/briar/android/navdrawer/NavDrawerActivity.java
index 61ef5bfdc0..7337dec054 100644
--- a/briar-android/src/main/java/org/briarproject/briar/android/navdrawer/NavDrawerActivity.java
+++ b/briar-android/src/main/java/org/briarproject/briar/android/navdrawer/NavDrawerActivity.java
@@ -34,6 +34,7 @@ import org.briarproject.briar.android.forum.ForumListFragment;
 import org.briarproject.briar.android.fragment.BaseFragment;
 import org.briarproject.briar.android.fragment.BaseFragment.BaseFragmentListener;
 import org.briarproject.briar.android.fragment.SignOutFragment;
+import org.briarproject.briar.android.navdrawer.NavDrawerController.ExpiryWarning;
 import org.briarproject.briar.android.privategroup.list.GroupListFragment;
 import org.briarproject.briar.android.settings.SettingsActivity;
 
@@ -48,6 +49,8 @@ import static android.support.v4.view.GravityCompat.START;
 import static android.support.v4.widget.DrawerLayout.LOCK_MODE_LOCKED_CLOSED;
 import static android.view.View.GONE;
 import static android.view.View.VISIBLE;
+import static org.briarproject.briar.android.navdrawer.NavDrawerController.ExpiryWarning.NO;
+import static org.briarproject.briar.android.navdrawer.NavDrawerController.ExpiryWarning.UPDATE;
 import static org.briarproject.briar.android.util.UiUtils.getDaysUntilExpiry;
 
 public class NavDrawerActivity extends BriarActivity implements
@@ -132,10 +135,10 @@ public class NavDrawerActivity extends BriarActivity implements
 	public void onStart() {
 		super.onStart();
 		updateTransports();
-		controller.showExpiryWarning(new UiResultHandler<Boolean>(this) {
+		controller.showExpiryWarning(new UiResultHandler<ExpiryWarning>(this) {
 			@Override
-			public void onResultUi(Boolean showWarning) {
-				if (showWarning) showExpiryWarning();
+			public void onResultUi(ExpiryWarning expiry) {
+				if (expiry != NO) showExpiryWarning(expiry);
 			}
 		});
 	}
@@ -265,7 +268,7 @@ public class NavDrawerActivity extends BriarActivity implements
 	}
 
 	@SuppressWarnings("ConstantConditions")
-	private void showExpiryWarning() {
+	private void showExpiryWarning(ExpiryWarning expiry) {
 		int daysUntilExpiry = getDaysUntilExpiry();
 		if (daysUntilExpiry < 0) signOut();
 
@@ -274,9 +277,18 @@ public class NavDrawerActivity extends BriarActivity implements
 				expiryWarning = (ViewGroup) findViewById(R.id.expiryWarning);
 		TextView expiryWarningText =
 				(TextView) expiryWarning.findViewById(R.id.expiryWarningText);
-		expiryWarningText.setText(getResources()
-				.getQuantityString(R.plurals.expiry_warning, daysUntilExpiry,
-						daysUntilExpiry));
+
+		// show a different snackbar in green if this is an update
+		if (expiry == UPDATE) {
+			expiryWarning.setBackgroundColor(
+					ContextCompat.getColor(this, R.color.briar_green_light));
+			expiryWarningText.setText(
+					getString(R.string.expiry_update, daysUntilExpiry));
+		} else {
+			expiryWarningText.setText(getResources()
+					.getQuantityString(R.plurals.expiry_warning, daysUntilExpiry,
+							daysUntilExpiry));
+		}
 
 		// make close button functional
 		ImageView expiryWarningClose =
diff --git a/briar-android/src/main/java/org/briarproject/briar/android/navdrawer/NavDrawerController.java b/briar-android/src/main/java/org/briarproject/briar/android/navdrawer/NavDrawerController.java
index 19f08bcd8f..79691695b3 100644
--- a/briar-android/src/main/java/org/briarproject/briar/android/navdrawer/NavDrawerController.java
+++ b/briar-android/src/main/java/org/briarproject/briar/android/navdrawer/NavDrawerController.java
@@ -8,9 +8,11 @@ import org.briarproject.briar.android.controller.handler.ResultHandler;
 @NotNullByDefault
 public interface NavDrawerController extends ActivityLifecycleController {
 
+	enum ExpiryWarning { SHOW, NO, UPDATE };
+
 	boolean isTransportRunning(TransportId transportId);
 
-	void showExpiryWarning(final ResultHandler<Boolean> handler);
+	void showExpiryWarning(final ResultHandler<ExpiryWarning> handler);
 
 	void expiryWarningDismissed();
 
diff --git a/briar-android/src/main/java/org/briarproject/briar/android/navdrawer/NavDrawerControllerImpl.java b/briar-android/src/main/java/org/briarproject/briar/android/navdrawer/NavDrawerControllerImpl.java
index 8c4e578b80..543bb9a976 100644
--- a/briar-android/src/main/java/org/briarproject/briar/android/navdrawer/NavDrawerControllerImpl.java
+++ b/briar-android/src/main/java/org/briarproject/briar/android/navdrawer/NavDrawerControllerImpl.java
@@ -27,7 +27,11 @@ import javax.inject.Inject;
 
 import static java.util.logging.Level.INFO;
 import static java.util.logging.Level.WARNING;
+import static org.briarproject.briar.BuildConfig.VERSION_CODE;
 import static org.briarproject.briar.android.BriarApplication.EXPIRY_DATE;
+import static org.briarproject.briar.android.navdrawer.NavDrawerController.ExpiryWarning.NO;
+import static org.briarproject.briar.android.navdrawer.NavDrawerController.ExpiryWarning.SHOW;
+import static org.briarproject.briar.android.navdrawer.NavDrawerController.ExpiryWarning.UPDATE;
 import static org.briarproject.briar.android.settings.SettingsFragment.SETTINGS_NAMESPACE;
 
 @MethodsNotNullByDefault
@@ -38,6 +42,7 @@ public class NavDrawerControllerImpl extends DbControllerImpl
 	private static final Logger LOG =
 			Logger.getLogger(NavDrawerControllerImpl.class.getName());
 	private static final String EXPIRY_DATE_WARNING = "expiryDateWarning";
+	private static final String EXPIRY_DATE_VERSION = "expiryDateVersion";
 
 	private final PluginManager pluginManager;
 	private final SettingsManager settingsManager;
@@ -103,7 +108,7 @@ public class NavDrawerControllerImpl extends DbControllerImpl
 	}
 
 	@Override
-	public void showExpiryWarning(final ResultHandler<Boolean> handler) {
+	public void showExpiryWarning(final ResultHandler<ExpiryWarning> handler) {
 		runOnDbThread(new Runnable() {
 			@Override
 			public void run() {
@@ -111,10 +116,11 @@ public class NavDrawerControllerImpl extends DbControllerImpl
 					Settings settings =
 							settingsManager.getSettings(SETTINGS_NAMESPACE);
 					int warningInt = settings.getInt(EXPIRY_DATE_WARNING, 0);
+					int versionCode = settings.getInt(EXPIRY_DATE_VERSION, 0);
 
 					if (warningInt == 0) {
 						// we have not warned before
-						handler.onResult(true);
+						handler.onResult(SHOW);
 					} else {
 						long warningLong = warningInt * 1000L;
 						long now = System.currentTimeMillis();
@@ -123,15 +129,14 @@ public class NavDrawerControllerImpl extends DbControllerImpl
 						long daysBeforeExpiry =
 								(EXPIRY_DATE - now) / 1000 / 60 / 60 / 24;
 
-						if (daysSinceLastWarning >= 30) {
-							handler.onResult(true);
-							return;
-						}
-						if (daysBeforeExpiry <= 3 && daysSinceLastWarning > 0) {
-							handler.onResult(true);
-							return;
-						}
-						handler.onResult(false);
+						if (versionCode < 1611) {
+							handler.onResult(UPDATE);
+						} else if (daysSinceLastWarning >= 30) {
+							handler.onResult(SHOW);
+						} else if (daysBeforeExpiry <= 3 &&
+								daysSinceLastWarning > 0) {
+							handler.onResult(SHOW);
+						} else handler.onResult(NO);
 					}
 				} catch (DbException e) {
 					if (LOG.isLoggable(WARNING))
@@ -150,6 +155,7 @@ public class NavDrawerControllerImpl extends DbControllerImpl
 					Settings settings = new Settings();
 					int date = (int) (System.currentTimeMillis() / 1000L);
 					settings.putInt(EXPIRY_DATE_WARNING, date);
+					settings.putInt(EXPIRY_DATE_VERSION, VERSION_CODE);
 					settingsManager.mergeSettings(settings, SETTINGS_NAMESPACE);
 				} catch (DbException e) {
 					if (LOG.isLoggable(WARNING))
diff --git a/briar-android/src/main/res/values/strings.xml b/briar-android/src/main/res/values/strings.xml
index 25ef8b471c..dd04625ccb 100644
--- a/briar-android/src/main/res/values/strings.xml
+++ b/briar-android/src/main/res/values/strings.xml
@@ -28,6 +28,7 @@
 		<item quantity="one">This is a beta version of Briar. Your account will expire in %d day and cannot be renewed.</item>
 		<item quantity="other">This is a beta version of Briar. Your account will expire in %d days and cannot be renewed.</item>
 	</plurals>
+	<string name="expiry_update">The beta expiry date has been extended. Your account will now expire in %d days.</string>
 	<string name="expiry_date_reached">This software has expired.\nThank you for testing!</string>
 
 	<!-- Navigation Drawer -->
-- 
GitLab