Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ThemeUtils.java « util « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8eef7ba2e47634aeba299d9cc1c4738357e01022 (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
package com.mapswithme.util;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.annotation.AttrRes;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.appcompat.view.ContextThemeWrapper;
import android.util.TypedValue;
import android.view.LayoutInflater;

import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;

public final class ThemeUtils
{
  public static final String THEME_DEFAULT = MwmApplication.get().getString(R.string.theme_default);
  public static final String THEME_NIGHT = MwmApplication.get().getString(R.string.theme_night);
  public static final String THEME_AUTO = MwmApplication.get().getString(R.string.theme_auto);

  private static final TypedValue VALUE_BUFFER = new TypedValue();

  private ThemeUtils() {}

  public static @ColorInt int getColor(Context context, @AttrRes int attr)
  {
    if (!context.getTheme().resolveAttribute(attr, VALUE_BUFFER, true))
      throw new IllegalArgumentException("Failed to resolve color theme attribute");

    return VALUE_BUFFER.data;
  }

  public static int getResource(Context context, @AttrRes int attr)
  {
    if (!context.getTheme().resolveAttribute(attr, VALUE_BUFFER, true))
      throw new IllegalArgumentException("Failed to resolve theme attribute");

    return VALUE_BUFFER.resourceId;
  }

  public static int getResource(Context context, @AttrRes int style, @AttrRes int attr)
  {
    int styleRef = getResource(context, style);

    int[] attrs = new int[] { attr };
    TypedArray ta = context.getTheme().obtainStyledAttributes(styleRef, attrs);
    ta.getValue(0, VALUE_BUFFER);
    ta.recycle();

    return VALUE_BUFFER.resourceId;
  }

  public static LayoutInflater themedInflater(LayoutInflater src, @StyleRes int theme)
  {
    Context wrapper = new ContextThemeWrapper(src.getContext(), theme);
    return src.cloneInContext(wrapper);
  }

  public static boolean isDefaultTheme()
  {
    return isDefaultTheme(Config.getCurrentUiTheme());
  }

  public static boolean isDefaultTheme(String theme)
  {
    return THEME_DEFAULT.equals(theme);
  }

  public static boolean isNightTheme()
  {
    return isNightTheme(Config.getCurrentUiTheme());
  }

  public static boolean isNightTheme(String theme)
  {
    return THEME_NIGHT.equals(theme);
  }

  public static boolean isAutoTheme()
  {
    return THEME_AUTO.equals(Config.getUiThemeSettings());
  }

  public static boolean isAutoTheme(String theme)
  {
    return THEME_AUTO.equals(theme);
  }

  public static boolean isValidTheme(String theme)
  {
    return (THEME_DEFAULT.equals(theme) ||
            THEME_NIGHT.equals(theme));
  }

  @StyleRes
  public static int getCardBgThemeResourceId(@NonNull String theme)
  {
    if (isDefaultTheme(theme))
      return R.style.MwmTheme_CardBg;

    if (isNightTheme(theme))
      return R.style.MwmTheme_Night_CardBg;

    throw new IllegalArgumentException("Attempt to apply unsupported theme: " + theme);
  }

  @StyleRes
  public static int getWindowBgThemeResourceId(@NonNull String theme)
  {
    if (isDefaultTheme(theme))
      return R.style.MwmTheme_WindowBg;

    if (isNightTheme(theme))
      return R.style.MwmTheme_Night_WindowBg;

    throw new IllegalArgumentException("Attempt to apply unsupported theme: " + theme);
  }
}