1/*
2 * Copyright (C) 2006 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
17package com.android.systemui.statusbar.phone;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.text.TextUtils;
24import android.util.AttributeSet;
25import android.util.Slog;
26import android.view.View;
27import android.widget.TextView;
28
29import com.android.internal.telephony.TelephonyIntents;
30
31import java.text.SimpleDateFormat;
32import java.util.Calendar;
33import java.util.TimeZone;
34
35import com.android.internal.R;
36
37/**
38 * This widget display an analogic clock with two hands for hours and
39 * minutes.
40 */
41public class CarrierLabel extends TextView {
42    private boolean mAttached;
43
44    public CarrierLabel(Context context) {
45        this(context, null);
46    }
47
48    public CarrierLabel(Context context, AttributeSet attrs) {
49        this(context, attrs, 0);
50    }
51
52    public CarrierLabel(Context context, AttributeSet attrs, int defStyle) {
53        super(context, attrs, defStyle);
54        updateNetworkName(false, null, false, null);
55    }
56
57    @Override
58    protected void onAttachedToWindow() {
59        super.onAttachedToWindow();
60
61        if (!mAttached) {
62            mAttached = true;
63            IntentFilter filter = new IntentFilter();
64            filter.addAction(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION);
65            getContext().registerReceiver(mIntentReceiver, filter, null, getHandler());
66        }
67    }
68
69    @Override
70    protected void onDetachedFromWindow() {
71        super.onDetachedFromWindow();
72        if (mAttached) {
73            getContext().unregisterReceiver(mIntentReceiver);
74            mAttached = false;
75        }
76    }
77
78    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
79        @Override
80        public void onReceive(Context context, Intent intent) {
81            String action = intent.getAction();
82            if (TelephonyIntents.SPN_STRINGS_UPDATED_ACTION.equals(action)) {
83                updateNetworkName(intent.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_SPN, false),
84                        intent.getStringExtra(TelephonyIntents.EXTRA_SPN),
85                        intent.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_PLMN, false),
86                        intent.getStringExtra(TelephonyIntents.EXTRA_PLMN));
87            }
88        }
89    };
90
91    void updateNetworkName(boolean showSpn, String spn, boolean showPlmn, String plmn) {
92        if (false) {
93            Slog.d("CarrierLabel", "updateNetworkName showSpn=" + showSpn + " spn=" + spn
94                    + " showPlmn=" + showPlmn + " plmn=" + plmn);
95        }
96        final String str;
97        // match logic in KeyguardStatusViewManager
98        final boolean plmnValid = showPlmn && !TextUtils.isEmpty(plmn);
99        final boolean spnValid = showSpn && !TextUtils.isEmpty(spn);
100        if (plmnValid && spnValid) {
101            str = plmn + "|" + spn;
102        } else if (plmnValid) {
103            str = plmn;
104        } else if (spnValid) {
105            str = spn;
106        } else {
107            str = "";
108        }
109        setText(str);
110    }
111
112
113}
114
115
116