|
|
|
|
@ -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;
|
|
|
|
|
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();
|
|
|
|
|
new DownloadNewVersion(context).execute();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static class DownloadNewVersion extends AsyncTask<String, Integer, Boolean> {
|
|
|
|
|
private ProgressDialog bar;
|
|
|
|
|
private final Context context;
|
|
|
|
|
|
|
|
|
|
DownloadNewVersion(Context context) {
|
|
|
|
|
this.context = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onPreExecute() {
|
|
|
|
|
super.onPreExecute();
|
|
|
|
|
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();
|
|
|
|
|
@ -61,16 +64,15 @@ public class DownloadApk extends Activity{
|
|
|
|
|
bar.setProgress(progress[0]);
|
|
|
|
|
String msg = "";
|
|
|
|
|
if (progress[0] > 99) {
|
|
|
|
|
msg="Finishing... ";
|
|
|
|
|
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) {
|
|
|
|
|
bar.dismiss();
|
|
|
|
|
@ -78,27 +80,36 @@ public class DownloadApk extends Activity{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result) {
|
|
|
|
|
Toast.makeText(context,"Update Done", Toast.LENGTH_SHORT).show();
|
|
|
|
|
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();
|
|
|
|
|
@ -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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|