• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.graphics.Typeface;
21 import android.graphics.drawable.Drawable;
22 import android.text.TextUtils;
23 import android.util.AttributeSet;
24 import android.view.MotionEvent;
25 import android.view.View;
26 import android.view.ViewConfiguration;
27 import android.widget.Button;
28 
29 import com.android.internal.util.EmergencyAffordanceManager;
30 import com.android.systemui.Flags;
31 import com.android.systemui.FontStyles;
32 import com.android.systemui.bouncer.shared.constants.KeyguardBouncerConstants;
33 
34 /**
35  * This class implements a smart emergency button that updates itself based
36  * on telephony state.  When the phone is idle, it is an emergency call button.
37  * When there's a call in progress, it presents an appropriate message and
38  * allows the user to return to the call.
39  */
40 public class EmergencyButton extends Button {
41 
42     private final EmergencyAffordanceManager mEmergencyAffordanceManager;
43 
44     private int mDownX;
45     private int mDownY;
46     private boolean mLongPressWasDragged;
47 
48     private final boolean mEnableEmergencyCallWhileSimLocked;
49 
EmergencyButton(Context context)50     public EmergencyButton(Context context) {
51         this(context, null);
52     }
53 
EmergencyButton(Context context, AttributeSet attrs)54     public EmergencyButton(Context context, AttributeSet attrs) {
55         super(context, attrs);
56         mEnableEmergencyCallWhileSimLocked = mContext.getResources().getBoolean(
57                 com.android.internal.R.bool.config_enable_emergency_call_while_sim_locked);
58         mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
59     }
60 
61     @Override
onFinishInflate()62     protected void onFinishInflate() {
63         super.onFinishInflate();
64         if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
65             setOnLongClickListener(v -> {
66                 boolean isEmergencyCallButton = getVisibility() == View.VISIBLE
67                         && TextUtils.equals(getText(), getEmergencyButtonLabel());
68                 if (isEmergencyCallButton
69                         && !mLongPressWasDragged
70                         && mEmergencyAffordanceManager.needsEmergencyAffordance()) {
71                     mEmergencyAffordanceManager.performEmergencyCall();
72                     return true;
73                 }
74                 return false;
75             });
76         }
77         if (Flags.bouncerUiRevamp2()) {
78             setTypeface(Typeface.create(FontStyles.GSF_TITLE_MEDIUM, Typeface.NORMAL));
79             Drawable background = getBackground();
80             int bgColor = mContext.getColor(KeyguardBouncerConstants.Color.actionButtonBg);
81             if (background != null) {
82                 background.setTint(bgColor);
83             } else {
84                 setBackgroundColor(bgColor);
85             }
86             setTextColor(mContext.getColor(KeyguardBouncerConstants.Color.actionButtonText));
87         }
88     }
89 
90     @Override
onTouchEvent(MotionEvent event)91     public boolean onTouchEvent(MotionEvent event) {
92         final int x = (int) event.getX();
93         final int y = (int) event.getY();
94         if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
95             mDownX = x;
96             mDownY = y;
97             mLongPressWasDragged = false;
98         } else {
99             final int xDiff = Math.abs(x - mDownX);
100             final int yDiff = Math.abs(y - mDownY);
101             int touchSlop = ViewConfiguration.get(mContext).getScaledTouchSlop();
102             if (Math.abs(yDiff) > touchSlop || Math.abs(xDiff) > touchSlop) {
103                 mLongPressWasDragged = true;
104             }
105         }
106         return super.onTouchEvent(event);
107     }
108 
109     @Override
performLongClick()110     public boolean performLongClick() {
111         return super.performLongClick();
112     }
113 
updateEmergencyCallButton(boolean isInCall, boolean hasTelephonyRadio, boolean simLocked, boolean isSecure)114     void updateEmergencyCallButton(boolean isInCall, boolean hasTelephonyRadio, boolean simLocked,
115             boolean isSecure) {
116         boolean visible = false;
117         if (hasTelephonyRadio) {
118             // Emergency calling requires a telephony radio.
119             if (isInCall) {
120                 visible = true; // always show "return to call" if phone is off-hook
121             } else {
122                 if (simLocked) {
123                     // Some countries can't handle emergency calls while SIM is locked.
124                     visible = mEnableEmergencyCallWhileSimLocked;
125                 } else {
126                     // Only show if there is a secure screen (pin/pattern/SIM pin/SIM puk);
127                     visible = isSecure;
128                 }
129             }
130         }
131         if (visible) {
132             setVisibility(View.VISIBLE);
133 
134             int textId;
135             if (isInCall) {
136                 textId = com.android.internal.R.string.lockscreen_return_to_call;
137             } else {
138                 textId = com.android.internal.R.string.lockscreen_emergency_call;
139             }
140             setText(textId);
141         } else {
142             setVisibility(View.GONE);
143         }
144     }
145 
getEmergencyButtonLabel()146     private String getEmergencyButtonLabel() {
147         return mContext.getString(com.android.internal.R.string.lockscreen_emergency_call);
148     }
149 }
150