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

Commit 9c512949 authored by Steve Kondik's avatar Steve Kondik
Browse files

settings: Add developer setting for root access

Change-Id: If96219d893c0dfdcf4ad36e1cd8de3a413db0e8b
parent be2abcbd
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2012-2015 The CyanogenMod Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<resources>
    <!-- Arrays for root access capability -->
    <string-array name="root_access_entries" translatable="false">
        <item>@string/root_access_none</item>
        <item>@string/root_access_apps</item>
        <item>@string/root_access_adb</item>
        <item>@string/root_access_all</item>
    </string-array>

    <string-array name="root_access_values" translatable="false">
        <item>0</item>
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </string-array>
</resources>
+26 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2012-2015 The CyanogenMod Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <!-- Setting checkbox title for root access -->
    <string name="root_access">Root access</string>
    <string name="root_access_warning_title">Allow root access?</string>
    <string name="root_access_warning_message">Allowing apps to request root access is very dangerous and could compromise the security of your system!</string>
    <string name="root_access_none">Disabled</string>
    <string name="root_access_apps">Apps only</string>
    <string name="root_access_adb">ADB only</string>
    <string name="root_access_all">Apps and ADB</string>
</resources>
+7 −0
Original line number Diff line number Diff line
@@ -99,6 +99,13 @@
        <intent android:action="com.android.settings.action.DEMO_MODE" />
    </PreferenceScreen>

    <ListPreference
        android:key="root_access"
        android:title="@string/root_access"
        android:persistent="false"
        android:entries="@array/root_access_entries"
        android:entryValues="@array/root_access_values" />

    <PreferenceCategory android:key="debug_debugging_category"
            android:title="@string/debug_debugging_category">

+100 −0
Original line number Diff line number Diff line
@@ -189,6 +189,9 @@ public class DevelopmentSettings extends RestrictedSettingsFragment

    private static final String INACTIVE_APPS_KEY = "inactive_apps";

    private static final String ROOT_ACCESS_KEY = "root_access";
    private static final String ROOT_ACCESS_PROPERTY = "persist.sys.root_access";

    private static final String IMMEDIATELY_DESTROY_ACTIVITIES_KEY
            = "immediately_destroy_activities";
    private static final String APP_PROCESS_LIMIT_KEY = "app_process_limit";
@@ -298,6 +301,9 @@ public class DevelopmentSettings extends RestrictedSettingsFragment

    private SwitchPreference mColorTemperaturePreference;
    
    private ListPreference mRootAccess;
    private Object mSelectedRootValue;

    private final ArrayList<Preference> mAllPrefs = new ArrayList<Preference>();

    private final ArrayList<SwitchPreference> mResetSwitchPrefs
@@ -311,6 +317,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment

    private Dialog mAdbKeysDialog;
    private boolean mUnavailable;
    private Dialog mRootDialog;

    public DevelopmentSettings() {
        super(UserManager.DISALLOW_DEBUGGING_FEATURES);
@@ -489,6 +496,12 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
            removePreference(COLOR_TEMPERATURE_KEY);
            mColorTemperaturePreference = null;
        }

        mRootAccess = (ListPreference) findPreference(ROOT_ACCESS_KEY);
        mRootAccess.setOnPreferenceChangeListener(this);
        if (!removeRootOptionsIfRequired()) {
            mAllPrefs.add(mRootAccess);
        }
    }

    private ListPreference addListPreference(String prefKey) {
@@ -515,6 +528,18 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
        return pref;
    }

    private boolean removeRootOptionsIfRequired() {
        // user builds don't get root, and eng always gets root
        if (!(Build.IS_DEBUGGABLE || "eng".equals(Build.TYPE))) {
            if (mRootAccess != null) {
                getPreferenceScreen().removePreference(mRootAccess);
                return true;
            }
        }

        return false;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
@@ -705,6 +730,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
            updateColorTemperature();
        }
        updateBluetoothDisableAbsVolumeOptions();
        updateRootAccessOptions();
    }

    private void resetDangerousOptions() {
@@ -718,6 +744,7 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
        }
        resetDebuggerOptions();
        writeLogdSizeOption(null);
        resetRootAccessOptions();
        writeAnimationScaleOption(0, mWindowAnimationScale, null);
        writeAnimationScaleOption(1, mTransitionAnimationScale, null);
        writeAnimationScaleOption(2, mAnimatorDurationScale, null);
