summaryrefslogtreecommitdiff
path: root/main/java/com/google/android/setupcompat/template/StatusBarMixin.java
blob: ef482bf91361e148fdcf84dd0cc180788b48f08e (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
/*
 * 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.google.android.setupcompat.template;

import static android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import android.util.AttributeSet;
import android.view.View;
import android.view.Window;
import com.google.android.setupcompat.PartnerCustomizationLayout;
import com.google.android.setupcompat.R;
import com.google.android.setupcompat.util.PartnerConfig;
import com.google.android.setupcompat.util.PartnerConfigHelper;
import com.google.android.setupcompat.view.StatusBarBackgroundLayout;

/**
 * A {@link Mixin} for setting and getting background color, and window compatible light/dark theme
 * of status bar.
 */
public class StatusBarMixin implements Mixin {

  private final PartnerCustomizationLayout partnerCustomizationLayout;
  private final StatusBarBackgroundLayout statusBarLayout;
  private final View decorView;
  @VisibleForTesting final boolean applyPartnerResources;

  /**
   * Creates a mixin for managing status bar.
   *
   * @param layout The layout this Mixin belongs to.
   * @param window The window this activity of Mixin belongs to.
   * @param attrs XML attributes given to the layout.
   * @param defStyleAttr The default style attribute as given to the constructor of the layout.
   * @param applyPartnerResources determine applies partner resources or not.
   */
  public StatusBarMixin(
      @NonNull PartnerCustomizationLayout layout,
      @NonNull Window window,
      @Nullable AttributeSet attrs,
      @AttrRes int defStyleAttr,
      boolean applyPartnerResources) {
    partnerCustomizationLayout = layout;
    statusBarLayout = partnerCustomizationLayout.findManagedViewById(R.id.suc_layout_status);
    decorView = window.getDecorView();
    this.applyPartnerResources = applyPartnerResources;

    if (statusBarLayout == null) {
      throw new NullPointerException("StatusBarBackgroundLayout cannot be null in StatusBarMixin");
    }

    // Override the color of status bar to transparent such that the color of
    // StatusBarBackgroundLayout can be seen.
    if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
      window.setStatusBarColor(Color.TRANSPARENT);
    }

    TypedArray a =
        layout
            .getContext()
            .obtainStyledAttributes(attrs, R.styleable.SucStatusBarMixin, defStyleAttr, 0);
    setStatusBarWindowLight(
        a.getBoolean(
            R.styleable.SucStatusBarMixin_sucStatusBarWindowLight, isStatusBarWindowLight()));
    setStatusBarBackground(a.getDrawable(R.styleable.SucStatusBarMixin_sucStatusBarBackground));
    a.recycle();
  }

  /**
   * Sets the background color of status bar. The color will be overridden by partner resource if
   * the activity is running in setup wizard flow.
   *
   * @param color The background color of status bar.
   */
  public void setStatusBarBackground(int color) {
    setStatusBarBackground(new ColorDrawable(color));
  }

  /**
   * Sets the background image of status bar. The drawable will be overridden by partner resource if
   * the activity is running in setup wizard flow.
   *
   * @param background The drawable of status bar.
   */
  public void setStatusBarBackground(Drawable background) {
    if (applyPartnerResources) {
      Context context = partnerCustomizationLayout.getContext();
      background =
          PartnerConfigHelper.get(context)
              .getDrawable(context, PartnerConfig.CONFIG_STATUS_BAR_BACKGROUND);
    }

    statusBarLayout.setStatusBarBackground(background);
  }

  /** Returns the background of status bar. */
  public Drawable getStatusBarBackground() {
    return statusBarLayout.getStatusBarBackground();
  }

  /**
   * Sets the status bar to draw in a mode that is compatible with light or dark status bar
   * backgrounds. The status bar drawing mode will be overridden by partner resource if the activity
   * is running in setup wizard flow.
   *
   * @param isLight true means compatible with light theme, otherwise compatible with dark theme
   */
  public void setStatusBarWindowLight(boolean isLight) {
    if (Build.VERSION.SDK_INT >= VERSION_CODES.M) {
      if (applyPartnerResources) {
        Context context = partnerCustomizationLayout.getContext();
        isLight =
            PartnerConfigHelper.get(context)
                .getBoolean(context, PartnerConfig.CONFIG_WINDOW_LIGHT_STATUS_BAR, false);
      }

      if (isLight) {
        decorView.setSystemUiVisibility(
            decorView.getSystemUiVisibility() | SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
      } else {
        decorView.setSystemUiVisibility(
            decorView.getSystemUiVisibility() & ~SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
      }
    }
  }

  /**
   * Returns true if status bar icons should be drawn on light background, false if the icons should
   * be light-on-dark.
   */
  public boolean isStatusBarWindowLight() {
    if (Build.VERSION.SDK_INT >= VERSION_CODES.M) {
      return (decorView.getSystemUiVisibility() & SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
          == SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
    }
    return true;
  }
}