Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f722214d authored by Jorge Ruesga's avatar Jorge Ruesga
Browse files

CMFM: Allow restrict access to secondary users

Change-Id: I1bffbb32a5f02ab4ca5cdcdbc21d40c7bb299933
JIRA: CYAN-1630
Issue: https://jira.cyanogenmod.org/browse/CYAN-1630


Signed-off-by: default avatarJorge Ruesga <jorge@ruesga.com>
parent b6d3b71b
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -645,6 +645,10 @@
    <string name="pref_access_mode_root">Root Access mode</string>
    <!-- Preferences - General - Root access mode summary -->
    <string name="pref_access_mode_root_summary">Root Access mode\n\nWarning! This mode allows operations that could break your device. It\'s your responsibility to ensure that an operation is safe</string>
    <!-- Preferences - General - Restrict secondary users access title -->
    <string name="pref_restrict_secondary_users_access_title">Restrict users access</string>
    <!-- Preferences - General - Restrict secondary users access summary -->
    <string name="pref_restrict_secondary_users_access_summary">Restrict access to the whole system to secondary users</string>
    <!-- Preferences - Search - Results category -->
    <string name="pref_search_results_category">Results</string>
    <!-- Preferences - Search - Show relevance widget -->
+8 −0
Original line number Diff line number Diff line
@@ -88,6 +88,14 @@
        android:defaultValue="0"
        android:persistent="true" />

      <!-- Restrict secondary users access -->
      <CheckBoxPreference
        android:key="cm_filemanager_restrict_secondary_users_access"
        android:title="@string/pref_restrict_secondary_users_access_title"
        android:summary="@string/pref_restrict_secondary_users_access_summary"
        android:persistent="false"
        android:defaultValue="false" />

      <!-- Capture debug traces -->
      <CheckBoxPreference
        android:key="cm_filemanager_show_debug_traces"
+36 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import com.cyanogenmod.filemanager.preferences.Preferences;
import com.cyanogenmod.filemanager.ui.ThemeManager;
import com.cyanogenmod.filemanager.ui.ThemeManager.Theme;
import com.cyanogenmod.filemanager.util.AIDHelper;
import com.cyanogenmod.filemanager.util.AndroidHelper;
import com.cyanogenmod.filemanager.util.MimeTypeHelper;

import java.io.File;
@@ -438,6 +439,41 @@ public final class FileManagerApplication extends Application {
        return mode;
    }

    public static boolean isRestrictSecondaryUsersAccess(Context context) {
        String value = Preferences.getWorldReadableProperties(
                context, FileManagerSettings.SETTINGS_RESTRICT_SECONDARY_USERS_ACCESS.getId());
        if (value == null) {
            value = String.valueOf(FileManagerSettings.SETTINGS_RESTRICT_SECONDARY_USERS_ACCESS.
                    getDefaultValue());
        }
        return Boolean.parseBoolean(value);
    }

    public static boolean checkRestrictSecondaryUsersAccess(Context context, boolean isChroot) {
        if (!AndroidHelper.isSecondaryUser(context)) {
            return true;
        }
        boolean needChroot = !isChroot && isRestrictSecondaryUsersAccess(context);
        if (!needChroot) {
            return true;
        }

        try {
            Preferences.savePreference(
                    FileManagerSettings.SETTINGS_ACCESS_MODE, AccessMode.SAFE, true);
        } catch (Throwable ex) {
            Log.w(TAG, "can't save console preference", ex); //$NON-NLS-1$
        }
        ConsoleBuilder.changeToNonPrivilegedConsole(context);

        // Notify the change
        Intent intent = new Intent(FileManagerSettings.INTENT_SETTING_CHANGED);
        intent.putExtra(FileManagerSettings.EXTRA_SETTING_CHANGED_KEY,
                FileManagerSettings.SETTINGS_ACCESS_MODE.getId());
        context.sendBroadcast(intent);
        return false;
    }

    /**
     * Method that reads the system properties
     */
