summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/app/fedilab/android/mastodon/helper/ImageListPreference.java
blob: 42408ce32ac524f3a978436ac7c3664d84af9e01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package app.fedilab.android.mastodon.helper;
/* Copyright 2023 Thomas Schneider
 *
 * This file is a part of Fedilab
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 *
 * Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with Fedilab; if not,
 * see <http://www.gnu.org/licenses>. */

import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.ListPreference;

import java.util.ArrayList;
import java.util.List;

import app.fedilab.android.R;

public class ImageListPreference extends ListPreference {

    private static final int DEFAULT_TINT = 0;
    private static final int DEFAULT_BACKGROUND_TINT = 0xFFFFFFFF;
    private final List<Integer> mImages;
    private int mErrorResource;
    private int mTintColor;
    private int mBackgroundColor;
    private boolean mUseCard;
    private int mCustomItemLayout;

    public ImageListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);

        mImages = new ArrayList<>();

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ImageListPreference);

        try {
            int entryImagesArrayResource = array.getResourceId(R.styleable.ImageListPreference_ilp_entryImages, 0);
            String tintKey = array.getNonResourceString(R.styleable.ImageListPreference_ilp_tintKey);
            String backgroundKey = array.getNonResourceString(R.styleable.ImageListPreference_ilp_backgroundTint);

            mTintColor = array.getColor(R.styleable.ImageListPreference_ilp_tint, DEFAULT_TINT);
            mBackgroundColor = array.getColor(R.styleable.ImageListPreference_ilp_backgroundTint, 0);
            mErrorResource = array.getResourceId(R.styleable.ImageListPreference_ilp_errorImage, 0);
            mUseCard = array.getBoolean(R.styleable.ImageListPreference_ilp_useCard, false);
            mCustomItemLayout = array.getResourceId(R.styleable.ImageListPreference_ilp_itemLayout, 0);

            if (tintKey != null) {
                mTintColor = sharedPreferences.getInt(tintKey, mTintColor);
            }
            if (backgroundKey != null) {
                mBackgroundColor = sharedPreferences.getInt(backgroundKey, mBackgroundColor);
            }

            TypedArray images = context.getResources().obtainTypedArray(entryImagesArrayResource);

            for (int i = 0; i < images.length(); i++) {
                mImages.add(images.getResourceId(i, 0));
            }

            images.recycle();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            array.recycle();
        }
    }

    @Override
    protected void onClick() {
        List<ImageListItem> items = new ArrayList<>();
        SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
        String launcher = sharedpreferences.getString(getContext().getString(R.string.SET_LOGO_LAUNCHER), "Bubbles");
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        int length = getEntries().length;
        for (int i = 0; i < length; i++) {
            int resource = 0;
            if (mImages.size() > i) {
                resource = mImages.get(i);
            }
            items.add(new ImageListItem(getEntries()[i], resource, getEntryValues()[i].equals(launcher)));
        }

        int layout = R.layout.imagelistpreference_item;
        if (mUseCard) {
            layout = R.layout.imagelistpreference_item_card;
        }
        if (mCustomItemLayout != 0) {
            layout = mCustomItemLayout;
        }

        ListAdapter adapter = new ImageListPreferenceAdapter(getContext(), layout, items);
        builder.setAdapter(adapter, (dialogInterface, which) -> {
            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putString(getContext().getString(R.string.SET_LOGO_LAUNCHER), String.valueOf(getEntryValues()[which]));
            editor.commit();
        });
        builder.create().show();

    }

    private static class ImageListItem {
        private final int resource;
        private final boolean isChecked;
        private final String name;

        ImageListItem(CharSequence name, int resource, boolean isChecked) {
            this(name.toString(), resource, isChecked);
        }

        ImageListItem(String name, int resource, boolean isChecked) {
            this.name = name;
            this.resource = resource;
            this.isChecked = isChecked;
        }
    }

    private static class ViewHolder {
        ImageView iconImage;
        TextView iconName;
        RadioButton radioButton;
    }

    private class ImageListPreferenceAdapter extends ArrayAdapter<ImageListItem> {
        private final List<ImageListItem> mItems;
        private final int mLayoutResource;

        ImageListPreferenceAdapter(Context context, int layoutResource, List<ImageListItem> items) {
            super(context, layoutResource, items);
            mLayoutResource = layoutResource;
            mItems = items;
        }

        @Override
        public @NonNull
        View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

            ViewHolder holder;
            if (convertView == null) {
                LayoutInflater inflater = (LayoutInflater) getContext().
                        getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                try {
                    assert inflater != null;
                    convertView = inflater.inflate(mLayoutResource, parent, false);

                    holder = new ViewHolder();
                    holder.iconName = convertView.findViewById(R.id.imagelistpreference_text);
                    holder.iconImage = convertView.findViewById(R.id.imagelistpreference_image);
                    holder.radioButton = convertView.findViewById(R.id.imagelistpreference_radio);
                    convertView.setTag(holder);
                } catch (Exception e) {
                    e.printStackTrace();
                    return super.getView(position, null, parent);
                }
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if (holder == null) {
                return super.getView(position, convertView, parent);
            }

            ImageListItem item = mItems.get(position);

            holder.iconName.setText(item.name);

            if (item.resource != 0) {
                holder.iconImage.setImageResource(item.resource);
            } else {
                holder.iconImage.setImageResource(mErrorResource);
            }

            if (mTintColor != 0) {
                holder.iconImage.setColorFilter(mTintColor);
            }
            if (mBackgroundColor != 0) {
                holder.iconImage.setBackgroundColor(mBackgroundColor);
            }

            holder.radioButton.setChecked(item.isChecked);

            return convertView;
        }
    }
}