forked from mirror/monocles_chat
removed the internal app updater
This commit is contained in:
parent
be9341dea6
commit
28a5ef1e3b
4 changed files with 2 additions and 738 deletions
|
@ -1,227 +0,0 @@
|
|||
package eu.siacs.conversations.services;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.URL;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
|
||||
import eu.siacs.conversations.BuildConfig;
|
||||
import eu.siacs.conversations.Config;
|
||||
import eu.siacs.conversations.R;
|
||||
import eu.siacs.conversations.http.NoSSLv3SocketFactory;
|
||||
import eu.siacs.conversations.ui.UpdaterActivity;
|
||||
import me.drakeet.support.toast.ToastCompat;
|
||||
|
||||
import static eu.siacs.conversations.http.HttpConnectionManager.getProxy;
|
||||
|
||||
public class UpdateService extends AsyncTask<String, Object, UpdateService.Wrapper> {
|
||||
private boolean mUseTor;
|
||||
private Context context;
|
||||
private String store;
|
||||
private NotificationService getNotificationService;
|
||||
|
||||
public UpdateService() {
|
||||
}
|
||||
|
||||
public UpdateService(Context context, String Store, XmppConnectionService mXmppConnectionService) {
|
||||
this.context = context;
|
||||
this.store = Store;
|
||||
this.mUseTor = mXmppConnectionService.useTorToConnect();
|
||||
this.getNotificationService = mXmppConnectionService.getNotificationService();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Wrapper doInBackground(String... params) {
|
||||
StringBuilder jsonString = new StringBuilder();
|
||||
boolean UpdateAvailable = false;
|
||||
boolean showNoUpdateToast = false;
|
||||
boolean isError = false;
|
||||
|
||||
if (params[0].equals("true")) {
|
||||
showNoUpdateToast = true;
|
||||
}
|
||||
SSLContext sslcontext = null;
|
||||
SSLSocketFactory NoSSLv3Factory = null;
|
||||
try {
|
||||
sslcontext = SSLContext.getInstance("TLSv1");
|
||||
if (sslcontext != null) {
|
||||
sslcontext.init(null, null, null);
|
||||
NoSSLv3Factory = new NoSSLv3SocketFactory(sslcontext.getSocketFactory());
|
||||
}
|
||||
} catch (NoSuchAlgorithmException | KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
HttpsURLConnection.setDefaultSSLSocketFactory(NoSSLv3Factory);
|
||||
HttpsURLConnection connection = null;
|
||||
try {
|
||||
// URL url = new URL(Config.UPDATE_URL);
|
||||
if (mUseTor) {
|
||||
// connection = (HttpsURLConnection) url.openConnection(getProxy());
|
||||
} else {
|
||||
// connection = (HttpsURLConnection) url.openConnection();
|
||||
}
|
||||
connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
|
||||
connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
|
||||
connection.setRequestProperty("User-agent", System.getProperty("http.agent"));
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
jsonString.append(line);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
isError = true;
|
||||
} finally {
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
JSONObject json = new JSONObject(jsonString.toString());
|
||||
if (json.getBoolean("success") && json.has("latestVersion") && json.has("appURI") && json.has("filesize")) {
|
||||
String version = json.getString("latestVersion");
|
||||
String ownVersion = BuildConfig.VERSION_NAME;
|
||||
String url = json.getString("appURI");
|
||||
String filesize = json.getString("filesize");
|
||||
String changelog = "";
|
||||
if (json.has("changelog")) {
|
||||
changelog = json.getString("changelog");
|
||||
}
|
||||
if (checkVersion(version, ownVersion) >= 1) {
|
||||
Log.d(Config.LOGTAG, "AppUpdater: Version " + ownVersion + " should be updated to " + version);
|
||||
UpdateAvailable = true;
|
||||
showNotification(url, changelog, version, filesize, store);
|
||||
} else {
|
||||
Log.d(Config.LOGTAG, "AppUpdater: Version " + ownVersion + " is up to date");
|
||||
UpdateAvailable = false;
|
||||
}
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Wrapper w = new Wrapper();
|
||||
w.isError = isError;
|
||||
w.UpdateAvailable = UpdateAvailable;
|
||||
w.NoUpdate = showNoUpdateToast;
|
||||
return w;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Wrapper w) {
|
||||
super.onPostExecute(w);
|
||||
if (w.isError) {
|
||||
showToastMessage(true, true);
|
||||
return;
|
||||
}
|
||||
if (!w.UpdateAvailable) {
|
||||
showToastMessage(w.NoUpdate, false);
|
||||
}
|
||||
}
|
||||
|
||||
private void showToastMessage(boolean show, final boolean error) {
|
||||
if (!show) {
|
||||
return;
|
||||
}
|
||||
Handler handler = new Handler(Looper.getMainLooper());
|
||||
handler.post(() -> {
|
||||
String ToastMessage = "";
|
||||
if (error) {
|
||||
ToastMessage = context.getString(R.string.failed);
|
||||
} else {
|
||||
ToastMessage = context.getString(R.string.no_update_available);
|
||||
}
|
||||
ToastCompat.makeText(context, ToastMessage, ToastCompat.LENGTH_LONG).show();
|
||||
});
|
||||
}
|
||||
|
||||
private void showNotification(String url, String changelog, String version, String filesize, String store) {
|
||||
Intent intent = new Intent(context, UpdaterActivity.class);
|
||||
intent.putExtra("update", "MonoclesMessenger_UpdateService");
|
||||
intent.putExtra("url", url);
|
||||
intent.putExtra("changelog", changelog);
|
||||
intent.putExtra("store", store);
|
||||
PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
getNotificationService.AppUpdateServiceNotification(getNotificationService.AppUpdateNotification(pi, version, filesize));
|
||||
}
|
||||
|
||||
private int checkVersion(String remoteVersion, String installedVersion) {
|
||||
// Use this instead of String.compareTo() for a non-lexicographical
|
||||
// comparison that works for version strings. e.g. "1.10".compareTo("1.6").
|
||||
//
|
||||
// @param str1 a string of ordinal numbers separated by decimal points.
|
||||
// @param str2 a string of ordinal numbers separated by decimal points.
|
||||
// @return The result is a negative integer if str1 is _numerically_ less than str2.
|
||||
// The result is a positive integer if str1 is _numerically_ greater than str2.
|
||||
// The result is zero if the strings are _numerically_ equal.
|
||||
// @note It does not work if "1.10" is supposed to be equal to "1.10.0".
|
||||
|
||||
String[] remote = null;
|
||||
String[] installed = null;
|
||||
String[] remoteV = null;
|
||||
String[] installedV = null;
|
||||
try {
|
||||
installedV = installedVersion.split("[ |\\-]");
|
||||
Log.d(Config.LOGTAG, "AppUpdater: Version installed: " + installedV[0]);
|
||||
installed = installedV[0].split("\\.");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
remoteV = remoteVersion.split(" ");
|
||||
if (installedV != null && installedV.length > 1) {
|
||||
if (installedV[1] != null && installedV[1].toLowerCase().contains("beta")) {
|
||||
remoteV[0] = remoteV[0] + ".1";
|
||||
}
|
||||
}
|
||||
Log.d(Config.LOGTAG, "AppUpdater: Version on server: " + remoteV[0]);
|
||||
remote = remoteV[0].split("\\.");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
int i = 0;
|
||||
// set index to first non-equal ordinal or length of shortest localVersion string
|
||||
try {
|
||||
if (remote != null && installed != null) {
|
||||
while (i < remote.length && i < installed.length && remote[i].equals(installed[i])) {
|
||||
i++;
|
||||
}
|
||||
// compare first non-equal ordinal number
|
||||
if (i < remote.length && i < installed.length) {
|
||||
int diff = Integer.valueOf(remote[i]).compareTo(Integer.valueOf(installed[i]));
|
||||
return Integer.signum(diff);
|
||||
}
|
||||
// the strings are equal or one string is a substring of the other
|
||||
// e.g. "1.2.3" = "1.2.3" or "1.2.3" < "1.2.3.4"
|
||||
return Integer.signum(remote.length - installed.length);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
showToastMessage(true, true);
|
||||
e.printStackTrace();
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
class Wrapper {
|
||||
boolean UpdateAvailable = false;
|
||||
boolean NoUpdate = false;
|
||||
boolean isError = false;
|
||||
}
|
||||
}
|
|
@ -215,16 +215,6 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio
|
|||
System.exit(0);
|
||||
}
|
||||
|
||||
if (useInternalUpdater()) {
|
||||
if (xmppConnectionService.getAccounts().size() != 0) {
|
||||
if (xmppConnectionService.hasInternetConnection()) {
|
||||
if (xmppConnectionService.isWIFI() || (xmppConnectionService.isMobile() && !xmppConnectionService.isMobileRoaming())) {
|
||||
AppUpdate(xmppConnectionService.installedFrom());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (@IdRes int id : FRAGMENT_ID_NOTIFICATION_ORDER) {
|
||||
notifyFragmentOfBackendConnected(id);
|
||||
}
|
||||
|
@ -1082,25 +1072,6 @@ public class ConversationsActivity extends XmppActivity implements OnConversatio
|
|||
runOnUiThread(() -> ToastCompat.makeText(this, resId, ToastCompat.LENGTH_SHORT).show());
|
||||
}
|
||||
|
||||
protected void AppUpdate(String Store) {
|
||||
String PREFS_NAME = "UpdateTimeStamp";
|
||||
SharedPreferences UpdateTimeStamp = getApplicationContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
||||
long lastUpdateTime = UpdateTimeStamp.getLong("lastUpdateTime", 0);
|
||||
Log.d(Config.LOGTAG, "AppUpdater: LastUpdateTime: " + lastUpdateTime);
|
||||
// if ((lastUpdateTime + (Config.UPDATE_CHECK_TIMER * 1000)) < System.currentTimeMillis()) {
|
||||
lastUpdateTime = System.currentTimeMillis();
|
||||
SharedPreferences.Editor editor = UpdateTimeStamp.edit();
|
||||
editor.putLong("lastUpdateTime", lastUpdateTime);
|
||||
editor.apply();
|
||||
Log.d(Config.LOGTAG, "AppUpdater: CurrentTime: " + lastUpdateTime);
|
||||
if (Store == null) {
|
||||
Log.d(Config.LOGTAG, "AppUpdater started");
|
||||
openInstallFromUnknownSourcesDialogIfNeeded(false);
|
||||
}
|
||||
// } else {
|
||||
Log.d(Config.LOGTAG, "AppUpdater stopped");
|
||||
}
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void onRoomDestroySucceeded() {
|
||||
|
|
|
@ -1,443 +0,0 @@
|
|||
package eu.siacs.conversations.ui;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.PowerManager;
|
||||
import android.util.Log;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.List;
|
||||
|
||||
import javax.net.ssl.HttpsURLConnection;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
|
||||
import eu.siacs.conversations.Config;
|
||||
import eu.siacs.conversations.R;
|
||||
import eu.siacs.conversations.http.NoSSLv3SocketFactory;
|
||||
import eu.siacs.conversations.persistance.FileBackend;
|
||||
import eu.siacs.conversations.services.XmppConnectionService;
|
||||
import eu.siacs.conversations.ui.util.CustomTab;
|
||||
import eu.siacs.conversations.utils.WakeLockHelper;
|
||||
import me.drakeet.support.toast.ToastCompat;
|
||||
|
||||
import static eu.siacs.conversations.Config.monocles;
|
||||
import static eu.siacs.conversations.http.HttpConnectionManager.getProxy;
|
||||
import static eu.siacs.conversations.services.XmppConnectionService.FDroid;
|
||||
import static eu.siacs.conversations.services.XmppConnectionService.PlayStore;
|
||||
|
||||
public class UpdaterActivity extends XmppActivity {
|
||||
static final private String FileName = "update.apk";
|
||||
String appURI = "";
|
||||
String changelog = "";
|
||||
Integer filesize = 0;
|
||||
String store;
|
||||
ProgressDialog mProgressDialog;
|
||||
DownloadTask downloadTask;
|
||||
TextView textView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
//set activity
|
||||
setContentView(R.layout.activity_updater);
|
||||
this.mTheme = findTheme();
|
||||
setTheme(this.mTheme);
|
||||
|
||||
textView = findViewById(R.id.updater);
|
||||
|
||||
mProgressDialog = new ProgressDialog(UpdaterActivity.this) {
|
||||
//show warning on back pressed
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
showCancelDialog();
|
||||
}
|
||||
};
|
||||
mProgressDialog.setMessage(getString(R.string.download_started));
|
||||
mProgressDialog.setProgressNumberFormat(null);
|
||||
mProgressDialog.setIndeterminate(true);
|
||||
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
|
||||
mProgressDialog.setCancelable(false);
|
||||
mProgressDialog.setCanceledOnTouchOutside(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void refreshUiReal() {
|
||||
//ignored
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
this.mTheme = findTheme();
|
||||
setTheme(this.mTheme);
|
||||
setTitle(getString(R.string.update_service));
|
||||
textView.setText(R.string.update_info);
|
||||
setSupportActionBar(findViewById(R.id.toolbar));
|
||||
configureActionBar(getSupportActionBar());
|
||||
if (getIntent() != null && getIntent().getStringExtra("update").equals("MonoclesMessenger_UpdateService")) {
|
||||
try {
|
||||
appURI = getIntent().getStringExtra("url");
|
||||
} catch (Exception e) {
|
||||
ToastCompat.makeText(getApplicationContext(), getText(R.string.failed), ToastCompat.LENGTH_LONG).show();
|
||||
UpdaterActivity.this.finish();
|
||||
}
|
||||
try {
|
||||
changelog = getIntent().getStringExtra("changelog");
|
||||
} catch (Exception e) {
|
||||
ToastCompat.makeText(getApplicationContext(), getText(R.string.failed), ToastCompat.LENGTH_LONG).show();
|
||||
UpdaterActivity.this.finish();
|
||||
}
|
||||
try {
|
||||
store = getIntent().getStringExtra("store");
|
||||
} catch (Exception e) {
|
||||
store = null;
|
||||
}
|
||||
//delete old downloaded localVersion files
|
||||
File dir = new File(FileBackend.getAppUpdateDirectory());
|
||||
if (dir.isDirectory()) {
|
||||
String[] children = dir.list();
|
||||
for (String aChildren : children) {
|
||||
Log.d(Config.LOGTAG, "AppUpdater: delete old update files " + aChildren + " in " + dir);
|
||||
new File(dir, aChildren).delete();
|
||||
}
|
||||
}
|
||||
|
||||
//oh yeah we do need an upgrade, let the user know send an alert message
|
||||
final AlertDialog.Builder builder = new AlertDialog.Builder(UpdaterActivity.this);
|
||||
builder.setCancelable(false);
|
||||
//open link to changelog
|
||||
//if the user agrees to upgrade
|
||||
builder.setMessage(getString(R.string.install_update))
|
||||
.setPositiveButton(R.string.update, (dialog, id) -> {
|
||||
Log.d(Config.LOGTAG, "AppUpdater: downloading " + FileName + " from " + appURI);
|
||||
//ask for permissions on devices >= SDK 23
|
||||
if (isStoragePermissionGranted() && isNetworkAvailable(getApplicationContext())) {
|
||||
//start downloading the file using the download manager
|
||||
if (store != null && store.equalsIgnoreCase(PlayStore)) {
|
||||
Uri uri = Uri.parse("market://details?id=" + getString(R.string.applicationId));
|
||||
Intent marketIntent = new Intent(Intent.ACTION_VIEW, uri);
|
||||
PackageManager manager = getApplicationContext().getPackageManager();
|
||||
List<ResolveInfo> infos = manager.queryIntentActivities(marketIntent, 0);
|
||||
if (infos.size() > 0) {
|
||||
startActivity(marketIntent);
|
||||
overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
|
||||
} else {
|
||||
try {
|
||||
uri = Uri.parse("https://" + monocles());
|
||||
CustomTab.openTab(this, uri, isDarkTheme());
|
||||
} catch (Exception e) {
|
||||
ToastCompat.makeText(this, R.string.no_application_found_to_open_link, ToastCompat.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
} else if (store != null && store.equalsIgnoreCase(FDroid)) {
|
||||
Uri uri = Uri.parse("https://f-droid.org/de/packages/" + getString(R.string.applicationId) + "/");
|
||||
Intent marketIntent = new Intent(Intent.ACTION_VIEW, uri);
|
||||
PackageManager manager = getApplicationContext().getPackageManager();
|
||||
List<ResolveInfo> infos = manager.queryIntentActivities(marketIntent, 0);
|
||||
if (infos.size() > 0) {
|
||||
startActivity(marketIntent);
|
||||
overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
|
||||
} else {
|
||||
uri = Uri.parse("https://" + monocles());
|
||||
try {
|
||||
CustomTab.openTab(this, uri, isDarkTheme());
|
||||
} catch (Exception e) {
|
||||
ToastCompat.makeText(this, R.string.no_application_found_to_open_link, ToastCompat.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ToastCompat.makeText(getApplicationContext(), getText(R.string.download_started), ToastCompat.LENGTH_LONG).show();
|
||||
downloadTask = new DownloadTask(UpdaterActivity.this);
|
||||
downloadTask.execute(appURI);
|
||||
}
|
||||
} else {
|
||||
Log.d(Config.LOGTAG, "AppUpdater: failed - has storage permissions " + isStoragePermissionGranted() + " and internet " + isNetworkAvailable(getApplicationContext()));
|
||||
}
|
||||
})
|
||||
.setNeutralButton(R.string.changelog, (dialog, id) -> {
|
||||
Uri uri = Uri.parse(Config.CHANGELOG_URL); // missing 'http://' will cause crash
|
||||
try {
|
||||
CustomTab.openTab(this, uri, isDarkTheme());
|
||||
} catch (Exception e) {
|
||||
ToastCompat.makeText(this, R.string.no_application_found_to_open_link, ToastCompat.LENGTH_SHORT).show();
|
||||
} finally {
|
||||
//restart updater to show dialog again after coming back after opening changelog
|
||||
recreate();
|
||||
}
|
||||
})
|
||||
.setNegativeButton(R.string.remind_later, (dialog, id) -> {
|
||||
// User cancelled the dialog
|
||||
UpdaterActivity.this.finish();
|
||||
});
|
||||
//show the alert message
|
||||
builder.create().show();
|
||||
} else {
|
||||
ToastCompat.makeText(getApplicationContext(), getText(R.string.failed), ToastCompat.LENGTH_LONG).show();
|
||||
UpdaterActivity.this.finish();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
void onBackendConnected() {
|
||||
//ignored
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle savedInstanceState) {
|
||||
super.onSaveInstanceState(savedInstanceState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRestoreInstanceState(Bundle savedInstanceState) {
|
||||
super.onRestoreInstanceState(savedInstanceState);
|
||||
}
|
||||
|
||||
//check for internet connection
|
||||
private boolean isNetworkAvailable(Context context) {
|
||||
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
if (connectivity != null) {
|
||||
NetworkInfo[] info = connectivity.getAllNetworkInfo();
|
||||
if (info != null) {
|
||||
for (NetworkInfo anInfo : info) {
|
||||
if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isStoragePermissionGranted() {
|
||||
if (Build.VERSION.SDK_INT >= 23) {
|
||||
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED && checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
||||
return true;
|
||||
} else {
|
||||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
|
||||
return false;
|
||||
}
|
||||
} else { //permission is automatically granted on sdk<23 upon installation
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
//show warning on back pressed
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
showCancelDialog();
|
||||
}
|
||||
|
||||
private void showCancelDialog() {
|
||||
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setMessage(R.string.cancel_update)
|
||||
.setCancelable(false)
|
||||
.setPositiveButton(R.string.yes, (dialog, id) -> {
|
||||
if (downloadTask != null && !downloadTask.getStatus().equals(AsyncTask.Status.FINISHED)) {
|
||||
downloadTask.cancel(true);
|
||||
}
|
||||
if (mProgressDialog.isShowing()) {
|
||||
mProgressDialog.dismiss();
|
||||
}
|
||||
UpdaterActivity.this.finish();
|
||||
})
|
||||
.setNegativeButton(R.string.no, (dialog, id) -> dialog.cancel());
|
||||
final AlertDialog alert = builder.create();
|
||||
alert.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
if (downloadTask != null && !downloadTask.getStatus().equals(AsyncTask.Status.FINISHED)) {
|
||||
downloadTask.cancel(true);
|
||||
}
|
||||
UpdaterActivity.this.finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
if (downloadTask != null && !downloadTask.getStatus().equals(AsyncTask.Status.FINISHED)) {
|
||||
downloadTask.cancel(true);
|
||||
}
|
||||
UpdaterActivity.this.finish();
|
||||
}
|
||||
|
||||
private class DownloadTask extends AsyncTask<String, Integer, String> {
|
||||
XmppActivity activity;
|
||||
File dir = new File(FileBackend.getAppUpdateDirectory());
|
||||
File file = new File(dir, FileName);
|
||||
XmppConnectionService xmppConnectionService;
|
||||
private Context context;
|
||||
private PowerManager.WakeLock mWakeLock;
|
||||
private long startTime = 0;
|
||||
private boolean mUseTor;
|
||||
|
||||
DownloadTask(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
super.onPreExecute();
|
||||
startTime = System.currentTimeMillis();
|
||||
// take CPU lock to prevent CPU from going off if the user
|
||||
// presses the power button during download
|
||||
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||
if (pm != null) {
|
||||
mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, getClass().getName());
|
||||
mWakeLock.acquire();
|
||||
mUseTor = xmppConnectionService != null && xmppConnectionService.useTorToConnect();
|
||||
}
|
||||
mProgressDialog.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onProgressUpdate(Integer... progress) {
|
||||
super.onProgressUpdate(progress);
|
||||
// if we get here, length is known, now set indeterminate to false
|
||||
mProgressDialog.setIndeterminate(false);
|
||||
mProgressDialog.setMax(100);
|
||||
mProgressDialog.setProgress(progress[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String doInBackground(String... sUrl) {
|
||||
InputStream is = null;
|
||||
OutputStream os = null;
|
||||
SSLContext sslcontext = null;
|
||||
SSLSocketFactory NoSSLv3Factory = null;
|
||||
try {
|
||||
sslcontext = SSLContext.getInstance("TLSv1");
|
||||
if (sslcontext != null) {
|
||||
sslcontext.init(null, null, null);
|
||||
NoSSLv3Factory = new NoSSLv3SocketFactory(sslcontext.getSocketFactory());
|
||||
}
|
||||
} catch (NoSuchAlgorithmException | KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
HttpsURLConnection.setDefaultSSLSocketFactory(NoSSLv3Factory);
|
||||
HttpsURLConnection connection = null;
|
||||
try {
|
||||
Log.d(Config.LOGTAG, "AppUpdater: save file to " + file.toString());
|
||||
Log.d(Config.LOGTAG, "AppUpdater: download update from url: " + sUrl[0] + " to file name: " + file.toString());
|
||||
|
||||
URL url = new URL(sUrl[0]);
|
||||
|
||||
if (mUseTor) {
|
||||
connection = (HttpsURLConnection) url.openConnection(getProxy());
|
||||
} else {
|
||||
connection = (HttpsURLConnection) url.openConnection();
|
||||
}
|
||||
connection.setConnectTimeout(Config.SOCKET_TIMEOUT * 1000);
|
||||
connection.setReadTimeout(Config.SOCKET_TIMEOUT * 1000);
|
||||
connection.setRequestProperty("User-agent", System.getProperty("http.agent"));
|
||||
connection.connect();
|
||||
|
||||
// expect HTTP 200 OK, so we don't mistakenly save error report
|
||||
// instead of the file
|
||||
if (connection.getResponseCode() != HttpsURLConnection.HTTP_OK) {
|
||||
ToastCompat.makeText(getApplicationContext(), getText(R.string.failed), ToastCompat.LENGTH_LONG).show();
|
||||
return connection.getResponseCode() + ": " + connection.getResponseMessage();
|
||||
}
|
||||
|
||||
// this will be useful to display download percentage
|
||||
// might be -1: server did not report the length
|
||||
int fileLength = connection.getContentLength();
|
||||
|
||||
// create folders
|
||||
File parentDirectory = file.getParentFile();
|
||||
if (parentDirectory.mkdirs()) {
|
||||
Log.d(Config.LOGTAG, "created " + parentDirectory.getAbsolutePath());
|
||||
}
|
||||
|
||||
// download the file
|
||||
is = connection.getInputStream();
|
||||
os = new FileOutputStream(file);
|
||||
|
||||
byte[] data = new byte[4096];
|
||||
long total = 0;
|
||||
int count;
|
||||
while ((count = is.read(data)) != -1) {
|
||||
// allow canceling with back button
|
||||
if (isCancelled()) {
|
||||
is.close();
|
||||
return "canceled";
|
||||
}
|
||||
total += count;
|
||||
// publishing the progress....
|
||||
if (fileLength > 0) // only if total length is known
|
||||
publishProgress((int) (total * 100 / fileLength));
|
||||
os.write(data, 0, count);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return e.toString();
|
||||
} finally {
|
||||
try {
|
||||
if (os != null)
|
||||
os.close();
|
||||
if (is != null)
|
||||
is.close();
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
|
||||
if (connection != null)
|
||||
connection.disconnect();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(String result) {
|
||||
WakeLockHelper.release(mWakeLock);
|
||||
mProgressDialog.dismiss();
|
||||
if (result != null) {
|
||||
ToastCompat.makeText(getApplicationContext(), getString(R.string.failed), ToastCompat.LENGTH_LONG).show();
|
||||
Log.d(Config.LOGTAG, "AppUpdater: failed with " + result);
|
||||
UpdaterActivity.this.finish();
|
||||
} else {
|
||||
Log.d(Config.LOGTAG, "AppUpdater: download ready in " + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
|
||||
|
||||
//start the installation of the latest localVersion
|
||||
Intent installIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
|
||||
installIntent.setDataAndType(FileBackend.getUriForFile(UpdaterActivity.this, file), "application/vnd.android.package-archive");
|
||||
installIntent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
|
||||
installIntent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
|
||||
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
installIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
startActivity(installIntent);
|
||||
overridePendingTransition(R.animator.fade_in, R.animator.fade_out);
|
||||
UpdaterActivity.this.finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -91,7 +91,6 @@ import eu.siacs.conversations.services.AvatarService;
|
|||
import eu.siacs.conversations.services.BarcodeProvider;
|
||||
import eu.siacs.conversations.services.EmojiService;
|
||||
import eu.siacs.conversations.services.QuickConversationsService;
|
||||
import eu.siacs.conversations.services.UpdateService;
|
||||
import eu.siacs.conversations.services.XmppConnectionService;
|
||||
import eu.siacs.conversations.services.XmppConnectionService.XmppConnectionBinder;
|
||||
import eu.siacs.conversations.ui.util.AvatarWorkerTask;
|
||||
|
@ -507,6 +506,7 @@ public abstract class XmppActivity extends ActionBarActivity {
|
|||
public boolean unicoloredBG() {
|
||||
return getBooleanPreference("unicolored_chatbg", R.bool.use_unicolored_chatbg) || getPreferences().getString(SettingsActivity.THEME, getString(R.string.theme)).equals("black");
|
||||
}
|
||||
|
||||
public boolean enableOTR() {
|
||||
return getBooleanPreference(ENABLE_OTR_ENCRYPTION, R.bool.enable_otr);
|
||||
}
|
||||
|
@ -1457,44 +1457,7 @@ public abstract class XmppActivity extends ActionBarActivity {
|
|||
return installFromUnknownSource;
|
||||
}
|
||||
|
||||
protected void openInstallFromUnknownSourcesDialogIfNeeded(boolean showToast) {
|
||||
String ShowToast;
|
||||
if (showToast == true) {
|
||||
ShowToast = "true";
|
||||
} else {
|
||||
ShowToast = "false";
|
||||
}
|
||||
if (!installFromUnknownSourceAllowed() && xmppConnectionService.installedFrom() == null) {
|
||||
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
builder.setTitle(R.string.install_from_unknown_sources_disabled);
|
||||
builder.setMessage(R.string.install_from_unknown_sources_disabled_dialog);
|
||||
builder.setPositiveButton(R.string.next, (dialog, which) -> {
|
||||
Intent intent;
|
||||
if (android.os.Build.VERSION.SDK_INT >= 26) {
|
||||
intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES);
|
||||
Uri uri = Uri.parse("package:" + getPackageName());
|
||||
intent.setData(uri);
|
||||
} else {
|
||||
intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
|
||||
}
|
||||
Log.d(Config.LOGTAG, "Allow install from unknown sources for Android SDK " + Build.VERSION.SDK_INT + " intent " + intent.toString());
|
||||
try {
|
||||
startActivityForResult(intent, REQUEST_UNKNOWN_SOURCE_OP);
|
||||
} catch (ActivityNotFoundException e) {
|
||||
ToastCompat.makeText(XmppActivity.this, R.string.device_does_not_support_unknown_source_op, ToastCompat.LENGTH_SHORT).show();
|
||||
} finally {
|
||||
UpdateService task = new UpdateService(this, xmppConnectionService.installedFrom(), xmppConnectionService);
|
||||
task.executeOnExecutor(UpdateService.THREAD_POOL_EXECUTOR, ShowToast);
|
||||
Log.d(Config.LOGTAG, "AppUpdater started");
|
||||
}
|
||||
});
|
||||
builder.create().show();
|
||||
} else {
|
||||
UpdateService task = new UpdateService(this, xmppConnectionService.installedFrom(), xmppConnectionService);
|
||||
task.executeOnExecutor(UpdateService.THREAD_POOL_EXECUTOR, ShowToast);
|
||||
Log.d(Config.LOGTAG, "AppUpdater started");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void ShowAvatarPopup(final Activity activity, final AvatarService.Avatarable user) {
|
||||
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
|
||||
|
|
Loading…
Add table
Reference in a new issue