1 /*
2  * Copyright (C) 2012 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.keyguard;
18 
19 import android.content.Context;
20 import android.content.res.TypedArray;
21 import android.text.TextUtils;
22 import android.text.method.SingleLineTransformationMethod;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.widget.TextView;
26 
27 import com.android.systemui.R;
28 
29 import java.util.Locale;
30 
31 public class CarrierText extends TextView {
32     private static final boolean DEBUG = KeyguardConstants.DEBUG;
33     private static final String TAG = "CarrierText";
34 
35     private static CharSequence mSeparator;
36 
37     private boolean mShowMissingSim;
38 
39     private boolean mShowAirplaneMode;
40     private boolean mShouldMarquee;
41 
42     private CarrierTextController mCarrierTextController;
43 
44     private CarrierTextController.CarrierTextCallback mCarrierTextCallback =
45             new CarrierTextController.CarrierTextCallback() {
46                 @Override
47                 public void updateCarrierInfo(CarrierTextController.CarrierTextCallbackInfo info) {
48                     setText(info.carrierText);
49                 }
50 
51                 @Override
52                 public void startedGoingToSleep() {
53                     setSelected(false);
54                 }
55 
56                 @Override
57                 public void finishedWakingUp() {
58                     setSelected(true);
59                 }
60             };
61 
CarrierText(Context context)62     public CarrierText(Context context) {
63         this(context, null);
64     }
65 
CarrierText(Context context, AttributeSet attrs)66     public CarrierText(Context context, AttributeSet attrs) {
67         super(context, attrs);
68         boolean useAllCaps;
69         TypedArray a = context.getTheme().obtainStyledAttributes(
70                 attrs, R.styleable.CarrierText, 0, 0);
71         try {
72             useAllCaps = a.getBoolean(R.styleable.CarrierText_allCaps, false);
73             mShowAirplaneMode = a.getBoolean(R.styleable.CarrierText_showAirplaneMode, false);
74             mShowMissingSim = a.getBoolean(R.styleable.CarrierText_showMissingSim, false);
75         } finally {
76             a.recycle();
77         }
78         setTransformationMethod(new CarrierTextTransformationMethod(mContext, useAllCaps));
79     }
80 
81     @Override
onFinishInflate()82     protected void onFinishInflate() {
83         super.onFinishInflate();
84         mSeparator = getResources().getString(
85                 com.android.internal.R.string.kg_text_message_separator);
86         mCarrierTextController = new CarrierTextController(mContext, mSeparator, mShowAirplaneMode,
87                 mShowMissingSim);
88         mShouldMarquee = KeyguardUpdateMonitor.getInstance(mContext).isDeviceInteractive();
89         setSelected(mShouldMarquee); // Allow marquee to work.
90     }
91 
92     @Override
onAttachedToWindow()93     protected void onAttachedToWindow() {
94         super.onAttachedToWindow();
95         mCarrierTextController.setListening(mCarrierTextCallback);
96     }
97 
98     @Override
onDetachedFromWindow()99     protected void onDetachedFromWindow() {
100         super.onDetachedFromWindow();
101         mCarrierTextController.setListening(null);
102     }
103 
104     @Override
onVisibilityChanged(View changedView, int visibility)105     protected void onVisibilityChanged(View changedView, int visibility) {
106         super.onVisibilityChanged(changedView, visibility);
107         // Only show marquee when visible
108         if (visibility == VISIBLE) {
109             setEllipsize(TextUtils.TruncateAt.MARQUEE);
110         } else {
111             setEllipsize(TextUtils.TruncateAt.END);
112         }
113     }
114 
115     private class CarrierTextTransformationMethod extends SingleLineTransformationMethod {
116         private final Locale mLocale;
117         private final boolean mAllCaps;
118 
CarrierTextTransformationMethod(Context context, boolean allCaps)119         public CarrierTextTransformationMethod(Context context, boolean allCaps) {
120             mLocale = context.getResources().getConfiguration().locale;
121             mAllCaps = allCaps;
122         }
123 
124         @Override
getTransformation(CharSequence source, View view)125         public CharSequence getTransformation(CharSequence source, View view) {
126             source = super.getTransformation(source, view);
127 
128             if (mAllCaps && source != null) {
129                 source = source.toString().toUpperCase(mLocale);
130             }
131 
132             return source;
133         }
134     }
135 }
136