[Safer intents] Permission controller

To avoid implicit intents, make intents launch explicitly.

Test: build, manual
Bug: 323061508
Change-Id: Ib95b417fc8682f690890fc55f321b4fece2c0369
This commit is contained in:
Jason Chiu
2024-03-27 12:25:32 +08:00
parent 67bfb4141f
commit cf13dfd4c6
22 changed files with 182 additions and 49 deletions

View File

@@ -58,9 +58,7 @@
android:key="default_apps"
android:title="@string/app_default_dashboard_title"
android:order="-996"
settings:controller="com.android.settings.applications.DefaultAppsPreferenceController">
<intent android:action="android.settings.MANAGE_DEFAULT_APPS_SETTINGS"/>
</Preference>
settings:controller="com.android.settings.applications.DefaultAppsPreferenceController"/>
<Preference
android:key="cloned_apps"
@@ -85,9 +83,7 @@
android:summary="@string/summary_placeholder"
android:order="15"
settings:keywords="app_hibernation_key"
settings:controller="com.android.settings.applications.HibernatedAppsPreferenceController">
<intent android:action="android.intent.action.MANAGE_UNUSED_APPS"/>
</Preference>
settings:controller="com.android.settings.applications.HibernatedAppsPreferenceController"/>
<Preference
android:key="app_battery_usage"

View File

@@ -61,12 +61,7 @@
<Preference
android:key="app_level_permissions"
android:title="@string/location_app_level_permissions"
settings:controller="com.android.settings.location.AppLocationPermissionPreferenceController">
<intent android:action="android.intent.action.MANAGE_PERMISSION_APPS">
<extra android:name="android.intent.extra.PERMISSION_NAME"
android:value="android.permission-group.LOCATION"/>
</intent>
</Preference>
settings:controller="com.android.settings.location.AppLocationPermissionPreferenceController"/>
<Preference
android:fragment="com.android.settings.location.LocationServices"

View File

@@ -38,14 +38,9 @@
<!-- This preference category gets removed if new_recent_location_ui is disabled -->
<Preference
android:key="app_level_permissions"
android:key="app_level_permissions_personal"
android:title="@string/location_app_level_permissions"
settings:controller="com.android.settings.location.AppLocationPermissionPreferenceController">
<intent android:action="android.intent.action.MANAGE_PERMISSION_APPS">
<extra android:name="android.intent.extra.PERMISSION_GROUP_NAME"
android:value="android.permission-group.LOCATION"/>
</intent>
</Preference>
settings:controller="com.android.settings.location.AppLocationPermissionPreferenceController"/>
<Preference
android:key="location_services"

View File

@@ -46,14 +46,9 @@
<!-- This preference category gets removed if new_recent_location_ui is disabled -->
<Preference
android:key="app_level_permissions"
android:key="app_level_permissions_work"
android:title="@string/location_app_level_permissions"
settings:controller="com.android.settings.location.AppLocationPermissionPreferenceController">
<intent android:action="android.intent.action.MANAGE_PERMISSION_APPS">
<extra android:name="android.intent.extra.PERMISSION_GROUP_NAME"
android:value="android.permission-group.LOCATION"/>
</intent>
</Preference>
settings:controller="com.android.settings.location.AppLocationPermissionPreferenceController"/>
<Preference
android:key="location_services_for_work"

View File

@@ -74,9 +74,7 @@
<Preference
android:key="privacy_accessibility_usage"
android:title="@string/accessibility_usage_title"
settings:controller="com.android.settings.privacy.AccessibilityUsagePreferenceController">
<intent android:action="android.intent.action.REVIEW_ACCESSIBILITY_SERVICES"/>
</Preference>
settings:controller="com.android.settings.privacy.AccessibilityUsagePreferenceController"/>
<!-- On lock screen notifications -->
<com.android.settings.RestrictedListPreference

View File