+26 −0
Original line number Diff line number Diff line
@@ -242,6 +242,22 @@ public class NavigationActivity extends Activity
                            }
                        }

                        // Restricted access
                        if (key.compareTo(FileManagerSettings.
                                SETTINGS_RESTRICT_SECONDARY_USERS_ACCESS.getId()) == 0) {
                            if (AndroidHelper.isSecondaryUser(context)) {
                                try {
                                    Preferences.savePreference(
                                            FileManagerSettings.SETTINGS_ACCESS_MODE,
                                            AccessMode.SAFE, true);
                                } catch (Throwable ex) {
                                    Log.w(TAG, "can't save console preference", ex); //$NON-NLS-1$
                                }
                                ConsoleBuilder.changeToNonPrivilegedConsole(context);
                                createChRooted();
                            }
                        }

                        // Filetime format mode
                        if (key.compareTo(FileManagerSettings.
                                SETTINGS_FILETIME_FORMAT_MODE.getId()) == 0) {
@@ -423,6 +439,16 @@ public class NavigationActivity extends Activity
        super.onCreate(state);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Check restrictions
        if (!FileManagerApplication.checkRestrictSecondaryUsersAccess(this, mChRooted)) {
            return;
        }
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
+43 −3
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.Preference.OnPreferenceChangeListener;
import android.util.Log;

@@ -33,6 +34,7 @@ import com.cyanogenmod.filemanager.preferences.AccessMode;
import com.cyanogenmod.filemanager.preferences.FileManagerSettings;
import com.cyanogenmod.filemanager.preferences.ObjectStringIdentifier;
import com.cyanogenmod.filemanager.preferences.Preferences;
import com.cyanogenmod.filemanager.util.AndroidHelper;

/**
 * A class that manages the commons options of the application
@@ -50,6 +52,7 @@ public class GeneralPreferenceFragment extends TitlePreferenceFragment {
    private CheckBoxPreference mDisplayThumbs;
    private CheckBoxPreference mUseFlinger;
    private ListPreference mAccessMode;
    private CheckBoxPreference mRestrictSecondaryUsersAccess;
    private CheckBoxPreference mDebugTraces;

    /**
@@ -62,6 +65,7 @@ public class GeneralPreferenceFragment extends TitlePreferenceFragment {
        @Override
        public boolean onPreferenceChange(final Preference preference, Object newValue) {
            boolean ret = true;
            boolean notify = false;

            String key = preference.getKey();
            if (DEBUG) {
@@ -120,9 +124,21 @@ public class GeneralPreferenceFragment extends TitlePreferenceFragment {
                                    preference.setSummary(summary[valueId]);
            }

            // Restricted secondary users access
            else if (FileManagerSettings.SETTINGS_RESTRICT_SECONDARY_USERS_ACCESS.getId().
                    compareTo(key) == 0) {
                String value = String.valueOf(newValue);
                if (Preferences.writeWorldReadableProperty(getActivity(), key, value)) {
                    ((CheckBoxPreference) preference).setChecked((Boolean) newValue);
                    updateAccessModeStatus();
                    notify = true;
                }
                ret = false;
            }

            // Notify the change (only if fragment is loaded. Default values are loaded
            // while not in loaded mode)
            if (GeneralPreferenceFragment.this.mLoaded && ret) {
            if (GeneralPreferenceFragment.this.mLoaded && (ret || notify)) {
                Intent intent = new Intent(FileManagerSettings.INTENT_SETTING_CHANGED);
                intent.putExtra(
                        FileManagerSettings.EXTRA_SETTING_CHANGED_KEY, preference.getKey());
@@ -206,8 +222,24 @@ public class GeneralPreferenceFragment extends TitlePreferenceFragment {
                            FileManagerSettings.SETTINGS_ACCESS_MODE.getId(),
                            defaultValue);
        this.mOnChangeListener.onPreferenceChange(this.mAccessMode, value);
        // If device is not rooted, this setting cannot be changed
        this.mAccessMode.setEnabled(FileManagerApplication.isDeviceRooted());
        updateAccessModeStatus();

        // Capture Debug traces
        this.mRestrictSecondaryUsersAccess =
                (CheckBoxPreference)findPreference(
                        FileManagerSettings.SETTINGS_RESTRICT_SECONDARY_USERS_ACCESS.getId());
        if (!AndroidHelper.hasSupportForMultipleUsers(getActivity()) ||
                AndroidHelper.isSecondaryUser(getActivity())) {
            // Remove if device doesn't support multiple users accounts or the current user
            // is a secondary user
            PreferenceCategory category = (PreferenceCategory) findPreference(
                    "general_advanced_settings");
            category.removePreference(this.mRestrictSecondaryUsersAccess);
        } else {
            this.mRestrictSecondaryUsersAccess.setChecked(
                    FileManagerApplication.isRestrictSecondaryUsersAccess(getActivity()));
            this.mRestrictSecondaryUsersAccess.setOnPreferenceChangeListener(this.mOnChangeListener);
        }

        // Capture Debug traces
        this.mDebugTraces =
@@ -219,6 +251,14 @@ public class GeneralPreferenceFragment extends TitlePreferenceFragment {
        this.mLoaded = true;
    }

    private void updateAccessModeStatus() {
        // If device is not rooted, or is a restricted user, this setting cannot be changed
        final Context context = getActivity();
        boolean restrictedAccess = AndroidHelper.isSecondaryUser(context) &&
                FileManagerApplication.isRestrictSecondaryUsersAccess(context);
        this.mAccessMode.setEnabled(FileManagerApplication.isDeviceRooted() && !restrictedAccess);
    }

    /**
     * {@inheritDoc}
     */
Loading