From ce79fe2c6f44ea1184094db3405f5d630ba1ef6a Mon Sep 17 00:00:00 2001 From: jruesga Date: Sat, 6 Oct 2012 21:45:15 +0200 Subject: [PATCH] Implemented Allow Console Selection setting This setting allow use the menu "Select Console" or deactivate the menu using only a privileged console, without additional privileged messages, but require su to run the app. By default, allow not privileged console to avoid startup problems --- res/xml/preferences_general.xml | 2 +- .../cyanogenmod/explorer/ExplorerApplication.java | 17 +++++++ .../explorer/activities/NavigationActivity.java | 10 +++++ .../preferences/SettingsPreferences.java | 2 +- .../explorer/console/ConsoleBuilder.java | 52 ++++++++++++++++------ .../explorer/preferences/ExplorerSettings.java | 2 +- .../explorer/ui/widgets/NavigationView.java | 19 ++++++++ .../cyanogenmod/explorer/util/ExceptionUtil.java | 12 +++-- 8 files changed, 96 insertions(+), 20 deletions(-) diff --git a/res/xml/preferences_general.xml b/res/xml/preferences_general.xml index 6f0efab..84477fc 100644 --- a/res/xml/preferences_general.xml +++ b/res/xml/preferences_general.xml @@ -72,7 +72,7 @@ android:summaryOn="@string/pref_allow_console_selection_on" android:summaryOff="@string/pref_allow_console_selection_off" android:persistent="true" - android:defaultValue="false" /> + android:defaultValue="true" /> \ No newline at end of file diff --git a/src/com/cyanogenmod/explorer/ExplorerApplication.java b/src/com/cyanogenmod/explorer/ExplorerApplication.java index 0c79be5..5f5c56b 100644 --- a/src/com/cyanogenmod/explorer/ExplorerApplication.java +++ b/src/com/cyanogenmod/explorer/ExplorerApplication.java @@ -77,6 +77,23 @@ public final class ExplorerApplication extends Application { c.reloadTrace(); } } catch (Throwable _throw) {/**NON BLOCK**/} + } else if (key != null && key.compareTo( + ExplorerSettings.SETTINGS_ALLOW_CONSOLE_SELECTION.getId()) == 0) { + // Force to change to a privileged console. + boolean allowConsoleSelection = Preferences.getSharedPreferences().getBoolean( + ExplorerSettings.SETTINGS_ALLOW_CONSOLE_SELECTION.getId(), + ((Boolean)ExplorerSettings. + SETTINGS_ALLOW_CONSOLE_SELECTION. + getDefaultValue()).booleanValue()); + if (!allowConsoleSelection) { + ConsoleBuilder.changeToPrivilegedConsole(context); + try { + Preferences.savePreference( + ExplorerSettings.SETTINGS_SUPERUSER_MODE, Boolean.TRUE, true); + } catch (Throwable ex) { + Log.w(TAG, "Can't save console preference", ex); //$NON-NLS-1$ + } + } } } } diff --git a/src/com/cyanogenmod/explorer/activities/NavigationActivity.java b/src/com/cyanogenmod/explorer/activities/NavigationActivity.java index 11c3f8d..90f858b 100644 --- a/src/com/cyanogenmod/explorer/activities/NavigationActivity.java +++ b/src/com/cyanogenmod/explorer/activities/NavigationActivity.java @@ -494,6 +494,7 @@ public class NavigationActivity extends Activity NavigationActivity.this, R.string.msgs_cant_create_console, Toast.LENGTH_LONG); finish(); + return; } //Is necessary navigate? @@ -906,6 +907,15 @@ public class NavigationActivity extends Activity } } + // Check if console selection is allowed + boolean allowConsoleSelection = Preferences.getSharedPreferences().getBoolean( + ExplorerSettings.SETTINGS_ALLOW_CONSOLE_SELECTION.getId(), + ((Boolean)ExplorerSettings. + SETTINGS_ALLOW_CONSOLE_SELECTION.getDefaultValue()).booleanValue()); + if (!allowConsoleSelection) { + menu.removeItem(R.id.mnu_console); + } + //Hide separator if (!hasActionBarMenus) { menu.removeItem(menu.getItem(this.mActionBar.getChildCount() - removed).getItemId()); diff --git a/src/com/cyanogenmod/explorer/activities/preferences/SettingsPreferences.java b/src/com/cyanogenmod/explorer/activities/preferences/SettingsPreferences.java index 1e2bbb6..26f920c 100644 --- a/src/com/cyanogenmod/explorer/activities/preferences/SettingsPreferences.java +++ b/src/com/cyanogenmod/explorer/activities/preferences/SettingsPreferences.java @@ -65,7 +65,7 @@ public class SettingsPreferences extends PreferenceActivity { * Method that initializes the titlebar of the activity. */ private void initTitleActionBar() { - //Configure the action bar options + //Configure the action bar options getActionBar().setBackgroundDrawable( getResources().getDrawable(R.drawable.bg_holo_titlebar)); getActionBar().setDisplayOptions( diff --git a/src/com/cyanogenmod/explorer/console/ConsoleBuilder.java b/src/com/cyanogenmod/explorer/console/ConsoleBuilder.java index f958cb7..511e564 100644 --- a/src/com/cyanogenmod/explorer/console/ConsoleBuilder.java +++ b/src/com/cyanogenmod/explorer/console/ConsoleBuilder.java @@ -153,6 +153,7 @@ public final class ConsoleBuilder { return sHolder.getConsole() instanceof PrivilegedConsole; } catch (Throwable e) { + destroyConsole(); if (holder != null) { holder.dispose(); } @@ -184,6 +185,21 @@ public final class ConsoleBuilder { ExplorerSettings.SETTINGS_SUPERUSER_MODE.getId(), ((Boolean)ExplorerSettings.SETTINGS_SUPERUSER_MODE. getDefaultValue()).booleanValue()); + boolean allowConsoleSelection = Preferences.getSharedPreferences().getBoolean( + ExplorerSettings.SETTINGS_ALLOW_CONSOLE_SELECTION.getId(), + ((Boolean)ExplorerSettings. + SETTINGS_ALLOW_CONSOLE_SELECTION. + getDefaultValue()).booleanValue()); + if (!requiredSuConsole && !allowConsoleSelection) { + // allowConsoleSelection forces the su console + try { + Preferences.savePreference( + ExplorerSettings.SETTINGS_SUPERUSER_MODE, Boolean.TRUE, true); + } catch (Throwable ex) { + Log.w(TAG, "Can't save console preference", ex); //$NON-NLS-1$ + } + requiredSuConsole = true; + } //Check if console settings has changed if (sHolder != null) { @@ -308,21 +324,31 @@ public final class ConsoleBuilder { } } - //Save settings - try { - Editor editor = Preferences.getSharedPreferences().edit(); - editor.putBoolean(ExplorerSettings.SETTINGS_SUPERUSER_MODE.getId(), false); - editor.commit(); - } catch (Exception ex) { - Log.e(TAG, - String.format("Failed to save %s property", //$NON-NLS-1$ - ExplorerSettings.SETTINGS_SUPERUSER_MODE.getId()), ex); + boolean allowConsoleSelection = Preferences.getSharedPreferences().getBoolean( + ExplorerSettings.SETTINGS_ALLOW_CONSOLE_SELECTION.getId(), + ((Boolean)ExplorerSettings. + SETTINGS_ALLOW_CONSOLE_SELECTION. + getDefaultValue()).booleanValue()); + if (allowConsoleSelection) { + //Save settings + try { + Editor editor = Preferences.getSharedPreferences().edit(); + editor.putBoolean(ExplorerSettings.SETTINGS_SUPERUSER_MODE.getId(), false); + editor.commit(); + } catch (Exception ex) { + Log.e(TAG, + String.format("Failed to save %s property", //$NON-NLS-1$ + ExplorerSettings.SETTINGS_SUPERUSER_MODE.getId()), ex); + } + + //Create the non-privileged console + NonPriviledgeConsole console = new NonPriviledgeConsole(initialDirectory); + console.alloc(); + return console; } - //Create the non-privileged console - NonPriviledgeConsole console = new NonPriviledgeConsole(initialDirectory); - console.alloc(); - return console; + // Rethrow the exception + throw caEx; } } diff --git a/src/com/cyanogenmod/explorer/preferences/ExplorerSettings.java b/src/com/cyanogenmod/explorer/preferences/ExplorerSettings.java index 5040ce9..2926dfe 100644 --- a/src/com/cyanogenmod/explorer/preferences/ExplorerSettings.java +++ b/src/com/cyanogenmod/explorer/preferences/ExplorerSettings.java @@ -94,7 +94,7 @@ public enum ExplorerSettings { * @hide */ SETTINGS_ALLOW_CONSOLE_SELECTION( - "cm_explorer_allow_console_selection", Boolean.FALSE), //$NON-NLS-1$ + "cm_explorer_allow_console_selection", Boolean.TRUE), //$NON-NLS-1$ /** * When to highlight the terms of the search in the search results diff --git a/src/com/cyanogenmod/explorer/ui/widgets/NavigationView.java b/src/com/cyanogenmod/explorer/ui/widgets/NavigationView.java index e67b4c0..7e479e1 100644 --- a/src/com/cyanogenmod/explorer/ui/widgets/NavigationView.java +++ b/src/com/cyanogenmod/explorer/ui/widgets/NavigationView.java @@ -16,6 +16,7 @@ package com.cyanogenmod.explorer.ui.widgets; +import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.AsyncTask; @@ -25,11 +26,13 @@ import android.view.View; import android.widget.AdapterView; import android.widget.ListAdapter; import android.widget.RelativeLayout; +import android.widget.Toast; import com.cyanogenmod.explorer.R; import com.cyanogenmod.explorer.adapters.FileSystemObjectAdapter; import com.cyanogenmod.explorer.adapters.FileSystemObjectAdapter.OnRequestMenuListener; import com.cyanogenmod.explorer.adapters.FileSystemObjectAdapter.OnSelectionChangedListener; +import com.cyanogenmod.explorer.console.ConsoleAllocException; import com.cyanogenmod.explorer.listeners.OnHistoryListener; import com.cyanogenmod.explorer.listeners.OnRequestRefreshListener; import com.cyanogenmod.explorer.listeners.OnSelectionListener; @@ -47,6 +50,7 @@ import com.cyanogenmod.explorer.preferences.ObjectStringIdentifier; import com.cyanogenmod.explorer.preferences.Preferences; import com.cyanogenmod.explorer.ui.policy.ActionsPolicy; import com.cyanogenmod.explorer.util.CommandHelper; +import com.cyanogenmod.explorer.util.DialogHelper; import com.cyanogenmod.explorer.util.ExceptionUtil; import com.cyanogenmod.explorer.util.FileHelper; @@ -561,6 +565,21 @@ public class NavigationView extends RelativeLayout implements files = CommandHelper.listFiles(getContext(), newDir, null); } return files; + } catch (final ConsoleAllocException e) { + //Show exception and exists + NavigationView.this.mTitle.post(new Runnable() { + @Override + public void run() { + Context ctx = getContext(); + Log.e(TAG, ctx.getString( + R.string.msgs_cant_create_console), e); + DialogHelper.showToast(ctx, + R.string.msgs_cant_create_console, + Toast.LENGTH_LONG); + ((Activity)ctx).finish(); + } + }); + return null; } catch (Exception ex) { //End of loading data diff --git a/src/com/cyanogenmod/explorer/util/ExceptionUtil.java b/src/com/cyanogenmod/explorer/util/ExceptionUtil.java index 9f561a4..7b516ef 100644 --- a/src/com/cyanogenmod/explorer/util/ExceptionUtil.java +++ b/src/com/cyanogenmod/explorer/util/ExceptionUtil.java @@ -209,10 +209,14 @@ public final class ExceptionUtil { ((Activity)context).runOnUiThread(new Runnable() { @Override public void run() { - if (fToast) { - DialogHelper.showToast(context, fMsgResId, Toast.LENGTH_SHORT); - } else { - DialogHelper.createErrorDialog(context, fMsgResId).show(); + try { + if (fToast) { + DialogHelper.showToast(context, fMsgResId, Toast.LENGTH_SHORT); + } else { + DialogHelper.createErrorDialog(context, fMsgResId).show(); + } + } catch (Exception e) { + Log.e(context.getClass().getSimpleName(), "ExceptionUtil. Failed to show dialog", ex); //$NON-NLS-1$ } } }); -- 2.11.0