Skip to content

Commit

Permalink
Update samples
Browse files Browse the repository at this point in the history
  • Loading branch information
Dionysis Karatzas committed Jul 23, 2019
1 parent 782e33e commit 8474417
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 22 deletions.
46 changes: 24 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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,
Expand All @@ -85,28 +85,29 @@ 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);

Snackbar snackbar = Snackbar.make(rootView,
"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());
}
}
```
Expand All @@ -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();
```


Expand All @@ -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);
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -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
*/
}

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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);

}

}

0 comments on commit 8474417

Please sign in to comment.