Skip to content

Commit

Permalink
Merge pull request #46 from SanojPunchihewa/dev
Browse files Browse the repository at this point in the history
Integrate new features from play core update 1.7.2
  • Loading branch information
SanojPunchihewa authored May 13, 2020
2 parents 01d6649 + 7ee1844 commit 0fbaa8c
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 34 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ dist: trusty

env:
global:
- ANDROID_API_LEVEL=28
- ANDROID_BUILD_TOOLS_VERSION=28.0.3
- ANDROID_API_LEVEL=29
- ANDROID_BUILD_TOOLS_VERSION=29.0.2
- ANDROID_ABI=armeabi-v7a

android:
Expand All @@ -22,7 +22,7 @@ android:
before_install:
- touch $HOME/.android/repositories.cfg
- yes | sdkmanager "platforms;android-28"
- yes | sdkmanager "build-tools;28.0.3"
- yes | sdkmanager "build-tools;29.0.2"

before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
Expand Down
36 changes: 24 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,39 @@ There are two modes

* Immediate(`UpdateManagerConstant.IMMEDIATE`) - User will be blocked until download and installation is finished, restart is triggered automatically

**Additionally** you can get the Available Version Code of the update and the Number of days passed since the user was notified of an update through the Google Play. You can find these codes in the [demo app](/app/src/main/java/com/zanojmobiapps/inappupdatedemoapp/MainActivity.java)

### Step 4: Resume the updates
Call `continueUpdate` method in your `onResume` method to install waiting updates
```java
@Override
protected void onResume() {
super.onResume();
// Continue updates when resumed
mUpdateManager.continueUpdate();
}
mUpdateManager.addUpdateInfoListener(new UpdateInfoListener() {
@Override
public void onReceiveVersionCode(final int code) {
// You can get the available version code of the apk in Google Play
// Do something here
}

@Override
public void onReceiveStalenessDays(final int days) {
// Number of days passed since the user was notified of an update through the Google Play
// If the user hasn't notified this will return -1 as days
// You can decide the type of update you want to call
}
});
```
**Additionally** you can get the Available Version Code of the update. You can find these codes in the [demo app](/app/src/main/java/com/zanojmobiapps/inappupdatedemoapp/MainActivity.java)

**Monitoring the flexible update download progres**

