Merge "Remove LooperMode.LEGACY usage from two test classes" into main
diff --git a/res/xml/apps.xml b/res/xml/apps.xml
index 646c1a5..2ffd0b1 100644
--- a/res/xml/apps.xml
+++ b/res/xml/apps.xml
@@ -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"
@@ -87,9 +85,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"
diff --git a/res/xml/location_settings.xml b/res/xml/location_settings.xml
index 206cc46..3f26d04 100644
--- a/res/xml/location_settings.xml
+++ b/res/xml/location_settings.xml
@@ -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"
diff --git a/res/xml/location_settings_personal.xml b/res/xml/location_settings_personal.xml
index f8b21b9..0cef607 100644
--- a/res/xml/location_settings_personal.xml
+++ b/res/xml/location_settings_personal.xml
@@ -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"
diff --git a/res/xml/location_settings_workprofile.xml b/res/xml/location_settings_workprofile.xml
index bcac084..6a559e9 100644
--- a/res/xml/location_settings_workprofile.xml
+++ b/res/xml/location_settings_workprofile.xml
@@ -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"
diff --git a/res/xml/more_security_privacy_settings.xml b/res/xml/more_security_privacy_settings.xml
index 3e11db2..f1678bc 100644
--- a/res/xml/more_security_privacy_settings.xml
+++ b/res/xml/more_security_privacy_settings.xml
@@ -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
@@ -175,7 +173,8 @@
             android:title="@string/financed_device_info"
             android:summary="@string/summary_placeholder"
             settings:controller="com.android.settings.devicelock.DeviceLockPreferenceController">
-            <intent android:action="com.android.devicelockcontroller.action.DEVICE_INFO_SETTINGS"/>
+            <intent android:action="com.android.devicelockcontroller.action.DEVICE_INFO_SETTINGS"
+                    android:targetPackage="@string/config_device_lock_controller_package_name"/>
         </Preference>
     </PreferenceCategory>
 
diff --git a/res/xml/privacy_dashboard_settings.xml b/res/xml/privacy_dashboard_settings.xml
index 551501b..d5cb2f5 100644
--- a/res/xml/privacy_dashboard_settings.xml
+++ b/res/xml/privacy_dashboard_settings.xml
@@ -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 -->
diff --git a/res/xml/shortcuts.xml b/res/xml/shortcuts.xml
index 058d22e..9c93688 100644
--- a/res/xml/shortcuts.xml
+++ b/res/xml/shortcuts.xml
@@ -14,12 +14,14 @@
   ~ See the License for the specific language governing permissions and
   ~ limitations under the License
   -->
-<shortcuts xmlns:android="http://schemas.android.com/apk/res/android" >
+<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
     <shortcut
         android:shortcutId="manifest-shortcut-wifi"
         android:icon="@drawable/ic_shortcut_wireless"
-        android:shortcutShortLabel="@string/wifi_settings" >
-        <intent android:action="android.settings.WIFI_SETTINGS" />
+        android:shortcutShortLabel="@string/wifi_settings">
+        <intent
+            android:action="android.settings.WIFI_SETTINGS"
+            android:targetPackage="${applicationId}"/>
     </shortcut>
     <shortcut
         android:shortcutId="manifest-shortcut-data-usage"
@@ -28,13 +30,15 @@
         android:shortcutShortLabel="@string/data_usage_summary_title">
         <intent
             android:action="android.intent.action.MAIN"
-            android:targetPackage="com.android.settings"
-            android:targetClass="com.android.settings.Settings$DataUsageSummaryActivity" />
+            android:targetPackage="${applicationId}"
+            android:targetClass="com.android.settings.Settings$DataUsageSummaryActivity"/>
     </shortcut>
     <shortcut
         android:shortcutId="manifest-shortcut-battery"
         android:icon="@drawable/ic_shortcut_battery"
