blob: b70d8341799a1e616f6beb9f913053c98ab73d78 [file] [log] [blame]
Jim Miller109f1fd2012-09-19 20:44:16 -07001/*
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
Jim Miller5ecd8112013-01-09 18:50:26 -080017package com.android.keyguard;
Jim Millerf9cb7ba2012-09-17 22:31:56 -070018
19import android.content.Context;
Jorim Jaggi25807932014-04-24 23:29:46 +020020import android.content.res.TypedArray;
Jim Millerf9cb7ba2012-09-17 22:31:56 -070021import android.text.TextUtils;
Jason Monk9ff69bd2014-12-02 16:43:17 -050022import android.text.method.SingleLineTransformationMethod;
Jim Millerf9cb7ba2012-09-17 22:31:56 -070023import android.util.AttributeSet;
Victoria Lease1d80e972013-11-01 16:34:03 -070024import android.view.View;
Jim Millerf9cb7ba2012-09-17 22:31:56 -070025import android.widget.TextView;
26
Sunny Goyal87fccf02019-08-13 17:39:10 -070027import com.android.systemui.R;
28
Gus Prevasab336792018-11-14 13:52:20 -050029import java.util.Locale;
Jim Millerf9cb7ba2012-09-17 22:31:56 -070030
31public class CarrierText extends TextView {
Jason Monk9ff69bd2014-12-02 16:43:17 -050032 private static final boolean DEBUG = KeyguardConstants.DEBUG;
33 private static final String TAG = "CarrierText";
34
Jim Millere9be1402012-11-01 16:14:20 -070035 private static CharSequence mSeparator;
36
Bill Linef81cbd2018-07-05 17:48:49 +080037 private boolean mShowMissingSim;
38
39 private boolean mShowAirplaneMode;
Fabian Kozynski02941af2019-01-17 17:57:37 -050040 private boolean mShouldMarquee;
Bill Linef81cbd2018-07-05 17:48:49 +080041
Fabian Kozynski02941af2019-01-17 17:57:37 -050042 private CarrierTextController mCarrierTextController;
Jim Millerf9cb7ba2012-09-17 22:31:56 -070043
Fabian Kozynski02941af2019-01-17 17:57:37 -050044 private CarrierTextController.CarrierTextCallback mCarrierTextCallback =
45 new CarrierTextController.CarrierTextCallback() {
46 @Override
Fabian Kozynski1823f112019-01-18 11:43:29 -050047 public void updateCarrierInfo(CarrierTextController.CarrierTextCallbackInfo info) {
48 setText(info.carrierText);
Fabian Kozynski02941af2019-01-17 17:57:37 -050049 }
Etan Cohen47051d82015-07-06 16:19:04 -070050
Fabian Kozynski02941af2019-01-17 17:57:37 -050051 @Override
52 public void startedGoingToSleep() {
53 setSelected(false);
54 }
Wileen Chiu6b8b22e2014-10-29 22:48:21 +053055
Fabian Kozynski02941af2019-01-17 17:57:37 -050056 @Override
57 public void finishedWakingUp() {
Fabian Kozynski1823f112019-01-18 11:43:29 -050058 setSelected(true);
Fabian Kozynski02941af2019-01-17 17:57:37 -050059 }
60 };
Jim Millerf9cb7ba2012-09-17 22:31:56 -070061
62 public CarrierText(Context context) {
63 this(context, null);
64 }
65
66 public CarrierText(Context context, AttributeSet attrs) {
67 super(context, attrs);
Jorim Jaggi25807932014-04-24 23:29:46 +020068 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);
Bill Linef81cbd2018-07-05 17:48:49 +080073 mShowAirplaneMode = a.getBoolean(R.styleable.CarrierText_showAirplaneMode, false);
74 mShowMissingSim = a.getBoolean(R.styleable.CarrierText_showMissingSim, false);
Jorim Jaggi25807932014-04-24 23:29:46 +020075 } finally {
76 a.recycle();
77 }
Victoria Lease1d80e972013-11-01 16:34:03 -070078 setTransformationMethod(new CarrierTextTransformationMethod(mContext, useAllCaps));
Amin Shaikh0e003312018-03-08 11:39:01 -050079 }
80
Jim Millerf9cb7ba2012-09-17 22:31:56 -070081 @Override
82 protected void onFinishInflate() {
83 super.onFinishInflate();
Jason Monk9ff69bd2014-12-02 16:43:17 -050084 mSeparator = getResources().getString(
85 com.android.internal.R.string.kg_text_message_separator);
Fabian Kozynski02941af2019-01-17 17:57:37 -050086 mCarrierTextController = new CarrierTextController(mContext, mSeparator, mShowAirplaneMode,
87 mShowMissingSim);
88 mShouldMarquee = KeyguardUpdateMonitor.getInstance(mContext).isDeviceInteractive();
89 setSelected(mShouldMarquee); // Allow marquee to work.
Jim Millerf9cb7ba2012-09-17 22:31:56 -070090 }
91
Jim Miller0928e012012-11-06 18:43:22 -080092 @Override
93 protected void onAttachedToWindow() {
94 super.onAttachedToWindow();
Fabian Kozynski02941af2019-01-17 17:57:37 -050095 mCarrierTextController.setListening(mCarrierTextCallback);
Jim Miller0928e012012-11-06 18:43:22 -080096 }
97
98 @Override
99 protected void onDetachedFromWindow() {
100 super.onDetachedFromWindow();
Fabian Kozynski02941af2019-01-17 17:57:37 -0500101 mCarrierTextController.setListening(null);
Jim Miller0928e012012-11-06 18:43:22 -0800102 }
103
Matthew Ng3a6db672017-11-28 14:35:25 -0800104 @Override
105 protected void onVisibilityChanged(View changedView, int visibility) {
106 super.onVisibilityChanged(changedView, visibility);
Matthew Ng3a6db672017-11-28 14:35:25 -0800107 // Only show marquee when visible
108 if (visibility == VISIBLE) {
109 setEllipsize(TextUtils.TruncateAt.MARQUEE);
110 } else {
111 setEllipsize(TextUtils.TruncateAt.END);
112 }
113 }
114
Victoria Lease1d80e972013-11-01 16:34:03 -0700115 private class CarrierTextTransformationMethod extends SingleLineTransformationMethod {
116 private final Locale mLocale;
117 private final boolean mAllCaps;
118
119 public CarrierTextTransformationMethod(Context context, boolean allCaps) {
120 mLocale = context.getResources().getConfiguration().locale;
121 mAllCaps = allCaps;
122 }
123
124 @Override
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 }
Jim Millerf9cb7ba2012-09-17 22:31:56 -0700135}