From e35610c88df1869059eb2a9ef0fb6093274e1421 Mon Sep 17 00:00:00 2001 From: Christian Schneppe Date: Sat, 17 Feb 2018 21:10:33 +0100 Subject: show hint to allow installs from unknown sources just before update starts and don't show this for PlayStore installs --- .../pixart/messenger/services/UpdateService.java | 10 ++- .../pixart/messenger/ui/ConversationActivity.java | 75 +++------------------- .../de/pixart/messenger/ui/UpdaterActivity.java | 14 ++-- .../java/de/pixart/messenger/ui/XmppActivity.java | 72 +++++++++++++++++++-- 4 files changed, 95 insertions(+), 76 deletions(-) diff --git a/src/main/java/de/pixart/messenger/services/UpdateService.java b/src/main/java/de/pixart/messenger/services/UpdateService.java index d4cf2afef..bc5ce90e1 100644 --- a/src/main/java/de/pixart/messenger/services/UpdateService.java +++ b/src/main/java/de/pixart/messenger/services/UpdateService.java @@ -33,9 +33,11 @@ public class UpdateService extends AsyncTask= 1) { Log.d(Config.LOGTAG, "AppUpdater: Version " + ownVersion + " should be updated to " + version); UpdateAvailable = true; - showNotification(url, changelog, version, filesize); + showNotification(url, changelog, version, filesize, playstore); } else { Log.d(Config.LOGTAG, "AppUpdater: Version " + ownVersion + " is up to date"); UpdateAvailable = false; + showNotification(url, changelog, version, filesize, playstore); } } } catch (JSONException e) { @@ -141,11 +144,12 @@ public class UpdateService extends AsyncTask= 26) { - /* - * On Android 8 with applications targeting lower versions, - * it's impossible to check unknown sources enabled: using old APIs will always return true - * and using the new one will always return false, - * so in order to avoid a stuck dialog that can't be bypassed we will assume true. - */ - installFromUnknownSource = this.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.O - || packageManager.canRequestPackageInstalls(); - } else if (Build.VERSION.SDK_INT >= 17 && Build.VERSION.SDK_INT < 26) { - try { - isUnknownAllowed = Settings.Global.getInt(this.getApplicationContext().getContentResolver(), Settings.Global.INSTALL_NON_MARKET_APPS); - } catch (Settings.SettingNotFoundException e) { - isUnknownAllowed = 0; - e.printStackTrace(); - } - installFromUnknownSource = isUnknownAllowed == 1; - } else { - try { - isUnknownAllowed = Settings.Secure.getInt(this.getApplicationContext().getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS); - } catch (Settings.SettingNotFoundException e) { - isUnknownAllowed = 0; - e.printStackTrace(); - } - installFromUnknownSource = isUnknownAllowed == 1; - } - Log.d(Config.LOGTAG, "Install from unknown sources for Android SDK " + Build.VERSION.SDK_INT + " allowed: " + installFromUnknownSource); - - if (!installFromUnknownSource) { - 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 = null; - 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) { - Toast.makeText(ConversationActivity.this, R.string.device_does_not_support_battery_op, Toast.LENGTH_SHORT).show(); - } - }); - builder.create().show(); - } - } - private boolean hasAccountWithoutPush() { for (Account account : xmppConnectionService.getAccounts()) { if (account.getStatus() == Account.State.ONLINE && !xmppConnectionService.getPushManagementService().available(account)) { diff --git a/src/main/java/de/pixart/messenger/ui/UpdaterActivity.java b/src/main/java/de/pixart/messenger/ui/UpdaterActivity.java index 7cc330155..c77d7f16a 100644 --- a/src/main/java/de/pixart/messenger/ui/UpdaterActivity.java +++ b/src/main/java/de/pixart/messenger/ui/UpdaterActivity.java @@ -40,6 +40,7 @@ public class UpdaterActivity extends XmppActivity { String appURI = ""; String changelog = ""; Integer filesize = 0; + boolean playstore = false; ProgressDialog mProgressDialog; DownloadTask downloadTask; @Override @@ -87,6 +88,11 @@ public class UpdaterActivity extends XmppActivity { Toast.makeText(getApplicationContext(), getText(R.string.failed), Toast.LENGTH_LONG).show(); UpdaterActivity.this.finish(); } + try { + playstore = getIntent().getBooleanExtra("playstore", false); + } catch (Exception e) { + playstore = false; + } //delete old downloaded localVersion files File dir = new File(FileBackend.getConversationsDirectory("Update", false)); if (dir.isDirectory()) { @@ -108,8 +114,8 @@ public class UpdaterActivity extends XmppActivity { //ask for permissions on devices >= SDK 23 if (isStoragePermissionGranted() && isNetworkAvailable(getApplicationContext())) { //start downloading the file using the download manager - if (xmppConnectionService.installedFromPlayStore()) { - Uri uri = Uri.parse("market://details?id=de.pixart.openmessenger"); + if (playstore) { + Uri uri = Uri.parse("market://details?id=de.pixart.messenger"); Intent marketIntent = new Intent(Intent.ACTION_VIEW, uri); PackageManager manager = getApplicationContext().getPackageManager(); List infos = manager.queryIntentActivities(marketIntent, 0); @@ -352,7 +358,7 @@ public class UpdaterActivity extends XmppActivity { @Override public void onPause() { super.onPause(); - if (!downloadTask.getStatus().equals(AsyncTask.Status.FINISHED)) { + if (downloadTask != null && !downloadTask.getStatus().equals(AsyncTask.Status.FINISHED)) { downloadTask.cancel(true); } UpdaterActivity.this.finish(); @@ -361,7 +367,7 @@ public class UpdaterActivity extends XmppActivity { @Override protected void onStop() { super.onStop(); - if (!downloadTask.getStatus().equals(AsyncTask.Status.FINISHED)) { + if (downloadTask != null && !downloadTask.getStatus().equals(AsyncTask.Status.FINISHED)) { downloadTask.cancel(true); } UpdaterActivity.this.finish(); diff --git a/src/main/java/de/pixart/messenger/ui/XmppActivity.java b/src/main/java/de/pixart/messenger/ui/XmppActivity.java index 4320c4a17..fa6a2d739 100644 --- a/src/main/java/de/pixart/messenger/ui/XmppActivity.java +++ b/src/main/java/de/pixart/messenger/ui/XmppActivity.java @@ -37,9 +37,11 @@ import android.os.IBinder; import android.os.PowerManager; import android.os.SystemClock; import android.preference.PreferenceManager; +import android.provider.Settings; import android.support.v4.content.ContextCompat; import android.text.InputType; import android.util.DisplayMetrics; +import android.util.Log; import android.util.Pair; import android.view.MenuItem; import android.view.View; @@ -68,7 +70,6 @@ import de.pixart.messenger.entities.Conversation; import de.pixart.messenger.entities.Message; import de.pixart.messenger.entities.MucOptions; import de.pixart.messenger.entities.Presences; -import de.pixart.messenger.http.HttpConnectionManager; import de.pixart.messenger.services.AvatarService; import de.pixart.messenger.services.BarcodeProvider; import de.pixart.messenger.services.UpdateService; @@ -93,7 +94,6 @@ public abstract class XmppActivity extends Activity { public static final String EXTRA_ACCOUNT = "account"; public XmppConnectionService xmppConnectionService; - public HttpConnectionManager mHttpConnectionManager; public boolean xmppConnectionServiceBound = false; protected boolean registeredListeners = false; @@ -384,8 +384,13 @@ public abstract class XmppActivity extends Activity { break; case R.id.action_check_updates: if (xmppConnectionService.hasInternetConnection()) { - UpdateService task = new UpdateService(this); - task.executeOnExecutor(UpdateService.THREAD_POOL_EXECUTOR, "true"); + if (!installFromUnknownSourceAllowed() && !xmppConnectionService.installedFromPlayStore()) { + openInstallFromUnknownSourcesDialogIfNeeded(); + } else { + UpdateService task = new UpdateService(this, xmppConnectionService.installedFromPlayStore()); + task.executeOnExecutor(UpdateService.THREAD_POOL_EXECUTOR, "true"); + Log.d(Config.LOGTAG, "AppUpdater started"); + } } else { Toast.makeText(this, R.string.account_status_no_internet, Toast.LENGTH_LONG).show(); } @@ -1224,4 +1229,63 @@ public abstract class XmppActivity extends Activity { return bitmapWorkerTaskReference.get(); } } + + protected boolean installFromUnknownSourceAllowed() { + boolean installFromUnknownSource = false; + final PackageManager packageManager = this.getPackageManager(); + int isUnknownAllowed = 0; + if (Build.VERSION.SDK_INT >= 26) { + /* + * On Android 8 with applications targeting lower versions, + * it's impossible to check unknown sources enabled: using old APIs will always return true + * and using the new one will always return false, + * so in order to avoid a stuck dialog that can't be bypassed we will assume true. + */ + installFromUnknownSource = this.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.O + || packageManager.canRequestPackageInstalls(); + } else if (Build.VERSION.SDK_INT >= 17 && Build.VERSION.SDK_INT < 26) { + try { + isUnknownAllowed = Settings.Global.getInt(this.getApplicationContext().getContentResolver(), Settings.Global.INSTALL_NON_MARKET_APPS); + } catch (Settings.SettingNotFoundException e) { + isUnknownAllowed = 0; + e.printStackTrace(); + } + installFromUnknownSource = isUnknownAllowed == 1; + } else { + try { + isUnknownAllowed = Settings.Secure.getInt(this.getApplicationContext().getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS); + } catch (Settings.SettingNotFoundException e) { + isUnknownAllowed = 0; + e.printStackTrace(); + } + installFromUnknownSource = isUnknownAllowed == 1; + } + Log.d(Config.LOGTAG, "Install from unknown sources for Android SDK " + Build.VERSION.SDK_INT + " allowed: " + installFromUnknownSource); + return installFromUnknownSource; + } + + protected void openInstallFromUnknownSourcesDialogIfNeeded() { + if (!installFromUnknownSourceAllowed()) { + 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 = null; + 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) { + Toast.makeText(XmppActivity.this, R.string.device_does_not_support_battery_op, Toast.LENGTH_SHORT).show(); + } + }); + builder.create().show(); + } + } } -- cgit v1.2.3