Better error handling

This commit is contained in:
Dominik Schürmann 2015-09-28 01:20:56 +02:00
parent 50215356ae
commit 2b0f2cfc67
5 changed files with 97 additions and 24 deletions

View File

@ -259,6 +259,8 @@
<string name="uninstall_error_unknown">Failed to uninstall due to an unknown error</string>
<string name="system_install_denied_title">F-Droid Privileged Extension is not available</string>
<string name="system_install_denied_body">This option is only available when F-Droid Privileged Extension is installed.</string>
<string name="system_install_denied_signature">The signature of the extension is wrong! Please create a bug report!</string>
<string name="system_install_denied_permissions">The privileged permissions have not been granted to the extension! Please create a bug report!</string>
<string name="system_install_button_install">Install</string>
<string name="system_install_button_open">Open Extension</string>
<string name="system_install_first_time_notification">Install F-Droid Privileged Extension?</string>
@ -276,7 +278,6 @@
<string name="system_install_warning_lollipop">This takes up to 10 seconds and the device will be &lt;b>rebooted&lt;/b> afterwards.</string>
<string name="system_install_first_time_message">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.</string>
<string name="system_uninstall">Do you want to uninstall F-Droid Privileged Extension?</string>
<string name="system_uninstall_message">This will uninstall F-Droid Privileged Extension.</string>
<string name="system_uninstall_button">Uninstall</string>
<string name="app_description">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.</string>

View File

@ -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 {

View File

@ -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

View File

@ -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) + "<br/><br/>"
+ InstallExtension.create(getApplicationContext()).getWarningString();
String message = InstallExtension.create(getApplicationContext()).getWarningString();
AlertDialog.Builder builder = new AlertDialog.Builder(theme)
.setTitle(R.string.system_uninstall)

View File

@ -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) +
"<br/><br/>" + 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) +
"<br/><br/>" + 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