-        android:shortcutShortLabel="@string/power_usage_summary_title" >
-        <intent android:action="android.intent.action.POWER_USAGE_SUMMARY" />
+        android:shortcutShortLabel="@string/power_usage_summary_title">
+        <intent
+            android:action="android.intent.action.POWER_USAGE_SUMMARY"
+            android:targetPackage="${applicationId}"/>
     </shortcut>
 </shortcuts>
\ No newline at end of file
diff --git a/src/com/android/settings/BrightnessPreference.java b/src/com/android/settings/BrightnessPreference.java
index b3cf433..8c8f3f6 100644
--- a/src/com/android/settings/BrightnessPreference.java
+++ b/src/com/android/settings/BrightnessPreference.java
@@ -31,7 +31,8 @@
 
     @Override
     protected void onClick() {
-        getContext().startActivityAsUser(new Intent(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG),
+        getContext().startActivityAsUser(new Intent(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG)
+                        .setPackage(Utils.SYSTEMUI_PACKAGE_NAME),
                 UserHandle.CURRENT_OR_SELF);
     }
 }
diff --git a/src/com/android/settings/RingtonePreference.java b/src/com/android/settings/RingtonePreference.java
index de5b7c3..6b9dad7 100644
--- a/src/com/android/settings/RingtonePreference.java
+++ b/src/com/android/settings/RingtonePreference.java
@@ -74,7 +74,8 @@
                 true);
         mShowSilent = a.getBoolean(com.android.internal.R.styleable.RingtonePreference_showSilent,
                 true);
-        setIntent(new Intent(RingtoneManager.ACTION_RINGTONE_PICKER));
+        setIntent(new Intent(RingtoneManager.ACTION_RINGTONE_PICKER)
+                .setPackage(context.getString(R.string.config_sound_picker_package_name)));
         setUserId(UserHandle.myUserId());
         a.recycle();
     }
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index b856032..9ebc124 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -140,7 +140,8 @@
 
             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 @@
             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);
diff --git a/src/com/android/settings/accessibility/LiveCaptionPreferenceController.java b/src/com/android/settings/accessibility/LiveCaptionPreferenceController.java
index 2d12f67..f515732 100644
--- a/src/com/android/settings/accessibility/LiveCaptionPreferenceController.java
+++ b/src/com/android/settings/accessibility/LiveCaptionPreferenceController.java
@@ -39,6 +39,7 @@
     public LiveCaptionPreferenceController(Context context, String preferenceKey) {
         super(context, preferenceKey);
         mPackageManager = context.getPackageManager();
+        LIVE_CAPTION_INTENT.setPackage(mPackageManager.getSystemCaptionsServicePackageName());
     }
 
     @Override
diff --git a/src/com/android/settings/accounts/AccountPreferenceController.java b/src/com/android/settings/accounts/AccountPreferenceController.java
index 4f03303..b120f38 100644
--- a/src/com/android/settings/accounts/AccountPreferenceController.java
+++ b/src/com/android/settings/accounts/AccountPreferenceController.java
@@ -256,6 +256,7 @@
             if (preference == profileData.addAccountPreference) {
                 mMetricsFeatureProvider.logClickedPreference(preference, metricsCategory);
                 Intent intent = new Intent(ACTION_ADD_ACCOUNT);
+                intent.setClass(mContext, AddAccountSettings.class);
                 intent.putExtra(EXTRA_USER, profileData.userInfo.getUserHandle());
                 intent.putExtra(EXTRA_AUTHORITIES, mAuthorities);
                 mContext.startActivity(intent);
diff --git a/src/com/android/settings/accounts/EnterpriseDisclosurePreferenceController.java b/src/com/android/settings/accounts/EnterpriseDisclosurePreferenceController.java
index f7816bb..33c494b 100644
--- a/src/com/android/settings/accounts/EnterpriseDisclosurePreferenceController.java
+++ b/src/com/android/settings/accounts/EnterpriseDisclosurePreferenceController.java
@@ -66,7 +66,8 @@
         final FooterPreference footerPreference = screen.findPreference(getPreferenceKey());
         footerPreference.setTitle(disclosure);
         footerPreference.setLearnMoreAction(view -> {
-            mContext.startActivity(new Intent(Settings.ACTION_ENTERPRISE_PRIVACY_SETTINGS));
+            mContext.startActivity(new Intent(Settings.ACTION_ENTERPRISE_PRIVACY_SETTINGS)
+                    .setPackage(mContext.getPackageName()));
         });
         final String learnMoreText = mContext.getString(
                 R.string.footer_learn_more_content_description, getLabelName());
diff --git a/src/com/android/settings/applications/DefaultAppsPreferenceController.java b/src/com/android/settings/applications/DefaultAppsPreferenceController.java
index c5b849d..6320db6 100644
--- a/src/com/android/settings/applications/DefaultAppsPreferenceController.java
+++ b/src/com/android/settings/applications/DefaultAppsPreferenceController.java
@@ -18,11 +18,16 @@
 
 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;
@@ -48,6 +53,17 @@
     }
 
     @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<>();
         final CharSequence defaultBrowserLabel = getDefaultAppLabel(RoleManager.ROLE_BROWSER);
