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