From e3dc1a161b3b3e21fcd6c6f7120ee2a56fc46bf4 Mon Sep 17 00:00:00 2001 From: Joel Date: Fri, 2 Apr 2021 21:06:15 +0200 Subject: [PATCH] imporved updating --- app/src/main/AndroidManifest.xml | 18 +++++-- .../java/de/joel/zoomhelper/MainActivity.java | 54 +++++++++++++++---- app/src/main/res/layout/buttons.xml | 14 ++--- app/src/main/res/xml/file_provider_paths.xml | 12 +++++ 4 files changed, 74 insertions(+), 24 deletions(-) create mode 100644 app/src/main/res/xml/file_provider_paths.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5c33dac..98f647d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,9 +5,12 @@ - - - + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/de/joel/zoomhelper/MainActivity.java b/app/src/main/java/de/joel/zoomhelper/MainActivity.java index 2e1093f..4a95eb1 100644 --- a/app/src/main/java/de/joel/zoomhelper/MainActivity.java +++ b/app/src/main/java/de/joel/zoomhelper/MainActivity.java @@ -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 = ""; diff --git a/app/src/main/res/layout/buttons.xml b/app/src/main/res/layout/buttons.xml index a3c946b..3e3ab72 100644 --- a/app/src/main/res/layout/buttons.xml +++ b/app/src/main/res/layout/buttons.xml @@ -1,8 +1,9 @@ + android:layout_height="wrap_content"> +