1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.qs.carrier;
18 
19 import android.content.Context;
20 import android.content.res.ColorStateList;
21 import android.text.TextUtils;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.widget.ImageView;
25 import android.widget.LinearLayout;
26 import android.widget.TextView;
27 
28 import com.android.settingslib.Utils;
29 import com.android.settingslib.graph.SignalDrawable;
30 import com.android.systemui.DualToneHandler;
31 import com.android.systemui.R;
32 import com.android.systemui.qs.QuickStatusBarHeader;
33 
34 import java.util.Objects;
35 
36 public class QSCarrier extends LinearLayout {
37 
38     private View mMobileGroup;
39     private TextView mCarrierText;
40     private ImageView mMobileSignal;
41     private ImageView mMobileRoaming;
42     private DualToneHandler mDualToneHandler;
43     private ColorStateList mColorForegroundStateList;
44     private float mColorForegroundIntensity;
45     private CellSignalState mLastSignalState;
46 
QSCarrier(Context context)47     public QSCarrier(Context context) {
48         super(context);
49     }
50 
QSCarrier(Context context, AttributeSet attrs)51     public QSCarrier(Context context, AttributeSet attrs) {
52         super(context, attrs);
53     }
54 
QSCarrier(Context context, AttributeSet attrs, int defStyleAttr)55     public QSCarrier(Context context, AttributeSet attrs, int defStyleAttr) {
56         super(context, attrs, defStyleAttr);
57     }
58 
QSCarrier(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)59     public QSCarrier(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
60         super(context, attrs, defStyleAttr, defStyleRes);
61     }
62 
63     @Override
onFinishInflate()64     protected void onFinishInflate() {
65         super.onFinishInflate();
66         mDualToneHandler = new DualToneHandler(getContext());
67         mMobileGroup = findViewById(R.id.mobile_combo);
68         mMobileSignal = findViewById(R.id.mobile_signal);
69         mMobileRoaming = findViewById(R.id.mobile_roaming);
70         mCarrierText = findViewById(R.id.qs_carrier_text);
71 
72         mMobileSignal.setImageDrawable(new SignalDrawable(mContext));
73 
74         int colorForeground = Utils.getColorAttrDefaultColor(mContext,
75                 android.R.attr.colorForeground);
76         mColorForegroundStateList = ColorStateList.valueOf(colorForeground);
77         mColorForegroundIntensity = QuickStatusBarHeader.getColorIntensity(colorForeground);
78     }
79 
80     /**
81      * Update the state of this view
82      * @param state the current state of the signal for this view
83      * @return true if the state was actually changed
84      */
updateState(CellSignalState state)85     public boolean updateState(CellSignalState state) {
86         if (Objects.equals(state, mLastSignalState)) return false;
87         mLastSignalState = state;
88         mMobileGroup.setVisibility(state.visible ? View.VISIBLE : View.GONE);
89         if (state.visible) {
90             mMobileRoaming.setVisibility(state.roaming ? View.VISIBLE : View.GONE);
91             ColorStateList colorStateList = ColorStateList.valueOf(
92                     mDualToneHandler.getSingleColor(mColorForegroundIntensity));
93             mMobileRoaming.setImageTintList(colorStateList);
94             mMobileSignal.setImageTintList(colorStateList);
95             mMobileSignal.setImageLevel(state.mobileSignalIconId);
96 
97             StringBuilder contentDescription = new StringBuilder();
98             if (state.contentDescription != null) {
99                 contentDescription.append(state.contentDescription).append(", ");
100             }
101             if (state.roaming) {
102                 contentDescription
103                         .append(mContext.getString(R.string.data_connection_roaming))
104                         .append(", ");
105             }
106             // TODO: show mobile data off/no internet text for 5 seconds before carrier text
107             if (hasValidTypeContentDescription(state.typeContentDescription)) {
108                 contentDescription.append(state.typeContentDescription);
109             }
110             mMobileSignal.setContentDescription(contentDescription);
111         }
112         return true;
113     }
114 
hasValidTypeContentDescription(String typeContentDescription)115     private boolean hasValidTypeContentDescription(String typeContentDescription) {
116         return TextUtils.equals(typeContentDescription,
117                 mContext.getString(R.string.data_connection_no_internet))
118                 || TextUtils.equals(typeContentDescription,
119                 mContext.getString(R.string.cell_data_off_content_description))
120                 || TextUtils.equals(typeContentDescription,
121                 mContext.getString(R.string.not_default_data_content_description));
122     }
123 
setCarrierText(CharSequence text)124     public void setCarrierText(CharSequence text) {
125         mCarrierText.setText(text);
126     }
127 }
128