From a63a75364b4ba426ae131de59c7890f146d983d8 Mon Sep 17 00:00:00 2001 From: Niko Diamadis Date: Sun, 13 Dec 2020 00:09:44 +0100 Subject: [PATCH] =?UTF-8?q?automatische=20Installation=20von=20Updates=20v?= =?UTF-8?q?ervollst=C3=A4ndigt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 1 + app/src/main/AndroidManifest.xml | 2 +- .../com/cyb3rko/techniklogger/MainActivity.kt | 29 +--- .../com/cyb3rko/techniklogger/UpdateUtils.kt | 67 ++------ installer/.gitignore | 1 + installer/build.gradle | 44 +++++ installer/consumer-rules.pro | 0 installer/proguard-rules.pro | 21 +++ installer/src/main/AndroidManifest.xml | 8 + .../com/cyb3rko/installer/DownloadApk.java | 150 ++++++++++++++++++ settings.gradle | 1 + 11 files changed, 238 insertions(+), 86 deletions(-) create mode 100644 installer/.gitignore create mode 100644 installer/build.gradle create mode 100644 installer/consumer-rules.pro create mode 100644 installer/proguard-rules.pro create mode 100644 installer/src/main/AndroidManifest.xml create mode 100644 installer/src/main/java/com/cyb3rko/installer/DownloadApk.java diff --git a/app/build.gradle b/app/build.gradle index 8e2c997..aa0d6a0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -72,4 +72,5 @@ dependencies { implementation "com.mikepenz:aboutlibraries-core:$about_libraries_version" implementation 'me.ibrahimyilmaz:kiel:1.1.0' implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + implementation project(':installer') } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4005512..3903f5f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,7 +5,6 @@ - = Build.VERSION_CODES.N) { - FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file) - } else { - Uri.fromFile(file) - } -} - -private fun installBelowAndroid10(context: Context, file: File, broadcastReceiver: BroadcastReceiver) { - val installIntent = Intent(Intent.ACTION_VIEW) - installIntent.setDataAndType(uriFromFile(context, file), "application/vnd.android.package-archive") - installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - context.startActivity(installIntent) - - context.unregisterReceiver(broadcastReceiver) -} \ No newline at end of file diff --git a/installer/.gitignore b/installer/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/installer/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/installer/build.gradle b/installer/build.gradle new file mode 100644 index 0000000..7617ac7 --- /dev/null +++ b/installer/build.gradle @@ -0,0 +1,44 @@ +plugins { + id 'com.android.library' + id 'kotlin-android' +} + +android { + compileSdkVersion 30 + buildToolsVersion "30.0.2" + + defaultConfig { + minSdkVersion 19 + targetSdkVersion 30 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + consumerProguardFiles "consumer-rules.pro" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = '1.8' + } +} + +dependencies { + + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + implementation 'androidx.core:core-ktx:1.3.2' + implementation 'androidx.appcompat:appcompat:1.2.0' + implementation 'com.google.android.material:material:1.2.1' + testImplementation 'junit:junit:4.+' + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' +} \ No newline at end of file diff --git a/installer/consumer-rules.pro b/installer/consumer-rules.pro new file mode 100644 index 0000000..e69de29 diff --git a/installer/proguard-rules.pro b/installer/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/installer/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/installer/src/main/AndroidManifest.xml b/installer/src/main/AndroidManifest.xml new file mode 100644 index 0000000..d68c97f --- /dev/null +++ b/installer/src/main/AndroidManifest.xml @@ -0,0 +1,8 @@ + + + + + + + \ No newline at end of file diff --git a/installer/src/main/java/com/cyb3rko/installer/DownloadApk.java b/installer/src/main/java/com/cyb3rko/installer/DownloadApk.java new file mode 100644 index 0000000..bcf2a75 --- /dev/null +++ b/installer/src/main/java/com/cyb3rko/installer/DownloadApk.java @@ -0,0 +1,150 @@ +package com.cyb3rko.installer; + +import android.app.Activity; +import android.app.ProgressDialog; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.os.AsyncTask; +import android.os.Build; +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; + private static Context context; + private static String downloadUrl; + private static String fileName; + + public DownloadApk(Context context){ + DownloadApk.context = context; + } + + 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(); + } + } + + private static class DownloadNewVersion extends AsyncTask { + @Override + protected void onPreExecute() { + super.onPreExecute(); + if(bar==null){ + bar = new ProgressDialog(context); + bar.setCancelable(false); + bar.setMessage("Downloading..."); + bar.setIndeterminate(true); + bar.setCanceledOnTouchOutside(false); + bar.show(); + } + } + + protected void onProgressUpdate(Integer... progress) { + super.onProgressUpdate(progress); + bar.setIndeterminate(false); + bar.setMax(100); + bar.setProgress(progress[0]); + String msg = ""; + if (progress[0]>99) { + msg="Finishing... "; + } else { + msg="Downloading... "+progress[0]+"%"; + } + bar.setMessage(msg); + } + + @Override + protected void onPostExecute(Boolean result) { + // TODO Auto-generated method stub + super.onPostExecute(result); + if (bar.isShowing() && bar!=null) { + bar.dismiss(); + bar=null; + } + + if (result){ + Toast.makeText(context,"Update Done", Toast.LENGTH_SHORT).show(); + } else { + Toast.makeText(context,"Error: Try Again", Toast.LENGTH_SHORT).show(); + } + } + + @Override + protected Boolean doInBackground(String... arg0) { + Boolean flag = false; + try { + 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(); + + // size of apk + int total_size = c.getContentLength(); + + byte[] buffer = new byte[1024]; + int len1; + int per; + int downloaded=0; + while ((len1 = is.read(buffer)) != -1) { + fos.write(buffer, 0, len1); + downloaded += len1; + per = downloaded * 100 / total_size; + publishProgress(per); + } + fos.close(); + is.close(); + openNewVersion(PATH); + flag = true; + } catch (MalformedURLException e) { + String TAG = "DownloadApk"; + Log.e(TAG, "Update Error: " + e.getMessage()); + flag = false; + } catch (IOException ex) { + ex.printStackTrace(); + } + return flag; + } + } + + private static void openNewVersion(String location) { + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setDataAndType(getUriFromFile(location), "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)); + } else { + return FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", + new File(location + fileName)); + } + } +} diff --git a/settings.gradle b/settings.gradle index c9559bd..e566ff0 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,3 @@ +include ':installer' include ':app' rootProject.name = "Technik-Logger" \ No newline at end of file