imporved updating

This commit is contained in:
2021-04-02 21:06:15 +02:00
parent 90dc2bb9eb
commit e3dc1a161b
4 changed files with 74 additions and 24 deletions
@@ -3,8 +3,11 @@ package de.joel.zoomhelper;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
@@ -23,6 +26,7 @@ import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;
import androidx.core.content.pm.ShortcutInfoCompat;
import androidx.core.content.pm.ShortcutManagerCompat;
import androidx.core.graphics.drawable.IconCompat;
@@ -34,6 +38,7 @@ import com.google.android.material.textfield.TextInputLayout;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
@@ -135,7 +140,6 @@ public class MainActivity extends AppCompatActivity {
watchShareIcon();
watchShortcutIcon();
watchTestDLIcon();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
@@ -149,26 +153,32 @@ public class MainActivity extends AppCompatActivity {
launchZoomUrl(url);
} else {
AppUpdater appUpdater = new AppUpdater(this).setUpdateFrom(UpdateFrom.XML).setUpdateXML("https://baldaufwd.de/ZoomHelper/update.xml");
appUpdater.setButtonUpdateClickListener((dialog, which) -> downloadAPK());
appUpdater.start();
}
}
private void watchTestDLIcon() {
Button testDLBtn = findViewById(R.id.btnTestDL);
testDLBtn.setOnClickListener(v -> {
downloadAPK();
});
}
private void downloadAPK() {
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = "ZoomHelper.apk";
destination += fileName;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// this will request for permission when user has not granted permission for the app
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
//Delete update file if exists
File file = new File(destination);
final Uri localUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file);
if (file.exists())
//file.delete() - test this, I think sometimes it doesnt work
file.delete();
//Download Script
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse("https://baldaufwd.de/ZoomHelper/ZoomHelper.apk");
@@ -176,10 +186,34 @@ public class MainActivity extends AppCompatActivity {
request.setVisibleInDownloadsUi(true);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, uri.getLastPathSegment());
downloadManager.enqueue(request);
final long downloadID = downloadManager.enqueue(request);
BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Fetching the download id received with the broadcast
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
//Checking if the received broadcast is for our enqueued download by matching download id
if (downloadID == id) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
install.setDataAndType(localUri,
downloadManager.getMimeTypeForDownloadedFile(downloadID));
startActivity(install);
unregisterReceiver(this);
}
}
};
registerReceiver(onDownloadComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
}
private void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
String meetingID = "";