diff options
| author | 2024-01-09 11:48:39 -0500 | |
|---|---|---|
| committer | 2024-01-09 13:39:22 -0500 | |
| commit | c50f7196ef9ae1abac546f49d45128e42678fec3 (patch) | |
| tree | aad44dc0a7c6e5aaf9dfff827bcf1efab950ff17 /java/src | |
| parent | 3453b29f41a51981f37cf424b13b4597849f8543 (diff) | |
Break system service deps into separate modules
This is required to selectively replace dependencies within
tests using @UninstallModules with @TestInstallIn or @BindValue.
Within this change, tests which override package manager behavior are
updated to use this mechanism, and PackageManager is removed from
ChooserActivityOverrideData.
Bug: 300157408
Test: atest IntentResolver-tests-activity:com.android.intentresolver.v2
Change-Id: I439b291b5768871f5a1c10f608bea1a9e7c2635b
Diffstat (limited to 'java/src')
3 files changed, 108 insertions, 81 deletions
diff --git a/java/src/com/android/intentresolver/inject/FrameworkModule.kt b/java/src/com/android/intentresolver/inject/FrameworkModule.kt deleted file mode 100644 index 2f6cc6a0..00000000 --- a/java/src/com/android/intentresolver/inject/FrameworkModule.kt +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (C) 2023 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.intentresolver.inject - -import android.app.ActivityManager -import android.app.admin.DevicePolicyManager -import android.content.ClipboardManager -import android.content.Context -import android.content.pm.LauncherApps -import android.content.pm.ShortcutManager -import android.os.UserManager -import android.view.WindowManager -import dagger.Module -import dagger.Provides -import dagger.hilt.InstallIn -import dagger.hilt.android.qualifiers.ApplicationContext -import dagger.hilt.components.SingletonComponent - -private fun <T> Context.requireSystemService(serviceClass: Class<T>): T { - return checkNotNull(getSystemService(serviceClass)) -} - -@Module -@InstallIn(SingletonComponent::class) -object FrameworkModule { - - @Provides - fun contentResolver(@ApplicationContext ctx: Context) = - requireNotNull(ctx.contentResolver) { "ContentResolver is expected but missing" } - - @Provides - fun activityManager(@ApplicationContext ctx: Context) = - ctx.requireSystemService(ActivityManager::class.java) - - @Provides - fun clipboardManager(@ApplicationContext ctx: Context) = - ctx.requireSystemService(ClipboardManager::class.java) - - @Provides - fun devicePolicyManager(@ApplicationContext ctx: Context) = - ctx.requireSystemService(DevicePolicyManager::class.java) - - @Provides - fun launcherApps(@ApplicationContext ctx: Context) = - ctx.requireSystemService(LauncherApps::class.java) - - @Provides - fun packageManager(@ApplicationContext ctx: Context) = - requireNotNull(ctx.packageManager) { "PackageManager is expected but missing" } - - @Provides - fun shortcutManager(@ApplicationContext ctx: Context) = - ctx.requireSystemService(ShortcutManager::class.java) - - @Provides - fun userManager(@ApplicationContext ctx: Context) = - ctx.requireSystemService(UserManager::class.java) - - @Provides - fun windowManager(@ApplicationContext ctx: Context) = - ctx.requireSystemService(WindowManager::class.java) -} diff --git a/java/src/com/android/intentresolver/inject/SystemServices.kt b/java/src/com/android/intentresolver/inject/SystemServices.kt new file mode 100644 index 00000000..32894d43 --- /dev/null +++ b/java/src/com/android/intentresolver/inject/SystemServices.kt @@ -0,0 +1,102 @@ +/* + * 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.intentresolver.inject + +import android.app.ActivityManager +import android.app.admin.DevicePolicyManager +import android.content.ClipboardManager +import android.content.Context +import android.content.pm.LauncherApps +import android.content.pm.ShortcutManager +import android.os.UserManager +import android.view.WindowManager +import androidx.core.content.getSystemService +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.qualifiers.ApplicationContext +import dagger.hilt.components.SingletonComponent + +inline fun <reified T> Context.requireSystemService(): T { + return checkNotNull(getSystemService()) +} + +@Module +@InstallIn(SingletonComponent::class) +class ActivityManagerModule { + @Provides + fun activityManager(@ApplicationContext ctx: Context): ActivityManager = + ctx.requireSystemService() +} + +@Module +@InstallIn(SingletonComponent::class) +class ClipboardManagerModule { + @Provides + fun clipboardManager(@ApplicationContext ctx: Context): ClipboardManager = + ctx.requireSystemService() +} + +@Module +@InstallIn(SingletonComponent::class) +class ContentResolverModule { + @Provides + fun contentResolver(@ApplicationContext ctx: Context) = requireNotNull(ctx.contentResolver) +} + +@Module +@InstallIn(SingletonComponent::class) +class DevicePolicyManagerModule { + @Provides + fun devicePolicyManager(@ApplicationContext ctx: Context): DevicePolicyManager = + ctx.requireSystemService() +} + +@Module +@InstallIn(SingletonComponent::class) +class LauncherAppsModule { + @Provides + fun launcherApps(@ApplicationContext ctx: Context): LauncherApps = ctx.requireSystemService() +} + +@Module +@InstallIn(SingletonComponent::class) +class PackageManagerModule { + @Provides + fun packageManager(@ApplicationContext ctx: Context) = requireNotNull(ctx.packageManager) +} + +@Module +@InstallIn(SingletonComponent::class) +class ShortcutManagerModule { + @Provides + fun shortcutManager(@ApplicationContext ctx: Context): ShortcutManager = + ctx.requireSystemService() +} + +@Module +@InstallIn(SingletonComponent::class) +class UserManagerModule { + @Provides + fun userManager(@ApplicationContext ctx: Context): UserManager = ctx.requireSystemService() +} + +@Module +@InstallIn(SingletonComponent::class) +class WindowManagerModule { + @Provides + fun windowManager(@ApplicationContext ctx: Context): WindowManager = ctx.requireSystemService() +} diff --git a/java/src/com/android/intentresolver/v2/ChooserActivity.java b/java/src/com/android/intentresolver/v2/ChooserActivity.java index 6be0175f..78246213 100644 --- a/java/src/com/android/intentresolver/v2/ChooserActivity.java +++ b/java/src/com/android/intentresolver/v2/ChooserActivity.java @@ -268,6 +268,7 @@ public class ChooserActivity extends Hilt_ChooserActivity implements @Inject @NearbyShare public Optional<ComponentName> mNearbyShare; @Inject public TargetDataLoader mTargetDataLoader; @Inject public DevicePolicyResources mDevicePolicyResources; + @Inject public PackageManager mPackageManager; private ChooserRefinementManager mRefinementManager; @@ -410,7 +411,7 @@ public class ChooserActivity extends Hilt_ChooserActivity implements } }); - boolean hasTouchScreen = getPackageManager() + boolean hasTouchScreen = mPackageManager .hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN); if (isVoiceInteraction() || !hasTouchScreen) { @@ -575,7 +576,7 @@ public class ChooserActivity extends Hilt_ChooserActivity implements private boolean canAppInteractCrossProfiles(String packageName) { ApplicationInfo applicationInfo; try { - applicationInfo = getPackageManager().getApplicationInfo(packageName, 0); + applicationInfo = mPackageManager.getApplicationInfo(packageName, 0); } catch (PackageManager.NameNotFoundException e) { Log.e(TAG, "Package " + packageName + " does not exist on current user."); return false; @@ -933,7 +934,7 @@ public class ChooserActivity extends Hilt_ChooserActivity implements private boolean supportsManagedProfiles(ResolveInfo resolveInfo) { try { - ApplicationInfo appInfo = getPackageManager().getApplicationInfo( + ApplicationInfo appInfo = mPackageManager.getApplicationInfo( resolveInfo.activityInfo.packageName, 0 /* default flags */); return appInfo.targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP; } catch (PackageManager.NameNotFoundException e) { @@ -2087,7 +2088,7 @@ public class ChooserActivity extends Hilt_ChooserActivity implements targetIntent, referrerFillInIntent, this, - context.getPackageManager(), + mPackageManager, getEventLog(), maxTargetsPerRow, initialIntentsUserSpace, @@ -2144,7 +2145,7 @@ public class ChooserActivity extends Hilt_ChooserActivity implements return new ChooserListController( this, - getPackageManager(), + mPackageManager, mLogic.getTargetIntent(), mLogic.getReferrerPackageName(), requireAnnotatedUserHandles().userIdOfCallingApp, |