1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.car.settings.applications; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 import android.graphics.drawable.Drawable; 22 23 import androidx.preference.Preference; 24 import androidx.preference.PreferenceGroup; 25 26 import com.android.car.settings.common.FragmentController; 27 import com.android.car.settings.common.PreferenceController; 28 import com.android.car.ui.preference.CarUiPreference; 29 import com.android.settingslib.applications.ApplicationsState; 30 31 import java.util.ArrayList; 32 33 /** Business logic which populates the applications in this setting. */ 34 public class ApplicationsSettingsPreferenceController extends 35 PreferenceController<PreferenceGroup> implements 36 ApplicationListItemManager.AppListItemListener { 37 ApplicationsSettingsPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)38 public ApplicationsSettingsPreferenceController(Context context, String preferenceKey, 39 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 40 super(context, preferenceKey, fragmentController, uxRestrictions); 41 } 42 43 @Override getPreferenceType()44 protected Class<PreferenceGroup> getPreferenceType() { 45 return PreferenceGroup.class; 46 } 47 48 @Override onDataLoaded(ArrayList<ApplicationsState.AppEntry> apps)49 public void onDataLoaded(ArrayList<ApplicationsState.AppEntry> apps) { 50 getPreference().removeAll(); 51 for (ApplicationsState.AppEntry appEntry : apps) { 52 getPreference().addPreference( 53 createPreference(appEntry.label, appEntry.sizeStr, appEntry.icon, 54 appEntry.info.packageName)); 55 } 56 } 57 createPreference(String title, String summary, Drawable icon, String packageName)58 private Preference createPreference(String title, String summary, Drawable icon, 59 String packageName) { 60 CarUiPreference preference = new CarUiPreference(getContext()); 61 preference.setTitle(title); 62 preference.setSummary(summary); 63 preference.setIcon(icon); 64 preference.setKey(packageName); 65 preference.setOnPreferenceClickListener(p -> { 66 getFragmentController().launchFragment( 67 ApplicationDetailsFragment.getInstance(packageName)); 68 return true; 69 }); 70 return preference; 71 } 72 } 73