diff --git a/README.md b/README.md index e6ffb75..e7d74a8 100644 --- a/README.md +++ b/README.md @@ -16,15 +16,14 @@ Add it in your root build.gradle at the end of repositories: Add the dependency dependencies { - implementation 'com.github.mohappsdev:InAppUpdate:v1.0.5' + implementation 'com.github.mohappsdev:InAppUpdate:v1.1.0' } #### Create [ForceUpdateActivity](https://github.com/mohappsdev/InAppUpdate/blob/master/app/src/main/java/mohapps/iaudemo/activity/ForceUpdateActivity.java) #### Modify your [MainActivity](https://github.com/mohappsdev/InAppUpdate/blob/master/app/src/main/java/mohapps/iaudemo/activity/MainActivity.java) - #### Modify your [App](https://github.com/mohappsdev/InAppUpdate/blob/master/app/src/main/java/mohapps/iaudemo/App.java) #### Add button_in_app_update (visibility = GONE) in your activity_main - #### Specify your settings in [Config](https://github.com/mohappsdev/InAppUpdate/blob/master/app/src/main/java/mohapps/iaudemo/config/Config.java) + #### IMPORTANT: Specify your settings in [Config](https://github.com/mohappsdev/InAppUpdate/blob/master/app/src/main/java/mohapps/iaudemo/config/Config.java) ## HOW IT WORKS #### button_in_app_update will be shown on main activity when there is an update available and when user taps it update dialog will be shown (Flexible update) diff --git a/app/build.gradle b/app/build.gradle index 6dad082..d6c7b83 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -39,7 +39,6 @@ dependencies { implementation 'com.google.android.play:core:1.7.3' implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation "androidx.core:core-ktx:1.3.0" - implementation 'com.github.mohappsdev:CacheManage:v1.4.0' implementation project(path: ':library') testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.3.0-rc01' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d21ca32..9229bfb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,7 +4,6 @@ package="mohapps.iaudemo"> + inAppUpdateHelper.handleInAppUpdate(this, AppUpdateType.FLEXIBLE, true, findViewById(R.id.button_in_app_update))); } -} +} \ No newline at end of file diff --git a/app/src/main/java/mohapps/iaudemo/config/Config.java b/app/src/main/java/mohapps/iaudemo/config/Config.java index eafbf3c..86b1a5b 100644 --- a/app/src/main/java/mohapps/iaudemo/config/Config.java +++ b/app/src/main/java/mohapps/iaudemo/config/Config.java @@ -10,11 +10,14 @@ public class Config { public static List FORCE_UPDATE_STRATEGY_LIST() { List FORCE_UPDATE_STRATEGY_LIST = new ArrayList<>(); - //TODO: Add force update strategies here + //TODO: Add or remove force update strategies here (uncomment to add) //FORCE_UPDATE_STRATEGY_LIST.add(ForceUpdateStrategy.LAST_DIGIT); //FORCE_UPDATE_STRATEGY_LIST.add(ForceUpdateStrategy.MAJOR_CHANGE); return FORCE_UPDATE_STRATEGY_LIST; } + + //TODO: Don't forget to modify corresponding value for each strategy below + //To use ForceUpdateStrategy.LAST_DIGIT you have to specify a positive number (no limit on digits) //example: If you set it as 4, updates with version code of *4 such as 4, 14, 24, 34, ..., 104, 114, ... will be forced to users //example: If you set it as 23, updates with version code of *23 such as 23, 123, 223, 323, ..., 1023, 1123, ... will be forced to users diff --git a/library/build.gradle b/library/build.gradle index 3b2fbc3..af48af1 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -27,8 +27,6 @@ android { dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - implementation 'com.google.code.gson:gson:2.8.6' - implementation 'com.github.mohappsdev:CacheManage:v1.4.0' implementation 'com.google.android.play:core:1.7.3' implementation 'androidx.core:core-ktx:1.3.0' implementation 'androidx.appcompat:appcompat:1.1.0' diff --git a/library/src/main/java/mohapps/inappupdate/entity/AppUpdateInfo.java b/library/src/main/java/mohapps/inappupdate/entity/AppUpdateInfo.java deleted file mode 100644 index 9d433e0..0000000 --- a/library/src/main/java/mohapps/inappupdate/entity/AppUpdateInfo.java +++ /dev/null @@ -1,73 +0,0 @@ -package mohapps.inappupdate.entity; - -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; - -public class AppUpdateInfo { - @SerializedName("updateAvailability") - @Expose - private int updateAvailability; - @SerializedName("availableVersionCode") - @Expose - private int availableVersionCode; - @SerializedName("isUpdateTypeAllowedIMMEDIATE") - @Expose - private boolean isUpdateTypeAllowedIMMEDIATE; - @SerializedName("isUpdateTypeAllowedFLEXIBLE") - @Expose - private boolean isUpdateTypeAllowedFLEXIBLE; - @SerializedName("packageName") - @Expose - private String packageName; - @SerializedName("installStatus") - @Expose - private int installStatus; - - public int getUpdateAvailability() { - return updateAvailability; - } - - public void setUpdateAvailability(int updateAvailability) { - this.updateAvailability = updateAvailability; - } - - public int getAvailableVersionCode() { - return availableVersionCode; - } - - public void setAvailableVersionCode(int availableVersionCode) { - this.availableVersionCode = availableVersionCode; - } - - public boolean isUpdateTypeAllowedIMMEDIATE() { - return isUpdateTypeAllowedIMMEDIATE; - } - - public void setUpdateTypeAllowedIMMEDIATE(boolean updateTypeAllowedIMMEDIATE) { - isUpdateTypeAllowedIMMEDIATE = updateTypeAllowedIMMEDIATE; - } - - public boolean isUpdateTypeAllowedFLEXIBLE() { - return isUpdateTypeAllowedFLEXIBLE; - } - - public void setUpdateTypeAllowedFLEXIBLE(boolean updateTypeAllowedFLEXIBLE) { - isUpdateTypeAllowedFLEXIBLE = updateTypeAllowedFLEXIBLE; - } - - public String getPackageName() { - return packageName; - } - - public void setPackageName(String packageName) { - this.packageName = packageName; - } - - public int getInstallStatus() { - return installStatus; - } - - public void setInstallStatus(int installStatus) { - this.installStatus = installStatus; - } -} diff --git a/library/src/main/java/mohapps/inappupdate/helper/Constants.java b/library/src/main/java/mohapps/inappupdate/helper/Constants.java index abe350b..755625e 100644 --- a/library/src/main/java/mohapps/inappupdate/helper/Constants.java +++ b/library/src/main/java/mohapps/inappupdate/helper/Constants.java @@ -1,6 +1,6 @@ package mohapps.inappupdate.helper; public class Constants { - public final static int IN_APP_UPDATE = 78546; + public final static int IN_APP_UPDATE = 2310; public final static String APP_UPDATE_INFO_CACHE_KEY="APP_UPDATE_INFO_CACHE_KEY"; } diff --git a/library/src/main/java/mohapps/inappupdate/helper/DataLoader.java b/library/src/main/java/mohapps/inappupdate/helper/DataLoader.java deleted file mode 100644 index c855f82..0000000 --- a/library/src/main/java/mohapps/inappupdate/helper/DataLoader.java +++ /dev/null @@ -1,33 +0,0 @@ -package mohapps.inappupdate.helper; - -import com.google.gson.Gson; -import com.haohaohu.cachemanage.CacheUtil; - -import mohapps.inappupdate.entity.AppUpdateInfo; - - -public class DataLoader{ - -public static AppUpdateInfo sideloadAppUpdateInfo() { - return new Gson().fromJson(CacheUtil.get(Constants.APP_UPDATE_INFO_CACHE_KEY, true), AppUpdateInfo.class); - } - - public static void saveAppUpdateInfo(int updateAvailability, int availableVersionCode, boolean isUpdateTypeAllowedIMMEDIATE, boolean isUpdateTypeAllowedFLEXIBLE, String packageName, int installStatus) { - - AppUpdateInfo appUpdateInfo= new AppUpdateInfo(); - appUpdateInfo.setAvailableVersionCode(availableVersionCode); - appUpdateInfo.setPackageName(packageName); - appUpdateInfo.setUpdateAvailability(updateAvailability); - appUpdateInfo.setUpdateTypeAllowedFLEXIBLE(isUpdateTypeAllowedFLEXIBLE); - appUpdateInfo.setUpdateTypeAllowedIMMEDIATE(isUpdateTypeAllowedIMMEDIATE); - appUpdateInfo.setInstallStatus(installStatus); - CacheUtil.put(Constants.APP_UPDATE_INFO_CACHE_KEY, new Gson().toJson(appUpdateInfo),true); - } - public static void deleteAppUpdateInfo() { - try { - CacheUtil.clear(Constants.APP_UPDATE_INFO_CACHE_KEY); - } catch (Exception e) {e.printStackTrace(); - } - } - -} \ No newline at end of file diff --git a/library/src/main/java/mohapps/inappupdate/helper/InAppUpdateHelper.kt b/library/src/main/java/mohapps/inappupdate/helper/InAppUpdateHelper.kt index c76e8cd..0fb1ac7 100644 --- a/library/src/main/java/mohapps/inappupdate/helper/InAppUpdateHelper.kt +++ b/library/src/main/java/mohapps/inappupdate/helper/InAppUpdateHelper.kt @@ -11,7 +11,6 @@ import androidx.core.content.pm.PackageInfoCompat import com.google.android.play.core.appupdate.AppUpdateInfo import com.google.android.play.core.appupdate.AppUpdateManager import com.google.android.play.core.appupdate.AppUpdateManagerFactory -import com.google.android.play.core.install.model.AppUpdateType.FLEXIBLE import com.google.android.play.core.install.model.AppUpdateType.IMMEDIATE import com.google.android.play.core.install.model.InstallStatus.* import com.google.android.play.core.install.model.UpdateAvailability @@ -21,20 +20,18 @@ import kotlin.math.pow class InAppUpdateHelper(private var forceUpdateStrategyConfig: ForceUpdateStrategyConfig?, private var forceUpdateActivity: Intent) { private var appUpdateManager: AppUpdateManager? = null - fun handleInAppUpdate(context: Context, appUpdateType: Int, launchedByUser: Boolean) { + fun handleInAppUpdate(context: Context, appUpdateType: Int, launchedByUser: Boolean, button_in_app_update: View?) { if (appUpdateManager == null) { appUpdateManager = AppUpdateManagerFactory.create(context) } + button_in_app_update?.visibility = View.GONE appUpdateManager?.appUpdateInfo?.addOnSuccessListener { appUpdateInfo: AppUpdateInfo -> - DataLoader.saveAppUpdateInfo(appUpdateInfo.updateAvailability(), - appUpdateInfo.availableVersionCode(), - appUpdateInfo.isUpdateTypeAllowed(IMMEDIATE), - appUpdateInfo.isUpdateTypeAllowed(FLEXIBLE), - appUpdateInfo.packageName(), - appUpdateInfo.installStatus()) + if (appUpdateInfo.installStatus() == DOWNLOADED) { + appUpdateManager?.completeUpdate() + } when (appUpdateInfo.updateAvailability()) { @@ -48,12 +45,13 @@ class InAppUpdateHelper(private var forceUpdateStrategyConfig: ForceUpdateStrate } UpdateAvailability.UPDATE_AVAILABLE -> { - if (appUpdateInfo.isUpdateTypeAllowed(appUpdateType) && appUpdateInfo.installStatus() != DOWNLOADED && appUpdateInfo.installStatus() != DOWNLOADING) { + if (appUpdateInfo.isUpdateTypeAllowed(appUpdateType) && appUpdateInfo.installStatus() != DOWNLOADED && appUpdateInfo.installStatus() != DOWNLOADING && appUpdateInfo.installStatus() != PENDING) { + button_in_app_update?.visibility = View.VISIBLE if (appUpdateType == IMMEDIATE || launchedByUser) { appUpdateManager?.startUpdateFlowForResult(appUpdateInfo, appUpdateType, context as Activity?, IN_APP_UPDATE) } else if (isForceUpdateNeeded(context, appUpdateInfo.availableVersionCode())) { - context.startActivity(Intent(context, forceUpdateActivity::class.java)) + context.startActivity(forceUpdateActivity) } } } @@ -66,30 +64,6 @@ class InAppUpdateHelper(private var forceUpdateStrategyConfig: ForceUpdateStrate } } - fun loadInAppUpdate(context: Context, button_in_app_update: View?) { - var appUpdateInfo: mohapps.inappupdate.entity.AppUpdateInfo? = null - try { - appUpdateInfo = DataLoader.sideloadAppUpdateInfo() - if (appUpdateInfo != null - && appUpdateInfo.availableVersionCode > getInstalledVersionCode(context) - && appUpdateInfo.installStatus != DOWNLOADED && appUpdateInfo.installStatus != DOWNLOADING && appUpdateInfo.installStatus != PENDING) { - - - button_in_app_update?.visibility = View.VISIBLE - button_in_app_update?.setOnClickListener { - handleInAppUpdate(context, FLEXIBLE, true) - } - } else { - button_in_app_update?.visibility = View.GONE - } - } catch (ignore: Exception) { - button_in_app_update?.visibility = View.GONE - } - if (appUpdateInfo?.installStatus == DOWNLOADED) { - appUpdateManager?.completeUpdate() - } - } - private fun getInstalledVersionCode(context: Context): Long { val pInfo: PackageInfo return try {