You can monitor the download progress of a Flexible Update using this callback.
***Note***: This is only available for Flexible update mode. You can find more from the [official doc](https://developer.android.com/guide/playcore/in-app-updates#monitor_flexible)
```java
mUpdateManager.getAvailableVersionCode(new onVersionCheckListener() {
// Callback from Flexible Update Progress
mUpdateManager.addFlexibleUpdateDownloadListener(new FlexibleUpdateDownloadListener() {
@Override
public void onReceiveVersionCode(final int code) {
// Do something here
public void onDownloadProgress(final long bytesDownloaded, final long totalBytes) {
// Show a progress bar or anything you want
}
});
```

## :movie_camera: Demo
Flexible Update | Immediate Update
:-------------------------:|:-------------------------:
Expand Down
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.zanojmobiapps.inappupdatedemoapp"
minSdkVersion 21
targetSdkVersion 28
targetSdkVersion 29
versionCode 2
versionName "2.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,65 @@
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.sanojpunchihewa.updatemanager.BuildConfig;
import com.sanojpunchihewa.updatemanager.UpdateManager;
import com.sanojpunchihewa.updatemanager.UpdateManager.onVersionCheckListener;
import com.sanojpunchihewa.updatemanager.UpdateManager.FlexibleUpdateDownloadListener;
import com.sanojpunchihewa.updatemanager.UpdateManager.UpdateInfoListener;
import com.sanojpunchihewa.updatemanager.UpdateManagerConstant;

public class MainActivity extends AppCompatActivity {

// Declare the UpdateManager
UpdateManager mUpdateManager;

TextView txtFlexibleUpdateProgress;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView txtCurrentVersion = findViewById(R.id.txt_current_version);
TextView txtAvailableVersion = findViewById(R.id.txt_available_version);
TextView txtStalenessDays = findViewById(R.id.txt_staleness_days);
txtFlexibleUpdateProgress = findViewById(R.id.txt_flexible_progress);

txtCurrentVersion.setText(String.valueOf(BuildConfig.VERSION_CODE));

// Initialize the Update Manager with the Activity and the Update Mode
mUpdateManager = UpdateManager.Builder(this);

// Callback from Available version code
mUpdateManager.getAvailableVersionCode(new onVersionCheckListener() {
// Callback from UpdateInfoListener
// You can get the available version code of the apk in Google Play
// Number of days passed since the user was notified of an update through the Google Play
mUpdateManager.addUpdateInfoListener(new UpdateInfoListener() {
@Override
public void onReceiveVersionCode(final int code) {
txtAvailableVersion.setText(String.valueOf(code));
}

@Override
public void onReceiveStalenessDays(final int days) {
txtStalenessDays.setText(String.valueOf(days));
}
});

}
// Callback from Flexible Update Progress
// This is only available for Flexible mode
// Find more from https://developer.android.com/guide/playcore/in-app-updates#monitor_flexible
mUpdateManager.addFlexibleUpdateDownloadListener(new FlexibleUpdateDownloadListener() {
@Override
public void onDownloadProgress(final long bytesDownloaded, final long totalBytes) {
txtFlexibleUpdateProgress.setText("Downloading: " + bytesDownloaded + " / " + totalBytes);
}
});

@Override
protected void onResume() {
super.onResume();
// Continue updates when resumed
mUpdateManager.continueUpdate();
}

public void callFlexibleUpdate(View view) {
// Start a Flexible Update
mUpdateManager.mode(UpdateManagerConstant.FLEXIBLE).start();
txtFlexibleUpdateProgress.setVisibility(View.VISIBLE);
}

public void callImmediateUpdate(View view) {
Expand Down
35 changes: 33 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="120dp"
android:gravity="center_horizontal"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/linearLayout2"
app:layout_constraintEnd_toStartOf="@id/linearLayout3"
app:layout_constraintTop_toTopOf="parent">

<TextView
Expand Down Expand Up @@ -61,6 +60,32 @@
android:textSize="15sp" />
</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:gravity="center_horizontal"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.84"
app:layout_constraintStart_toEndOf="@+id/linearLayout"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:id="@+id/txt_staleness_days"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
tools:text="0" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Staleness Days"
android:textSize="15sp" />
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -74,6 +99,12 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout">

<TextView
android:id="@+id/txt_flexible_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />

<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:textColor="@color/colorAccent"
Expand Down
5 changes: 3 additions & 2 deletions updatemanager/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 28
compileSdkVersion 29
buildToolsVersion "29.0.2"

defaultConfig {
minSdkVersion 21
targetSdkVersion 28
targetSdkVersion 29
versionCode 1
versionName "1.0"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class UpdateManager implements LifecycleObserver {
// Returns an intent object that you use to check for an update.
private Task<AppUpdateInfo> appUpdateInfoTask;

private FlexibleUpdateDownloadListener flexibleUpdateDownloadListener;

private UpdateManager(AppCompatActivity activity) {
mActivityWeakReference = new WeakReference<>(activity);
this.appUpdateManager = AppUpdateManagerFactory.create(getActivity());
Expand Down Expand Up @@ -115,6 +117,13 @@ private void startUpdate(AppUpdateInfo appUpdateInfo) {
private InstallStateUpdatedListener listener = new InstallStateUpdatedListener() {
@Override
public void onStateUpdate(InstallState installState) {
if (installState.installStatus() == InstallStatus.DOWNLOADING) {
long bytesDownloaded = installState.bytesDownloaded();
long totalBytesToDownload = installState.totalBytesToDownload();
if (flexibleUpdateDownloadListener != null) {
flexibleUpdateDownloadListener.onDownloadProgress(bytesDownloaded, totalBytesToDownload);
}
}
if (installState.installStatus() == InstallStatus.DOWNLOADED) {
// After the update is downloaded, show a notification
// and request user confirmation to restart the app.
Expand All @@ -128,7 +137,8 @@ private void setUpListener() {
appUpdateManager.registerListener(listener);
}

public void continueUpdate() {
private void continueUpdate() {

if (instance.mode == FLEXIBLE) {
continueUpdateForFlexible();
} else {
Expand Down Expand Up @@ -190,22 +200,29 @@ public void onClick(View v) {
snackbar.show();
}

public void getAvailableVersionCode(final onVersionCheckListener onVersionCheckListener) {
public void addUpdateInfoListener(final UpdateInfoListener updateInfoListener) {
appUpdateInfoTask.addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>() {
@Override
public void onSuccess(AppUpdateInfo appUpdateInfo) {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE) {
// Request the update.
Log.d(TAG, "Update available");
int availableVersionCode = appUpdateInfo.availableVersionCode();
onVersionCheckListener.onReceiveVersionCode(availableVersionCode);
int stalenessDays = appUpdateInfo.clientVersionStalenessDays() != null ? appUpdateInfo
.clientVersionStalenessDays() : -1;
updateInfoListener.onReceiveVersionCode(availableVersionCode);
updateInfoListener.onReceiveStalenessDays(stalenessDays);
} else {
Log.d(TAG, "No Update available");
}
}
});
}

public void addFlexibleUpdateDownloadListener(FlexibleUpdateDownloadListener flexibleUpdateDownloadListener) {
this.flexibleUpdateDownloadListener = flexibleUpdateDownloadListener;
}

private Activity getActivity() {
return mActivityWeakReference.get();
}
Expand All @@ -217,9 +234,22 @@ private void unregisterListener() {
}
}

public interface onVersionCheckListener {
public interface UpdateInfoListener {

void onReceiveVersionCode(int code);

void onReceiveStalenessDays(int days);
}

public interface FlexibleUpdateDownloadListener {

void onDownloadProgress(long bytesDownloaded, long totalBytes);

}

@OnLifecycleEvent(Event.ON_RESUME)
private void onResume() {
continueUpdate();
}

@OnLifecycleEvent(Event.ON_DESTROY)
Expand Down

0 comments on commit 0fbaa8c

Please sign in to comment.