summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/app/fedilab/android/mastodon/helper/ThemeHelper.java
blob: 460a9ff8db3894ef01c31b6d483871e51d58fc11 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package app.fedilab.android.mastodon.helper;
/* Copyright 2021 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 static android.content.Context.WINDOW_SERVICE;
import static app.fedilab.android.BaseMainActivity.currentInstance;
import static app.fedilab.android.BaseMainActivity.currentNightMode;
import static app.fedilab.android.BaseMainActivity.currentUserID;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

import androidx.annotation.AttrRes;
import androidx.annotation.ColorInt;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;

import com.google.android.material.color.DynamicColors;
import com.google.android.material.color.DynamicColorsOptions;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import app.fedilab.android.R;


public class ThemeHelper {


    @ColorInt
    public static int getAttColor(Context context, @AttrRes int attColor) {
        TypedValue typedValue = new TypedValue();
        Resources.Theme theme = context.getTheme();
        theme.resolveAttribute(attColor, typedValue, true);
        return typedValue.data;
    }


    public static int fetchAccentColor(Context context) {
        TypedValue typedValue = new TypedValue();
        TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[]{R.attr.colorAccent});
        int color = a.getColor(0, 0);
        a.recycle();
        return color;
    }


    /**
     * Animate two views, the current view will be hidden to left
     *
     * @param viewToHide     View to hide
     * @param viewToShow     View to show
     * @param slideAnimation listener for the animation
     */
    public static void slideViewsToLeft(View viewToHide, View viewToShow, SlideAnimation slideAnimation) {

        TranslateAnimation animateHide = new TranslateAnimation(
                0,
                -viewToHide.getWidth(),
                0,
                0);
        TranslateAnimation animateShow = new TranslateAnimation(
                viewToShow.getWidth(),
                0,
                0,
                0);
        animateShow.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                viewToShow.setVisibility(View.VISIBLE);
                viewToHide.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                viewToHide.setVisibility(View.GONE);
                if (slideAnimation != null) {
                    slideAnimation.onAnimationEnded();
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        animateHide.setDuration(300);
        animateHide.setFillAfter(true);
        animateShow.setDuration(300);
        animateShow.setFillAfter(true);
        viewToHide.startAnimation(animateHide);
        viewToShow.startAnimation(animateShow);
    }

    /**
     * Animate two views, the current view will be hidden to right
     *
     * @param viewToHide     View to hide
     * @param viewToShow     View to show
     * @param slideAnimation listener for the animation
     */
    public static void slideViewsToRight(View viewToHide, View viewToShow, SlideAnimation slideAnimation) {

        TranslateAnimation animateHide = new TranslateAnimation(
                0,
                viewToHide.getWidth(),
                0,
                0);
        TranslateAnimation animateShow = new TranslateAnimation(
                -viewToShow.getWidth(),
                0,
                0,
                0);
        animateShow.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                viewToShow.setVisibility(View.VISIBLE);
                viewToHide.setVisibility(View.VISIBLE);
                if (slideAnimation != null) {
                    slideAnimation.onAnimationEnded();
                }
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                viewToHide.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        animateHide.setDuration(300);
        animateShow.setDuration(300);
        viewToHide.requestLayout();
        viewToHide.startAnimation(animateHide);
        viewToShow.startAnimation(animateShow);
    }

    public static List<LinkedHashMap<String, String>> getContributorsTheme(Context context) {
        List<LinkedHashMap<String, String>> linkedHashMaps = new ArrayList<>();
        String[] list;
        try {
            list = context.getAssets().list("themes/contributors");
            for (String file : list) {
                InputStream is = context.getAssets().open("themes/contributors/" + file);
                LinkedHashMap<String, String> data = readCSVFile(is);
                linkedHashMaps.add(data);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return linkedHashMaps;
    }

    private static LinkedHashMap<String, String> readCSVFile(InputStream inputStream) {
        LinkedHashMap<String, String> readValues = new LinkedHashMap<>();
        if (inputStream != null) {
            BufferedReader br = null;
            try {
                br = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
                String sCurrentLine;
                while ((sCurrentLine = br.readLine()) != null) {
                    String[] line = sCurrentLine.split(",");
                    if (line.length > 1) {
                        String key = line[0];
                        String value = line[1];
                        readValues.put(key, value);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null) br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
        return readValues;
    }


    /**
     * Allow to set colors for no description on media
     *
     * @param context - Context
     * @return - ColorStateList
     */
    public static ColorStateList getNoDescriptionColorStateList(Context context) {
        int[][] states = new int[][]{
                new int[]{android.R.attr.state_selected},
                new int[]{-android.R.attr.state_selected},
        };
        int[] colors = new int[]{
                ContextCompat.getColor(context, R.color.no_description),
                ContextCompat.getColor(context, R.color.no_description),
        };
        return new ColorStateList(states, colors);
    }


    /**
     * Allow to set colors for having description on media
     *
     * @param context - Context
     * @return - ColorStateList
     */
    public static ColorStateList getHavingDescriptionColorStateList(Context context) {
        int[][] states = new int[][]{
                new int[]{android.R.attr.state_selected},
                new int[]{-android.R.attr.state_selected},
        };
        int[] colors = new int[]{
                ContextCompat.getColor(context, R.color.having_description),
                ContextCompat.getColor(context, R.color.having_description),
        };
        return new ColorStateList(states, colors);
    }


    /**
     * Allow to change font scale in activities
     *
     * @param activity      - Activity
     * @param configuration - Configuration
     */
    public static void adjustFontScale(Activity activity, Configuration configuration) {
        SharedPreferences prefs