From 2b0f2cfc679caeb5f277aac2c70aee606727acd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Mon, 28 Sep 2015 01:20:56 +0200 Subject: [PATCH] Better error handling --- F-Droid/res/values/strings.xml | 3 +- .../fdroid/fdroid/installer/Installer.java | 7 +- .../fdroid/installer/PrivilegedInstaller.java | 25 ++++++-- .../InstallExtensionDialogActivity.java | 64 +++++++++++++++---- .../views/fragments/PreferencesFragment.java | 22 ++++++- 5 files changed, 97 insertions(+), 24 deletions(-) diff --git a/F-Droid/res/values/strings.xml b/F-Droid/res/values/strings.xml index be82aaecb..ee1062f18 100644 --- a/F-Droid/res/values/strings.xml +++ b/F-Droid/res/values/strings.xml @@ -259,6 +259,8 @@ Failed to uninstall due to an unknown error F-Droid Privileged Extension is not available This option is only available when F-Droid Privileged Extension is installed. + The signature of the extension is wrong! Please create a bug report! + The privileged permissions have not been granted to the extension! Please create a bug report! Install Open Extension Install F-Droid Privileged Extension? @@ -276,7 +278,6 @@ This takes up to 10 seconds and the device will be <b>rebooted</b> afterwards. Looks like you have root access on your device. You can now install F-Droid Privileged Extension, tightly coupled with the Android operating system. This allows F-Droid to install, upgrade and uninstall apps on its own. Do you want to uninstall F-Droid Privileged Extension? - This will uninstall F-Droid Privileged Extension. Uninstall F-Droid is an installable catalogue of FOSS (Free and Open Source Software) applications for the Android platform. The client makes it easy to browse, install, and keep track of updates on your device. diff --git a/F-Droid/src/org/fdroid/fdroid/installer/Installer.java b/F-Droid/src/org/fdroid/fdroid/installer/Installer.java index e6a6e3f46..847b0cf02 100644 --- a/F-Droid/src/org/fdroid/fdroid/installer/Installer.java +++ b/F-Droid/src/org/fdroid/fdroid/installer/Installer.java @@ -109,7 +109,8 @@ abstract public class Installer { // system permissions and pref enabled -> SystemInstaller boolean isSystemInstallerEnabled = Preferences.get().isPrivilegedInstallerEnabled(); if (isSystemInstallerEnabled) { - if (PrivilegedInstaller.isExtensionInstalledCorrectly(activity)) { + if (PrivilegedInstaller.isExtensionInstalledCorrectly(activity) + == PrivilegedInstaller.EXTENSION_INSTALLED_YES) { Utils.debugLog(TAG, "system permissions -> SystemInstaller"); try { @@ -154,7 +155,7 @@ abstract public class Installer { return; } - // special case: Install F-Droid Privileged + // special case: F-Droid Privileged if (packageName != null && packageName.equals(PrivilegedInstaller.PRIVILEGED_PACKAGE_NAME)) { Activity activity; try { @@ -195,7 +196,7 @@ abstract public class Installer { return; } - // special case: Install F-Droid Privileged + // special case: F-Droid Privileged if (packageName != null && packageName.equals(PrivilegedInstaller.PRIVILEGED_PACKAGE_NAME)) { Activity activity; try { diff --git a/F-Droid/src/org/fdroid/fdroid/installer/PrivilegedInstaller.java b/F-Droid/src/org/fdroid/fdroid/installer/PrivilegedInstaller.java index 8c4bc826d..cb8e8d17d 100644 --- a/F-Droid/src/org/fdroid/fdroid/installer/PrivilegedInstaller.java +++ b/F-Droid/src/org/fdroid/fdroid/installer/PrivilegedInstaller.java @@ -81,6 +81,11 @@ public class PrivilegedInstaller extends Installer { public static final int REQUEST_CONFIRM_PERMS = 0; + public static final int EXTENSION_INSTALLED_NO = 0; + public static final int EXTENSION_INSTALLED_YES = 1; + public static final int EXTENSION_INSTALLED_SIGNATURE_PROBLEM = 2; + public static final int EXTENSION_INSTALLED_PERMISSIONS_PROBLEM = 3; + public PrivilegedInstaller(Activity activity, PackageManager pm, InstallerCallback callback) throws AndroidNotCompatibleException { super(activity, pm, callback); @@ -97,11 +102,11 @@ public class PrivilegedInstaller extends Installer { } } - public static boolean isExtensionInstalledCorrectly(Context context) { + public static int isExtensionInstalledCorrectly(Context context) { // check if installed if (!isExtensionInstalled(context)) { - return false; + return EXTENSION_INSTALLED_NO; } // check if it has the privileged permissions granted @@ -128,8 +133,13 @@ public class PrivilegedInstaller extends Installer { }; Intent serviceIntent = new Intent(PRIVILEGED_SERVICE_INTENT); serviceIntent.setPackage(PRIVILEGED_PACKAGE_NAME); - context.getApplicationContext().bindService(serviceIntent, mServiceConnection, - Context.BIND_AUTO_CREATE); + + try { + context.getApplicationContext().bindService(serviceIntent, mServiceConnection, + Context.BIND_AUTO_CREATE); + } catch (SecurityException e) { + return EXTENSION_INSTALLED_SIGNATURE_PROBLEM; + } synchronized (mutex) { try { @@ -139,7 +149,12 @@ public class PrivilegedInstaller extends Installer { } } - return (returnBundle.getBoolean("has_permission", false)); + boolean hasPermissions = returnBundle.getBoolean("has_permission", false); + if (hasPermissions) { + return EXTENSION_INSTALLED_YES; + } else { + return EXTENSION_INSTALLED_PERMISSIONS_PROBLEM; + } } @Override diff --git a/F-Droid/src/org/fdroid/fdroid/privileged/install/InstallExtensionDialogActivity.java b/F-Droid/src/org/fdroid/fdroid/privileged/install/InstallExtensionDialogActivity.java index 0add420a7..6933a6843 100644 --- a/F-Droid/src/org/fdroid/fdroid/privileged/install/InstallExtensionDialogActivity.java +++ b/F-Droid/src/org/fdroid/fdroid/privileged/install/InstallExtensionDialogActivity.java @@ -96,10 +96,20 @@ public class InstallExtensionDialogActivity extends FragmentActivity { if (Preferences.get().isFirstTime()) { Preferences.get().setFirstTime(false); - if (PrivilegedInstaller.isExtensionInstalledCorrectly(context)) { - Preferences.get().setPrivilegedInstallerEnabled(true); - } else { - runFirstTime(context); + int isInstalledCorrectly = PrivilegedInstaller.isExtensionInstalledCorrectly(context); + switch (isInstalledCorrectly) { + case PrivilegedInstaller.EXTENSION_INSTALLED_YES: + Preferences.get().setPrivilegedInstallerEnabled(true); + break; + + case PrivilegedInstaller.EXTENSION_INSTALLED_NO: + runFirstTime(context); + break; + + case PrivilegedInstaller.EXTENSION_INSTALLED_PERMISSIONS_PROBLEM: + case PrivilegedInstaller.EXTENSION_INSTALLED_SIGNATURE_PROBLEM: + default: + // do nothing } } } @@ -322,18 +332,49 @@ public class InstallExtensionDialogActivity extends FragmentActivity { // hack to get theme applied (which is not automatically applied due to activity's Theme.NoDisplay ContextThemeWrapper theme = new ContextThemeWrapper(this, FDroidApp.getCurThemeResId()); - final boolean success = PrivilegedInstaller.isExtensionInstalledCorrectly(this); + int isInstalledCorrectly = + PrivilegedInstaller.isExtensionInstalledCorrectly(this); - // enable system installer on installation success - Preferences.get().setPrivilegedInstallerEnabled(success); + String title; + String message; + final int result; + switch (isInstalledCorrectly) { + case PrivilegedInstaller.EXTENSION_INSTALLED_YES: + title = getString(R.string.system_install_post_success); + message = getString(R.string.system_install_post_success_message); + result = Activity.RESULT_OK; + + // enable system installer on installation success + Preferences.get().setPrivilegedInstallerEnabled(true); + break; + case PrivilegedInstaller.EXTENSION_INSTALLED_NO: + title = getString(R.string.system_install_post_fail); + message = getString(R.string.system_install_post_fail_message); + result = Activity.RESULT_CANCELED; + break; + case PrivilegedInstaller.EXTENSION_INSTALLED_SIGNATURE_PROBLEM: + title = getString(R.string.system_install_post_fail); + message = getString(R.string.system_install_post_fail_message) + + "\n\n" + getString(R.string.system_install_denied_signature); + result = Activity.RESULT_CANCELED; + break; + case PrivilegedInstaller.EXTENSION_INSTALLED_PERMISSIONS_PROBLEM: + title = getString(R.string.system_install_post_fail); + message = getString(R.string.system_install_post_fail_message) + + "\n\n" + getString(R.string.system_install_denied_permissions); + result = Activity.RESULT_CANCELED; + break; + default: + throw new RuntimeException("unhandled return"); + } AlertDialog.Builder builder = new AlertDialog.Builder(theme) - .setTitle(success ? R.string.system_install_post_success : R.string.system_install_post_fail) - .setMessage(success ? R.string.system_install_post_success_message : R.string.system_install_post_fail_message) + .setTitle(title) + .setMessage(message) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { - InstallExtensionDialogActivity.this.setResult(success ? Activity.RESULT_OK : Activity.RESULT_CANCELED); + InstallExtensionDialogActivity.this.setResult(result); InstallExtensionDialogActivity.this.finish(); startActivity(new Intent(InstallExtensionDialogActivity.this, FDroid.class)); } @@ -349,8 +390,7 @@ public class InstallExtensionDialogActivity extends FragmentActivity { final boolean isInstalled = PrivilegedInstaller.isExtensionInstalled(this); if (isInstalled) { - String message = getString(R.string.system_uninstall_message) + "

" - + InstallExtension.create(getApplicationContext()).getWarningString(); + String message = InstallExtension.create(getApplicationContext()).getWarningString(); AlertDialog.Builder builder = new AlertDialog.Builder(theme) .setTitle(R.string.system_uninstall) diff --git a/F-Droid/src/org/fdroid/fdroid/views/fragments/PreferencesFragment.java b/F-Droid/src/org/fdroid/fdroid/views/fragments/PreferencesFragment.java index 7d3c760b9..04ee38f55 100644 --- a/F-Droid/src/org/fdroid/fdroid/views/fragments/PreferencesFragment.java +++ b/F-Droid/src/org/fdroid/fdroid/views/fragments/PreferencesFragment.java @@ -194,7 +194,9 @@ public class PreferencesFragment extends PreferenceFragment final CheckBoxPreference pref = (CheckBoxPreference) preference; if (pref.isChecked()) { - if (PrivilegedInstaller.isExtensionInstalledCorrectly(getActivity())) { + int isInstalledCorrectly = + PrivilegedInstaller.isExtensionInstalledCorrectly(getActivity()); + if (isInstalledCorrectly == PrivilegedInstaller.EXTENSION_INSTALLED_YES) { // privileged permission are granted, i.e. the extension is installed correctly SharedPreferences.Editor editor = pref.getSharedPreferences().edit(); editor.putBoolean(Preferences.PREF_PRIVILEGED_INSTALLER, true); @@ -209,8 +211,22 @@ public class PreferencesFragment extends PreferenceFragment AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity()); alertBuilder.setTitle(R.string.system_install_denied_title); - String message = getActivity().getString(R.string.system_install_denied_body) + - "

" + getActivity().getString(R.string.system_install_question); + + String message = null; + switch (isInstalledCorrectly) { + case PrivilegedInstaller.EXTENSION_INSTALLED_NO: + message = getActivity().getString(R.string.system_install_denied_body) + + "

" + getActivity().getString(R.string.system_install_question); + break; + case PrivilegedInstaller.EXTENSION_INSTALLED_SIGNATURE_PROBLEM: + message = getActivity().getString(R.string.system_install_denied_signature); + break; + case PrivilegedInstaller.EXTENSION_INSTALLED_PERMISSIONS_PROBLEM: + message = getActivity().getString(R.string.system_install_denied_permissions); + break; + default: + throw new RuntimeException("unhandled return"); + } alertBuilder.setMessage(Html.fromHtml(message)); alertBuilder.setPositiveButton(R.string.system_install_button_open, new DialogInterface.OnClickListener() { @Override