From da7293252f197e9b54c1abd8127b4abe45b95110 Mon Sep 17 00:00:00 2001 From: Fup Duck Date: Wed, 6 Dec 2017 01:58:21 +0100 Subject: move constants * move EIPConstants to Constants * unify EIPConstants where possible * create Updater --- .../java/se/leap/bitmaskclient/StartActivity.java | 116 +++++++++++++-------- 1 file changed, 70 insertions(+), 46 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/StartActivity.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/StartActivity.java b/app/src/main/java/se/leap/bitmaskclient/StartActivity.java index 410daf7d..ade43701 100644 --- a/app/src/main/java/se/leap/bitmaskclient/StartActivity.java +++ b/app/src/main/java/se/leap/bitmaskclient/StartActivity.java @@ -13,6 +13,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import de.blinkt.openvpn.core.VpnStatus; +import se.leap.bitmaskclient.updates.ConstantUnification; import se.leap.bitmaskclient.userstatus.User; /** @@ -31,40 +32,76 @@ public class StartActivity extends Activity { private static final int UPGRADE = 2; private static final int DOWNGRADE = 3; + private int versionCode; + private int previousVersionCode; + + private SharedPreferences preferences; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + preferences = getSharedPreferences(Constants.SHARED_PREFERENCES, MODE_PRIVATE); + + Log.d(TAG, "Started"); + + switch (checkAppStart()) { + case NORMAL: + break; + + case FIRST: + // TODO start ProfileCreation & replace below code + break; + + case UPGRADE: + executeUpgrade(); + // TODO show donation dialog + break; + + case DOWNGRADE: + // TODO think how and why this should happen and what todo + break; + } + + // initialize app necessities + ProviderAPICommand.initialize(this); + VpnStatus.initLogCache(getApplicationContext().getCacheDir()); + User.init(getString(R.string.default_username)); + + // go to Dashboard + Intent intent = new Intent(this, Dashboard.class); + startActivity(intent); + } + /** * check if normal start, first run, up or downgrade * @return @StartupMode */ @StartupMode private int checkAppStart() { - SharedPreferences preferences = getSharedPreferences(Constants.SHARED_PREFERENCES, MODE_PRIVATE); try { - int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode; - int lastDetectedVersion = preferences.getInt(Constants.PREFERENCES_APP_VERSION, -1); + versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode; + previousVersionCode = preferences.getInt(Constants.PREFERENCES_APP_VERSION, -1); // versions do match -> normal start - if (versionCode == lastDetectedVersion) { + if (versionCode == previousVersionCode) { Log.d(TAG, "App start was: NORMAL START"); return NORMAL; } - // something changed -> save current version - preferences.edit().putInt(Constants.PREFERENCES_APP_VERSION, versionCode).apply(); - // no previous app version -> first start - if (lastDetectedVersion == -1 ) { - Log.d(TAG, "App start was: FIRST START"); + if (previousVersionCode == -1 ) { + Log.d(TAG, "FIRST START"); return FIRST; } // version has increased -> upgrade - if (versionCode > lastDetectedVersion) { - Log.d(TAG, "App start was: UPGRADE"); + if (versionCode > previousVersionCode) { + Log.d(TAG, "UPGRADE"); return UPGRADE; } // version has decreased -> downgrade - if (versionCode < lastDetectedVersion) { - Log.d(TAG, "App start was: DOWNGRADE"); + if (versionCode < previousVersionCode) { + Log.d(TAG, "DOWNGRADE"); return DOWNGRADE; } @@ -75,41 +112,28 @@ public class StartActivity extends Activity { return NORMAL; } - - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - Intent intent; - - Log.d(TAG, "Started"); - - switch (checkAppStart()) { - case NORMAL: - break; - - case FIRST: - // TODO start ProfileCreation & replace below code - intent = new Intent(this, Dashboard.class); - startActivity(intent); - break; - - case UPGRADE: - // TODO appropriate data copying - // TODO show donation dialog - break; - - case DOWNGRADE: - // TODO think how and why this should happen and what todo - break; + /** + * execute necessary updates for version change + */ + private void executeUpgrade() { + if (passedMilestone(VersionMilestone.MULTIPLE_PROFILES)) { + // TODO prepare usage of multiple profiles + } + if (passedMilestone(VersionMilestone.CONSTANT_UNIFICATION)) { + ConstantUnification.upgrade(preferences); } - // initialize app necessities - ProviderAPICommand.initialize(this); - VpnStatus.initLogCache(getApplicationContext().getCacheDir()); - User.init(getString(R.string.default_username)); + // ensure all upgrades have passed before storing new information + preferences.edit().putInt(Constants.PREFERENCES_APP_VERSION, versionCode).apply(); + } - // go to Dashboard - intent = new Intent(this, Dashboard.class); - startActivity(intent); + /** + * check if an upgrade passed or moved to given milestone + * @param milestone Version code of the Milestone VersionMilestone.MILE_STONE + * @return true if milestone is reached - false otherwise + */ + private boolean passedMilestone(int milestone) { + return previousVersionCode < milestone && versionCode >= milestone; } + } -- cgit v1.2.3