@@ -40,35 +40,28 @@
<Preference
android:key="privacy_accessibility_usage"
android:title="@string/accessibility_usage_title"
settings:controller="com.android.settings.privacy.AccessibilityUsagePreferenceController">
<intent android:action="android.intent.action.REVIEW_ACCESSIBILITY_SERVICES"/>
</Preference>
settings:controller="com.android.settings.privacy.AccessibilityUsagePreferenceController"/>
<!-- Permissions usage -->
<Preference
android:key="privacy_permissions_usage"
android:title="@string/permissions_usage_title"
android:summary="@string/permissions_usage_summary"
settings:controller="com.android.settings.privacy.PrivacyHubPreferenceController">
<intent android:action="android.intent.action.REVIEW_PERMISSION_USAGE"/>
</Preference>
settings:controller="com.android.settings.privacy.PrivacyHubPreferenceController"/>
<!-- App permissions -->
<Preference
android:key="privacy_manage_perms"
android:title="@string/app_permissions"
android:summary="@string/runtime_permissions_summary_control_app_access">
<intent android:action="android.intent.action.MANAGE_PERMISSIONS"/>
</Preference>
android:summary="@string/runtime_permissions_summary_control_app_access"
settings:controller="com.android.settings.privacy.ManagePermissionsPreferenceController"/>
<!-- App Data Sharing Update -->
<Preference
android:key="privacy_app_data_sharing_updates"
android:title="@string/app_data_sharing_updates_title"
android:summary="@string/app_data_sharing_updates_summary"
settings:controller="com.android.settings.privacy.AppDataSharingUpdatesPreferenceController">
<intent android:action="android.intent.action.REVIEW_APP_DATA_SHARING_UPDATES"/>
</Preference>
settings:controller="com.android.settings.privacy.AppDataSharingUpdatesPreferenceController"/>
<!-- Camera toggle -->

View File

