Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 1 | /* |
| 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 | */ |
micky387 | 9850641 | 2023-10-23 16:33:47 +0200 | [diff] [blame^] | 18 | package omnirom.preference; |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 19 | |
| 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.pm.PackageManager; |
| 23 | import android.graphics.drawable.Drawable; |
Vachounet | 41e03b6 | 2019-09-11 10:40:12 +0200 | [diff] [blame] | 24 | import androidx.preference.Preference; |
| 25 | import androidx.preference.PreferenceViewHolder; |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 26 | import android.util.AttributeSet; |
| 27 | import android.util.Log; |
| 28 | import android.view.LayoutInflater; |
| 29 | import android.view.View; |
| 30 | import android.widget.ImageView; |
| 31 | import android.widget.LinearLayout; |
| 32 | |
micky387 | 9850641 | 2023-10-23 16:33:47 +0200 | [diff] [blame^] | 33 | import omnirom.preference.R; |
Juan Ezquerro LLanes | bd134a7 | 2018-05-22 23:11:23 +0200 | [diff] [blame] | 34 | |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.Collection; |
| 37 | import java.util.List; |
| 38 | |
| 39 | public 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 | } |