summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/theme/base/impl/AospThemeImpl.java
blob: 35dcef27cdfed5ddb3d50c06c08a92e846195315 (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
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.theme.base.impl;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.support.annotation.ColorInt;
import android.support.annotation.StyleRes;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import com.android.dialer.common.Assert;
import com.android.dialer.theme.base.R;
import com.android.dialer.theme.base.Theme;
import javax.inject.Singleton;

/** Utility for fetching */
@SuppressWarnings("unused")
@Singleton
public class AospThemeImpl implements Theme {

  private int colorIcon = -1;
  private final ColorStateList colorIconStateList;
  private int colorIconSecondary = -1;
  private int colorPrimary = -1;
  private int colorPrimaryDark = -1;
  private int colorAccent = -1;
  private int textColorPrimary = -1;
  private int textColorSecondary = -1;
  private int textColorPrimaryInverse = -1;
  private int textColorHint = -1;
  private int colorBackground = -1;
  private int colorBackgroundFloating = -1;
  private int colorTextOnUnthemedDarkBackground = -1;
  private int colorIconOnUnthemedDarkBackground = -1;

  public AospThemeImpl(Context context) {

    context = context.getApplicationContext();
    context.setTheme(getApplicationThemeRes());
    TypedArray array =
        context
            .getTheme()
            .obtainStyledAttributes(
                getApplicationThemeRes(),
                new int[] {
                  android.R.attr.colorPrimary,
                  android.R.attr.colorPrimaryDark,
                  android.R.attr.colorAccent,
                  android.R.attr.textColorPrimary,
                  android.R.attr.textColorSecondary,
                  android.R.attr.textColorPrimaryInverse,
                  android.R.attr.textColorHint,
                  android.R.attr.colorBackground,
                  android.R.attr.colorBackgroundFloating,
                  R.attr.colorIcon,
                  R.attr.colorIconSecondary,
                  R.attr.colorTextOnUnthemedDarkBackground,
                  R.attr.colorIconOnUnthemedDarkBackground,
                });
    colorPrimary = array.getColor(/* index= */ 0, /* defValue= */ -1);
    colorPrimaryDark = array.getColor(/* index= */ 1, /* defValue= */ -1);
    colorAccent = array.getColor(/* index= */ 2, /* defValue= */ -1);
    textColorPrimary = array.getColor(/* index= */ 3, /* defValue= */ -1);
    textColorSecondary = array.getColor(/* index= */ 4, /* defValue= */ -1);
    textColorPrimaryInverse = array.getColor(/* index= */ 5, /* defValue= */ -1);
    textColorHint = array.getColor(/* index= */ 6, /* defValue= */ -1);
    colorBackground = array.getColor(/* index= */ 7, /* defValue= */ -1);
    colorBackgroundFloating = array.getColor(/* index= */ 8, /* defValue= */ -1);
    colorIcon = array.getColor(/* index= */ 9, /* defValue= */ -1);
    colorIconStateList = array.getColorStateList(/* index= */ 9);
    colorIconSecondary = array.getColor(/* index= */ 10, /* defValue= */ -1);
    colorTextOnUnthemedDarkBackground = array.getColor(/* index= */ 11, /* defValue= */ -1);
    colorIconOnUnthemedDarkBackground = array.getColor(/* index= */ 12, /* defValue= */ -1);
    array.recycle();
  }

  /**
   * Returns the {@link Theme} that the application is using. Activities should check this value if
   * their custom style needs to customize further based on the application theme.
   */
  @Override
  public @Type int getTheme() {
    // TODO(a bug): add share prefs check to configure this
    return LIGHT;
  }

  @Override
  public @StyleRes int getApplicationThemeRes() {
    switch (getTheme()) {
      case DARK:
        return R.style.Dialer_Dark_ThemeBase_NoActionBar;
      case LIGHT:
        return R.style.Dialer_ThemeBase_NoActionBar;
      case UNKNOWN:
      default:
        throw Assert.createIllegalStateFailException("Theme hasn't been set yet.");
    }
  }

  @Override
  public Context getThemedContext(Context context) {
    return new ContextThemeWrapper(context, getApplicationThemeRes());
  }

  @Override
  public LayoutInflater getThemedLayoutInflator(LayoutInflater inflater) {
    return inflater.cloneInContext(getThemedContext(inflater.getContext()));
  }

  @Override
  public @ColorInt int getColorIcon() {
    Assert.checkArgument(colorIcon != -1);
    return colorIcon;
  }

  @Override
  public ColorStateList getColorIconStateList() {
    Assert.checkArgument(colorIconStateList != null);
    return colorIconStateList;
  }

  @Override
  public @ColorInt int getColorIconSecondary() {
    Assert.checkArgument(colorIconSecondary != -1);
    return colorIconSecondary;
  }

  @Override
  public @ColorInt int getColorPrimary() {
    Assert.checkArgument(colorPrimary != -1);
    return colorPrimary;
  }

  @Override
  public int getColorPrimaryDark() {
    Assert.checkArgument(colorPrimaryDark != -1);
    return colorPrimaryDark;
  }

  @Override
  public @ColorInt int getColorAccent() {
    Assert.checkArgument(colorAccent != -1);
    return colorAccent;
  }

  @Override
  public @ColorInt int getTextColorSecondary() {
    Assert.checkArgument(textColorSecondary != -1);
    return textColorSecondary;
  }

  @Override
  public @ColorInt int getTextColorPrimary() {
    Assert.checkArgument(textColorPrimary != -1);
    return textColorPrimary;
  }

  @Override
  public @ColorInt int getColorTextOnUnthemedDarkBackground() {
    Assert.checkArgument(colorTextOnUnthemedDarkBackground != -1);
    return colorTextOnUnthemedDarkBackground;
  }

  @Override
  public @ColorInt int getColorIconOnUnthemedDarkBackground() {
    Assert.checkArgument(colorIconOnUnthemedDarkBackground != -1);
    return colorIconOnUnthemedDarkBackground;
  }
}