Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Implement in-app updates #1312

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ dependencies {
// Play services
implementation 'com.google.android.gms:play-services-base:16.1.0'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.android.play:core:1.5.0'

// Firebase
implementation 'com.google.firebase:firebase-core:16.0.8'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager.NetworkCallback;
import android.net.Network;
Expand All @@ -11,6 +12,15 @@
import android.view.View;

import com.google.android.material.snackbar.Snackbar;
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.InstallState;
import com.google.android.play.core.install.InstallStateUpdatedListener;
import com.google.android.play.core.install.model.AppUpdateType;
import com.google.android.play.core.install.model.InstallStatus;
import com.google.android.play.core.install.model.UpdateAvailability;
import com.google.android.play.core.tasks.Task;

import java.util.List;

Expand Down Expand Up @@ -50,6 +60,9 @@ public class MainActivity extends BaseActivity
private CardAdapter mAdapter;
private SwipeRefreshLayout mSwipeRefreshLayout;

private final int FLEXIBLE_UPDATE_REQUEST_CODE = 1;
private final int IMMEDIATE_UPDATE_REQUEST_CODE = 2;

@Inject
Provider<MainActivityViewModel> viewModelProvider;

Expand Down Expand Up @@ -118,6 +131,64 @@ protected void onCreate(Bundle savedInstanceState) {
});
}

private void checkForUpdates() {
// Creates instance of the manager.
AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(this);
// Returns an intent object that you use to check for an update.
Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();
// Checks that the platform will allow the specified type of update.
appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
try {
if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo, AppUpdateType.IMMEDIATE, this, IMMEDIATE_UPDATE_REQUEST_CODE);
} else if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) {
InstallStateUpdatedListener installStateUpdatedListener = new InstallStateUpdatedListener() {
@Override
public void onStateUpdate(InstallState installState) {
if (installState.installStatus() == InstallStatus.DOWNLOADED) {
showInstallUpdateSnackbar(appUpdateManager);
appUpdateManager.unregisterListener(this);
}
}
};
appUpdateManager.registerListener(installStateUpdatedListener);
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo, AppUpdateType.FLEXIBLE, this, FLEXIBLE_UPDATE_REQUEST_CODE);
}
} catch (IntentSender.SendIntentException e) {
Utils.log(e);
}
}
});
}

private void showInstallUpdateSnackbar(AppUpdateManager appUpdateManager) {
Snackbar snackbar =
Snackbar.make(
findViewById(R.id.drawer_layout),
getApplicationContext().getString(R.string.update_downloaded),
Snackbar.LENGTH_INDEFINITE);
snackbar.setAction(getString(R.string.update_restart), view -> appUpdateManager.completeUpdate());
snackbar.setActionTextColor(
getResources().getColor(R.color.color_primary));
snackbar.show();
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == IMMEDIATE_UPDATE_REQUEST_CODE || requestCode == FLEXIBLE_UPDATE_REQUEST_CODE) {
if (resultCode != RESULT_OK) {
Utils.log("Update flow failed! Result code: " + resultCode);
// retry immediate updates
if (requestCode == IMMEDIATE_UPDATE_REQUEST_CODE) {
checkForUpdates();
}
}
}
}

private void onNewCardsAvailable(List<Card> cards) {
mSwipeRefreshLayout.setRefreshing(false);
mAdapter.updateItems(cards);
Expand Down Expand Up @@ -158,6 +229,31 @@ protected void onResume() {
refreshCards();
Utils.setSetting(this, Const.REFRESH_CARDS, false);
}

AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(this);
appUpdateManager
.getAppUpdateInfo()
.addOnSuccessListener(appUpdateInfo -> {
// If the update is downloaded but not installed, notify the user to complete the update.
// For flexible updates
if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED) {
showInstallUpdateSnackbar(appUpdateManager);
}
// For immediate updates
if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) {
// If an in-app update is already running, resume the update.
try {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
AppUpdateType.IMMEDIATE,
this,
IMMEDIATE_UPDATE_REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
Utils.log(e);
}
}
});

}

/**
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -699,4 +699,7 @@ Signatur: %5$s</string>
<string name="cafeteria_cards_settings_title">Wähle Speisepläne</string>
<string name="settings_chose_cafeteria_cards">Mensakarten wählen</string>
<string name="settings_no_location_selected">Kein Standort ausgewählt</string>

<string name="update_downloaded">Ein Update wurde heruntergeladen.</string>
<string name="update_restart">NEUSTART</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -752,4 +752,6 @@ Signature: %5$s</string>
<string name="cafeteria_cards_settings_title">Select cafeterias to be shown</string>
<string name="settings_chose_cafeteria_cards">Choose Cafeteria Cards</string>
<string name="settings_no_location_selected">No Location Selected</string>
<string name="update_downloaded">An update has just been downloaded.</string>
<string name="update_restart">RESTART</string>
</resources>