diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-02-17 21:10:33 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-02-17 21:10:33 +0100 |
commit | e35610c88df1869059eb2a9ef0fb6093274e1421 (patch) | |
tree | 219dabe0ba9dfa88f909fd5857f8c8bdfd62ac88 /src/main/java/de | |
parent | 3bb696a5d6c057b6d5df178fa97722680c10580b (diff) |
show hint to allow installs from unknown sources just before update starts and don't show this for PlayStore installs
Diffstat (limited to 'src/main/java/de')
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<String, Object, UpdateService.Wrapp } private Context context; + private boolean playstore; - public UpdateService(Context context) { + public UpdateService(Context context, boolean PlayStore) { this.context = context; + this.playstore = PlayStore; } public class Wrapper @@ -93,10 +95,11 @@ public class UpdateService extends AsyncTask<String, Object, UpdateService.Wrapp if (checkVersion(version, ownVersion) >= 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<String, Object, UpdateService.Wrapp }); } - private void showNotification(String url, String changelog, String version, String filesize) { + private void showNotification(String url, String changelog, String version, String filesize, boolean playstore) { Intent intent = new Intent(context, UpdaterActivity.class); intent.putExtra("update", "PixArtMessenger_UpdateService"); intent.putExtra("url", url); intent.putExtra("changelog", changelog); + intent.putExtra("playstore", playstore); PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); diff --git a/src/main/java/de/pixart/messenger/ui/ConversationActivity.java b/src/main/java/de/pixart/messenger/ui/ConversationActivity.java index b461b5d6f..22be6ebb5 100644 --- a/src/main/java/de/pixart/messenger/ui/ConversationActivity.java +++ b/src/main/java/de/pixart/messenger/ui/ConversationActivity.java @@ -320,7 +320,7 @@ public class ConversationActivity extends XmppActivity return false; } - protected void AppUpdate() { + protected void AppUpdate(boolean PlayStore) { String PREFS_NAME = "UpdateTimeStamp"; SharedPreferences UpdateTimeStamp = getApplicationContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); long lastUpdateTime = UpdateTimeStamp.getLong("lastUpdateTime", 0); @@ -329,11 +329,15 @@ public class ConversationActivity extends XmppActivity lastUpdateTime = System.currentTimeMillis(); SharedPreferences.Editor editor = UpdateTimeStamp.edit(); editor.putLong("lastUpdateTime", lastUpdateTime); - editor.commit(); + editor.apply(); Log.d(Config.LOGTAG, "AppUpdater: CurrentTime: " + lastUpdateTime); - UpdateService task = new UpdateService(this); - task.executeOnExecutor(UpdateService.THREAD_POOL_EXECUTOR, "false"); - Log.d(Config.LOGTAG, "AppUpdater started"); + if (!installFromUnknownSourceAllowed() && !PlayStore) { + openInstallFromUnknownSourcesDialogIfNeeded(); + } else { + UpdateService task = new UpdateService(this, PlayStore); + task.executeOnExecutor(UpdateService.THREAD_POOL_EXECUTOR, "false"); + Log.d(Config.LOGTAG, "AppUpdater started"); + } } else { Log.d(Config.LOGTAG, "AppUpdater stopped"); return; @@ -1396,7 +1400,7 @@ public class ConversationActivity extends XmppActivity if (xmppConnectionService.hasInternetConnection()) { if (xmppConnectionService.isWIFI() || (xmppConnectionService.isMobile() && !xmppConnectionService.isMobileRoaming())) { if (!xmppConnectionService.installedFromFDroid()) { - AppUpdate(); + AppUpdate(xmppConnectionService.installedFromPlayStore()); } } } @@ -1441,9 +1445,6 @@ public class ConversationActivity extends XmppActivity if (!ExceptionHelper.checkForCrash(this, this.xmppConnectionService) && !mRedirected.get()) { openBatteryOptimizationDialogIfNeeded(); - if (!xmppConnectionService.installedFromFDroid() && !xmppConnectionService.installedFromPlayStore()) { - openInstallFromUnknownSourcesDialogIfNeeded(); - } } if (isConversationsOverviewVisable() && isConversationsOverviewHideable()) { @@ -1713,62 +1714,6 @@ public class ConversationActivity extends XmppActivity } } - private void openInstallFromUnknownSourcesDialogIfNeeded() { - final PackageManager packageManager = this.getPackageManager(); - boolean installFromUnknownSource = false; - 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); - - 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<ResolveInfo> 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(); + } + } } |