diff --git a/src/com/android/settings/applications/HibernatedAppsPreferenceController.java b/src/com/android/settings/applications/HibernatedAppsPreferenceController.java
index eb6edb2..d85f8aa 100644
--- a/src/com/android/settings/applications/HibernatedAppsPreferenceController.java
+++ b/src/com/android/settings/applications/HibernatedAppsPreferenceController.java
@@ -21,6 +21,7 @@
 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 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()));
+        }
     }
 
     /**
diff --git a/src/com/android/settings/applications/appinfo/AppPermissionPreferenceController.java b/src/com/android/settings/applications/appinfo/AppPermissionPreferenceController.java
index b7069d9..6ba4e46 100644
--- a/src/com/android/settings/applications/appinfo/AppPermissionPreferenceController.java
+++ b/src/com/android/settings/applications/appinfo/AppPermissionPreferenceController.java
@@ -132,6 +132,7 @@
     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();
diff --git a/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBase.java b/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBase.java
index 5a1883b..184f907 100644
--- a/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBase.java
+++ b/src/com/android/settings/applications/appinfo/DefaultAppShortcutPreferenceControllerBase.java
@@ -105,6 +105,7 @@
             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;
diff --git a/src/com/android/settings/applications/manageapplications/ManageApplications.java b/src/com/android/settings/applications/manageapplications/ManageApplications.java
index c2fabff..b392d9a 100644
--- a/src/com/android/settings/applications/manageapplications/ManageApplications.java
+++ b/src/com/android/settings/applications/manageapplications/ManageApplications.java
@@ -921,7 +921,9 @@
                         .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;
diff --git a/src/com/android/settings/applications/specialaccess/DefaultPaymentSettingsPreferenceController.java b/src/com/android/settings/applications/specialaccess/DefaultPaymentSettingsPreferenceController.java
index c66c97e..c76e448 100644
--- a/src/com/android/settings/applications/specialaccess/DefaultPaymentSettingsPreferenceController.java
+++ b/src/com/android/settings/applications/specialaccess/DefaultPaymentSettingsPreferenceController.java
@@ -71,7 +71,8 @@
                 && 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;
             }
diff --git a/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java b/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
index 8214d0b..b95d927 100644
--- a/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
+++ b/src/com/android/settings/dashboard/DashboardFeatureProviderImpl.java
@@ -223,8 +223,9 @@
     @Override
     public void openTileIntent(FragmentActivity activity, Tile tile) {
         if (tile == null) {
-            Intent intent = new Intent(Settings.ACTION_SETTINGS).addFlags(
-                    Intent.FLAG_ACTIVITY_CLEAR_TASK);
+            Intent intent = new Intent(Settings.ACTION_SETTINGS)
+                    .setPackage(mContext.getPackageName())
+                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
             mContext.startActivity(intent);
             return;
         }
diff --git a/src/com/android/settings/datetime/LocationToggleDisabledDialogFragment.java b/src/com/android/settings/datetime/LocationToggleDisabledDialogFragment.java
index 046a659..c9362c0 100644
--- a/src/com/android/settings/datetime/LocationToggleDisabledDialogFragment.java
+++ b/src/com/android/settings/datetime/LocationToggleDisabledDialogFragment.java
@@ -42,7 +42,8 @@
                 .setPositiveButton(
                         R.string.location_time_zone_detection_location_is_off_dialog_ok_button,
                         (dialog, which) -> {
-                            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
+                            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
+                                    .setPackage(getContext().getPackageName());
                             getContext().startActivity(intent);
                         })
                 .setNegativeButton(
diff --git a/src/com/android/settings/development/AdbIpAddressPreferenceController.java b/src/com/android/settings/development/AdbIpAddressPreferenceController.java
index 36d1b99..e6a60ad 100644
--- a/src/com/android/settings/development/AdbIpAddressPreferenceController.java
+++ b/src/com/android/settings/development/AdbIpAddressPreferenceController.java
@@ -22,8 +22,10 @@
 import android.net.LinkAddress;
 import android.net.LinkProperties;
 import android.net.wifi.WifiManager;
+import android.os.Build;
 import android.os.RemoteException;
 import android.os.ServiceManager;
+import android.os.SystemProperties;
 import android.util.Log;
 
 import androidx.preference.Preference;
@@ -100,12 +102,23 @@
     }
 
     public String getIpv4Address() {
+        // ARC specific - from here
+        // Get the host's Wi-Fi IP address instead of guest's IP.
+        if (Build.IS_ARC) {
+            final String[] addrs =
+                    SystemProperties.get("vendor.arc.net.ipv4.host_wifi_address").split(",");
+            // ADB over Wi-Fi uses the first available available network configuration.
+            if (addrs.length > 0) {
+                return addrs[0];
+            }
+        }
+        // ARC specific - till here.
         return getDefaultIpAddresses(mCM);
     }
 
     @Override
     protected void updateConnectivity() {
-        String ipAddress = getDefaultIpAddresses(mCM);
+        String ipAddress = getIpv4Address();
         if (ipAddress != null) {
             int port = getPort();
             if (port <= 0) {
diff --git a/src/com/android/settings/display/AdaptiveSleepPermissionPreferenceController.java b/src/com/android/settings/display/AdaptiveSleepPermissionPreferenceController.java
index c3db6f7..03b9d09 100644
--- a/src/com/android/settings/display/AdaptiveSleepPermissionPreferenceController.java
+++ b/src/com/android/settings/display/AdaptiveSleepPermissionPreferenceController.java
@@ -68,6 +68,7 @@
                     mContext.getPackageManager().getAttentionServicePackageName();
             final Intent intent = new Intent(
                     android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
+            intent.setPackage(mContext.getPackageName());
             intent.setData(Uri.parse("package:" + packageName));
             mPreference = new BannerMessagePreference(mContext);
             mPreference.setTitle(R.string.adaptive_sleep_title_no_permission);
diff --git a/src/com/android/settings/display/BrightnessLevelPreferenceController.java b/src/com/android/settings/display/BrightnessLevelPreferenceController.java
index 468a1d8..49b8da3 100644
--- a/src/com/android/settings/display/BrightnessLevelPreferenceController.java
+++ b/src/com/android/settings/display/BrightnessLevelPreferenceController.java
@@ -39,6 +39,7 @@
 import androidx.preference.Preference;
 import androidx.preference.PreferenceScreen;
 
+import com.android.settings.Utils;
 import com.android.settings.core.PreferenceControllerMixin;
 import com.android.settings.core.SettingsBaseActivity;
 import com.android.settingslib.core.AbstractPreferenceController;
@@ -145,6 +146,7 @@
             return false;
         }
         final Intent intent = new Intent(ACTION_SHOW_BRIGHTNESS_DIALOG);
+        intent.setPackage(Utils.SYSTEMUI_PACKAGE_NAME);
         intent.putExtra(SettingsBaseActivity.EXTRA_PAGE_TRANSITION_TYPE,
                 SettingsTransitionHelper.TransitionType.TRANSITION_NONE);
         intent.putExtra(EXTRA_BRIGHTNESS_DIALOG_IS_FULL_WIDTH, true);
diff --git a/src/com/android/settings/display/SmartAutoRotatePermissionController.java b/src/com/android/settings/display/SmartAutoRotatePermissionController.java
index aa6a96d..c45ab11 100644
--- a/src/com/android/settings/display/SmartAutoRotatePermissionController.java
+++ b/src/com/android/settings/display/SmartAutoRotatePermissionController.java
@@ -47,6 +47,7 @@
         final String packageName = context.getPackageManager().getRotationResolverPackageName();
         mIntent = new Intent(
                 android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
+        mIntent.setPackage(context.getPackageName());
         mIntent.setData(Uri.parse("package:" + packageName));
     }
 
diff --git a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java
index ef542a3..0c8f408 100644
--- a/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java
+++ b/src/com/android/settings/enterprise/EnterprisePrivacyFeatureProviderImpl.java
@@ -34,8 +34,6 @@
 import android.os.UserManager;
 import android.provider.Settings;
 import android.text.SpannableStringBuilder;
-import android.text.style.ClickableSpan;
-import android.view.View;
 
 import com.android.settings.R;
 import com.android.settings.vpn2.VpnUtils;
@@ -303,24 +301,4 @@
         }
         return UserHandle.USER_NULL;
     }
-
-    protected static class EnterprisePrivacySpan extends ClickableSpan {
-        private final Context mContext;
-
-        public EnterprisePrivacySpan(Context context) {
-            mContext = context;
-        }
-
-        @Override
-        public void onClick(View widget) {
-            mContext.startActivity(new Intent(Settings.ACTION_ENTERPRISE_PRIVACY_SETTINGS)
-                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
-        }
-
-        @Override
-        public boolean equals(Object object) {
-            return object instanceof EnterprisePrivacySpan
-                    && ((EnterprisePrivacySpan) object).mContext == mContext;
-        }
-    }
 }
diff --git a/src/com/android/settings/flashlight/FlashlightHandleActivity.java b/src/com/android/settings/flashlight/FlashlightHandleActivity.java
index 6740da4..6ae07cd 100644
--- a/src/com/android/settings/flashlight/FlashlightHandleActivity.java
+++ b/src/com/android/settings/flashlight/FlashlightHandleActivity.java
@@ -52,7 +52,7 @@
 
         // Caller's choice: fallback to homepage, or just exit?
         if (getIntent().getBooleanExtra(EXTRA_FALLBACK_TO_HOMEPAGE, false)) {
-            startActivity(new Intent(Settings.ACTION_SETTINGS));
+            startActivity(new Intent(Settings.ACTION_SETTINGS).setPackage(getPackageName()));
         }
         finish();
     }
diff --git a/src/com/android/settings/fuelgauge/batteryusage/PowerUsageAdvanced.java b/src/com/android/settings/fuelgauge/batteryusage/PowerUsageAdvanced.java
index 65d941d..14e07d3 100644
--- a/src/com/android/settings/fuelgauge/batteryusage/PowerUsageAdvanced.java
+++ b/src/com/android/settings/fuelgauge/batteryusage/PowerUsageAdvanced.java
@@ -95,7 +95,11 @@
         super.onCreate(icicle);
         mHistPref = findPreference(KEY_BATTERY_CHART);
         setBatteryChartPreferenceController();
-        AsyncTask.execute(() -> BootBroadcastReceiver.invokeJobRecheck(getContext()));
+        AsyncTask.execute(() -> {
+            if (getContext() != null) {
+                BootBroadcastReceiver.invokeJobRecheck(getContext());
+            }
+        });
     }
 
     @Override
diff --git a/src/com/android/settings/homepage/SettingsHomepageActivity.java b/src/com/android/settings/homepage/SettingsHomepageActivity.java
index 5f091d9..8016799 100644
--- a/src/com/android/settings/homepage/SettingsHomepageActivity.java
+++ b/src/com/android/settings/homepage/SettingsHomepageActivity.java
@@ -208,6 +208,8 @@
                 if (TextUtils.equals(intent.getAction(), ACTION_SETTINGS_EMBED_DEEP_LINK_ACTIVITY)
                         && this instanceof DeepLinkHomepageActivity) {
                     intent.setClass(this, DeepLinkHomepageActivityInternal.class);
+                } else {
+                    intent.setPackage(getPackageName());
                 }
                 intent.removeFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 startActivityAsUser(intent, um.getProfileParent(userInfo.id).getUserHandle());
diff --git a/src/com/android/settings/homepage/contextualcards/FaceReEnrollDialog.java b/src/com/android/settings/homepage/contextualcards/FaceReEnrollDialog.java
index a823924..e2eb6ec 100644
--- a/src/com/android/settings/homepage/contextualcards/FaceReEnrollDialog.java
+++ b/src/com/android/settings/homepage/contextualcards/FaceReEnrollDialog.java
@@ -113,8 +113,8 @@
                     return;
                 }
                 // Send user to the enroll flow.
-                final Intent reEnroll = new Intent(BIOMETRIC_ENROLL_ACTION);
-                final Context context = getApplicationContext();
+                final Intent reEnroll = new Intent(BIOMETRIC_ENROLL_ACTION)
+                        .setPackage(getPackageName());
 
                 try {
                     startActivity(reEnroll);
diff --git a/src/com/android/settings/homepage/contextualcards/conditional/AbnormalRingerConditionController.java b/src/com/android/settings/homepage/contextualcards/conditional/AbnormalRingerConditionController.java
index 6a66d2f..f8cfbea 100644
--- a/src/com/android/settings/homepage/contextualcards/conditional/AbnormalRingerConditionController.java
+++ b/src/com/android/settings/homepage/contextualcards/conditional/AbnormalRingerConditionController.java
@@ -42,7 +42,8 @@
 
     @Override
     public void onPrimaryClick(Context context) {
-        context.startActivity(new Intent(Settings.ACTION_SOUND_SETTINGS));
+        context.startActivity(new Intent(Settings.ACTION_SOUND_SETTINGS)
+                .setPackage(context.getPackageName()));
     }
 
     @Override
diff --git a/src/com/android/settings/location/AppLocationPermissionPreferenceController.java b/src/com/android/settings/location/AppLocationPermissionPreferenceController.java
index 37bbd08..6a61e1a 100644
--- a/src/com/android/settings/location/AppLocationPermissionPreferenceController.java
+++ b/src/com/android/settings/location/AppLocationPermissionPreferenceController.java
@@ -4,14 +4,18 @@
 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 @@
         }
     }
 
+    @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;
diff --git a/src/com/android/settings/location/RecentLocationAccessPreferenceController.java b/src/com/android/settings/location/RecentLocationAccessPreferenceController.java
index 61682d0..a5c0e55 100644
--- a/src/com/android/settings/location/RecentLocationAccessPreferenceController.java
+++ b/src/com/android/settings/location/RecentLocationAccessPreferenceController.java
@@ -67,6 +67,7 @@
         @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);
diff --git a/src/com/android/settings/panel/VolumePanel.java b/src/com/android/settings/panel/VolumePanel.java
index 1a53de0..4f38504 100644
--- a/src/com/android/settings/panel/VolumePanel.java
+++ b/src/com/android/settings/panel/VolumePanel.java
@@ -151,7 +151,9 @@
 
     @Override
     public Intent getSeeMoreIntent() {
-        return new Intent(Settings.ACTION_SOUND_SETTINGS).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        return new Intent(Settings.ACTION_SOUND_SETTINGS)
+                .setPackage(mContext.getPackageName())
+                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     }
 
     @Override
diff --git a/src/com/android/settings/privacy/AccessibilityUsagePreferenceController.java b/src/com/android/settings/privacy/AccessibilityUsagePreferenceController.java
index a78ccee..6a46a98 100644
--- a/src/com/android/settings/privacy/AccessibilityUsagePreferenceController.java
+++ b/src/com/android/settings/privacy/AccessibilityUsagePreferenceController.java
@@ -18,10 +18,12 @@
 
 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 @@
         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()));
+        }
+    }
 }
diff --git a/src/com/android/settings/privacy/AppDataSharingUpdatesPreferenceController.java b/src/com/android/settings/privacy/AppDataSharingUpdatesPreferenceController.java
index 5372a16..5d634eb 100644
--- a/src/com/android/settings/privacy/AppDataSharingUpdatesPreferenceController.java
+++ b/src/com/android/settings/privacy/AppDataSharingUpdatesPreferenceController.java
@@ -19,9 +19,14 @@
 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;
 
 /**
@@ -35,6 +40,17 @@
     }
 
     @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)
                 ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
diff --git a/src/com/android/settings/privacy/ManagePermissionsPreferenceController.java b/src/com/android/settings/privacy/ManagePermissionsPreferenceController.java
new file mode 100644
index 0000000..441c5c9
--- /dev/null
+++ b/src/com/android/settings/privacy/ManagePermissionsPreferenceController.java
@@ -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;
+    }
+}
diff --git a/src/com/android/settings/privacy/PrivacyHubPreferenceController.java b/src/com/android/settings/privacy/PrivacyHubPreferenceController.java
index b68b5ec..20e5290 100644
--- a/src/com/android/settings/privacy/PrivacyHubPreferenceController.java
+++ b/src/com/android/settings/privacy/PrivacyHubPreferenceController.java
@@ -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 @@
         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()));
+        }
+    }
 }
diff --git a/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceController.java b/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceController.java
index af9509b..64d78b8 100644
--- a/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceController.java
+++ b/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceController.java
@@ -50,7 +50,8 @@
         }
 
         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;
diff --git a/src/com/android/settings/security/ScreenPinningSettings.java b/src/com/android/settings/security/ScreenPinningSettings.java
index 8690847..4d3743c 100644
--- a/src/com/android/settings/security/ScreenPinningSettings.java
+++ b/src/com/android/settings/security/ScreenPinningSettings.java
@@ -142,6 +142,7 @@
         if (isEnabled) {
             if (passwordQuality == DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
                 Intent chooseLockIntent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
+                chooseLockIntent.setPackage(getContext().getPackageName());
                 chooseLockIntent.putExtra(
                         ChooseLockGeneric.ChooseLockGenericFragment.HIDE_INSECURE_OPTIONS,
                         true);
diff --git a/src/com/android/settings/slices/SliceDeepLinkSpringBoard.java b/src/com/android/settings/slices/SliceDeepLinkSpringBoard.java
index 852f286..e89f2c9 100644
--- a/src/com/android/settings/slices/SliceDeepLinkSpringBoard.java
+++ b/src/com/android/settings/slices/SliceDeepLinkSpringBoard.java
@@ -67,7 +67,7 @@
             finish();
         } catch (Exception e) {
             Log.w(TAG, "Couldn't launch Slice intent", e);
-            startActivity(new Intent(Settings.ACTION_SETTINGS));
+            startActivity(new Intent(Settings.ACTION_SETTINGS).setPackage(getPackageName()));
             finish();
         }
     }
diff --git a/src/com/android/settings/theme/ContrastPreferenceController.kt b/src/com/android/settings/theme/ContrastPreferenceController.kt
index 7f3844a..7478ab6 100644
--- a/src/com/android/settings/theme/ContrastPreferenceController.kt
+++ b/src/com/android/settings/theme/ContrastPreferenceController.kt
@@ -26,6 +26,7 @@
 import androidx.preference.Preference
 import com.android.internal.annotations.VisibleForTesting
 import com.android.settings.R
+import com.android.settings.Utils
 import com.android.settings.core.BasePreferenceController
 
 /**
@@ -47,6 +48,7 @@
     override fun handlePreferenceTreeClick(preference: Preference): Boolean {
         if (TextUtils.equals(preference.key, preferenceKey)) {
             val intent = Intent(Intent.ACTION_SHOW_CONTRAST_DIALOG)
+            intent.setPackage(Utils.SYSTEMUI_PACKAGE_NAME)
             context.startActivityAsUser(intent, UserHandle(UserHandle.USER_CURRENT))
             return true
         }
diff --git a/src/com/android/settings/users/UserSettings.java b/src/com/android/settings/users/UserSettings.java
index 70d8ed3..83dc127 100644
--- a/src/com/android/settings/users/UserSettings.java
+++ b/src/com/android/settings/users/UserSettings.java
@@ -555,7 +555,8 @@
     }
 
     private void launchChooseLockscreen() {
-        Intent chooseLockIntent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
+        Intent chooseLockIntent = new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD)
+                .setPackage(getContext().getPackageName());
         chooseLockIntent.putExtra(ChooseLockGeneric.ChooseLockGenericFragment.HIDE_INSECURE_OPTIONS,
                 true);
         startActivityForResult(chooseLockIntent, REQUEST_CHOOSE_LOCK);
diff --git a/tests/robotests/src/com/android/settings/accessibility/LiveCaptionPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/accessibility/LiveCaptionPreferenceControllerTest.java
index a7a7f8f..314aeff 100644
--- a/tests/robotests/src/com/android/settings/accessibility/LiveCaptionPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/accessibility/LiveCaptionPreferenceControllerTest.java
@@ -21,7 +21,11 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.spy;
+
 import android.content.Context;
+import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.provider.Settings;
 
@@ -48,7 +52,10 @@
 
     @Before
     public void setUp() {
-        mContext = ApplicationProvider.getApplicationContext();
+        mContext = spy(ApplicationProvider.getApplicationContext());
+        PackageManager pm = spy(mContext.getPackageManager());
+        doReturn(pm).when(mContext).getPackageManager();
+        doReturn("com.caption").when(pm).getSystemCaptionsServicePackageName();
         mController = new LiveCaptionPreferenceController(mContext, "test_key");
         mLiveCaptionPreference = new Preference(mContext);
         mLiveCaptionPreference.setSummary(R.string.live_caption_summary);
diff --git a/tests/unit/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceControllerTest.java b/tests/unit/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceControllerTest.java
index 25cd774..b376825 100644
--- a/tests/unit/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceControllerTest.java
+++ b/tests/unit/src/com/android/settings/safetycenter/TopLevelSafetyCenterEntryPreferenceControllerTest.java
@@ -20,13 +20,16 @@
 
 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 @@
     @Mock
     private SafetyCenterManagerWrapper mSafetyCenterManagerWrapper;
 
-    @Mock
     private Context mContext;
 
     @Before
@@ -60,7 +62,12 @@
         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 @@
 
     @Test
     public void handlePreferenceTreeClick_forDifferentPreferenceKey_isNotHandled() {
-        Preference preference = new Preference(ApplicationProvider.getApplicationContext());
+        Preference preference = new Preference(mContext);
         preference.setKey("some_other_preference");
 
         boolean preferenceHandled =
diff --git a/tests/unit/src/com/android/settings/security/SecurityDashboardActivityTest.java b/tests/unit/src/com/android/settings/security/SecurityDashboardActivityTest.java
index 82112c9..0e51733 100644
--- a/tests/unit/src/com/android/settings/security/SecurityDashboardActivityTest.java
+++ b/tests/unit/src/com/android/settings/security/SecurityDashboardActivityTest.java
@@ -20,6 +20,8 @@
 
 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 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 @@
             }
         });
         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