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