blob: 8069ce4c0100d839875b83576046fe6e36c08a18 [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
Gus Prevasab336792018-11-14 13:52:20 -050027import java.util.Locale;
Jim Millerf9cb7ba2012-09-17 22:31:56 -070028
29public class CarrierText extends TextView {
Jason Monk9ff69bd2014-12-02 16:43:17 -050030 private static final boolean DEBUG = KeyguardConstants.DEBUG;
31 private static final String TAG = "CarrierText";
32
Jim Millere9be1402012-11-01 16:14:20 -070033 private static CharSequence mSeparator;
34
Bill Linef81cbd2018-07-05 17:48:49 +080035 private boolean mShowMissingSim;
36
37 private boolean mShowAirplaneMode;
Fabian Kozynski02941af2019-01-17 17:57:37 -050038 private boolean mShouldMarquee;
Bill Linef81cbd2018-07-05 17:48:49 +080039
Fabian Kozynski02941af2019-01-17 17:57:37 -050040 private CarrierTextController mCarrierTextController;
Jim Millerf9cb7ba2012-09-17 22:31:56 -070041
Fabian Kozynski02941af2019-01-17 17:57:37 -050042 private CarrierTextController.CarrierTextCallback mCarrierTextCallback =
43 new CarrierTextController.CarrierTextCallback() {
44 @Override
45 public void updateCarrierText(CharSequence carrierText, boolean simsReady) {
46 setText(carrierText);
47 }
Etan Cohen47051d82015-07-06 16:19:04 -070048
Fabian Kozynski02941af2019-01-17 17:57:37 -050049 @Override
50 public void startedGoingToSleep() {
51 setSelected(false);
52 }
Wileen Chiu6b8b22e2014-10-29 22:48:21 +053053
Fabian Kozynski02941af2019-01-17 17:57:37 -050054 @Override
55 public void finishedWakingUp() {
56 setSelected(mShouldMarquee);
57 }
58 };
Jim Millerf9cb7ba2012-09-17 22:31:56 -070059
60 public CarrierText(Context context) {
61 this(context, null);
62 }
63
64 public CarrierText(Context context, AttributeSet attrs) {
65 super(context, attrs);
Jorim Jaggi25807932014-04-24 23:29:46 +020066 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);
Bill Linef81cbd2018-07-05 17:48:49 +080071 mShowAirplaneMode = a.getBoolean(R.styleable.CarrierText_showAirplaneMode, false);
72 mShowMissingSim = a.getBoolean(R.styleable.CarrierText_showMissingSim, false);
Jorim Jaggi25807932014-04-24 23:29:46 +020073 } finally {
74 a.recycle();
75 }
Victoria Lease1d80e972013-11-01 16:34:03 -070076 setTransformationMethod(new CarrierTextTransformationMethod(mContext, useAllCaps));
Amin Shaikh0e003312018-03-08 11:39:01 -050077 }
78
Jim Millerf9cb7ba2012-09-17 22:31:56 -070079 @Override
80 protected void onFinishInflate() {
81 super.onFinishInflate();
Jason Monk9ff69bd2014-12-02 16:43:17 -050082 mSeparator = getResources().getString(
83 com.android.internal.R.string.kg_text_message_separator);
Fabian Kozynski02941af2019-01-17 17:57:37 -050084 mCarrierTextController = new CarrierTextController(mContext, mSeparator, mShowAirplaneMode,
85 mShowMissingSim);
86 mShouldMarquee = KeyguardUpdateMonitor.getInstance(mContext).isDeviceInteractive();
87 setSelected(mShouldMarquee); // Allow marquee to work.
88
Jim Millerf9cb7ba2012-09-17 22:31:56 -070089 }
90
Jim Miller0928e012012-11-06 18:43:22 -080091 @Override
92 protected void onAttachedToWindow() {
93 super.onAttachedToWindow();
Fabian Kozynski02941af2019-01-17 17:57:37 -050094 mCarrierTextController.setListening(mCarrierTextCallback);
Jim Miller0928e012012-11-06 18:43:22 -080095 }
96
97 @Override
98 protected void onDetachedFromWindow() {
99 super.onDetachedFromWindow();
Fabian Kozynski02941af2019-01-17 17:57:37 -0500100 mCarrierTextController.setListening(null);
Jim Miller0928e012012-11-06 18:43:22 -0800101 }
102
Matthew Ng3a6db672017-11-28 14:35:25 -0800103 @Override
104 protected void onVisibilityChanged(View changedView, int visibility) {
105 super.onVisibilityChanged(changedView, visibility);
Matthew Ng3a6db672017-11-28 14:35:25 -0800106 // Only show marquee when visible
107 if (visibility == VISIBLE) {
108 setEllipsize(TextUtils.TruncateAt.MARQUEE);
109 } else {
110 setEllipsize(TextUtils.TruncateAt.END);
111 }
112 }
113
Victoria Lease1d80e972013-11-01 16:34:03 -0700114 private class CarrierTextTransformationMethod extends SingleLineTransformationMethod {
115 private final Locale mLocale;
116 private final boolean mAllCaps;
117
118 public CarrierTextTransformationMethod(Context context, boolean allCaps) {
119 mLocale = context.getResources().getConfiguration().locale;
120 mAllCaps = allCaps;
121 }
122
123 @Override
124 public CharSequence getTransformation(CharSequence source, View view) {
125 source = super.getTransformation(source, view);
126
127 if (mAllCaps && source != null) {
128 source = source.toString().toUpperCase(mLocale);
129 }
130
131 return source;
132 }
133 }
Jim Millerf9cb7ba2012-09-17 22:31:56 -0700134}