diff --git a/src/com/android/settings/location/AppSettingsInjector.java b/src/com/android/settings/location/AppSettingsInjector.java new file mode 100644 index 00000000000..6b79a7e0962 --- /dev/null +++ b/src/com/android/settings/location/AppSettingsInjector.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2018 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.location; + +import android.content.Context; +import android.text.TextUtils; + +import androidx.preference.Preference; + +import com.android.settings.widget.AppPreference; +import com.android.settings.widget.RestrictedAppPreference; +import com.android.settingslib.location.InjectedSetting; +import com.android.settingslib.location.SettingsInjector; + +import java.util.List; + +/** + * Adds the preferences specified by the {@link InjectedSetting} objects to a preference group. + */ +public class AppSettingsInjector extends SettingsInjector { + + public AppSettingsInjector(Context context) { + super(context); + } + + @Override + protected Preference createPreference(Context prefContext, InjectedSetting setting) { + return TextUtils.isEmpty(setting.userRestriction) + ? new AppPreference(prefContext) + : new RestrictedAppPreference(prefContext, setting.userRestriction); + } +} diff --git a/src/com/android/settings/location/LocationServicePreferenceController.java b/src/com/android/settings/location/LocationServicePreferenceController.java index 67ec1526a0d..035faad16c9 100644 --- a/src/com/android/settings/location/LocationServicePreferenceController.java +++ b/src/com/android/settings/location/LocationServicePreferenceController.java @@ -47,19 +47,19 @@ public class LocationServicePreferenceController extends LocationBasePreferenceC private PreferenceCategory mCategoryLocationServices; private final LocationSettings mFragment; - private final SettingsInjector mInjector; + private final AppSettingsInjector mInjector; /** Receives UPDATE_INTENT */ @VisibleForTesting BroadcastReceiver mInjectedSettingsReceiver; public LocationServicePreferenceController(Context context, LocationSettings fragment, Lifecycle lifecycle) { - this(context, fragment, lifecycle, new SettingsInjector(context)); + this(context, fragment, lifecycle, new AppSettingsInjector(context)); } @VisibleForTesting LocationServicePreferenceController(Context context, LocationSettings fragment, - Lifecycle lifecycle, SettingsInjector injector) { + Lifecycle lifecycle, AppSettingsInjector injector) { super(context, lifecycle); mFragment = fragment; mInjector = injector; diff --git a/src/com/android/settings/location/SettingsInjector.java b/src/com/android/settings/location/SettingsInjector.java deleted file mode 100644 index 41379330b9d..00000000000 --- a/src/com/android/settings/location/SettingsInjector.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * Copyright (C) 2018 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.location; - -import android.content.Context; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageItemInfo; -import android.content.pm.PackageManager; -import android.graphics.drawable.Drawable; -import android.os.UserHandle; -import android.os.UserManager; -import android.text.TextUtils; -import android.util.IconDrawableFactory; -import android.util.Log; - -import com.android.settings.widget.AppPreference; -import com.android.settings.widget.RestrictedAppPreference; -import com.android.settingslib.location.BaseSettingsInjector; -import com.android.settingslib.location.InjectedSetting; - -import java.util.ArrayList; -import java.util.List; - -import androidx.preference.Preference; - -/** - * Adds the preferences specified by the {@link InjectedSetting} objects to a preference group. - */ -public class SettingsInjector extends BaseSettingsInjector { - static final String TAG = "SettingsInjector"; - - Context mContext; - - public SettingsInjector(Context context) { - super(context); - mContext = context; - } - - /** - * Adds an injected setting to the root. - */ - private Preference addServiceSetting(Context prefContext, List prefs, - InjectedSetting info) { - final PackageManager pm = mContext.getPackageManager(); - Drawable appIcon = null; - try { - final PackageItemInfo itemInfo = new PackageItemInfo(); - itemInfo.icon = info.iconId; - itemInfo.packageName = info.packageName; - final ApplicationInfo appInfo = pm.getApplicationInfo(info.packageName, - PackageManager.GET_META_DATA); - appIcon = IconDrawableFactory.newInstance(mContext) - .getBadgedIcon(itemInfo, appInfo, info.mUserHandle.getIdentifier()); - } catch (PackageManager.NameNotFoundException e) { - Log.e(TAG, "Can't get ApplicationInfo for " + info.packageName, e); - } - Preference pref = TextUtils.isEmpty(info.userRestriction) - ? new AppPreference(prefContext) - : new RestrictedAppPreference(prefContext, info.userRestriction); - pref.setTitle(info.title); - pref.setSummary(null); - pref.setIcon(appIcon); - pref.setOnPreferenceClickListener(new ServiceSettingClickedListener(info)); - prefs.add(pref); - return pref; - } - - /** - * Gets a list of preferences that other apps have injected. - * - * @param profileId Identifier of the user/profile to obtain the injected settings for or - * UserHandle.USER_CURRENT for all profiles associated with current user. - */ - public List getInjectedSettings(Context prefContext, final int profileId) { - final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE); - final List profiles = um.getUserProfiles(); - ArrayList prefs = new ArrayList<>(); - final int profileCount = profiles.size(); - for (int i = 0; i < profileCount; ++i) { - final UserHandle userHandle = profiles.get(i); - if (profileId == UserHandle.USER_CURRENT || profileId == userHandle.getIdentifier()) { - Iterable settings = getSettings(userHandle); - for (InjectedSetting setting : settings) { - Preference pref = addServiceSetting(prefContext, prefs, setting); - mSettings.add(new Setting(setting, pref)); - } - } - } - - reloadStatusMessages(); - - return prefs; - } -} diff --git a/tests/robotests/src/com/android/settings/location/LocationServicePreferenceControllerTest.java b/tests/robotests/src/com/android/settings/location/LocationServicePreferenceControllerTest.java index ba5decceb70..f0904d03316 100644 --- a/tests/robotests/src/com/android/settings/location/LocationServicePreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/location/LocationServicePreferenceControllerTest.java @@ -67,7 +67,7 @@ public class LocationServicePreferenceControllerTest { @Mock private PreferenceScreen mScreen; @Mock - private SettingsInjector mSettingsInjector; + private AppSettingsInjector mSettingsInjector; @Mock private DevicePolicyManager mDevicePolicyManager;