1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui.statusbar;
16 
17 import android.content.Context;
18 import android.text.TextUtils;
19 import android.util.AttributeSet;
20 import android.widget.TextView;
21 
22 import com.android.settingslib.WirelessUtils;
23 
24 import java.util.List;
25 
26 /** Shows the operator name */
27 public class OperatorNameView extends TextView {
28     private boolean mDemoMode;
29 
OperatorNameView(Context context)30     public OperatorNameView(Context context) {
31         this(context, null);
32     }
33 
OperatorNameView(Context context, AttributeSet attrs)34     public OperatorNameView(Context context, AttributeSet attrs) {
35         this(context, attrs, 0);
36     }
37 
OperatorNameView(Context context, AttributeSet attrs, int defStyle)38     public OperatorNameView(Context context, AttributeSet attrs, int defStyle) {
39         super(context, attrs, defStyle);
40     }
41 
setDemoMode(boolean demoMode)42     void setDemoMode(boolean demoMode) {
43         mDemoMode = demoMode;
44     }
45 
update(boolean showOperatorName, boolean hasMobile, List<OperatorNameViewController.SubInfo> subs)46     void update(boolean showOperatorName, boolean hasMobile,
47             List<OperatorNameViewController.SubInfo> subs) {
48         setVisibility(showOperatorName ? VISIBLE : GONE);
49 
50         boolean airplaneMode = WirelessUtils.isAirplaneModeOn(mContext);
51         if (!hasMobile || airplaneMode) {
52             setText(null);
53             setVisibility(GONE);
54             return;
55         }
56 
57         if (!mDemoMode) {
58             updateText(subs);
59         }
60     }
61 
updateText(List<OperatorNameViewController.SubInfo> subs)62     void updateText(List<OperatorNameViewController.SubInfo> subs) {
63         CharSequence displayText = null;
64         final int N = subs.size();
65         for (int i = 0; i < N; i++) {
66             OperatorNameViewController.SubInfo subInfo = subs.get(i);
67             CharSequence carrierName = subs.get(i).getCarrierName();
68             if (!TextUtils.isEmpty(carrierName) && subInfo.simReady()) {
69                 if (subInfo.stateInService()) {
70                     displayText = subInfo.getCarrierName();
71                     break;
72                 }
73             }
74         }
75 
76         setText(displayText);
77     }
78 }
79