Home | History | Annotate | Download | only in home
      1 package com.android.car.settings.home;
      2 
      3 import static com.android.settingslib.drawer.TileUtils.EXTRA_SETTINGS_ACTION;
      4 import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_ICON;
      5 import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_SUMMARY;
      6 import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_TITLE;
      7 
      8 import android.app.ActivityManager;
      9 import android.content.Context;
     10 import android.content.Intent;
     11 import android.content.pm.ActivityInfo;
     12 import android.content.pm.PackageManager;
     13 import android.content.pm.ResolveInfo;
     14 import android.content.res.Resources;
     15 import android.graphics.drawable.Icon;
     16 import android.os.Bundle;
     17 import android.text.TextUtils;
     18 
     19 import com.android.car.list.LaunchAppLineItem;
     20 import com.android.car.list.TypedPagedListAdapter;
     21 import com.android.car.settings.R;
     22 import com.android.car.settings.common.Logger;
     23 
     24 import java.util.Collection;
     25 import java.util.HashMap;
     26 import java.util.LinkedList;
     27 import java.util.List;
     28 import java.util.Map;
     29 
     30 /**
     31  * Loads Activity with TileUtils.EXTRA_SETTINGS_ACTION.
     32  * TODO: remove after all list new switched to androidx.car.widget
     33  */
     34 public class ExtraSettingsLoader {
     35     private static final Logger LOG = new Logger(ExtraSettingsLoader.class);
     36     private static final String META_DATA_PREFERENCE_CATEGORY = "com.android.settings.category";
     37     public static final String WIRELESS_CATEGORY = "com.android.settings.category.wireless";
     38     public static final String DEVICE_CATEGORY = "com.android.settings.category.device";
     39     public static final String SYSTEM_CATEGORY = "com.android.settings.category.system";
     40     public static final String PERSONAL_CATEGORY = "com.android.settings.category.personal";
     41     private final Context mContext;
     42 
     43     public ExtraSettingsLoader(Context context) {
     44         mContext = context;
     45     }
     46 
     47     public Map<String, Collection<TypedPagedListAdapter.LineItem>> load() {
     48         PackageManager pm = mContext.getPackageManager();
     49         Intent intent = new Intent(EXTRA_SETTINGS_ACTION);
     50         Map<String, Collection<TypedPagedListAdapter.LineItem>> extraSettings = new HashMap<>();
     51         // initialize the categories
     52         extraSettings.put(WIRELESS_CATEGORY, new LinkedList());
     53         extraSettings.put(DEVICE_CATEGORY, new LinkedList());
     54         extraSettings.put(SYSTEM_CATEGORY, new LinkedList());
     55         extraSettings.put(PERSONAL_CATEGORY, new LinkedList());
     56 
     57         List<ResolveInfo> results = pm.queryIntentActivitiesAsUser(intent,
     58                 PackageManager.GET_META_DATA, ActivityManager.getCurrentUser());
     59 
     60         for (ResolveInfo resolved : results) {
     61             if (!resolved.system) {
     62                 // Do not allow any app to add to settings, only system ones.
     63                 continue;
     64             }
     65             String title = null;
     66             String summary = null;
     67             String category = null;
     68             ActivityInfo activityInfo = resolved.activityInfo;
     69             Bundle metaData = activityInfo.metaData;
     70             try {
     71                 Resources res = pm.getResourcesForApplication(activityInfo.packageName);
     72                 if (metaData.containsKey(META_DATA_PREFERENCE_TITLE)) {
     73                     if (metaData.get(META_DATA_PREFERENCE_TITLE) instanceof Integer) {
     74                         title = res.getString(metaData.getInt(META_DATA_PREFERENCE_TITLE));
     75                     } else {
     76                         title = metaData.getString(META_DATA_PREFERENCE_TITLE);
     77                     }
     78                 }
     79                 if (metaData.containsKey(META_DATA_PREFERENCE_SUMMARY)) {
     80                     if (metaData.get(META_DATA_PREFERENCE_SUMMARY) instanceof Integer) {
     81                         summary = res.getString(metaData.getInt(META_DATA_PREFERENCE_SUMMARY));
     82                     } else {
     83                         summary = metaData.getString(META_DATA_PREFERENCE_SUMMARY);
     84                     }
     85                 } else {
     86                     LOG.d("no description.");
     87                 }
     88                 if (metaData.containsKey(META_DATA_PREFERENCE_CATEGORY)) {
     89                     if (metaData.get(META_DATA_PREFERENCE_CATEGORY) instanceof Integer) {
     90                         category = res.getString(metaData.getInt(META_DATA_PREFERENCE_CATEGORY));
     91                     } else {
     92                         category = metaData.getString(META_DATA_PREFERENCE_CATEGORY);
     93                     }
     94                 } else {
     95                     LOG.d("no category.");
     96                 }
     97             } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
     98                 LOG.d("Couldn't find info", e);
     99             }
    100             if (TextUtils.isEmpty(title)) {
    101                 LOG.d("no title.");
    102                 title = activityInfo.loadLabel(pm).toString();
    103             }
    104             Integer iconRes = null;
    105             Icon icon = null;
    106             if (metaData.containsKey(META_DATA_PREFERENCE_ICON)) {
    107                 iconRes = metaData.getInt(META_DATA_PREFERENCE_ICON);
    108                 icon = Icon.createWithResource(activityInfo.packageName, iconRes);
    109             } else {
    110                 icon = Icon.createWithResource(mContext, R.drawable.ic_settings_gear);
    111                 LOG.d("no icon.");
    112             }
    113             Intent extraSettingIntent =
    114                     new Intent().setClassName(activityInfo.packageName, activityInfo.name);
    115             if (category == null || !extraSettings.containsKey(category)) {
    116                 // If category is not specified or not supported, default to device.
    117                 category = DEVICE_CATEGORY;
    118             }
    119             extraSettings.get(category).add(new LaunchAppLineItem(
    120                     title, icon, mContext, summary, extraSettingIntent));
    121         }
    122         return extraSettings;
    123     }
    124 }
    125