Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.systemui.statusbar.policy; |
| 18 | |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorListenerAdapter; |
Adrian Roos | 4c1fcc8 | 2016-03-31 14:39:39 -0700 | [diff] [blame] | 21 | import android.app.Notification; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 22 | import android.app.PendingIntent; |
| 23 | import android.app.RemoteInput; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Makoto Onuki | d6e1f3b | 2016-06-14 11:17:59 -0700 | [diff] [blame] | 26 | import android.content.pm.ShortcutManager; |
Adrian Roos | dc5b453 | 2016-01-06 20:49:41 +0100 | [diff] [blame] | 27 | import android.graphics.Rect; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
| 29 | import android.os.Bundle; |
Selim Cinek | 1397ea3 | 2018-01-16 17:34:52 -0800 | [diff] [blame] | 30 | import android.os.SystemClock; |
Adrian Roos | 245aa87 | 2015-12-07 14:53:53 -0800 | [diff] [blame] | 31 | import android.text.Editable; |
Selim Cinek | 1397ea3 | 2018-01-16 17:34:52 -0800 | [diff] [blame] | 32 | import android.text.SpannedString; |
Adrian Roos | 245aa87 | 2015-12-07 14:53:53 -0800 | [diff] [blame] | 33 | import android.text.TextWatcher; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 34 | import android.util.AttributeSet; |
| 35 | import android.util.Log; |
| 36 | import android.view.KeyEvent; |
| 37 | import android.view.LayoutInflater; |
Adrian Roos | 0bd8a4b | 2016-03-14 16:21:44 -0700 | [diff] [blame] | 38 | import android.view.MotionEvent; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 39 | import android.view.View; |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 40 | import android.view.ViewAnimationUtils; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 41 | import android.view.ViewGroup; |
Adrian Roos | 0bd8a4b | 2016-03-14 16:21:44 -0700 | [diff] [blame] | 42 | import android.view.ViewParent; |
Adrian Roos | f17c86b | 2016-09-07 14:08:40 -0700 | [diff] [blame] | 43 | import android.view.accessibility.AccessibilityEvent; |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 44 | import android.view.inputmethod.CompletionInfo; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 45 | import android.view.inputmethod.EditorInfo; |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 46 | import android.view.inputmethod.InputConnection; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 47 | import android.view.inputmethod.InputMethodManager; |
| 48 | import android.widget.EditText; |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 49 | import android.widget.ImageButton; |
| 50 | import android.widget.LinearLayout; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 51 | import android.widget.ProgressBar; |
| 52 | import android.widget.TextView; |
| 53 | |
Adrian Roos | ceeb04c | 2016-04-25 14:00:54 -0700 | [diff] [blame] | 54 | import com.android.internal.logging.MetricsLogger; |
Tamas Berghammer | 383db5eb | 2016-06-22 15:21:38 +0100 | [diff] [blame] | 55 | import com.android.internal.logging.nano.MetricsProto; |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 56 | import com.android.systemui.Interpolators; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 57 | import com.android.systemui.R; |
| 58 | import com.android.systemui.statusbar.NotificationData; |
| 59 | import com.android.systemui.statusbar.RemoteInputController; |
Adrian Roos | 7b9ed0d | 2017-01-24 15:55:18 -0800 | [diff] [blame] | 60 | import com.android.systemui.statusbar.notification.NotificationViewWrapper; |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 61 | import com.android.systemui.statusbar.stack.StackStateAnimator; |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 62 | |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 63 | /** |
| 64 | * Host for the remote input. |
| 65 | */ |
Adrian Roos | 245aa87 | 2015-12-07 14:53:53 -0800 | [diff] [blame] | 66 | public class RemoteInputView extends LinearLayout implements View.OnClickListener, TextWatcher { |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 67 | |
| 68 | private static final String TAG = "RemoteInput"; |
| 69 | |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 70 | // A marker object that let's us easily find views of this class. |
| 71 | public static final Object VIEW_TAG = new Object(); |
| 72 | |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 73 | public final Object mToken = new Object(); |
| 74 | |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 75 | private RemoteEditText mEditText; |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 76 | private ImageButton mSendButton; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 77 | private ProgressBar mProgressBar; |
| 78 | private PendingIntent mPendingIntent; |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 79 | private RemoteInput[] mRemoteInputs; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 80 | private RemoteInput mRemoteInput; |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 81 | private RemoteInputController mController; |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 82 | |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 83 | private NotificationData.Entry mEntry; |
Adrian Roos | 5153d4a | 2016-03-22 10:01:56 -0700 | [diff] [blame] | 84 | |
Adrian Roos | d009ab1 | 2016-05-20 17:58:53 -0700 | [diff] [blame] | 85 | private boolean mRemoved; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 86 | |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 87 | private int mRevealCx; |
| 88 | private int mRevealCy; |
| 89 | private int mRevealR; |
| 90 | |
Adrian Roos | f17c86b | 2016-09-07 14:08:40 -0700 | [diff] [blame] | 91 | private boolean mResetting; |
Adrian Roos | 7b9ed0d | 2017-01-24 15:55:18 -0800 | [diff] [blame] | 92 | private NotificationViewWrapper mWrapper; |
Adrian Roos | f17c86b | 2016-09-07 14:08:40 -0700 | [diff] [blame] | 93 | |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 94 | public RemoteInputView(Context context, AttributeSet attrs) { |
| 95 | super(context, attrs); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | protected void onFinishInflate() { |
| 100 | super.onFinishInflate(); |
| 101 | |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 102 | mProgressBar = findViewById(R.id.remote_input_progress); |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 103 | |
Alan Viverette | 51efddb | 2017-04-05 10:00:01 -0400 | [diff] [blame] | 104 | mSendButton = findViewById(R.id.remote_input_send); |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 105 | mSendButton.setOnClickListener(this); |
| 106 | |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 107 | mEditText = (RemoteEditText) getChildAt(0); |
| 108 | mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { |
| 109 | @Override |
| 110 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 111 | final boolean isSoftImeEvent = event == null |
| 112 | && (actionId == EditorInfo.IME_ACTION_DONE |
| 113 | || actionId == EditorInfo.IME_ACTION_NEXT |
| 114 | || actionId == EditorInfo.IME_ACTION_SEND); |
| 115 | final boolean isKeyboardEnterKey = event != null |
| 116 | && KeyEvent.isConfirmKey(event.getKeyCode()) |
| 117 | && event.getAction() == KeyEvent.ACTION_DOWN; |
| 118 | |
| 119 | if (isSoftImeEvent || isKeyboardEnterKey) { |
Adrian Roos | 6db43cb | 2016-04-29 10:45:38 -0700 | [diff] [blame] | 120 | if (mEditText.length() > 0) { |
| 121 | sendRemoteInput(); |
| 122 | } |
| 123 | // Consume action to prevent IME from closing. |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 124 | return true; |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | }); |
Adrian Roos | 245aa87 | 2015-12-07 14:53:53 -0800 | [diff] [blame] | 129 | mEditText.addTextChangedListener(this); |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 130 | mEditText.setInnerFocusable(false); |
Adrian Roos | 5153d4a | 2016-03-22 10:01:56 -0700 | [diff] [blame] | 131 | mEditText.mRemoteInputView = this; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | private void sendRemoteInput() { |
| 135 | Bundle results = new Bundle(); |
| 136 | results.putString(mRemoteInput.getResultKey(), mEditText.getText().toString()); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 137 | Intent fillInIntent = new Intent().addFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 138 | RemoteInput.addResultsToIntent(mRemoteInputs, fillInIntent, |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 139 | results); |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 140 | RemoteInput.setResultsSource(fillInIntent, RemoteInput.SOURCE_FREE_FORM_INPUT); |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 141 | |
| 142 | mEditText.setEnabled(false); |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 143 | mSendButton.setVisibility(INVISIBLE); |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 144 | mProgressBar.setVisibility(VISIBLE); |
Adrian Roos | c0a579e | 2016-03-30 16:43:58 -0700 | [diff] [blame] | 145 | mEntry.remoteInputText = mEditText.getText(); |
Selim Cinek | 1397ea3 | 2018-01-16 17:34:52 -0800 | [diff] [blame] | 146 | mEntry.lastRemoteInputSent = SystemClock.elapsedRealtime(); |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 147 | mController.addSpinning(mEntry.key, mToken); |
| 148 | mController.removeRemoteInput(mEntry, mToken); |
Adrian Roos | 245aa87 | 2015-12-07 14:53:53 -0800 | [diff] [blame] | 149 | mEditText.mShowImeOnInputConnection = false; |
Adrian Roos | c0a579e | 2016-03-30 16:43:58 -0700 | [diff] [blame] | 150 | mController.remoteInputSent(mEntry); |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 151 | |
Makoto Onuki | d6e1f3b | 2016-06-14 11:17:59 -0700 | [diff] [blame] | 152 | // Tell ShortcutManager that this package has been "activated". ShortcutManager |
| 153 | // will reset the throttling for this package. |
| 154 | // Strictly speaking, the intent receiver may be different from the notification publisher, |
| 155 | // but that's an edge case, and also because we can't always know which package will receive |
| 156 | // an intent, so we just reset for the publisher. |
| 157 | getContext().getSystemService(ShortcutManager.class).onApplicationActive( |
| 158 | mEntry.notification.getPackageName(), |
| 159 | mEntry.notification.getUser().getIdentifier()); |
| 160 | |
Adrian Roos | ceeb04c | 2016-04-25 14:00:54 -0700 | [diff] [blame] | 161 | MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_REMOTE_INPUT_SEND, |
| 162 | mEntry.notification.getPackageName()); |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 163 | try { |
| 164 | mPendingIntent.send(mContext, 0, fillInIntent); |
| 165 | } catch (PendingIntent.CanceledException e) { |
| 166 | Log.i(TAG, "Unable to send remote input result", e); |
Adrian Roos | ceeb04c | 2016-04-25 14:00:54 -0700 | [diff] [blame] | 167 | MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_REMOTE_INPUT_FAIL, |
| 168 | mEntry.notification.getPackageName()); |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | public static RemoteInputView inflate(Context context, ViewGroup root, |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 173 | NotificationData.Entry entry, |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 174 | RemoteInputController controller) { |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 175 | RemoteInputView v = (RemoteInputView) |
| 176 | LayoutInflater.from(context).inflate(R.layout.remote_input, root, false); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 177 | v.mController = controller; |
| 178 | v.mEntry = entry; |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 179 | v.setTag(VIEW_TAG); |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 180 | |
| 181 | return v; |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void onClick(View v) { |
Adrian Roos | 5389a17 | 2016-04-20 13:58:03 -0700 | [diff] [blame] | 186 | if (v == mSendButton) { |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 187 | sendRemoteInput(); |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Adrian Roos | 5389a17 | 2016-04-20 13:58:03 -0700 | [diff] [blame] | 191 | @Override |
| 192 | public boolean onTouchEvent(MotionEvent event) { |
| 193 | super.onTouchEvent(event); |
| 194 | |
| 195 | // We never want for a touch to escape to an outer view or one we covered. |
| 196 | return true; |
| 197 | } |
| 198 | |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 199 | private void onDefocus(boolean animate) { |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 200 | mController.removeRemoteInput(mEntry, mToken); |
Adrian Roos | 777ef56 | 2015-12-01 17:37:14 -0800 | [diff] [blame] | 201 | mEntry.remoteInputText = mEditText.getText(); |
Adrian Roos | d009ab1 | 2016-05-20 17:58:53 -0700 | [diff] [blame] | 202 | |
| 203 | // During removal, we get reattached and lose focus. Not hiding in that |
| 204 | // case to prevent flicker. |
| 205 | if (!mRemoved) { |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 206 | if (animate && mRevealR > 0) { |
| 207 | Animator reveal = ViewAnimationUtils.createCircularReveal( |
| 208 | this, mRevealCx, mRevealCy, mRevealR, 0); |
| 209 | reveal.setInterpolator(Interpolators.FAST_OUT_LINEAR_IN); |
| 210 | reveal.setDuration(StackStateAnimator.ANIMATION_DURATION_CLOSE_REMOTE_INPUT); |
| 211 | reveal.addListener(new AnimatorListenerAdapter() { |
| 212 | @Override |
| 213 | public void onAnimationEnd(Animator animation) { |
| 214 | setVisibility(INVISIBLE); |
Adrian Roos | 7b9ed0d | 2017-01-24 15:55:18 -0800 | [diff] [blame] | 215 | if (mWrapper != null) { |
| 216 | mWrapper.setRemoteInputVisible(false); |
| 217 | } |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 218 | } |
| 219 | }); |
| 220 | reveal.start(); |
| 221 | } else { |
| 222 | setVisibility(INVISIBLE); |
Adrian Roos | 7b9ed0d | 2017-01-24 15:55:18 -0800 | [diff] [blame] | 223 | if (mWrapper != null) { |
| 224 | mWrapper.setRemoteInputVisible(false); |
| 225 | } |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 226 | } |
Adrian Roos | d009ab1 | 2016-05-20 17:58:53 -0700 | [diff] [blame] | 227 | } |
Adrian Roos | ceeb04c | 2016-04-25 14:00:54 -0700 | [diff] [blame] | 228 | MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_REMOTE_INPUT_CLOSE, |
| 229 | mEntry.notification.getPackageName()); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Override |
Adrian Roos | 14503e2 | 2016-03-09 14:01:24 -0800 | [diff] [blame] | 233 | protected void onAttachedToWindow() { |
| 234 | super.onAttachedToWindow(); |
| 235 | if (mEntry.row.isChangingPosition()) { |
| 236 | if (getVisibility() == VISIBLE && mEditText.isFocusable()) { |
| 237 | mEditText.requestFocus(); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | @Override |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 243 | protected void onDetachedFromWindow() { |
| 244 | super.onDetachedFromWindow(); |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 245 | if (mEntry.row.isChangingPosition() || isTemporarilyDetached()) { |
Adrian Roos | 14503e2 | 2016-03-09 14:01:24 -0800 | [diff] [blame] | 246 | return; |
| 247 | } |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 248 | mController.removeRemoteInput(mEntry, mToken); |
| 249 | mController.removeSpinning(mEntry.key, mToken); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 252 | public void setPendingIntent(PendingIntent pendingIntent) { |
| 253 | mPendingIntent = pendingIntent; |
| 254 | } |
| 255 | |
| 256 | public void setRemoteInput(RemoteInput[] remoteInputs, RemoteInput remoteInput) { |
| 257 | mRemoteInputs = remoteInputs; |
| 258 | mRemoteInput = remoteInput; |
| 259 | mEditText.setHint(mRemoteInput.getLabel()); |
| 260 | } |
| 261 | |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 262 | public void focusAnimated() { |
| 263 | if (getVisibility() != VISIBLE) { |
| 264 | Animator animator = ViewAnimationUtils.createCircularReveal( |
| 265 | this, mRevealCx, mRevealCy, 0, mRevealR); |
| 266 | animator.setDuration(StackStateAnimator.ANIMATION_DURATION_STANDARD); |
| 267 | animator.setInterpolator(Interpolators.LINEAR_OUT_SLOW_IN); |
| 268 | animator.start(); |
| 269 | } |
| 270 | focus(); |
| 271 | } |
| 272 | |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 273 | public void focus() { |
Adrian Roos | ceeb04c | 2016-04-25 14:00:54 -0700 | [diff] [blame] | 274 | MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_REMOTE_INPUT_OPEN, |
| 275 | mEntry.notification.getPackageName()); |
| 276 | |
Adrian Roos | 4c1fcc8 | 2016-03-31 14:39:39 -0700 | [diff] [blame] | 277 | setVisibility(VISIBLE); |
Adrian Roos | 7b9ed0d | 2017-01-24 15:55:18 -0800 | [diff] [blame] | 278 | if (mWrapper != null) { |
| 279 | mWrapper.setRemoteInputVisible(true); |
| 280 | } |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 281 | mController.addRemoteInput(mEntry, mToken); |
Adrian Roos | dc5b453 | 2016-01-06 20:49:41 +0100 | [diff] [blame] | 282 | mEditText.setInnerFocusable(true); |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 283 | mEditText.mShowImeOnInputConnection = true; |
Adrian Roos | 777ef56 | 2015-12-01 17:37:14 -0800 | [diff] [blame] | 284 | mEditText.setText(mEntry.remoteInputText); |
| 285 | mEditText.setSelection(mEditText.getText().length()); |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 286 | mEditText.requestFocus(); |
Adrian Roos | 245aa87 | 2015-12-07 14:53:53 -0800 | [diff] [blame] | 287 | updateSendButton(); |
| 288 | } |
| 289 | |
Adrian Roos | 0789ee0 | 2016-06-01 11:34:58 -0700 | [diff] [blame] | 290 | public void onNotificationUpdateOrReset() { |
Adrian Roos | 245aa87 | 2015-12-07 14:53:53 -0800 | [diff] [blame] | 291 | boolean sending = mProgressBar.getVisibility() == VISIBLE; |
| 292 | |
| 293 | if (sending) { |
| 294 | // Update came in after we sent the reply, time to reset. |
| 295 | reset(); |
| 296 | } |
Adrian Roos | 7b9ed0d | 2017-01-24 15:55:18 -0800 | [diff] [blame] | 297 | |
| 298 | if (isActive() && mWrapper != null) { |
| 299 | mWrapper.setRemoteInputVisible(true); |
| 300 | } |
Adrian Roos | 245aa87 | 2015-12-07 14:53:53 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | private void reset() { |
Adrian Roos | f17c86b | 2016-09-07 14:08:40 -0700 | [diff] [blame] | 304 | mResetting = true; |
Selim Cinek | 1397ea3 | 2018-01-16 17:34:52 -0800 | [diff] [blame] | 305 | mEntry.remoteInputTextWhenReset = SpannedString.valueOf(mEditText.getText()); |
Adrian Roos | f17c86b | 2016-09-07 14:08:40 -0700 | [diff] [blame] | 306 | |
Adrian Roos | 245aa87 | 2015-12-07 14:53:53 -0800 | [diff] [blame] | 307 | mEditText.getText().clear(); |
| 308 | mEditText.setEnabled(true); |
| 309 | mSendButton.setVisibility(VISIBLE); |
| 310 | mProgressBar.setVisibility(INVISIBLE); |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 311 | mController.removeSpinning(mEntry.key, mToken); |
Adrian Roos | 245aa87 | 2015-12-07 14:53:53 -0800 | [diff] [blame] | 312 | updateSendButton(); |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 313 | onDefocus(false /* animate */); |
Adrian Roos | f17c86b | 2016-09-07 14:08:40 -0700 | [diff] [blame] | 314 | |
| 315 | mResetting = false; |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) { |
| 320 | if (mResetting && child == mEditText) { |
| 321 | // Suppress text events if it happens during resetting. Ideally this would be |
| 322 | // suppressed by the text view not being shown, but that doesn't work here because it |
| 323 | // needs to stay visible for the animation. |
| 324 | return false; |
| 325 | } |
| 326 | return super.onRequestSendAccessibilityEvent(child, event); |
Adrian Roos | 245aa87 | 2015-12-07 14:53:53 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | private void updateSendButton() { |
| 330 | mSendButton.setEnabled(mEditText.getText().length() != 0); |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public void beforeTextChanged(CharSequence s, int start, int count, int after) {} |
| 335 | |
| 336 | @Override |
| 337 | public void onTextChanged(CharSequence s, int start, int before, int count) {} |
| 338 | |
| 339 | @Override |
| 340 | public void afterTextChanged(Editable s) { |
| 341 | updateSendButton(); |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Adrian Roos | 0bd8a4b | 2016-03-14 16:21:44 -0700 | [diff] [blame] | 344 | public void close() { |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 345 | mEditText.defocusIfNeeded(false /* animated */); |
Adrian Roos | 0bd8a4b | 2016-03-14 16:21:44 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 350 | if (ev.getAction() == MotionEvent.ACTION_DOWN) { |
yoshiki iguchi | ff4696c | 2017-11-22 16:47:41 +0900 | [diff] [blame] | 351 | mController.requestDisallowLongPressAndDismiss(); |
Adrian Roos | 0bd8a4b | 2016-03-14 16:21:44 -0700 | [diff] [blame] | 352 | } |
| 353 | return super.onInterceptTouchEvent(ev); |
| 354 | } |
| 355 | |
Adrian Roos | 5153d4a | 2016-03-22 10:01:56 -0700 | [diff] [blame] | 356 | public boolean requestScrollTo() { |
yoshiki iguchi | ff4696c | 2017-11-22 16:47:41 +0900 | [diff] [blame] | 357 | mController.lockScrollTo(mEntry); |
Adrian Roos | 5153d4a | 2016-03-22 10:01:56 -0700 | [diff] [blame] | 358 | return true; |
| 359 | } |
| 360 | |
Adrian Roos | 4c1fcc8 | 2016-03-31 14:39:39 -0700 | [diff] [blame] | 361 | public boolean isActive() { |
Adrian Roos | 9550f2e | 2016-08-23 18:23:01 +0200 | [diff] [blame] | 362 | return mEditText.isFocused() && mEditText.isEnabled(); |
Adrian Roos | 4c1fcc8 | 2016-03-31 14:39:39 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | public void stealFocusFrom(RemoteInputView other) { |
| 366 | other.close(); |
| 367 | setPendingIntent(other.mPendingIntent); |
| 368 | setRemoteInput(other.mRemoteInputs, other.mRemoteInput); |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 369 | setRevealParameters(other.mRevealCx, other.mRevealCy, other.mRevealR); |
Adrian Roos | 4c1fcc8 | 2016-03-31 14:39:39 -0700 | [diff] [blame] | 370 | focus(); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Tries to find an action in {@param actions} that matches the current pending intent |
| 375 | * of this view and updates its state to that of the found action |
| 376 | * |
| 377 | * @return true if a matching action was found, false otherwise |
| 378 | */ |
| 379 | public boolean updatePendingIntentFromActions(Notification.Action[] actions) { |
Adrian Roos | 4c1fcc8 | 2016-03-31 14:39:39 -0700 | [diff] [blame] | 380 | if (mPendingIntent == null || actions == null) { |
| 381 | return false; |
| 382 | } |
| 383 | Intent current = mPendingIntent.getIntent(); |
| 384 | if (current == null) { |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | for (Notification.Action a : actions) { |
| 389 | RemoteInput[] inputs = a.getRemoteInputs(); |
| 390 | if (a.actionIntent == null || inputs == null) { |
| 391 | continue; |
| 392 | } |
| 393 | Intent candidate = a.actionIntent.getIntent(); |
| 394 | if (!current.filterEquals(candidate)) { |
| 395 | continue; |
| 396 | } |
| 397 | |
| 398 | RemoteInput input = null; |
| 399 | for (RemoteInput i : inputs) { |
| 400 | if (i.getAllowFreeFormInput()) { |
| 401 | input = i; |
| 402 | } |
| 403 | } |
| 404 | if (input == null) { |
| 405 | continue; |
| 406 | } |
| 407 | setPendingIntent(a.actionIntent); |
| 408 | setRemoteInput(inputs, input); |
| 409 | return true; |
| 410 | } |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | public PendingIntent getPendingIntent() { |
| 415 | return mPendingIntent; |
| 416 | } |
| 417 | |
Adrian Roos | d009ab1 | 2016-05-20 17:58:53 -0700 | [diff] [blame] | 418 | public void setRemoved() { |
| 419 | mRemoved = true; |
| 420 | } |
| 421 | |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 422 | public void setRevealParameters(int cx, int cy, int r) { |
| 423 | mRevealCx = cx; |
| 424 | mRevealCy = cy; |
| 425 | mRevealR = r; |
| 426 | } |
| 427 | |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 428 | @Override |
| 429 | public void dispatchStartTemporaryDetach() { |
| 430 | super.dispatchStartTemporaryDetach(); |
| 431 | // Detach the EditText temporarily such that it doesn't get onDetachedFromWindow and |
| 432 | // won't lose IME focus. |
| 433 | detachViewFromParent(mEditText); |
| 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public void dispatchFinishTemporaryDetach() { |
| 438 | if (isAttachedToWindow()) { |
| 439 | attachViewToParent(mEditText, 0, mEditText.getLayoutParams()); |
| 440 | } else { |
| 441 | removeDetachedView(mEditText, false /* animate */); |
| 442 | } |
| 443 | super.dispatchFinishTemporaryDetach(); |
| 444 | } |
| 445 | |
Adrian Roos | 7b9ed0d | 2017-01-24 15:55:18 -0800 | [diff] [blame] | 446 | public void setWrapper(NotificationViewWrapper wrapper) { |
| 447 | mWrapper = wrapper; |
| 448 | } |
| 449 | |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 450 | /** |
| 451 | * An EditText that changes appearance based on whether it's focusable and becomes |
| 452 | * un-focusable whenever the user navigates away from it or it becomes invisible. |
| 453 | */ |
| 454 | public static class RemoteEditText extends EditText { |
| 455 | |
| 456 | private final Drawable mBackground; |
Adrian Roos | 5153d4a | 2016-03-22 10:01:56 -0700 | [diff] [blame] | 457 | private RemoteInputView mRemoteInputView; |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 458 | boolean mShowImeOnInputConnection; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 459 | |
| 460 | public RemoteEditText(Context context, AttributeSet attrs) { |
| 461 | super(context, attrs); |
| 462 | mBackground = getBackground(); |
| 463 | } |
| 464 | |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 465 | private void defocusIfNeeded(boolean animate) { |
Adrian Roos | 7813dd7 | 2016-09-23 17:12:17 -0700 | [diff] [blame] | 466 | if (mRemoteInputView != null && mRemoteInputView.mEntry.row.isChangingPosition() |
| 467 | || isTemporarilyDetached()) { |
| 468 | if (isTemporarilyDetached()) { |
| 469 | // We might get reattached but then the other one of HUN / expanded might steal |
| 470 | // our focus, so we'll need to save our text here. |
| 471 | if (mRemoteInputView != null) { |
| 472 | mRemoteInputView.mEntry.remoteInputText = getText(); |
| 473 | } |
| 474 | } |
Adrian Roos | 14503e2 | 2016-03-09 14:01:24 -0800 | [diff] [blame] | 475 | return; |
| 476 | } |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 477 | if (isFocusable() && isEnabled()) { |
| 478 | setInnerFocusable(false); |
Adrian Roos | 5153d4a | 2016-03-22 10:01:56 -0700 | [diff] [blame] | 479 | if (mRemoteInputView != null) { |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 480 | mRemoteInputView.onDefocus(animate); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 481 | } |
| 482 | mShowImeOnInputConnection = false; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
| 486 | @Override |
| 487 | protected void onVisibilityChanged(View changedView, int visibility) { |
| 488 | super.onVisibilityChanged(changedView, visibility); |
| 489 | |
| 490 | if (!isShown()) { |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 491 | defocusIfNeeded(false /* animate */); |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
| 495 | @Override |
Adrian Roos | 967f619 | 2016-03-09 12:47:26 -0800 | [diff] [blame] | 496 | protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { |
| 497 | super.onFocusChanged(focused, direction, previouslyFocusedRect); |
| 498 | if (!focused) { |
Adrian Roos | af06bf2 | 2016-07-15 12:26:49 -0700 | [diff] [blame] | 499 | defocusIfNeeded(true /* animate */); |
Adrian Roos | 967f619 | 2016-03-09 12:47:26 -0800 | [diff] [blame] | 500 | } |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | @Override |
Adrian Roos | dc5b453 | 2016-01-06 20:49:41 +0100 | [diff] [blame] | 504 | public void getFocusedRect(Rect r) { |
| 505 | super.getFocusedRect(r); |
| 506 | r.top = mScrollY; |
| 507 | r.bottom = mScrollY + (mBottom - mTop); |
| 508 | } |
| 509 | |
| 510 | @Override |
Adrian Roos | 5153d4a | 2016-03-22 10:01:56 -0700 | [diff] [blame] | 511 | public boolean requestRectangleOnScreen(Rect rectangle) { |
| 512 | return mRemoteInputView.requestScrollTo(); |
| 513 | } |
| 514 | |
| 515 | @Override |
Adrian Roos | 56cf73a | 2016-07-18 14:23:32 -0700 | [diff] [blame] | 516 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 517 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
| 518 | // Eat the DOWN event here to prevent any default behavior. |
Adrian Roos | 777ef56 | 2015-12-01 17:37:14 -0800 | [diff] [blame] | 519 | return true; |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 520 | } |
Adrian Roos | 56cf73a | 2016-07-18 14:23:32 -0700 | [diff] [blame] | 521 | return super.onKeyDown(keyCode, event); |
| 522 | } |
| 523 | |
| 524 | @Override |
| 525 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 526 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
| 527 | defocusIfNeeded(true /* animate */); |
| 528 | return true; |
| 529 | } |
| 530 | return super.onKeyUp(keyCode, event); |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 533 | @Override |
Adrian Roos | d009ab1 | 2016-05-20 17:58:53 -0700 | [diff] [blame] | 534 | public boolean onCheckIsTextEditor() { |
| 535 | // Stop being editable while we're being removed. During removal, we get reattached, |
| 536 | // and editable views get their spellchecking state re-evaluated which is too costly |
| 537 | // during the removal animation. |
| 538 | boolean flyingOut = mRemoteInputView != null && mRemoteInputView.mRemoved; |
| 539 | return !flyingOut && super.onCheckIsTextEditor(); |
| 540 | } |
| 541 | |
| 542 | @Override |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 543 | public InputConnection onCreateInputConnection(EditorInfo outAttrs) { |
| 544 | final InputConnection inputConnection = super.onCreateInputConnection(outAttrs); |
| 545 | |
| 546 | if (mShowImeOnInputConnection && inputConnection != null) { |
| 547 | final InputMethodManager imm = InputMethodManager.getInstance(); |
| 548 | if (imm != null) { |
| 549 | // onCreateInputConnection is called by InputMethodManager in the middle of |
| 550 | // setting up the connection to the IME; wait with requesting the IME until that |
| 551 | // work has completed. |
| 552 | post(new Runnable() { |
| 553 | @Override |
| 554 | public void run() { |
| 555 | imm.viewClicked(RemoteEditText.this); |
| 556 | imm.showSoftInput(RemoteEditText.this, 0); |
| 557 | } |
| 558 | }); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | return inputConnection; |
| 563 | } |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 564 | |
Adrian Roos | fe84e1f | 2015-11-04 15:55:39 -0800 | [diff] [blame] | 565 | @Override |
| 566 | public void onCommitCompletion(CompletionInfo text) { |
| 567 | clearComposingText(); |
| 568 | setText(text.getText()); |
| 569 | setSelection(getText().length()); |
| 570 | } |
| 571 | |
Adrian Roos | 497ab02 | 2015-02-10 20:49:33 +0100 | [diff] [blame] | 572 | void setInnerFocusable(boolean focusable) { |
| 573 | setFocusableInTouchMode(focusable); |
| 574 | setFocusable(focusable); |
| 575 | setCursorVisible(focusable); |
| 576 | |
| 577 | if (focusable) { |
| 578 | requestFocus(); |
| 579 | setBackground(mBackground); |
| 580 | } else { |
| 581 | setBackground(null); |
| 582 | } |
| 583 | |
| 584 | } |
| 585 | } |
| 586 | } |