@@ -785,6 +812,47 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
        }
    }

   private void updateRootAccessOptions() {
        String value = SystemProperties.get(ROOT_ACCESS_PROPERTY, "0");
        mRootAccess.setValue(value);
        mRootAccess.setSummary(getResources()
                .getStringArray(R.array.root_access_entries)[Integer.valueOf(value)]);
    }

    public static boolean isRootForAppsEnabled() {
        int value = SystemProperties.getInt(ROOT_ACCESS_PROPERTY, 0);
        boolean daemonState =
                SystemProperties.get("init.svc.su_daemon", "absent").equals("running");
        return daemonState && (value == 1 || value == 3);
    }

    private void writeRootAccessOptions(Object newValue) {
        String oldValue = SystemProperties.get(ROOT_ACCESS_PROPERTY, "0");
        SystemProperties.set(ROOT_ACCESS_PROPERTY, newValue.toString());
        if (Integer.valueOf(newValue.toString()) < 2 && !oldValue.equals(newValue)
                && "1".equals(SystemProperties.get("service.adb.root", "0"))) {
            SystemProperties.set("service.adb.root", "0");
            Settings.Secure.putInt(getActivity().getContentResolver(),
                    Settings.Secure.ADB_ENABLED, 0);
            Settings.Secure.putInt(getActivity().getContentResolver(),
                    Settings.Secure.ADB_ENABLED, 1);
        }
        updateRootAccessOptions();
    }

    private void resetRootAccessOptions() {
        String oldValue = SystemProperties.get(ROOT_ACCESS_PROPERTY, "0");
        SystemProperties.set(ROOT_ACCESS_PROPERTY, "0");
        if (!oldValue.equals("0") && "1".equals(SystemProperties.get("service.adb.root", "0"))) {
            SystemProperties.set("service.adb.root", "0");
            Settings.Secure.putInt(getActivity().getContentResolver(),
                    Settings.Secure.ADB_ENABLED, 0);
            Settings.Secure.putInt(getActivity().getContentResolver(),
                    Settings.Secure.ADB_ENABLED, 1);
        }
        updateRootAccessOptions();
    }

    private void updateHdcpValues() {
        ListPreference hdcpChecking = (ListPreference) findPreference(HDCP_CHECKING_KEY);
        if (hdcpChecking != null) {
@@ -1978,6 +2046,24 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
        } else if (preference == mSimulateColorSpace) {
            writeSimulateColorSpace(newValue);
            return true;
        } else if (preference == mRootAccess) {
            if ("0".equals(SystemProperties.get(ROOT_ACCESS_PROPERTY, "0"))
                    && !"0".equals(newValue)) {
                mSelectedRootValue = newValue;
                mDialogClicked = false;
                if (mRootDialog != null) {
                    dismissDialogs();
                }
                mRootDialog = new AlertDialog.Builder(getActivity())
                        .setMessage(getResources().getString(R.string.root_access_warning_message))
                        .setTitle(R.string.root_access_warning_title)
                        .setPositiveButton(android.R.string.yes, this)
                        .setNegativeButton(android.R.string.no, this).show();
                mRootDialog.setOnDismissListener(this);
            } else {
                writeRootAccessOptions(newValue);
            }
            return true;
        }
        return false;
    }
@@ -1995,6 +2081,10 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
            mEnableDialog.dismiss();
            mEnableDialog = null;
        }
        if (mRootDialog != null) {
            mRootDialog.dismiss();
            mRootDialog = null;
        }
    }

    public void onClick(DialogInterface dialog, int which) {
@@ -2031,6 +2121,13 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
                // Reset the toggle
                mSwitchBar.setChecked(false);
            }
        } else if (dialog == mRootDialog) {
            if (which == DialogInterface.BUTTON_POSITIVE) {
                writeRootAccessOptions(mSelectedRootValue);
            } else {
                // Reset the option
                writeRootAccessOptions("0");
            }
        }
    }

@@ -2046,6 +2143,9 @@ public class DevelopmentSettings extends RestrictedSettingsFragment
                mSwitchBar.setChecked(false);
            }
            mEnableDialog = null;
        } else if (dialog == mRootDialog) {
            updateRootAccessOptions();
            mRootDialog = null;
        }
    }