From 84744171bf482c402325f77b9319460f826886da Mon Sep 17 00:00:00 2001 From: Dionysis Karatzas Date: Tue, 23 Jul 2019 13:08:41 +0300 Subject: [PATCH] Update samples --- README.md | 46 ++++----- .../sample/FlexibleDefaultSnackbar.java | 82 +++++++++++++++ .../FlexibleWithCustomNotification.java | 99 +++++++++++++++++++ .../inapp/update/sample/Immediate.java | 64 ++++++++++++ 4 files changed, 269 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/eu/dkaratzas/android/inapp/update/sample/FlexibleDefaultSnackbar.java create mode 100644 app/src/main/java/eu/dkaratzas/android/inapp/update/sample/FlexibleWithCustomNotification.java create mode 100644 app/src/main/java/eu/dkaratzas/android/inapp/update/sample/Immediate.java diff --git a/README.md b/README.md index 2e67057..f724d1e 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - UpdateManager inAppUpdateManager = UpdateManager.Builder(this, REQ_CODE_VERSION_UPDATE) + InAppUpdateManager inAppUpdateManager = InAppUpdateManager.Builder(this, REQ_CODE_VERSION_UPDATE) .resumeUpdates(true) // Resume the update, if the update was stalled. Default is true .mode(UpdateMode.FLEXIBLE) .snackBarMessage("An update has just been downloaded.") @@ -60,17 +60,17 @@ protected void onCreate(Bundle savedInstanceState) { * With custom user confirmation, need to set the `useCustomNotification(true)` and monitor the update for the `UpdateStatus.DOWNLOADED` status. Then a notification (or some other UI indication) can be used, to inform the user that installation is ready and requests user confirmation to restart the app. The confirmation must call the `completeUpdate()` method to finish the update. ```java -public class FlexibleWithCustomSnackbar extends AppCompatActivity implements UpdateManager.InAppUpdateHandler { +public class FlexibleWithCustomNotification extends AppCompatActivity implements InAppUpdateManager.InAppUpdateHandler { private static final int REQ_CODE_VERSION_UPDATE = 530; - private static final String TAG = "FlexibleCustomSnackbar"; - private UpdateManager inAppUpdateManager; + private static final String TAG = "Sample"; + private InAppUpdateManager inAppUpdateManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - inAppUpdateManager = UpdateManager.Builder(this, REQ_CODE_VERSION_UPDATE) + inAppUpdateManager = InAppUpdateManager.Builder(this, REQ_CODE_VERSION_UPDATE) .resumeUpdates(true) // Resume the update, if the update was stalled. Default is true .mode(UpdateMode.FLEXIBLE) // default is false. If is set to true you, @@ -85,8 +85,12 @@ public class FlexibleWithCustomSnackbar extends AppCompatActivity implements Upd // InAppUpdateHandler implementation @Override - public void onStatusUpdate(UpdateStatus status) { - if (status == UpdateStatus.DOWNLOADED) { + public void onInAppUpdateStatus(InAppUpdateStatus status) { + /* + * If the update downloaded, ask user confirmation and complete the update + */ + + if (status.isDownloaded()) { View rootView = getWindow().getDecorView().findViewById(android.R.id.content); @@ -94,19 +98,16 @@ public class FlexibleWithCustomSnackbar extends AppCompatActivity implements Upd "An update has just been downloaded.", Snackbar.LENGTH_INDEFINITE); - snackbar.setAction("RESTART", new View.OnClickListener() { - @Override - public void onClick(View view) { - // Triggers the completion of the update of the app for the flexible flow. - inAppUpdateManager.completeUpdate(); - } + snackbar.setAction("RESTART", view -> { + + // Triggers the completion of the update of the app for the flexible flow. + updateManager.completeUpdate(); + }); snackbar.show(); } - - Log.d(TAG, "status: " + status.id()); } } ``` @@ -117,11 +118,11 @@ public class FlexibleWithCustomSnackbar extends AppCompatActivity implements Upd To perform an Immediate update, need only to set the mode to `IMMEDIATE` and call the `checkForAppUpdate()` method. ```java -inAppUpdateManager = UpdateManager.Builder(this, REQ_CODE_VERSION_UPDATE) - .resumeUpdates(true) // Resume the update, if the update was stalled. Default is true - .mode(UpdateMode.IMMEDIATE); +InAppUpdateManager.Builder(this, REQ_CODE_VERSION_UPDATE) + .resumeUpdates(true) // Resume the update, if the update was stalled. Default is true + .mode(UpdateMode.IMMEDIATE) + .checkForAppUpdate(); -inAppUpdateManager.checkForAppUpdate(); ``` @@ -132,14 +133,15 @@ There are sometimes, that we need to force all users to get a critical update. W @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { if (requestCode == REQ_CODE_VERSION_UPDATE) { - if (resultCode == RESULT_CANCELED) { + if (resultCode == Activity.RESULT_CANCELED) { // If the update is cancelled by the user, // you can request to start the update again. - inAppUpdateManager.checkForAppUpdate(); - + updateManager.checkForAppUpdate(); + Log.d(TAG, "Update flow failed! Result code: " + resultCode); } } + super.onActivityResult(requestCode, resultCode, data); } ``` diff --git a/app/src/main/java/eu/dkaratzas/android/inapp/update/sample/FlexibleDefaultSnackbar.java b/app/src/main/java/eu/dkaratzas/android/inapp/update/sample/FlexibleDefaultSnackbar.java new file mode 100644 index 0000000..6f27aca --- /dev/null +++ b/app/src/main/java/eu/dkaratzas/android/inapp/update/sample/FlexibleDefaultSnackbar.java @@ -0,0 +1,82 @@ +/* + * Copyright 2019 Dionysios Karatzas + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package eu.dkaratzas.android.inapp.update.sample; + +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; + +import eu.dkaratzas.android.inapp.update.InAppUpdateManager; +import eu.dkaratzas.android.inapp.update.InAppUpdateStatus; + +import static eu.dkaratzas.android.inapp.update.Constants.UpdateMode; + +public class FlexibleDefaultSnackbar extends AppCompatActivity implements InAppUpdateManager.InAppUpdateHandler { + private static final int REQ_CODE_VERSION_UPDATE = 530; + private static final String TAG = "Sample"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + InAppUpdateManager inAppUpdateManager = InAppUpdateManager.Builder(this, REQ_CODE_VERSION_UPDATE) + .resumeUpdates(true) // Resume the update, if the update was stalled. Default is true + .mode(UpdateMode.FLEXIBLE) + .useCustomNotification(false) //default is false + .snackBarMessage("An update has just been downloaded.") + .snackBarAction("RESTART") + .handler(this); + + inAppUpdateManager.checkForAppUpdate(); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + if (requestCode == REQ_CODE_VERSION_UPDATE) { + if (resultCode != RESULT_OK) { + // If the update is cancelled or fails, + // you can request to start the update again. + Log.d(TAG, "Update flow failed! Result code: " + resultCode); + } + } + + super.onActivityResult(requestCode, resultCode, data); + + } + + // InAppUpdateHandler implementation + + @Override + public void onInAppUpdateError(int code, Throwable error) { + /* + * Called when some error occurred. See Constants class for more details + */ + Log.d(TAG, "code: " + code, error); + } + + @Override + public void onInAppUpdateStatus(InAppUpdateStatus status) { + /* + * Called when the update status change occurred. See Constants class for more details + */ + } + +} diff --git a/app/src/main/java/eu/dkaratzas/android/inapp/update/sample/FlexibleWithCustomNotification.java b/app/src/main/java/eu/dkaratzas/android/inapp/update/sample/FlexibleWithCustomNotification.java new file mode 100644 index 0000000..b231469 --- /dev/null +++ b/app/src/main/java/eu/dkaratzas/android/inapp/update/sample/FlexibleWithCustomNotification.java @@ -0,0 +1,99 @@ +/* + * Copyright 2019 Dionysios Karatzas + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package eu.dkaratzas.android.inapp.update.sample; + +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; + +import eu.dkaratzas.android.inapp.update.InAppUpdateManager; +import eu.dkaratzas.android.inapp.update.InAppUpdateStatus; + +import static eu.dkaratzas.android.inapp.update.Constants.UpdateMode; + +public class FlexibleWithCustomNotification extends AppCompatActivity implements InAppUpdateManager.InAppUpdateHandler { + private static final int REQ_CODE_VERSION_UPDATE = 530; + private static final String TAG = "Sample"; + private InAppUpdateManager inAppUpdateManager; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + inAppUpdateManager = InAppUpdateManager.Builder(this, REQ_CODE_VERSION_UPDATE) + .resumeUpdates(true) // Resume the update, if the update was stalled. Default is true + .mode(UpdateMode.FLEXIBLE) + // default is false. If is set to true you, + // have to manage the user confirmation when + // you detect the InstallStatus.DOWNLOADED status, + .useCustomNotification(true) + .handler(this); + + inAppUpdateManager.checkForAppUpdate(); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + if (requestCode == REQ_CODE_VERSION_UPDATE) { + if (resultCode != RESULT_OK) { + // If the update is cancelled or fails, + // you can request to start the update again. + inAppUpdateManager.checkForAppUpdate(); + + Log.d(TAG, "Update flow failed! Result code: " + resultCode); + } + } + + super.onActivityResult(requestCode, resultCode, data); + + } + + // InAppUpdateHandler implementation + + @Override + public void onInAppUpdateError(int code, Throwable error) { + /* + * Called when some error occurred. See Constants class for more details + */ + Log.d(TAG, "code: " + code, error); + } + + @Override + public void onInAppUpdateStatus(InAppUpdateStatus status) { + /* + * If the update downloaded, ask user confirmation and complete the update + */ + + if (status.isDownloaded()) { + + new AlertDialog.Builder(this) + .setTitle("InAppUpdate") + .setMessage("An update has just been downloaded.") + .setPositiveButton("Complete", (dialog, which) -> { + // Triggers the completion of the update of the app for the flexible flow. + inAppUpdateManager.completeUpdate(); + }) + .setNegativeButton("Cancel", null) + .show(); + } + } +} diff --git a/app/src/main/java/eu/dkaratzas/android/inapp/update/sample/Immediate.java b/app/src/main/java/eu/dkaratzas/android/inapp/update/sample/Immediate.java new file mode 100644 index 0000000..68d0342 --- /dev/null +++ b/app/src/main/java/eu/dkaratzas/android/inapp/update/sample/Immediate.java @@ -0,0 +1,64 @@ +/* + * Copyright 2019 Dionysios Karatzas + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package eu.dkaratzas.android.inapp.update.sample; + +import android.app.Activity; +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; + +import androidx.annotation.Nullable; +import androidx.appcompat.app.AppCompatActivity; + +import eu.dkaratzas.android.inapp.update.InAppUpdateManager; + +import static eu.dkaratzas.android.inapp.update.Constants.UpdateMode; + +public class Immediate extends AppCompatActivity { + private static final int REQ_CODE_VERSION_UPDATE = 530; + private static final String TAG = "Sample"; + private InAppUpdateManager inAppUpdateManager; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + inAppUpdateManager = InAppUpdateManager.Builder(this, REQ_CODE_VERSION_UPDATE) + .resumeUpdates(true) // Resume the update, if the update was stalled. Default is true + .mode(UpdateMode.IMMEDIATE); + + inAppUpdateManager.checkForAppUpdate(); + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + if (requestCode == REQ_CODE_VERSION_UPDATE) { + if (resultCode == Activity.RESULT_CANCELED) { + // If the update is cancelled by the user, + // you can request to start the update again. + inAppUpdateManager.checkForAppUpdate(); + + Log.d(TAG, "Update flow failed! Result code: " + resultCode); + } + } + + super.onActivityResult(requestCode, resultCode, data); + + } + +}