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 androidx.annotation.VisibleForTesting;
29 
30 import com.android.settingslib.Utils;
31 import com.android.settingslib.graph.SignalDrawable;
32 import com.android.systemui.R;
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 View mSpacer;
43     private CellSignalState mLastSignalState;
44     private boolean mProviderModelInitialized = false;
45     private boolean mIsSingleCarrier;
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         mMobileGroup = findViewById(R.id.mobile_combo);
67         mMobileRoaming = findViewById(R.id.mobile_roaming);
68         mMobileSignal = findViewById(R.id.mobile_signal);
69         mCarrierText = findViewById(R.id.qs_carrier_text);
70         mSpacer = findViewById(R.id.spacer);
71     }
72 
73     /**
74      * Update the state of this view
75      * @param state the current state of the signal for this view
76      * @param isSingleCarrier whether there is a single carrier being shown in the container
77      * @return true if the state was actually changed
78      */
updateState(CellSignalState state, boolean isSingleCarrier)79     public boolean updateState(CellSignalState state, boolean isSingleCarrier) {
80         if (Objects.equals(state, mLastSignalState) && isSingleCarrier == mIsSingleCarrier) {
81             return false;
82         }
83         mLastSignalState = state;
84         mIsSingleCarrier = isSingleCarrier;
85         final boolean visible = state.visible && !isSingleCarrier;
86         mMobileGroup.setVisibility(visible ? View.VISIBLE : View.GONE);
87         mSpacer.setVisibility(isSingleCarrier ? View.VISIBLE : View.GONE);
88         if (visible) {
89             mMobileRoaming.setVisibility(state.roaming ? View.VISIBLE : View.GONE);
90             ColorStateList colorStateList = Utils.getColorAttr(mContext,
91                     android.R.attr.textColorPrimary);
92             mMobileRoaming.setImageTintList(colorStateList);
93             mMobileSignal.setImageTintList(colorStateList);
94 
95             if (state.providerModelBehavior) {
96                 if (!mProviderModelInitialized) {
97                     mProviderModelInitialized = true;
98                     mMobileSignal.setImageDrawable(
99                             mContext.getDrawable(R.drawable.ic_qs_no_calling_sms));
100                 }
101                 mMobileSignal.setImageDrawable(mContext.getDrawable(state.mobileSignalIconId));
102                 mMobileSignal.setContentDescription(state.contentDescription);
103             } else {
104                 if (!mProviderModelInitialized) {
105                     mProviderModelInitialized = true;
106                     mMobileSignal.setImageDrawable(new SignalDrawable(mContext));
107                 }
108                 mMobileSignal.setImageLevel(state.mobileSignalIconId);
109                 StringBuilder contentDescription = new StringBuilder();
110                 if (state.contentDescription != null) {
111                     contentDescription.append(state.contentDescription).append(", ");
112                 }
113                 if (state.roaming) {
114                     contentDescription
115                             .append(mContext.getString(R.string.data_connection_roaming))
116                             .append(", ");
117                 }
118                 // TODO: show mobile data off/no internet text for 5 seconds before carrier text
119                 if (hasValidTypeContentDescription(state.typeContentDescription)) {
120                     contentDescription.append(state.typeContentDescription);
121                 }
122                 mMobileSignal.setContentDescription(contentDescription);
123             }
124         }
125         return true;
126     }
127 
hasValidTypeContentDescription(String typeContentDescription)128     private boolean hasValidTypeContentDescription(String typeContentDescription) {
129         return TextUtils.equals(typeContentDescription,
130                 mContext.getString(R.string.data_connection_no_internet))
131                 || TextUtils.equals(typeContentDescription,
132                 mContext.getString(
133                         com.android.settingslib.R.string.cell_data_off_content_description))
134                 || TextUtils.equals(typeContentDescription,
135                 mContext.getString(
136                         com.android.settingslib.R.string.not_default_data_content_description));
137     }
138 
139     @VisibleForTesting
getRSSIView()140     View getRSSIView() {
141         return mMobileGroup;
142     }
143 
setCarrierText(CharSequence text)144     public void setCarrierText(CharSequence text) {
145         mCarrierText.setText(text);
146     }
147 }
148