Skip to content

Commit

Permalink
Merge pull request #40 from dofranko/#34-issue
Browse files Browse the repository at this point in the history
automatic download
  • Loading branch information
adbednarz authored Sep 12, 2021
2 parents 6d119b5 + fd42c90 commit 47b96e4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 23 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
minSdkVersion 26
targetSdkVersion 30
versionCode 1
versionName "0.8.3"
versionName "0.8.4"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
18 changes: 18 additions & 0 deletions app/release/output-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"version": 2,
"artifactType": {
"type": "APK",
"kind": "Directory"
},
"applicationId": "com.example.towerofflamingblames",
"variantName": "release",
"elements": [
{
"type": "SINGLE",
"filters": [],
"versionCode": 1,
"versionName": "0.8.3.5",
"outputFile": "app-release.apk"
}
]
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.towerofflamingblames">
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
Expand Down Expand Up @@ -45,24 +49,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
TextView appVersion = findViewById(R.id.appVersion);
appVersion.setText("version: " + BuildConfig.VERSION_NAME);

AppUpdaterUtils appUpdaterUtils = new AppUpdaterUtils(this)
.setUpdateFrom(UpdateFrom.GITHUB)
.setGitHubUserAndRepo("dofranko", "tower-of-flaming-blames")
.withListener(new AppUpdaterUtils.UpdateListener() {
@Override
public void onSuccess(Update update, Boolean isUpdateAvailable) {
if (isUpdateAvailable) {
informAboutNewVersionApp(update.getUrlToDownload().toString());
}
}

@Override
public void onFailed(AppUpdaterError error) {
Log.d("AppUpdater Error", "Something went wrong");
}
});
appUpdaterUtils.start();
checkNewVersion();
}

@Override
Expand Down Expand Up @@ -245,16 +232,56 @@ protected void onDestroy() {
}
}

private void checkNewVersion() {
AppUpdaterUtils appUpdaterUtils = new AppUpdaterUtils(this)
.setUpdateFrom(UpdateFrom.GITHUB)
.setGitHubUserAndRepo("dofranko", "tower-of-flaming-blames")
.withListener(new AppUpdaterUtils.UpdateListener() {
@Override
public void onSuccess(Update update, Boolean isUpdateAvailable) {
if (isUpdateAvailable) {
informAboutNewVersionApp(update.getUrlToDownload().toString());
}
}

@Override
public void onFailed(AppUpdaterError error) {
Log.d("AppUpdater Error", "Something went wrong");
}
});
appUpdaterUtils.start();
}

private void informAboutNewVersionApp(String url) {
AlertDialog newUpdateDialog = new AlertDialog.Builder(this)
.setTitle("New update available!")
.setMessage("Enjoy a new version of Tower of Flaming Blames!")
.setPositiveButton("Update", (dialog, which) -> {
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(launchBrowser);
})
.setPositiveButton("Update", (dialog, which) -> downloadUpdate(url))
.setNegativeButton("Cancel", (dialog, which) -> {})
.show();
newUpdateDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(Color.RED);
}

private void downloadUpdate(String url) {
DownloadManager.Request request = new DownloadManager
.Request(Uri.parse(url + "/download/TowerOfFlamingBlames.apk"));
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);

BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.setDataAndType(
manager.getUriForDownloadedFile(downloadId),
manager.getMimeTypeForDownloadedFile(downloadId)
);
startActivity(install);
unregisterReceiver(this);
finish();
}
};

registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
}

0 comments on commit 47b96e4

Please sign in to comment.