From fe5c7ca98c8762b59ca60ef6fd56e54cd98fe50f Mon Sep 17 00:00:00 2001 From: Niko Diamadis Date: Fri, 3 Sep 2021 19:41:42 +0200 Subject: [PATCH] =?UTF-8?q?F=C3=BCge=20Auto-Update=20f=C3=BCr=20h=C3=B6her?= =?UTF-8?q?e=20Android-Versionen=20hinzu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/cyb3rko/techniklogger/UpdateUtils.kt | 2 +- installer/build.gradle | 4 +- .../com/cyb3rko/installer/DownloadApk.java | 89 +++++++++++-------- 3 files changed, 53 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/cyb3rko/techniklogger/UpdateUtils.kt b/app/src/main/java/com/cyb3rko/techniklogger/UpdateUtils.kt index 7d39ce0..847a8df 100644 --- a/app/src/main/java/com/cyb3rko/techniklogger/UpdateUtils.kt +++ b/app/src/main/java/com/cyb3rko/techniklogger/UpdateUtils.kt @@ -68,6 +68,6 @@ private fun downloadApk(context: Context, newestVersion: String) { val link = "https://cdn.cyb3rko.de/Apps/Technik-Logger/Technik-Logger%20v$newestVersion.apk" DownloadApk(context).startDownloadingApk(link) } else { - Toasty.error(context, "Fehlende Berechtigung, erteilen Sie diese beim nächsten App-Start", Toasty.LENGTH_LONG).show() + Toasty.error(context, "Fehlende Berechtigung, erteile diese beim nächsten App-Start", Toasty.LENGTH_LONG).show() } } diff --git a/installer/build.gradle b/installer/build.gradle index 1abc65d..5c6e0a2 100644 --- a/installer/build.gradle +++ b/installer/build.gradle @@ -10,8 +10,8 @@ android { defaultConfig { minSdkVersion 19 targetSdkVersion 30 - versionCode 1 - versionName "1.0" + versionCode 2 + versionName "1.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" diff --git a/installer/src/main/java/com/cyb3rko/installer/DownloadApk.java b/installer/src/main/java/com/cyb3rko/installer/DownloadApk.java index bcf2a75..b4b3d5f 100644 --- a/installer/src/main/java/com/cyb3rko/installer/DownloadApk.java +++ b/installer/src/main/java/com/cyb3rko/installer/DownloadApk.java @@ -11,20 +11,18 @@ import android.os.Environment; import android.util.Log; import android.widget.Toast; import androidx.core.content.FileProvider; - import java.io.File; import java.io.FileOutputStream; -import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; -public class DownloadApk extends Activity{ - private static ProgressDialog bar; +public class DownloadApk extends Activity { private static Context context; private static String downloadUrl; private static String fileName; + private static final String TAG = "DownloadApk"; public DownloadApk(Context context){ DownloadApk.context = context; @@ -32,22 +30,27 @@ public class DownloadApk extends Activity{ public void startDownloadingApk(String url){ downloadUrl = url; - System.out.println(downloadUrl); fileName = url.split("Technik-Logger/")[1].replace("%20", " "); - System.out.println(fileName); - if (downloadUrl!=null){ - new DownloadNewVersion().execute(); + if (downloadUrl != null){ + new DownloadNewVersion(context).execute(); } } private static class DownloadNewVersion extends AsyncTask { + private ProgressDialog bar; + private final Context context; + + DownloadNewVersion(Context context) { + this.context = context; + } + @Override protected void onPreExecute() { super.onPreExecute(); - if(bar==null){ + if (bar == null){ bar = new ProgressDialog(context); bar.setCancelable(false); - bar.setMessage("Downloading..."); + bar.setMessage("Lädt herunter..."); bar.setIndeterminate(true); bar.setCanceledOnTouchOutside(false); bar.show(); @@ -60,45 +63,53 @@ public class DownloadApk extends Activity{ bar.setMax(100); bar.setProgress(progress[0]); String msg = ""; - if (progress[0]>99) { - msg="Finishing... "; + if (progress[0] > 99) { + msg = "Beenden... "; } else { - msg="Downloading... "+progress[0]+"%"; + msg = "Herunterladen... " + progress[0] + "%"; } bar.setMessage(msg); } @Override protected void onPostExecute(Boolean result) { - // TODO Auto-generated method stub super.onPostExecute(result); - if (bar.isShowing() && bar!=null) { + if (bar.isShowing() && bar != null) { bar.dismiss(); - bar=null; + bar = null; } - if (result){ - Toast.makeText(context,"Update Done", Toast.LENGTH_SHORT).show(); + if (result) { + Toast.makeText(context,"Download erfolgreich", Toast.LENGTH_SHORT).show(); } else { - Toast.makeText(context,"Error: Try Again", Toast.LENGTH_SHORT).show(); + Toast.makeText(context,"Download fehlgeschlagen", Toast.LENGTH_SHORT).show(); } } @Override protected Boolean doInBackground(String... arg0) { - Boolean flag = false; + boolean flag = false; try { + String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/"; + File outputFile = new File(destination + fileName); + if (outputFile.exists()) { + try { + openNewVersion(outputFile.getPath()); + return true; + } catch (Exception ignored) { + } + } + outputFile.createNewFile(); + + File directory = new File(destination); + if (!directory.exists()) { + directory.mkdirs(); + } + URL url = new URL(downloadUrl); HttpURLConnection c = (HttpURLConnection) url.openConnection(); c.setRequestMethod("GET"); c.connect(); - String PATH = Environment.getExternalStorageDirectory()+"/Download/"; - File file = new File(PATH); - File outputFile = new File(file, fileName); - - if(outputFile.exists()){ - outputFile.delete(); - } FileOutputStream fos = new FileOutputStream(outputFile); InputStream is = c.getInputStream(); @@ -109,7 +120,7 @@ public class DownloadApk extends Activity{ byte[] buffer = new byte[1024]; int len1; int per; - int downloaded=0; + int downloaded = 0; while ((len1 = is.read(buffer)) != -1) { fos.write(buffer, 0, len1); downloaded += len1; @@ -118,33 +129,33 @@ public class DownloadApk extends Activity{ } fos.close(); is.close(); - openNewVersion(PATH); + openNewVersion(outputFile.getPath()); flag = true; } catch (MalformedURLException e) { - String TAG = "DownloadApk"; - Log.e(TAG, "Update Error: " + e.getMessage()); + Log.e(TAG, "Update-Fehler: " + e.getMessage()); flag = false; - } catch (IOException ex) { - ex.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + Log.e(TAG, "Update-Fehler: " + e.getMessage()); } return flag; } } - private static void openNewVersion(String location) { + private static void openNewVersion(String filePath) { Intent intent = new Intent(Intent.ACTION_VIEW); - intent.setDataAndType(getUriFromFile(location), "application/vnd.android.package-archive"); + intent.setDataAndType(getUriFromFile(filePath), "application/vnd.android.package-archive"); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); context.startActivity(intent); } - private static Uri getUriFromFile(String location) { - if (Build.VERSION.SDK_INT<24) { - return Uri.fromFile(new File(location + fileName)); + private static Uri getUriFromFile(String filePath) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { + return Uri.fromFile(new File(filePath)); } else { return FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", - new File(location + fileName)); + new File(filePath)); } } }