/** * Adds or updates given service in the system. * * If service with the same {@link ComponentInfo#name} and {@code ComponentInfo#packageName} * exists it will be updated. Its {@link ComponentInfo#applicationInfo} is always set to {@link * ApplicationInfo} already existing in the system, but if no application exists a new one will be * created using {@link ComponentInfo#applicationInfo} in this component. */ public void addOrUpdateService(ServiceInfo serviceInfo) { addComponent( serviceFilters, p -> p.services, (p, a) -> p.services = a, new ServiceInfo(serviceInfo), true); }
private static ServiceInfo getServiceInfo(final Context context, final String packageName) { try { PackageManager pkgMgr = context.getPackageManager(); PackageInfo pkg = pkgMgr.getPackageInfo(packageName, PackageManager.GET_SERVICES); if (pkg != null) { ServiceInfo[] services = pkg.services; if (services != null) { for (int i = 0; i < services.length; i++) { String pkgName = services[i].packageName; String className = services[i].name; ComponentName component = new ComponentName(pkgName, className); ServiceInfo serviceInfo = pkgMgr.getServiceInfo(component, PackageManager.GET_META_DATA); if (serviceInfo.metaData != null) { Object xmlData = serviceInfo.metaData.get(PLUGIN_META_DATA); if (xmlData instanceof Integer) { XmlResourceParser xrp = serviceInfo.loadXmlMetaData(pkgMgr, PLUGIN_META_DATA); if (xrp != null) { return serviceInfo; } } } } } } return null; } catch (NameNotFoundException e) { return null; } }
public static String getOpenPgpProviderName(PackageManager packageManager, String openPgpProvider) { Intent intent = new Intent(OpenPgpApi.SERVICE_INTENT_2); intent.setPackage(openPgpProvider); List<ResolveInfo> resInfo = packageManager.queryIntentServices(intent, 0); if (resInfo == null) { return null; } for (ResolveInfo resolveInfo : resInfo) { if (resolveInfo.serviceInfo == null) { continue; } return String.valueOf(resolveInfo.serviceInfo.loadLabel(packageManager)); } return null; }
@Test @Config(maxSdk = 23) @SdkSuppress(maxSdkVersion = 23) public void getPackageInfo_disabledAplication_stillReturned_below24() throws Exception { pm.setApplicationEnabledSetting( context.getPackageName(), COMPONENT_ENABLED_STATE_DISABLED, DONT_KILL_APP); PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), GET_SERVICES | GET_ACTIVITIES); ActivityInfo[] activities = filterExtraneous(packageInfo.activities); assertThat(packageInfo.packageName).isEqualTo(context.getPackageName()); assertThat(packageInfo.applicationInfo.enabled).isFalse(); // Seems that although disabled app makes everything disabled it is still returned with its // manifest state below API 23 assertThat(activities).hasLength(1); assertThat(packageInfo.services).hasLength(1); assertThat(activities[0].enabled).isTrue(); assertThat(packageInfo.services[0].enabled).isTrue(); assertThat(activities[0].isEnabled()).isFalse(); assertThat(packageInfo.services[0].isEnabled()).isFalse(); }
final XmlResourceParser parser = serviceInfo.loadXmlMetaData( pm, metadataName); if (parser != null) {
/** * Makes sure that given service exists. * * If the service doesn't exist yet, it will be created with {@code applicationInfo} set to an * existing application, or if it doesn't exist, a new package will be created. * * @return existing or newly created service info. */ public ServiceInfo addServiceIfNotPresent(ComponentName componentName) { return addComponent( serviceFilters, p -> p.services, (p, a) -> p.services = a, updateName(componentName, new ServiceInfo()), false); }
public static ServiceInfo generateServiceInfo(VPackage.ServiceComponent s, int flags, PackageUserState state, int userId) { if (s == null) return null; if (!checkUseInstalledOrHidden(state, flags)) { return null; } ServiceInfo si = new ServiceInfo(s.info); // Make shallow copies so we can store the metadata safely if ((flags & PackageManager.GET_META_DATA) != 0 && s.metaData != null) { si.metaData = s.metaData; } si.applicationInfo = generateApplicationInfo(s.owner, flags, state, userId); return si; }
private ArgumentCaptor<Intent> mockPackageManagerInfo() { PackageManager packageManager = mock(PackageManager.class); when(mMockContext.getPackageManager()).thenReturn(packageManager); ArgumentCaptor<Intent> intentArgCaptor = ArgumentCaptor.forClass(Intent.class); ResolveInfo info = new ResolveInfo(); info.serviceInfo = new ServiceInfo(); info.serviceInfo.enabled = true; // noinspection WrongConstant when(packageManager.queryIntentServices(intentArgCaptor.capture(), eq(0))) .thenReturn(Arrays.asList(info)); return intentArgCaptor; }
@Test public void shouldReturnExpectedAccessibilityServiceList() { List<ServiceInfo> expected = new ArrayList<>(Arrays.asList(new ServiceInfo())); shadowOf(accessibilityManager).setAccessibilityServiceList(expected); assertThat(accessibilityManager.getAccessibilityServiceList()).isEqualTo(expected); }
@Test public void validateService_noActiveService() { when(mockContext.getPackageManager()).thenReturn(packageManagerMock); ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.serviceInfo = new ServiceInfo(); resolveInfo.serviceInfo.enabled = false; when(packageManagerMock.queryIntentServices(any(Intent.class), eq(0))) .thenReturn(Arrays.asList(resolveInfo)); when(mockContext.getPackageName()).thenReturn("package"); List<String> errors = validator.validateService("service"); assertTrue(errors.contains("service is disabled.")); }
@Test public void queryIntentServices_Match() throws Exception { Intent i = new Intent(Intent.ACTION_MAIN, null); ResolveInfo info = new ResolveInfo(); info.serviceInfo = new ServiceInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; shadowPackageManager.addResolveInfoForIntent(i, info); List<ResolveInfo> services = packageManager.queryIntentServices(i, 0); assertThat(services).hasSize(1); assertThat(services.get(0).nonLocalizedLabel.toString()).isEqualTo(TEST_PACKAGE_LABEL); }
/** * @return A mocked context which returns a spy of {@link RuntimeEnvironment#application} in * {@link Context#getApplicationContext()}. */ public static Context createMockContext() { // otherwise the JobScheduler isn't supported we check if the service is enabled // Robolectric doesn't parse services from the manifest, see https://github.com/robolectric/robolectric/issues/416 PackageManager packageManager = mock(PackageManager.class); when(packageManager.queryBroadcastReceivers(any(Intent.class), anyInt())).thenReturn(Collections.singletonList(new ResolveInfo())); ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.serviceInfo = new ServiceInfo(); resolveInfo.serviceInfo.permission = "android.permission.BIND_JOB_SERVICE"; when(packageManager.queryIntentServices(any(Intent.class), anyInt())).thenReturn(Collections.singletonList(resolveInfo)); Context context = spy(RuntimeEnvironment.application); when(context.getPackageManager()).thenReturn(packageManager); when(context.getApplicationContext()).thenReturn(context); Context mockContext = mock(MockContext.class); when(mockContext.getApplicationContext()).thenReturn(context); return mockContext; } }
@Test public void validateService() { when(mockContext.getPackageManager()).thenReturn(packageManagerMock); ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.serviceInfo = new ServiceInfo(); resolveInfo.serviceInfo.enabled = true; when(packageManagerMock.queryIntentServices(any(Intent.class), eq(0))) .thenReturn(Arrays.asList(resolveInfo)); when(mockContext.getPackageName()).thenReturn("package"); List<String> errors = validator.validateService("service"); assertNull(errors); } }
@Test public void addService() throws Exception { ServiceInfo serviceInfo = new ServiceInfo(); serviceInfo.name = "name"; serviceInfo.packageName = "package"; shadowPackageManager.addOrUpdateService(serviceInfo); assertThat(packageManager.getServiceInfo(new ComponentName("package", "name"), 0)).isNotNull(); }
@Test public void queryIntentActivities_ServiceMatch() throws Exception { Intent i = new Intent("SomeStrangeAction"); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; info.serviceInfo = new ServiceInfo(); info.serviceInfo.name = "name"; info.serviceInfo.packageName = TEST_PACKAGE_NAME; shadowPackageManager.addResolveInfoForIntent(i, info); List<ResolveInfo> activities = packageManager.queryIntentActivities(i, 0); assertThat(activities).isNotNull(); assertThat(activities).isEmpty(); }
private ResolveInfo buildResolveInfo(final String pkg, final boolean service_or_receiver, final int uid) { final ResolveInfo r = new ResolveInfo() { @Override public String toString() { return "ResolveInfo{test}"; } }; final ComponentInfo info = service_or_receiver ? (r.serviceInfo = new ServiceInfo()) : (r.activityInfo = new ActivityInfo()); info.packageName = pkg; info.applicationInfo = new ApplicationInfo(); info.applicationInfo.packageName = pkg; info.applicationInfo.uid = uid; return r; } };
private void mockChromeCustomTabsSupported(final boolean supported, final String packageName) { final List<ResolveInfo> resolveInfos = new ArrayList<>(); ResolveInfo resolveInfo = new ResolveInfo(); ServiceInfo serviceInfo = new ServiceInfo(); serviceInfo.packageName = packageName; resolveInfo.serviceInfo = serviceInfo; if (supported) { resolveInfos.add(resolveInfo); } final PackageManager packageManager = mock(PackageManager.class); when(packageManager.queryIntentServices(any(Intent.class), anyInt())) .thenReturn(resolveInfos); activity = mock(FacebookActivity.class); when(mockLoginClient.getActivity()).thenReturn(activity); when(activity.getPackageManager()).thenReturn(packageManager); }
@Test public void getServiceInfo_shouldFindServiceIfAddedInResolveInfo() throws Exception { ComponentName componentName = new ComponentName("com.test", "com.test.ServiceName"); ResolveInfo resolveInfo = new ResolveInfo(); resolveInfo.serviceInfo = new ServiceInfo(); resolveInfo.serviceInfo.name = componentName.getClassName(); resolveInfo.serviceInfo.applicationInfo = new ApplicationInfo(); resolveInfo.serviceInfo.applicationInfo.packageName = componentName.getPackageName(); shadowPackageManager.addResolveInfoForIntent(new Intent("RANDOM_ACTION"), resolveInfo); ServiceInfo serviceInfo = packageManager.getServiceInfo(componentName, 0); assertThat(serviceInfo).isNotNull(); }
@Test public void removeResolveInfosForIntent_forService() throws Exception { Intent intent = new Intent(Intent.ACTION_APP_ERROR, null).addCategory(Intent.CATEGORY_APP_BROWSER); ResolveInfo info = new ResolveInfo(); info.nonLocalizedLabel = TEST_PACKAGE_LABEL; info.serviceInfo = new ServiceInfo(); info.serviceInfo.packageName = "com.org"; shadowPackageManager.addResolveInfoForIntent(intent, info); shadowPackageManager.removeResolveInfosForIntent(intent, "com.org"); assertThat(packageManager.resolveService(intent, 0)).isNull(); }