-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from elimu-ai/90-add-version-upgrade-function…
…ality feat: add version upgrade functionality
- Loading branch information
Showing
6 changed files
with
110 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
app/src/main/java/ai/elimu/content_provider/language/SharedPreferencesHelper.java
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
app/src/main/java/ai/elimu/content_provider/util/SharedPreferencesHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package ai.elimu.content_provider.util; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.text.TextUtils; | ||
|
||
import ai.elimu.model.v2.enums.Language; | ||
|
||
public class SharedPreferencesHelper { | ||
|
||
public static final String SHARED_PREFS = "shared_prefs"; | ||
|
||
public static final String PREF_APP_VERSION_CODE = "pref_app_version_code"; | ||
public static final String PREF_LANGUAGE = "pref_language"; | ||
|
||
public static void clearAllPreferences(Context context) { | ||
SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE); | ||
sharedPreferences.edit().clear().apply(); | ||
} | ||
|
||
|
||
public static void storeAppVersionCode(Context context, int appVersionCode) { | ||
SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE); | ||
sharedPreferences.edit().putInt(PREF_APP_VERSION_CODE, appVersionCode).apply(); | ||
} | ||
|
||
public static int getAppVersionCode(Context context) { | ||
SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE); | ||
return sharedPreferences.getInt(PREF_APP_VERSION_CODE, 0); | ||
} | ||
|
||
|
||
public static void storeLanguage(Context context, Language language) { | ||
SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE); | ||
sharedPreferences.edit().putString(PREF_LANGUAGE, language.toString()).apply(); | ||
} | ||
|
||
public static Language getLanguage(Context context) { | ||
SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE); | ||
String languageAsString = sharedPreferences.getString(PREF_LANGUAGE, null); | ||
if (TextUtils.isEmpty(languageAsString)) { | ||
return null; | ||
} else { | ||
try { | ||
return Language.valueOf(languageAsString); | ||
} catch (IllegalArgumentException e) { | ||
return null; | ||
} | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
app/src/main/java/ai/elimu/content_provider/util/VersionHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ai.elimu.content_provider.util; | ||
|
||
import android.content.Context; | ||
import android.content.pm.PackageInfo; | ||
import android.content.pm.PackageManager; | ||
import android.util.Log; | ||
|
||
/** | ||
* Helps detect upgrades from previously installed versions of the app. | ||
*/ | ||
public class VersionHelper { | ||
|
||
public static int getAppVersionCode(Context context) { | ||
Log.i(VersionHelper.class.getName(), "getAppVersionCode"); | ||
|
||
try { | ||
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); | ||
return packageInfo.versionCode; | ||
} catch (PackageManager.NameNotFoundException e) { | ||
throw new RuntimeException("Could not get package name: " + e); | ||
} | ||
} | ||
|
||
/** | ||
* Stores the version code of the application currently installed. And detects upgrades from previously installed | ||
* versions. | ||
*/ | ||
public static void updateAppVersion(Context context) { | ||
Log.i(VersionHelper.class.getName(), "updateAppVersion"); | ||
|
||
// Check if the application's versionCode was upgraded | ||
int oldVersionCode = SharedPreferencesHelper.getAppVersionCode(context); | ||
int newVersionCode = VersionHelper.getAppVersionCode(context); | ||
if (oldVersionCode == 0) { | ||
SharedPreferencesHelper.storeAppVersionCode(context, newVersionCode); | ||
oldVersionCode = newVersionCode; | ||
} | ||
Log.i(VersionHelper.class.getName(),"oldVersionCode: " + oldVersionCode); | ||
Log.i(VersionHelper.class.getName(),"newVersionCode: " + newVersionCode); | ||
|
||
// Handle upgrade from previous version | ||
if (oldVersionCode < newVersionCode) { | ||
Log.i(VersionHelper.class.getName(), "Upgrading application from version " + oldVersionCode + " to " + newVersionCode + "..."); | ||
|
||
// if (oldVersionCode < ???) { | ||
// ... | ||
// } | ||
|
||
SharedPreferencesHelper.storeAppVersionCode(context, newVersionCode); | ||
} | ||
} | ||
} |