automatische Installation von Updates vervollständigt
parent
f24fd24c9e
commit
a63a75364b
@ -0,0 +1 @@
|
||||
/build
|
||||
@ -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'
|
||||
}
|
||||
@ -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
|
||||
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.cyb3rko.installer">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
|
||||
</manifest>
|
||||
@ -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<String, Integer, Boolean> {
|
||||
@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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,2 +1,3 @@
|
||||
include ':installer'
|
||||
include ':app'
|
||||
rootProject.name = "Technik-Logger"
|
||||
Loading…
Reference in New Issue