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