refactor to allow more code reuse
Merge from pi-car-dev to master
Bug: 110376110
Test: Tested on device
Change-Id: Ibfd85571b72d0bbc9277a1a68245fdac78fe9cc4
diff --git a/packages/SettingsLib/src/com/android/settingslib/location/InjectedSetting.java b/packages/SettingsLib/src/com/android/settingslib/location/InjectedSetting.java
index 7a5c3be..1805f1a 100644
--- a/packages/SettingsLib/src/com/android/settingslib/location/InjectedSetting.java
+++ b/packages/SettingsLib/src/com/android/settingslib/location/InjectedSetting.java
@@ -178,8 +178,8 @@
         public InjectedSetting build() {
             if (mPackageName == null || mClassName == null || TextUtils.isEmpty(mTitle)
                     || TextUtils.isEmpty(mSettingsActivity)) {
-                if (Log.isLoggable(BaseSettingsInjector.TAG, Log.WARN)) {
-                    Log.w(BaseSettingsInjector.TAG, "Illegal setting specification: package="
+                if (Log.isLoggable(SettingsInjector.TAG, Log.WARN)) {
+                    Log.w(SettingsInjector.TAG, "Illegal setting specification: package="
                             + mPackageName + ", class=" + mClassName
                             + ", title=" + mTitle + ", settingsActivity=" + mSettingsActivity);
                 }
diff --git a/packages/SettingsLib/src/com/android/settingslib/location/BaseSettingsInjector.java b/packages/SettingsLib/src/com/android/settingslib/location/SettingsInjector.java
similarity index 88%
rename from packages/SettingsLib/src/com/android/settingslib/location/BaseSettingsInjector.java
rename to packages/SettingsLib/src/com/android/settingslib/location/SettingsInjector.java
index f2d730f..780fcba 100644
--- a/packages/SettingsLib/src/com/android/settingslib/location/BaseSettingsInjector.java
+++ b/packages/SettingsLib/src/com/android/settingslib/location/SettingsInjector.java
@@ -20,12 +20,14 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageItemInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.content.pm.ServiceInfo;
 import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.content.res.XmlResourceParser;
+import android.graphics.drawable.Drawable;
 import android.location.SettingInjectorService;
 import android.os.Bundle;
 import android.os.Handler;
@@ -36,6 +38,7 @@
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.util.AttributeSet;
+import android.util.IconDrawableFactory;
 import android.util.Log;
 import android.util.Xml;
 
@@ -62,7 +65,7 @@
  * android.content.pm.RegisteredServicesCache#parseServiceAttributes(android.content.res.Resources,
  * String, android.util.AttributeSet)} into an interface, which didn't seem worth it.
  */
-public class BaseSettingsInjector {
+public class SettingsInjector {
     static final String TAG = "SettingsInjector";
 
     /**
@@ -96,7 +99,7 @@
 
     private final Handler mHandler;
 
-    public BaseSettingsInjector(Context context) {
+    public SettingsInjector(Context context) {
         mContext = context;
         mSettings = new HashSet<Setting>();
         mHandler = new StatusLoadingHandler();
@@ -145,6 +148,65 @@
     }
 
     /**
+     * Adds the InjectedSetting information to a Preference object
+     */
+    private void populatePreference(Preference preference, InjectedSetting setting) {
+        final PackageManager pm = mContext.getPackageManager();
+        Drawable appIcon = null;
+        try {
+            final PackageItemInfo itemInfo = new PackageItemInfo();
+            itemInfo.icon = setting.iconId;
+            itemInfo.packageName = setting.packageName;
+            final ApplicationInfo appInfo = pm.getApplicationInfo(setting.packageName,
+                    PackageManager.GET_META_DATA);
+            appIcon = IconDrawableFactory.newInstance(mContext)
+                    .getBadgedIcon(itemInfo, appInfo, setting.mUserHandle.getIdentifier());
+        } catch (PackageManager.NameNotFoundException e) {
+            Log.e(TAG, "Can't get ApplicationInfo for " + setting.packageName, e);
+        }
+        preference.setTitle(setting.title);
+        preference.setSummary(null);
+        preference.setIcon(appIcon);
+        preference.setOnPreferenceClickListener(new ServiceSettingClickedListener(setting));
+    }
+
+    /**
+     * 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<Preference> getInjectedSettings(Context prefContext, final int profileId) {
+        final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
+        final List<UserHandle> profiles = um.getUserProfiles();
+        ArrayList<Preference> prefs = new ArrayList<>();
+        for (UserHandle userHandle : profiles) {
+            if (profileId == UserHandle.USER_CURRENT || profileId == userHandle.getIdentifier()) {
+                Iterable<InjectedSetting> settings = getSettings(userHandle);
+                for (InjectedSetting setting : settings) {
+                    Preference preference = createPreference(prefContext, setting);
+                    populatePreference(preference, setting);
+                    prefs.add(preference);
+                    mSettings.add(new Setting(setting, preference));
+                }
+            }
+        }
+
+        reloadStatusMessages();
+
+        return prefs;
+    }
+
+    /**
+     * Creates an injected Preference
+     *
+     * @return the created Preference
+     */
+    protected Preference createPreference(Context prefContext, InjectedSetting setting) {
+        return new Preference(prefContext);
+    }
+
+    /**
      * Returns the settings parsed from the attributes of the
      * {@link SettingInjectorService#META_DATA_NAME} tag, or null.
      *