Skip to content

Commit

Permalink
commit at Aug 06, 2020
Browse files Browse the repository at this point in the history
fixed some bugs
removed unnecessary complications
  • Loading branch information
mohappsdev committed Aug 6, 2020
1 parent 00ad4e9 commit 94cccc6
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 195 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package="mohapps.iaudemo">

<application
android:name=".App"
tools:replace="android:allowBackup"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
Expand Down
22 changes: 0 additions & 22 deletions app/src/main/java/mohapps/iaudemo/App.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ protected void onResume() {
if(inAppUpdateHelper==null){
inAppUpdateHelper = new InAppUpdateHelper(Config.getForceUpdateStrategyConfig(), new Intent(this, ForceUpdateActivity.class));
}
inAppUpdateHelper.handleInAppUpdate(this, AppUpdateType.IMMEDIATE, false);
inAppUpdateHelper.handleInAppUpdate(this, AppUpdateType.IMMEDIATE, false, null);
}
}
27 changes: 4 additions & 23 deletions app/src/main/java/mohapps/iaudemo/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


import androidx.appcompat.app.AppCompatActivity;
import mohapps.inappupdate.helper.DataLoader;
import mohapps.inappupdate.helper.InAppUpdateHelper;
import mohapps.iaudemo.R;
import mohapps.iaudemo.config.Config;
Expand All @@ -31,35 +30,17 @@ protected void onCreate(Bundle savedInstanceState) {

}



@Override
protected void onResume() {
super.onResume();
if(inAppUpdateHelper==null){
inAppUpdateHelper = new InAppUpdateHelper(Config.getForceUpdateStrategyConfig(), new Intent(this, ForceUpdateActivity.class));
}
inAppUpdateHelper.handleInAppUpdate(this, AppUpdateType.FLEXIBLE, false);
inAppUpdateHelper.handleInAppUpdate(this, AppUpdateType.FLEXIBLE, false, findViewById(R.id.button_in_app_update));

//TODO: customize button_in_app_update (visibility = GONE) and place it in desired place in your layout
inAppUpdateHelper.loadInAppUpdate(this, findViewById(R.id.button_in_app_update));
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == IN_APP_UPDATE) {
if (resultCode != Activity.RESULT_OK) {
Log.i("Update flow failed!", String.valueOf(resultCode));
// If the update is cancelled or fails,
// you can request to start the update again.
}
if(resultCode== Activity.RESULT_OK){
inAppUpdateHelper.loadInAppUpdate(this, findViewById(R.id.button_in_app_update));
new Handler().postDelayed(DataLoader::deleteAppUpdateInfo, 1000);
}

}
findViewById(R.id.button_in_app_update).setOnClickListener(v ->
inAppUpdateHelper.handleInAppUpdate(this, AppUpdateType.FLEXIBLE, true, findViewById(R.id.button_in_app_update)));
}
}
}
5 changes: 4 additions & 1 deletion app/src/main/java/mohapps/iaudemo/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ public class Config {

public static List<Integer> FORCE_UPDATE_STRATEGY_LIST() {
List<Integer> 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
Expand Down
2 changes: 0 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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";
}
33 changes: 0 additions & 33 deletions library/src/main/java/mohapps/inappupdate/helper/DataLoader.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()) {

Expand All @@ -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)
}
}
}
Expand All @@ -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 {
Expand Down

0 comments on commit 94cccc6

Please sign in to comment.