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

Commit 11d3012c authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

Enforcement control for READ_EXTERNAL permission.

Surface enforcement controls for READ_EXTERNAL_STORAGE permission
using new PackageManager API.

Bug: 6131916
Change-Id: I0ece4742666fda58e41410cb4b0b7175f280fa31
parent d88d7daf
Loading
Loading
Loading
Loading

res/menu/storage.xml

0 → 100644
+25 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2012 The Android Open Source 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.
-->

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/storage_usb"
        android:title="@string/storage_menu_usb" />
    <item
        android:id="@+id/storage_enforce_read_external"
        android:title="@string/storage_menu_enforce_read_external"
        android:checkable="true" />
</menu>
+3 −0
Original line number Diff line number Diff line
@@ -1878,6 +1878,9 @@

    <!-- Storage setting.  Menu option for USB transfer settings [CHAR LIMIT=30]-->
    <string name="storage_menu_usb">USB computer connection</string>
    <!-- Storage setting.  Menu option to enforce read external storage permission. [CHAR LIMIT=30]-->
    <string name="storage_menu_enforce_read_external">Enforce read external</string>

    <!-- Storage setting.  Title for USB transfer settings [CHAR LIMIT=30]-->
    <string name="storage_title_usb">USB computer connection</string>
    <!-- Storage setting.  USB connection category [CHAR LIMIT=30]-->
+3 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settings;

import com.android.settings.accounts.AccountSyncSettings;
import com.android.settings.bluetooth.BluetoothEnabler;
import com.android.settings.deviceinfo.Memory;
import com.android.settings.fuelgauge.PowerUsageSummary;
import com.android.settings.wifi.WifiEnabler;

@@ -294,7 +295,8 @@ public class Settings extends PreferenceActivity implements ButtonBarHandler {
        if (DataUsageSummary.class.getName().equals(fragmentName) ||
                PowerUsageSummary.class.getName().equals(fragmentName) ||
                AccountSyncSettings.class.getName().equals(fragmentName) ||
                UserDictionarySettings.class.getName().equals(fragmentName)) {
                UserDictionarySettings.class.getName().equals(fragmentName) ||
                Memory.class.getName().equals(fragmentName)) {
            intent.putExtra(EXTRA_CLEAR_UI_OPTIONS, true);
        }

+47 −11
Original line number Diff line number Diff line
@@ -16,6 +16,11 @@

package com.android.settings.deviceinfo;

import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.content.pm.PackageManager.ENFORCEMENT_DEFAULT;
import static android.content.pm.PackageManager.ENFORCEMENT_YES;

import android.app.ActivityThread;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
@@ -23,6 +28,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.IPackageManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Environment;
@@ -51,8 +57,6 @@ public class Memory extends SettingsPreferenceFragment {
    private static final int DLG_CONFIRM_UNMOUNT = 1;
    private static final int DLG_ERROR_UNMOUNT = 2;

    private static final int MENU_ID_USB = Menu.FIRST;

    private Resources mResources;

    // The mountToggle Preference that has last been clicked.
@@ -65,6 +69,7 @@ public class Memory extends SettingsPreferenceFragment {
    private IMountService mMountService = null;

    private StorageManager mStorageManager = null;
    private IPackageManager mPackageService;

    private StorageVolumePreferenceCategory mInternalStorageVolumePreferenceCategory;
    private StorageVolumePreferenceCategory[] mStorageVolumePreferenceCategories;
@@ -78,6 +83,8 @@ public class Memory extends SettingsPreferenceFragment {
            mStorageManager.registerListener(mStorageListener);
        }

        mPackageService = ActivityThread.getPackageManager();

        addPreferencesFromResource(R.xml.device_info_memory);

        mResources = getResources();
@@ -92,9 +99,6 @@ public class Memory extends SettingsPreferenceFragment {
        }

        StorageVolume[] storageVolumes = mStorageManager.getVolumeList();
        // mass storage is enabled if primary volume supports it
        boolean massStorageEnabled = (storageVolumes.length > 0
                && storageVolumes[0].allowMassStorage());
        int length = storageVolumes.length;
        mStorageVolumePreferenceCategories = new StorageVolumePreferenceCategory[length];
        for (int i = 0; i < length; i++) {
@@ -106,8 +110,13 @@ public class Memory extends SettingsPreferenceFragment {
            mStorageVolumePreferenceCategories[i].init();
        }

        // only show options menu if we are not using the legacy USB mass storage support
        setHasOptionsMenu(!massStorageEnabled);
        setHasOptionsMenu(true);
    }

    private boolean isMassStorageEnabled() {
        // mass storage is enabled if primary volume supports it
        final StorageVolume[] storageVolumes = mStorageManager.getVolumeList();
        return (storageVolumes.length > 0 && storageVolumes[0].allowMassStorage());
    }

    @Override
@@ -163,15 +172,29 @@ public class Memory extends SettingsPreferenceFragment {

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.add(Menu.NONE, MENU_ID_USB, 0, R.string.storage_menu_usb)
                //.setIcon(com.android.internal.R.drawable.stat_sys_data_usb)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
        inflater.inflate(R.menu.storage, menu);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        final MenuItem usb = menu.findItem(R.id.storage_usb);
        usb.setVisible(!isMassStorageEnabled());

        final int enforcement;
        try {
            enforcement = mPackageService.getPermissionEnforcement(READ_EXTERNAL_STORAGE);
        } catch (RemoteException e) {
            throw new RuntimeException("Problem talking with PackageManager", e);
        }

        final MenuItem enforceReadExternal = menu.findItem(R.id.storage_enforce_read_external);
        enforceReadExternal.setChecked(enforcement == ENFORCEMENT_YES);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_ID_USB:
            case R.id.storage_usb:
                if (getActivity() instanceof PreferenceActivity) {
                    ((PreferenceActivity) getActivity()).startPreferencePanel(
                            UsbSettings.class.getCanonicalName(),
@@ -182,6 +205,19 @@ public class Memory extends SettingsPreferenceFragment {
                    startFragment(this, UsbSettings.class.getCanonicalName(), -1, null);
                }
                return true;
            case R.id.storage_enforce_read_external: {
                final boolean checked = !item.isChecked();
                item.setChecked(checked);

                final int enforcement = checked ? ENFORCEMENT_YES : ENFORCEMENT_DEFAULT;
                try {
                    // TODO: offload to background thread
                    mPackageService.setPermissionEnforcement(READ_EXTERNAL_STORAGE, enforcement);
                } catch (RemoteException e) {
                    throw new RuntimeException("Problem talking with PackageManager", e);
                }
                return true;
            }
        }
        return super.onOptionsItemSelected(item);
    }