Füge Auto-Update für höhere Android-Versionen hinzu

master
Niko Diamadis 5 years ago
parent 7439f91845
commit fe5c7ca98c

@ -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" val link = "https://cdn.cyb3rko.de/Apps/Technik-Logger/Technik-Logger%20v$newestVersion.apk"
DownloadApk(context).startDownloadingApk(link) DownloadApk(context).startDownloadingApk(link)
} else { } 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()
} }
} }

@ -10,8 +10,8 @@ android {
defaultConfig { defaultConfig {
minSdkVersion 19 minSdkVersion 19
targetSdkVersion 30 targetSdkVersion 30
versionCode 1 versionCode 2
versionName "1.0" versionName "1.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro" consumerProguardFiles "consumer-rules.pro"

@ -11,20 +11,18 @@ import android.os.Environment;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
public class DownloadApk extends Activity{ public class DownloadApk extends Activity {
private static ProgressDialog bar;
private static Context context; private static Context context;
private static String downloadUrl; private static String downloadUrl;
private static String fileName; private static String fileName;
private static final String TAG = "DownloadApk";
public DownloadApk(Context context){ public DownloadApk(Context context){
DownloadApk.context = context; DownloadApk.context = context;
@ -32,22 +30,27 @@ public class DownloadApk extends Activity{
public void startDownloadingApk(String url){ public void startDownloadingApk(String url){
downloadUrl = url; downloadUrl = url;
System.out.println(downloadUrl);
fileName = url.split("Technik-Logger/")[1].replace("%20", " "); fileName = url.split("Technik-Logger/")[1].replace("%20", " ");
System.out.println(fileName); if (downloadUrl != null){
if (downloadUrl!=null){ new DownloadNewVersion(context).execute();
new DownloadNewVersion().execute();
} }
} }
private static class DownloadNewVersion extends AsyncTask<String, Integer, Boolean> { private static class DownloadNewVersion extends AsyncTask<String, Integer, Boolean> {
private ProgressDialog bar;
private final Context context;
DownloadNewVersion(Context context) {
this.context = context;
}
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
super.onPreExecute(); super.onPreExecute();
if(bar==null){ if (bar == null){
bar = new ProgressDialog(context); bar = new ProgressDialog(context);
bar.setCancelable(false); bar.setCancelable(false);
bar.setMessage("Downloading..."); bar.setMessage("Lädt herunter...");
bar.setIndeterminate(true); bar.setIndeterminate(true);
bar.setCanceledOnTouchOutside(false); bar.setCanceledOnTouchOutside(false);
bar.show(); bar.show();
@ -60,45 +63,53 @@ public class DownloadApk extends Activity{
bar.setMax(100); bar.setMax(100);
bar.setProgress(progress[0]); bar.setProgress(progress[0]);
String msg = ""; String msg = "";
if (progress[0]>99) { if (progress[0] > 99) {
msg="Finishing... "; msg = "Beenden... ";
} else { } else {
msg="Downloading... "+progress[0]+"%"; msg = "Herunterladen... " + progress[0] + "%";
} }
bar.setMessage(msg); bar.setMessage(msg);
} }
@Override @Override
protected void onPostExecute(Boolean result) { protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result); super.onPostExecute(result);
if (bar.isShowing() && bar!=null) { if (bar.isShowing() && bar != null) {
bar.dismiss(); bar.dismiss();
bar=null; bar = null;
} }
if (result){ if (result) {
Toast.makeText(context,"Update Done", Toast.LENGTH_SHORT).show(); Toast.makeText(context,"Download erfolgreich", Toast.LENGTH_SHORT).show();
} else { } else {
Toast.makeText(context,"Error: Try Again", Toast.LENGTH_SHORT).show(); Toast.makeText(context,"Download fehlgeschlagen", Toast.LENGTH_SHORT).show();
} }
} }
@Override @Override
protected Boolean doInBackground(String... arg0) { protected Boolean doInBackground(String... arg0) {
Boolean flag = false; boolean flag = false;
try { 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); URL url = new URL(downloadUrl);
HttpURLConnection c = (HttpURLConnection) url.openConnection(); HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET"); c.setRequestMethod("GET");
c.connect(); 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); FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream(); InputStream is = c.getInputStream();
@ -109,7 +120,7 @@ public class DownloadApk extends Activity{
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int len1; int len1;
int per; int per;
int downloaded=0; int downloaded = 0;
while ((len1 = is.read(buffer)) != -1) { while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1); fos.write(buffer, 0, len1);
downloaded += len1; downloaded += len1;
@ -118,33 +129,33 @@ public class DownloadApk extends Activity{
} }
fos.close(); fos.close();
is.close(); is.close();
openNewVersion(PATH); openNewVersion(outputFile.getPath());
flag = true; flag = true;
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
String TAG = "DownloadApk"; Log.e(TAG, "Update-Fehler: " + e.getMessage());
Log.e(TAG, "Update Error: " + e.getMessage());
flag = false; flag = false;
} catch (IOException ex) { } catch (Exception e) {
ex.printStackTrace(); e.printStackTrace();
Log.e(TAG, "Update-Fehler: " + e.getMessage());
} }
return flag; return flag;
} }
} }
private static void openNewVersion(String location) { private static void openNewVersion(String filePath) {
Intent intent = new Intent(Intent.ACTION_VIEW); 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_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent); context.startActivity(intent);
} }
private static Uri getUriFromFile(String location) { private static Uri getUriFromFile(String filePath) {
if (Build.VERSION.SDK_INT<24) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return Uri.fromFile(new File(location + fileName)); return Uri.fromFile(new File(filePath));
} else { } else {
return FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", return FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider",
new File(location + fileName)); new File(filePath));
} }
} }
} }

Loading…
Cancel
Save