blob: bf844311e48f1762f8c1701f54ba088668b995ec [file] [log] [blame]
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +02001/*
2 * Copyright (C) 2017 The OmniROM Project
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
micky38798506412023-10-23 16:33:47 +020018package omnirom.preference;
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020019
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.pm.PackageManager;
23import android.graphics.drawable.Drawable;
Vachounet41e03b62019-09-11 10:40:12 +020024import androidx.preference.Preference;
25import androidx.preference.PreferenceViewHolder;
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020026import android.util.AttributeSet;
27import android.util.Log;
28import android.view.LayoutInflater;
29import android.view.View;
30import android.widget.ImageView;
31import android.widget.LinearLayout;
32
micky38798506412023-10-23 16:33:47 +020033import omnirom.preference.R;
Juan Ezquerro LLanesbd134a72018-05-22 23:11:23 +020034
35import java.util.ArrayList;
36import java.util.Collection;
37import java.util.List;
38
39public class ScrollAppsViewPreference extends Preference {
40 private static final String TAG = "ScrollAppsPreference";
41
42 private Context mContext;
43 private List<String> mValues = new ArrayList<String>();
44 private PackageManager mPm;
45 private LayoutInflater mInflater;
46
47 public ScrollAppsViewPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
48 super(context, attrs, defStyleAttr, defStyleRes);
49 initPreference(context);
50 }
51
52 public ScrollAppsViewPreference(Context context, AttributeSet attrs, int defStyleAttr) {
53 super(context, attrs, defStyleAttr);
54 initPreference(context);
55 }
56
57 public ScrollAppsViewPreference(Context context, AttributeSet attrs) {
58 super(context, attrs);
59 initPreference(context);
60 }
61
62 public void setValues(Collection<String> values) {
63 mValues.clear();
64 mValues.addAll(values);
65 }
66
67 @Override
68 public void onBindViewHolder(PreferenceViewHolder holder) {
69 super.onBindViewHolder(holder);
70 LinearLayout linearLayout = (LinearLayout) holder.findViewById(R.id.selected_apps);
71 if (linearLayout.getChildCount() > 0) linearLayout.removeAllViews();
72
73 for (String value : mValues) {
74 try {
75 View v = mInflater.inflate(R.layout.app_grid_item, null);
76 ComponentName componentName = ComponentName.unflattenFromString(value);
77 Drawable icon = mPm.getActivityIcon(componentName);
78 ((ImageView) v.findViewById(R.id.appIcon)).setImageDrawable(icon);
79 v.setPadding(10, 5, 10, 5);
80 linearLayout.addView(v);
81 } catch (PackageManager.NameNotFoundException e) {
82 Log.e(TAG, "Set app icon", e);
83 }
84 }
85 }
86
87 private void initPreference(Context context) {
88 mContext = context;
89 setLayoutResource(R.layout.preference_selected_apps_view);
90 mPm = context.getPackageManager();
91 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
92 }
93}