@@ -140,7 +140,8 @@ public class Settings extends SettingsActivity {
if (SafetyCenterManagerWrapper.get().isEnabled(this)) {
try {
startActivity(new Intent(Intent.ACTION_SAFETY_CENTER));
startActivity(new Intent(Intent.ACTION_SAFETY_CENTER)
.setPackage(getPackageManager().getPermissionControllerPackageName()));
finish();
} catch (ActivityNotFoundException e) {
Log.e(TAG, "Unable to open safety center", e);
@@ -235,7 +236,8 @@ public class Settings extends SettingsActivity {
if (ACTION_PRIVACY_SETTINGS.equals(getIntent().getAction())
&& SafetyCenterManagerWrapper.get().isEnabled(this)) {
try {
startActivity(new Intent(Intent.ACTION_SAFETY_CENTER));
startActivity(new Intent(Intent.ACTION_SAFETY_CENTER)
.setPackage(getPackageManager().getPermissionControllerPackageName()));
finish();
} catch (ActivityNotFoundException e) {
Log.e(TAG, "Unable to open safety center", e);

View File

@@ -18,11 +18,16 @@ package com.android.settings.applications;
import android.app.role.RoleManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.icu.text.ListFormatter;
import android.provider.Settings;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.core.text.BidiFormatter;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.applications.AppUtils;
@@ -47,6 +52,17 @@ public class DefaultAppsPreferenceController extends BasePreferenceController {
return AVAILABLE;
}
@Override
public void displayPreference(@NonNull PreferenceScreen screen) {
super.displayPreference(screen);
Preference pref = screen.findPreference(getPreferenceKey());
if (pref != null) {
pref.setIntent(new Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS)
.setPackage(mPackageManager.getPermissionControllerPackageName()));
}
}
@Override
public CharSequence getSummary() {
final List<CharSequence> defaultAppLabels = new ArrayList<>();

View File

@@ -21,6 +21,7 @@ import static android.provider.DeviceConfig.NAMESPACE_APP_HIBERNATION;
import static com.android.settings.Utils.PROPERTY_APP_HIBERNATION_ENABLED;
import android.content.Context;
import android.content.Intent;
import android.icu.text.MessageFormat;
import android.permission.PermissionControllerManager;
import android.provider.DeviceConfig;
@@ -85,6 +86,12 @@ public final class HibernatedAppsPreferenceController extends BasePreferenceCont
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mScreen = screen;
Preference pref = screen.findPreference(getPreferenceKey());
if (pref != null) {
pref.setIntent(new Intent(Intent.ACTION_MANAGE_UNUSED_APPS)
.setPackage(mContext.getPackageManager().getPermissionControllerPackageName()));
}
}
/**

View File

@@ -132,6 +132,7 @@ public class AppPermissionPreferenceController extends AppInfoPreferenceControll
private void startManagePermissionsActivity() {
// start new activity to manage app permissions
final Intent permIntent = new Intent(Intent.ACTION_MANAGE_APP_PERMISSIONS);
permIntent.setPackage(mPackageManager.getPermissionControllerPackageName());
permIntent.putExtra(Intent.EXTRA_PACKAGE_NAME, mParent.getAppEntry().info.packageName);
permIntent.putExtra(EXTRA_HIDE_INFO_BUTTON, true);
Activity activity = mParent.getActivity();

View File

@@ -105,6 +105,7 @@ public abstract class DefaultAppShortcutPreferenceControllerBase extends BasePre
return false;
}
final Intent intent = new Intent(Intent.ACTION_MANAGE_DEFAULT_APP)
.setPackage(mContext.getPackageManager().getPermissionControllerPackageName())
.putExtra(Intent.EXTRA_ROLE_NAME, mRoleName);
mContext.startActivity(intent);
return true;

View File

@@ -921,7 +921,9 @@ public class ManageApplications extends InstrumentedFragment
.setResultListener(this, ADVANCED_SETTINGS)
.launch();
} else {
Intent intent = new Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS);
Intent intent = new Intent(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS)
.setPackage(getContext()
.getPackageManager().getPermissionControllerPackageName());
startActivityForResult(intent, ADVANCED_SETTINGS);
}
return true;

View File

@@ -71,7 +71,8 @@ public class DefaultPaymentSettingsPreferenceController extends BasePreferenceCo
&& mPreferenceKey.equals(preference.getKey())) {
RoleManager roleManager = mContext.getSystemService(RoleManager.class);
if (roleManager.isRoleAvailable(RoleManager.ROLE_WALLET)) {
Intent intent = new Intent(CardEmulation.ACTION_CHANGE_DEFAULT);
Intent intent = new Intent(CardEmulation.ACTION_CHANGE_DEFAULT)
.setPackage(mPackageManager.getPermissionControllerPackageName());
mContext.startActivity(intent);
return true;
}

View File

@@ -4,14 +4,18 @@ import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.permission.PermissionControllerManager;
import android.provider.Settings;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.Utils;
@@ -69,6 +73,21 @@ public class AppLocationPermissionPreferenceController extends
}
}
@Override
public void displayPreference(@NonNull PreferenceScreen screen) {
super.displayPreference(screen);
Preference pref = screen.findPreference(getPreferenceKey());
if (pref != null) {
pref.setIntent(new Intent(Intent.ACTION_MANAGE_PERMISSION_APPS)
.setPackage(mContext.getPackageManager().getPermissionControllerPackageName())
.putExtra(TextUtils.equals(pref.getKey(), "app_level_permissions")
? Intent.EXTRA_PERMISSION_NAME
: Intent.EXTRA_PERMISSION_GROUP_NAME,
"android.permission-group.LOCATION"));
}
}
private void setAppCounts(int numTotal, int numHasLocation) {
mNumTotal = numTotal;
mNumHasLocation = numHasLocation;

View File

@@ -67,6 +67,7 @@ public class RecentLocationAccessPreferenceController extends LocationBasePrefer
@Override
public boolean onPreferenceClick(Preference preference) {
final Intent intent = new Intent(Intent.ACTION_MANAGE_APP_PERMISSION);
intent.setPackage(mContext.getPackageManager().getPermissionControllerPackageName());
intent.putExtra(Intent.EXTRA_PERMISSION_GROUP_NAME, LOCATION);
intent.putExtra(Intent.EXTRA_PACKAGE_NAME, mPackage);
intent.putExtra(Intent.EXTRA_USER, mUserHandle);

View File

@@ -18,10 +18,12 @@ package com.android.settings.privacy;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.view.accessibility.AccessibilityManager;
import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
@@ -64,4 +66,15 @@ public class AccessibilityUsagePreferenceController extends BasePreferenceContro
return StringUtil.getIcuPluralsString(mContext, mEnabledServiceInfos.size(),
R.string.accessibility_usage_summary);
}
@Override
public void displayPreference(@NonNull PreferenceScreen screen) {
super.displayPreference(screen);
Preference pref = screen.findPreference(getPreferenceKey());
if (pref != null) {
pref.setIntent(new Intent(Intent.ACTION_REVIEW_ACCESSIBILITY_SERVICES)
.setPackage(mContext.getPackageManager().getPermissionControllerPackageName()));
}
}
}

View File

@@ -19,9 +19,14 @@ package com.android.settings.privacy;
import static android.safetylabel.SafetyLabelConstants.SAFETY_LABEL_CHANGE_NOTIFICATIONS_ENABLED;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.provider.DeviceConfig;
import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
/**
@@ -34,6 +39,17 @@ public class AppDataSharingUpdatesPreferenceController extends BasePreferenceCon
super(context, preferenceKey);
}
@Override
public void displayPreference(@NonNull PreferenceScreen screen) {
super.displayPreference(screen);
Preference pref = screen.findPreference(getPreferenceKey());
if (pref != null) {
pref.setIntent(new Intent(Intent.ACTION_REVIEW_APP_DATA_SHARING_UPDATES)
.setPackage(mContext.getPackageManager().getPermissionControllerPackageName()));
}
}
@Override
public int getAvailabilityStatus() {
return isPrivacySafetyLabelChangeNotificationsEnabled(mContext)

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2024 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.
*/
package com.android.settings.privacy;
import android.content.Context;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
/**
* The preference controller for managing permissions
*/
public class ManagePermissionsPreferenceController extends BasePreferenceController {
public ManagePermissionsPreferenceController(@NonNull Context context,
@NonNull String preferenceKey) {
super(context, preferenceKey);
}
@Override
public void displayPreference(@NonNull PreferenceScreen screen) {
super.displayPreference(screen);
Preference pref = screen.findPreference(getPreferenceKey());
if (pref != null) {
pref.setIntent(new Intent(Intent.ACTION_MANAGE_PERMISSIONS)
.setPackage(mContext.getPackageManager().getPermissionControllerPackageName()));
}
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
}

View File

@@ -17,9 +17,12 @@
package com.android.settings.privacy;
import android.content.Context;
import android.content.Intent;
import android.provider.DeviceConfig;
import androidx.annotation.NonNull;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.core.BasePreferenceController;
@@ -38,4 +41,15 @@ public final class PrivacyHubPreferenceController extends BasePreferenceControll
return DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY,
PROPERTY_PRIVACY_HUB_ENABLED, true) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
@Override
public void displayPreference(@NonNull PreferenceScreen screen) {
super.displayPreference(screen);
Preference pref = screen.findPreference(getPreferenceKey());
if (pref != null) {
pref.setIntent(new Intent(Intent.ACTION_REVIEW_PERMISSION_USAGE)
.setPackage(mContext.getPackageManager().getPermissionControllerPackageName()));
}
}
}

View File

@@ -50,7 +50,8 @@ public class TopLevelSafetyCenterEntryPreferenceController extends BasePreferenc
}
try {
mContext.startActivity(new Intent(Intent.ACTION_SAFETY_CENTER));
mContext.startActivity(new Intent(Intent.ACTION_SAFETY_CENTER)
.setPackage(mContext.getPackageManager().getPermissionControllerPackageName()));
} catch (ActivityNotFoundException e) {
Log.e(TAG, "Unable to open safety center", e);
return false;

View File

@@ -20,13 +20,16 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import androidx.preference.Preference;
import androidx.test.core.app.ApplicationProvider;
@@ -52,7 +55,6 @@ public class TopLevelSafetyCenterEntryPreferenceControllerTest {
@Mock
private SafetyCenterManagerWrapper mSafetyCenterManagerWrapper;
@Mock
private Context mContext;
@Before
@@ -60,7 +62,12 @@ public class TopLevelSafetyCenterEntryPreferenceControllerTest {
MockitoAnnotations.initMocks(this);
SafetyCenterManagerWrapper.sInstance = mSafetyCenterManagerWrapper;
mPreference = new Preference(ApplicationProvider.getApplicationContext());
mContext = spy(ApplicationProvider.getApplicationContext());
PackageManager pm = spy(mContext.getPackageManager());
doReturn(pm).when(mContext).getPackageManager();
doReturn("com.android.permissioncontroller").when(pm).getPermissionControllerPackageName();
mPreference = new Preference(mContext);
mPreference.setKey(PREFERENCE_KEY);
doNothing().when(mContext).startActivity(any(Intent.class));
@@ -75,7 +82,7 @@ public class TopLevelSafetyCenterEntryPreferenceControllerTest {
@Test
public void handlePreferenceTreeClick_forDifferentPreferenceKey_isNotHandled() {
Preference preference = new Preference(ApplicationProvider.getApplicationContext());
Preference preference = new Preference(mContext);
preference.setKey("some_other_preference");
boolean preferenceHandled =

View File

@@ -20,6 +20,8 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -27,6 +29,7 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
@@ -80,6 +83,10 @@ public class SecurityDashboardActivityTest {
}
});
doNothing().when(mActivity).startActivity(any(Intent.class));
PackageManager pm = mock(PackageManager.class);
doReturn(pm).when(mActivity).getPackageManager();
doReturn("com.android.permissioncontroller").when(pm).getPermissionControllerPackageName();
}
@Test