/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.widget;
import static android.view.ContentInfo.SOURCE_DRAG_AND_DROP;
import static android.widget.TextView.ACCESSIBILITY_ACTION_SMART_START_ID;
import static com.android.graphics.hwui.flags.Flags.highContrastTextSmallTextRect;
import android.R;
import android.animation.ValueAnimator;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.AppGlobals;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.app.RemoteAction;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.ClipData;
import android.content.Context;
import android.content.Intent;
import android.content.UndoManager;
import android.content.UndoOperation;
import android.content.UndoOwner;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.RecordingCanvas;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.RenderNode;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.LocaleList;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.ParcelableParcel;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.DynamicLayout;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Layout;
import android.text.ParcelableSpan;
import android.text.Selection;
import android.text.SpanWatcher;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.StaticLayout;
import android.text.TextFlags;
import android.text.TextUtils;
import android.text.method.InsertModeTransformationMethod;
import android.text.method.KeyListener;
import android.text.method.MetaKeyKeyListener;
import android.text.method.MovementMethod;
import android.text.method.OffsetMapping;
import android.text.method.TransformationMethod;
import android.text.method.WordIterator;
import android.text.style.EasyEditSpan;
import android.text.style.SuggestionRangeSpan;
import android.text.style.SuggestionSpan;
import android.text.style.TextAppearanceSpan;
import android.text.style.URLSpan;
import android.util.ArraySet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.Pair;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.ActionMode;
import android.view.ActionMode.Callback;
import android.view.ContentInfo;
import android.view.ContextMenu;
import android.view.ContextThemeWrapper;
import android.view.DragAndDropPermissions;
import android.view.DragEvent;
import android.view.Gravity;
import android.view.HapticFeedbackConstants;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.OnReceiveContentListener;
import android.view.SubMenu;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import android.view.ViewRootImpl;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
import android.view.animation.LinearInterpolator;
import android.view.inputmethod.CorrectionInfo;
import android.view.inputmethod.CursorAnchorInfo;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import android.view.textclassifier.TextClassification;
import android.view.textclassifier.TextClassificationManager;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView.Drawables;
import android.widget.TextView.OnEditorActionListener;
import android.window.OnBackInvokedCallback;
import android.window.OnBackInvokedDispatcher;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.graphics.ColorUtils;
import com.android.internal.inputmethod.EditableInputConnection;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.GrowingArrayUtils;
import com.android.internal.util.Preconditions;
import com.android.internal.view.FloatingActionMode;
import com.android.text.flags.Flags;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Helper class used by TextView to handle editable text views.
*
* @hide
*/
public class Editor {
private static final String TAG = "Editor";
private static final boolean DEBUG_UNDO = false;
// Specifies whether to use the magnifier when pressing the insertion or selection handles.
private static final boolean FLAG_USE_MAGNIFIER = true;
// Specifies how far to make the cursor start float when drag the cursor away from the
// beginning or end of the line.
private static final int CURSOR_START_FLOAT_DISTANCE_PX = 20;
private static final int DELAY_BEFORE_HANDLE_FADES_OUT = 4000;
private static final int RECENT_CUT_COPY_DURATION_MS = 15 * 1000; // 15 seconds in millis
static final int BLINK = 500;
private static final int DRAG_SHADOW_MAX_TEXT_LENGTH = 20;
private static final int UNSET_X_VALUE = -1;
private static final int UNSET_LINE = -1;
// Tag used when the Editor maintains its own separate UndoManager.
private static final String UNDO_OWNER_TAG = "Editor";
// Ordering constants used to place the Action Mode items in their menu.
private static final int ACTION_MODE_MENU_ITEM_ORDER_ASSIST = 0;
private static final int ACTION_MODE_MENU_ITEM_ORDER_CUT = 4;
private static final int ACTION_MODE_MENU_ITEM_ORDER_COPY = 5;
private static final int ACTION_MODE_MENU_ITEM_ORDER_PASTE = 6;
private static final int ACTION_MODE_MENU_ITEM_ORDER_SHARE = 7;
private static final int ACTION_MODE_MENU_ITEM_ORDER_SELECT_ALL = 8;
private static final int ACTION_MODE_MENU_ITEM_ORDER_REPLACE = 9;
private static final int ACTION_MODE_MENU_ITEM_ORDER_AUTOFILL = 10;
private static final int ACTION_MODE_MENU_ITEM_ORDER_PASTE_AS_PLAIN_TEXT = 11;
private static final int ACTION_MODE_MENU_ITEM_ORDER_SECONDARY_ASSIST_ACTIONS_START = 50;
private static final int ACTION_MODE_MENU_ITEM_ORDER_PROCESS_TEXT_INTENT_ACTIONS_START = 100;
private static final int CONTEXT_MENU_ITEM_ORDER_REPLACE = 11;
private static final int CONTEXT_MENU_GROUP_UNDO_REDO = Menu.FIRST;
private static final int CONTEXT_MENU_GROUP_CLIPBOARD = Menu.FIRST + 1;
private static final int CONTEXT_MENU_GROUP_MISC = Menu.FIRST + 2;
private static final int FLAG_MISSPELLED_OR_GRAMMAR_ERROR =
SuggestionSpan.FLAG_MISSPELLED | SuggestionSpan.FLAG_GRAMMAR_ERROR;
@IntDef({MagnifierHandleTrigger.SELECTION_START,
MagnifierHandleTrigger.SELECTION_END,
MagnifierHandleTrigger.INSERTION})
@Retention(RetentionPolicy.SOURCE)
private @interface MagnifierHandleTrigger {
int INSERTION = 0;
int SELECTION_START = 1;
int SELECTION_END = 2;
}
@IntDef({TextActionMode.SELECTION, TextActionMode.INSERTION, TextActionMode.TEXT_LINK})
@interface TextActionMode {
int SELECTION = 0;
int INSERTION = 1;
int TEXT_LINK = 2;
}
// Default content insertion handler.
private final TextViewOnReceiveContentListener mDefaultOnReceiveContentListener =
new TextViewOnReceiveContentListener();
// Each Editor manages its own undo stack.
private final UndoManager mUndoManager = new UndoManager();
private UndoOwner mUndoOwner = mUndoManager.getOwner(UNDO_OWNER_TAG, this);
final UndoInputFilter mUndoInputFilter = new UndoInputFilter(this);
boolean mAllowUndo = true;
private final MetricsLogger mMetricsLogger = new MetricsLogger();
// Cursor Controllers.
InsertionPointCursorController mInsertionPointCursorController;
SelectionModifierCursorController mSelectionModifierCursorController;
// Action mode used when text is selected or when actions on an insertion cursor are triggered.
private ActionMode mTextActionMode;
@UnsupportedAppUsage
private boolean mInsertionControllerEnabled;
@UnsupportedAppUsage
private boolean mSelectionControllerEnabled;
private final boolean mHapticTextHandleEnabled;
/** Handles OnBackInvokedCallback back dispatch */
private final OnBackInvokedCallback mBackCallback = this::stopTextActionMode;
private boolean mBackCallbackRegistered;
@Nullable
private MagnifierMotionAnimator mMagnifierAnimator;
private final Runnable mUpdateMagnifierRunnable = new Runnable() {
@Override
public void run() {
mMagnifierAnimator.update();
}
};
// Update the magnifier contents whenever anything in the view hierarchy is updated.
// Note: this only captures UI thread-visible changes, so it's a known issue that an animating
// VectorDrawable or Ripple animation will not trigger capture, since they're owned by
// RenderThread.
private final ViewTreeObserver.OnDrawListener mMagnifierOnDrawListener =
new ViewTreeObserver.OnDrawListener() {
@Override
public void onDraw() {
if (mMagnifierAnimator != null) {
// Posting the method will ensure that updating the magnifier contents will
// happen right after the rendering of the current frame.
mTextView.post(mUpdateMagnifierRunnable);
}
}
};
// Used to highlight a word when it is corrected by the IME
private CorrectionHighlighter mCorrectionHighlighter;
/**
* {@code true} when {@link TextView#setText(CharSequence, TextView.BufferType, boolean, int)}
* is being executed and {@link InputMethodManager#restartInput(View)} is scheduled to be
* called.
*
*
This is also used to avoid an unnecessary invocation of
* {@link InputMethodManager#updateSelection(View, int, int, int, int)} when
* {@link InputMethodManager#restartInput(View)} is scheduled to be called already
* See bug 186582769 for details.
*
* TODO(186582769): Come up with better way.
*/
private boolean mHasPendingRestartInputForSetText = false;
InputContentType mInputContentType;
InputMethodState mInputMethodState;
private static class TextRenderNode {
// Render node has 3 recording states:
// 1. Recorded operations are valid.
// #needsRecord() returns false, but needsToBeShifted is false.
// 2. Recorded operations are not valid, but just the position needed to be updated.
// #needsRecord() returns false, but needsToBeShifted is true.
// 3. Recorded operations are not valid. Need to record operations. #needsRecord() returns
// true.
RenderNode renderNode;
boolean isDirty;
// Becomes true when recorded operations can be reused, but the position has to be updated.
boolean needsToBeShifted;
public TextRenderNode(String name) {
renderNode = RenderNode.create(name, null);
isDirty = true;
needsToBeShifted = true;
}
boolean needsRecord() {
return isDirty || !renderNode.hasDisplayList();
}
}
private TextRenderNode[] mTextRenderNodes;
boolean mFrozenWithFocus;
boolean mSelectionMoved;
boolean mTouchFocusSelected;
KeyListener mKeyListener;
int mInputType = EditorInfo.TYPE_NULL;
boolean mDiscardNextActionUp;
boolean mIgnoreActionUpEvent;
/**
* To set a custom cursor, you should use {@link TextView#setTextCursorDrawable(Drawable)}
* or {@link TextView#setTextCursorDrawable(int)}.
*/
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
private long mShowCursor;
private boolean mRenderCursorRegardlessTiming;
private Blink mBlink;
// Whether to let magnifier draw cursor on its surface. This is for floating cursor effect.
// And it can only be true when |mNewMagnifierEnabled| is true.
private boolean mDrawCursorOnMagnifier;
boolean mCursorVisible = true;
boolean mSelectAllOnFocus;
boolean mTextIsSelectable;
CharSequence mError;
boolean mErrorWasChanged;
private ErrorPopup mErrorPopup;
/**
* This flag is set if the TextView tries to display an error before it
* is attached to the window (so its position is still unknown).
* It causes the error to be shown later, when onAttachedToWindow()
* is called.
*/
private boolean mShowErrorAfterAttach;
boolean mInBatchEditControllers;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
boolean mShowSoftInputOnFocus = true;
private boolean mPreserveSelection;
private boolean mRestartActionModeOnNextRefresh;
private boolean mRequestingLinkActionMode;
private SelectionActionModeHelper mSelectionActionModeHelper;
boolean mIsBeingLongClicked;
boolean mIsBeingLongClickedByAccessibility;
private SuggestionsPopupWindow mSuggestionsPopupWindow;
SuggestionRangeSpan mSuggestionRangeSpan;
private Runnable mShowSuggestionRunnable;
Drawable mDrawableForCursor = null;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Drawable mSelectHandleLeft;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Drawable mSelectHandleRight;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
Drawable mSelectHandleCenter;
// Global listener that detects changes in the global position of the TextView
private PositionListener mPositionListener;
private float mContextMenuAnchorX, mContextMenuAnchorY;
Callback mCustomSelectionActionModeCallback;
Callback mCustomInsertionActionModeCallback;
// Set when this TextView gained focus with some text selected. Will start selection mode.
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
boolean mCreatedWithASelection;
// The button state as of the last time #onTouchEvent is called.
private int mLastButtonState;
private final EditorTouchState mTouchState = new EditorTouchState();
private Runnable mInsertionActionModeRunnable;
// The span controller helps monitoring the changes to which the Editor needs to react:
// - EasyEditSpans, for which we have some UI to display on attach and on hide
// - SelectionSpans, for which we need to call updateSelection if an IME is attached
private SpanController mSpanController;
private WordIterator mWordIterator;
SpellChecker mSpellChecker;
// This word iterator is set with text and used to determine word boundaries
// when a user is selecting text.
private WordIterator mWordIteratorWithText;
// Indicate that the text in the word iterator needs to be updated.
private boolean mUpdateWordIteratorText;
private Rect mTempRect;
private final TextView mTextView;
final ProcessTextIntentActionsHandler mProcessTextIntentActionsHandler;
private final CursorAnchorInfoNotifier mCursorAnchorInfoNotifier =
new CursorAnchorInfoNotifier();
private final Runnable mShowFloatingToolbar = new Runnable() {
@Override
public void run() {
if (mTextActionMode != null) {
mTextActionMode.hide(0); // hide off.
}
}
};
boolean mIsInsertionActionModeStartPending = false;
private final SuggestionHelper mSuggestionHelper = new SuggestionHelper();
private boolean mFlagCursorDragFromAnywhereEnabled;
private float mCursorDragDirectionMinXYRatio;
private boolean mFlagInsertionHandleGesturesEnabled;
// Specifies whether the new magnifier (with fish-eye effect) is enabled.
private final boolean mNewMagnifierEnabled;
// Line height range in DP for the new magnifier.
static private final int MIN_LINE_HEIGHT_FOR_MAGNIFIER = 20;
static private final int MAX_LINE_HEIGHT_FOR_MAGNIFIER = 32;
// Line height range in pixels for the new magnifier.
// - If the line height is bigger than the max, magnifier should be dismissed.
// - If the line height is smaller than the min, magnifier should apply a bigger zoom factor
// to make sure the text can be seen clearly.
private int mMinLineHeightForMagnifier;
private int mMaxLineHeightForMagnifier;
// The zoom factor initially configured.
// The actual zoom value may changes based on this initial zoom value.
private float mInitialZoom = 1f;
// For calculating the line change slops while moving cursor/selection.
// The slop value as ratio of the current line height. It indicates the tolerant distance to
// avoid the cursor jumps to upper/lower line when the hit point is moving vertically out of
// the current line.
private final float mLineSlopRatio;
// The slop max/min value include line height and the slop on the upper/lower line.
private static final int LINE_CHANGE_SLOP_MAX_DP = 45;
private static final int LINE_CHANGE_SLOP_MIN_DP = 8;
private int mLineChangeSlopMax;
private int mLineChangeSlopMin;
private boolean mUseNewContextMenu;
private final AccessibilitySmartActions mA11ySmartActions;
private InsertModeController mInsertModeController;
Editor(TextView textView) {
mTextView = textView;
// Synchronize the filter list, which places the undo input filter at the end.
mTextView.setFilters(mTextView.getFilters());
mProcessTextIntentActionsHandler = new ProcessTextIntentActionsHandler(this);
mA11ySmartActions = new AccessibilitySmartActions(mTextView);
mHapticTextHandleEnabled = mTextView.getContext().getResources().getBoolean(
com.android.internal.R.bool.config_enableHapticTextHandle);
mFlagCursorDragFromAnywhereEnabled = AppGlobals.getIntCoreSetting(
WidgetFlags.KEY_ENABLE_CURSOR_DRAG_FROM_ANYWHERE,
WidgetFlags.ENABLE_CURSOR_DRAG_FROM_ANYWHERE_DEFAULT ? 1 : 0) != 0;
final int cursorDragMinAngleFromVertical = AppGlobals.getIntCoreSetting(
WidgetFlags.KEY_CURSOR_DRAG_MIN_ANGLE_FROM_VERTICAL,
WidgetFlags.CURSOR_DRAG_MIN_ANGLE_FROM_VERTICAL_DEFAULT);
mCursorDragDirectionMinXYRatio = EditorTouchState.getXYRatio(
cursorDragMinAngleFromVertical);
mFlagInsertionHandleGesturesEnabled = AppGlobals.getIntCoreSetting(
WidgetFlags.KEY_ENABLE_INSERTION_HANDLE_GESTURES,
WidgetFlags.ENABLE_INSERTION_HANDLE_GESTURES_DEFAULT ? 1 : 0) != 0;
mNewMagnifierEnabled = AppGlobals.getIntCoreSetting(
WidgetFlags.KEY_ENABLE_NEW_MAGNIFIER,
WidgetFlags.ENABLE_NEW_MAGNIFIER_DEFAULT ? 1 : 0) != 0;
mLineSlopRatio = AppGlobals.getFloatCoreSetting(
WidgetFlags.KEY_LINE_SLOP_RATIO,
WidgetFlags.LINE_SLOP_RATIO_DEFAULT);
mUseNewContextMenu = AppGlobals.getIntCoreSetting(
TextFlags.KEY_ENABLE_NEW_CONTEXT_MENU,
TextFlags.ENABLE_NEW_CONTEXT_MENU_DEFAULT ? 1 : 0) != 0;
if (TextView.DEBUG_CURSOR) {
logCursor("Editor", "Cursor drag from anywhere is %s.",
mFlagCursorDragFromAnywhereEnabled ? "enabled" : "disabled");
logCursor("Editor", "Cursor drag min angle from vertical is %d (= %f x/y ratio)",
cursorDragMinAngleFromVertical, mCursorDragDirectionMinXYRatio);
logCursor("Editor", "Insertion handle gestures is %s.",
mFlagInsertionHandleGesturesEnabled ? "enabled" : "disabled");
logCursor("Editor", "New magnifier is %s.",
mNewMagnifierEnabled ? "enabled" : "disabled");
}
mLineChangeSlopMax = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, LINE_CHANGE_SLOP_MAX_DP,
mTextView.getContext().getResources().getDisplayMetrics());
mLineChangeSlopMin = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, LINE_CHANGE_SLOP_MIN_DP,
mTextView.getContext().getResources().getDisplayMetrics());
}
@VisibleForTesting
public boolean getFlagCursorDragFromAnywhereEnabled() {
return mFlagCursorDragFromAnywhereEnabled;
}
@VisibleForTesting
public void setFlagCursorDragFromAnywhereEnabled(boolean enabled) {
mFlagCursorDragFromAnywhereEnabled = enabled;
}
@VisibleForTesting
public void setCursorDragMinAngleFromVertical(int degreesFromVertical) {
mCursorDragDirectionMinXYRatio = EditorTouchState.getXYRatio(degreesFromVertical);
}
@VisibleForTesting
public boolean getFlagInsertionHandleGesturesEnabled() {
return mFlagInsertionHandleGesturesEnabled;
}
@VisibleForTesting
public void setFlagInsertionHandleGesturesEnabled(boolean enabled) {
mFlagInsertionHandleGesturesEnabled = enabled;
}
// Lazy creates the magnifier animator.
private MagnifierMotionAnimator getMagnifierAnimator() {
if (FLAG_USE_MAGNIFIER && mMagnifierAnimator == null) {
// Lazy creates the magnifier instance because it requires the text height which cannot
// be measured at the time of Editor instance being created.
final Magnifier.Builder builder = mNewMagnifierEnabled
? createBuilderWithInlineMagnifierDefaults()
: Magnifier.createBuilderWithOldMagnifierDefaults(mTextView);
mMagnifierAnimator = new MagnifierMotionAnimator(builder.build());
}
return mMagnifierAnimator;
}
private Magnifier.Builder createBuilderWithInlineMagnifierDefaults() {
final Magnifier.Builder params = new Magnifier.Builder(mTextView);
float zoom = AppGlobals.getFloatCoreSetting(
WidgetFlags.KEY_MAGNIFIER_ZOOM_FACTOR,
WidgetFlags.MAGNIFIER_ZOOM_FACTOR_DEFAULT);
float aspectRatio = AppGlobals.getFloatCoreSetting(
WidgetFlags.KEY_MAGNIFIER_ASPECT_RATIO,
WidgetFlags.MAGNIFIER_ASPECT_RATIO_DEFAULT);
// Avoid invalid/unsupported values.
if (zoom < 1.2f || zoom > 1.8f) {
zoom = 1.5f;
}
if (aspectRatio < 3 || aspectRatio > 8) {
aspectRatio = 5.5f;
}
mInitialZoom = zoom;
mMinLineHeightForMagnifier = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, MIN_LINE_HEIGHT_FOR_MAGNIFIER,
mTextView.getContext().getResources().getDisplayMetrics());
mMaxLineHeightForMagnifier = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, MAX_LINE_HEIGHT_FOR_MAGNIFIER,
mTextView.getContext().getResources().getDisplayMetrics());
final Layout layout = mTextView.getLayout();
final int line = layout.getLineForOffset(mTextView.getSelectionStartTransformed());
final int sourceHeight = layout.getLineBottom(line, /* includeLineSpacing= */ false)
- layout.getLineTop(line);
final int height = (int)(sourceHeight * zoom);
final int width = (int)(aspectRatio * Math.max(sourceHeight, mMinLineHeightForMagnifier));
params.setFishEyeStyle()
.setSize(width, height)
.setSourceSize(width, sourceHeight)
.setElevation(0)
.setInitialZoom(zoom)
.setClippingEnabled(false);
final Context context = mTextView.getContext();
final TypedArray a = context.obtainStyledAttributes(
null, com.android.internal.R.styleable.Magnifier,
com.android.internal.R.attr.magnifierStyle, 0);
params.setDefaultSourceToMagnifierOffset(
a.getDimensionPixelSize(
com.android.internal.R.styleable.Magnifier_magnifierHorizontalOffset, 0),
a.getDimensionPixelSize(
com.android.internal.R.styleable.Magnifier_magnifierVerticalOffset, 0));
a.recycle();
return params.setSourceBounds(
Magnifier.SOURCE_BOUND_MAX_VISIBLE,
Magnifier.SOURCE_BOUND_MAX_IN_SURFACE,
Magnifier.SOURCE_BOUND_MAX_VISIBLE,
Magnifier.SOURCE_BOUND_MAX_IN_SURFACE);
}
ParcelableParcel saveInstanceState() {
ParcelableParcel state = new ParcelableParcel(getClass().getClassLoader());
Parcel parcel = state.getParcel();
mUndoManager.saveInstanceState(parcel);
mUndoInputFilter.saveInstanceState(parcel);
return state;
}
void restoreInstanceState(ParcelableParcel state) {
Parcel parcel = state.getParcel();
mUndoManager.restoreInstanceState(parcel, state.getClassLoader());
mUndoInputFilter.restoreInstanceState(parcel);
// Re-associate this object as the owner of undo state.
mUndoOwner = mUndoManager.getOwner(UNDO_OWNER_TAG, this);
}
/**
* Returns the default handler for receiving content in an editable {@link TextView}. This
* listener impl is used to encapsulate the default behavior but it is not part of the public
* API. If an app wants to execute the default platform behavior for receiving content, it
* should call {@link View#onReceiveContent}. Alternatively, if an app implements a custom
* listener for receiving content and wants to delegate some of the content to be handled by
* the platform, it should return the corresponding content from its listener. See
* {@link View#setOnReceiveContentListener} and {@link OnReceiveContentListener} for more info.
*/
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE)
@NonNull
public TextViewOnReceiveContentListener getDefaultOnReceiveContentListener() {
return mDefaultOnReceiveContentListener;
}
/**
* Forgets all undo and redo operations for this Editor.
*/
void forgetUndoRedo() {
UndoOwner[] owners = { mUndoOwner };
mUndoManager.forgetUndos(owners, -1 /* all */);
mUndoManager.forgetRedos(owners, -1 /* all */);
}
boolean canUndo() {
UndoOwner[] owners = { mUndoOwner };
return mAllowUndo && mUndoManager.countUndos(owners) > 0;
}
boolean canRedo() {
UndoOwner[] owners = { mUndoOwner };
return mAllowUndo && mUndoManager.countRedos(owners) > 0;
}
void undo() {
if (!mAllowUndo) {
return;
}
UndoOwner[] owners = { mUndoOwner };
mUndoManager.undo(owners, 1); // Undo 1 action.
}
void redo() {
if (!mAllowUndo) {
return;
}
UndoOwner[] owners = { mUndoOwner };
mUndoManager.redo(owners, 1); // Redo 1 action.
}
void replace() {
if (mSuggestionsPopupWindow == null) {
mSuggestionsPopupWindow = new SuggestionsPopupWindow();
}
hideCursorAndSpanControllers();
mSuggestionsPopupWindow.show();
int middle = (mTextView.getSelectionStart() + mTextView.getSelectionEnd()) / 2;
Selection.setSelection((Spannable) mTextView.getText(), middle);
}
void onAttachedToWindow() {
if (mShowErrorAfterAttach) {
showError();
mShowErrorAfterAttach = false;
}
final ViewTreeObserver observer = mTextView.getViewTreeObserver();
if (observer.isAlive()) {
// No need to create the controller.
// The get method will add the listener on controller creation.
if (mInsertionPointCursorController != null) {
observer.addOnTouchModeChangeListener(mInsertionPointCursorController);
}
if (mSelectionModifierCursorController != null) {
mSelectionModifierCursorController.resetTouchOffsets();
observer.addOnTouchModeChangeListener(mSelectionModifierCursorController);
}
if (FLAG_USE_MAGNIFIER) {
observer.addOnDrawListener(mMagnifierOnDrawListener);
}
}
updateSpellCheckSpans(0, mTextView.getText().length(),
true /* create the spell checker if needed */);
if (mTextView.hasSelection()) {
refreshTextActionMode();
}
getPositionListener().addSubscriber(mCursorAnchorInfoNotifier, true);
// Call resumeBlink here instead of makeBlink to ensure that if mBlink is not null the
// Blink object is uncancelled. This ensures when a view is removed and added back the
// cursor will resume blinking.
resumeBlink();
}
void onDetachedFromWindow() {
getPositionListener().removeSubscriber(mCursorAnchorInfoNotifier);
if (mError != null) {
hideError();
}
suspendBlink();
if (mInsertionPointCursorController != null) {
mInsertionPointCursorController.onDetached();
}
if (mSelectionModifierCursorController != null) {
mSelectionModifierCursorController.onDetached();
}
if (mShowSuggestionRunnable != null) {
mTextView.removeCallbacks(mShowSuggestionRunnable);
}
// Cancel the single tap delayed runnable.
if (mInsertionActionModeRunnable != null) {
mTextView.removeCallbacks(mInsertionActionModeRunnable);
}
mTextView.removeCallbacks(mShowFloatingToolbar);
discardTextDisplayLists();
if (mSpellChecker != null) {
mSpellChecker.closeSession();
// Forces the creation of a new SpellChecker next time this window is created.
// Will handle the cases where the settings has been changed in the meantime.
mSpellChecker = null;
}
if (FLAG_USE_MAGNIFIER) {
final ViewTreeObserver observer = mTextView.getViewTreeObserver();
if (observer.isAlive()) {
observer.removeOnDrawListener(mMagnifierOnDrawListener);
}
}
hideCursorAndSpanControllers();
stopTextActionModeWithPreservingSelection();
mDefaultOnReceiveContentListener.clearInputConnectionInfo();
unregisterOnBackInvokedCallback();
}
private void unregisterOnBackInvokedCallback() {
if (!mBackCallbackRegistered) {
return;
}
ViewRootImpl viewRootImpl = getTextView().getViewRootImpl();
if (viewRootImpl != null
&& viewRootImpl.getOnBackInvokedDispatcher().isOnBackInvokedCallbackEnabled()) {
viewRootImpl.getOnBackInvokedDispatcher()
.unregisterOnBackInvokedCallback(mBackCallback);
mBackCallbackRegistered = false;
}
}
private void registerOnBackInvokedCallback() {
if (mBackCallbackRegistered) {
return;
}
ViewRootImpl viewRootImpl = mTextView.getViewRootImpl();
if (viewRootImpl != null
&& viewRootImpl.getOnBackInvokedDispatcher().isOnBackInvokedCallbackEnabled()) {
viewRootImpl.getOnBackInvokedDispatcher().registerOnBackInvokedCallback(
OnBackInvokedDispatcher.PRIORITY_DEFAULT, mBackCallback);
mBackCallbackRegistered = true;
}
}
private void discardTextDisplayLists() {
if (mTextRenderNodes != null) {
for (int i = 0; i < mTextRenderNodes.length; i++) {
RenderNode displayList = mTextRenderNodes[i] != null
? mTextRenderNodes[i].renderNode : null;
if (displayList != null && displayList.hasDisplayList()) {
displayList.discardDisplayList();
}
}
}
}
private void showError() {
if (mTextView.getWindowToken() == null) {
mShowErrorAfterAttach = true;
return;
}
if (mErrorPopup == null) {
LayoutInflater inflater = LayoutInflater.from(mTextView.getContext());
final TextView err = (TextView) inflater.inflate(
com.android.internal.R.layout.textview_hint, null);
final float scale = mTextView.getResources().getDisplayMetrics().density;
mErrorPopup =
new ErrorPopup(err, (int) (200 * scale + 0.5f), (int) (50 * scale + 0.5f));
mErrorPopup.setFocusable(false);
// The user is entering text, so the input method is needed. We
// don't want the popup to be displayed on top of it.
mErrorPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
}
TextView tv = (TextView) mErrorPopup.getContentView();
chooseSize(mErrorPopup, mError, tv);
tv.setText(mError);
mErrorPopup.showAsDropDown(mTextView, getErrorX(), getErrorY(),
Gravity.TOP | Gravity.LEFT);
mErrorPopup.fixDirection(mErrorPopup.isAboveAnchor());
}
public void setError(CharSequence error, Drawable icon) {
mError = TextUtils.stringOrSpannedString(error);
mErrorWasChanged = true;
if (mError == null) {
setErrorIcon(null);
if (mErrorPopup != null) {
if (mErrorPopup.isShowing()) {
mErrorPopup.dismiss();
}
mErrorPopup = null;
}
mShowErrorAfterAttach = false;
} else {
setErrorIcon(icon);
if (mTextView.isFocused()) {
showError();
}
}
}
private void setErrorIcon(Drawable icon) {
Drawables dr = mTextView.mDrawables;
if (dr == null) {
mTextView.mDrawables = dr = new Drawables(mTextView.getContext());
}
dr.setErrorDrawable(icon, mTextView);
mTextView.resetResolvedDrawables();
mTextView.invalidate();
mTextView.requestLayout();
}
private void hideError() {
if (mErrorPopup != null) {
if (mErrorPopup.isShowing()) {
mErrorPopup.dismiss();
}
}
mShowErrorAfterAttach = false;
}
/**
* Returns the X offset to make the pointy top of the error point
* at the middle of the error icon.
*/
private int getErrorX() {
/*
* The "25" is the distance between the point and the right edge
* of the background
*/
final float scale = mTextView.getResources().getDisplayMetrics().density;
final Drawables dr = mTextView.mDrawables;
final int layoutDirection = mTextView.getLayoutDirection();
int errorX;
int offset;
switch (layoutDirection) {
default:
case View.LAYOUT_DIRECTION_LTR:
offset = -(dr != null ? dr.mDrawableSizeRight : 0) / 2 + (int) (25 * scale + 0.5f);
errorX = mTextView.getWidth() - mErrorPopup.getWidth()
- mTextView.getPaddingRight() + offset;
break;
case View.LAYOUT_DIRECTION_RTL:
offset = (dr != null ? dr.mDrawableSizeLeft : 0) / 2 - (int) (25 * scale + 0.5f);
errorX = mTextView.getPaddingLeft() + offset;
break;
}
return errorX;
}
/**
* Returns the Y offset to make the pointy top of the error point
* at the bottom of the error icon.
*/
private int getErrorY() {
/*
* Compound, not extended, because the icon is not clipped
* if the text height is smaller.
*/
final int compoundPaddingTop = mTextView.getCompoundPaddingTop();
int vspace = mTextView.getBottom() - mTextView.getTop()
- mTextView.getCompoundPaddingBottom() - compoundPaddingTop;
final Drawables dr = mTextView.mDrawables;
final int layoutDirection = mTextView.getLayoutDirection();
int height;
switch (layoutDirection) {
default:
case View.LAYOUT_DIRECTION_LTR:
height = (dr != null ? dr.mDrawableHeightRight : 0);
break;
case View.LAYOUT_DIRECTION_RTL:
height = (dr != null ? dr.mDrawableHeightLeft : 0);
break;
}
int icontop = compoundPaddingTop + (vspace - height) / 2;
/*
* The "2" is the distance between the point and the top edge
* of the background.
*/
final float scale = mTextView.getResources().getDisplayMetrics().density;
return icontop + height - mTextView.getHeight() - (int) (2 * scale + 0.5f);
}
void createInputContentTypeIfNeeded() {
if (mInputContentType == null) {
mInputContentType = new InputContentType();
}
}
void createInputMethodStateIfNeeded() {
if (mInputMethodState == null) {
mInputMethodState = new InputMethodState();
}
}
private boolean isCursorVisible() {
// The default value is true, even when there is no associated Editor
return mCursorVisible && mTextView.isTextEditable();
}
boolean shouldRenderCursor() {
if (!isCursorVisible()) {
return false;
}
if (mRenderCursorRegardlessTiming) {
return true;
}
final long showCursorDelta = SystemClock.uptimeMillis() - mShowCursor;
return showCursorDelta % (2 * BLINK) < BLINK;
}
void prepareCursorControllers() {
boolean windowSupportsHandles = false;
ViewGroup.LayoutParams params = mTextView.getRootView().getLayoutParams();
if (params instanceof WindowManager.LayoutParams) {
WindowManager.LayoutParams windowParams = (WindowManager.LayoutParams) params;
windowSupportsHandles = windowParams.type < WindowManager.LayoutParams.FIRST_SUB_WINDOW
|| windowParams.type > WindowManager.LayoutParams.LAST_SUB_WINDOW;
}
boolean enabled = windowSupportsHandles && mTextView.getLayout() != null;
mInsertionControllerEnabled = enabled && (mDrawCursorOnMagnifier || isCursorVisible());
mSelectionControllerEnabled = enabled && mTextView.textCanBeSelected();
if (!mInsertionControllerEnabled) {
hideInsertionPointCursorController();
if (mInsertionPointCursorController != null) {
mInsertionPointCursorController.onDetached();
mInsertionPointCursorController = null;
}
}
if (!mSelectionControllerEnabled) {
stopTextActionMode();
if (mSelectionModifierCursorController != null) {
mSelectionModifierCursorController.onDetached();
mSelectionModifierCursorController = null;
}
}
}
void hideInsertionPointCursorController() {
if (mInsertionPointCursorController != null) {
mInsertionPointCursorController.hide();
}
}
/**
* Hides the insertion and span controllers.
*/
void hideCursorAndSpanControllers() {
hideCursorControllers();
hideSpanControllers();
}
private void hideSpanControllers() {
if (mSpanController != null) {
mSpanController.hide();
}
}
private void hideCursorControllers() {
// When mTextView is not ExtractEditText, we need to distinguish two kinds of focus-lost.
// One is the true focus lost where suggestions pop-up (if any) should be dismissed, and the
// other is an side effect of showing the suggestions pop-up itself. We use isShowingUp()
// to distinguish one from the other.
if (mSuggestionsPopupWindow != null && ((mTextView.isInExtractedMode())
|| !mSuggestionsPopupWindow.isShowingUp())) {
// Should be done before hide insertion point controller since it triggers a show of it
mSuggestionsPopupWindow.hide();
}
hideInsertionPointCursorController();
}
/**
* Create new SpellCheckSpans on the modified region.
*/
private void updateSpellCheckSpans(int start, int end, boolean createSpellChecker) {
// Remove spans whose adjacent characters are text not punctuation
mTextView.removeAdjacentSuggestionSpans(start);
mTextView.removeAdjacentSuggestionSpans(end);
if (mTextView.isTextEditable() && mTextView.isSuggestionsEnabled()
&& !(mTextView.isInExtractedMode())) {
final InputMethodManager imm = getInputMethodManager();
if (imm != null && imm.isInputMethodSuppressingSpellChecker()) {
// Do not close mSpellChecker here as it may be reused when the current IME has been
// changed.
return;
}
if (mSpellChecker == null && createSpellChecker) {
mSpellChecker = new SpellChecker(mTextView);
}
if (mSpellChecker != null) {
mSpellChecker.spellCheck(start, end);
}
}
}
void onScreenStateChanged(int screenState) {
switch (screenState) {
case View.SCREEN_STATE_ON:
resumeBlink();
break;
case View.SCREEN_STATE_OFF:
suspendBlink();
break;
}
}
private void suspendBlink() {
if (mBlink != null) {
mBlink.cancel();
}
}
private void resumeBlink() {
if (mBlink != null) {
mBlink.uncancel();
}
// Moving makeBlink outside of the null check block ensures that mBlink object gets
// instantiated when the view is added to the window if mBlink is still null.
makeBlink();
}
void adjustInputType(boolean password, boolean passwordInputType,
boolean webPasswordInputType, boolean numberPasswordInputType) {
// mInputType has been set from inputType, possibly modified by mInputMethod.
// Specialize mInputType to [web]password if we have a text class and the original input
// type was a password.
if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) {
if (password || passwordInputType) {
mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION))
| EditorInfo.TYPE_TEXT_VARIATION_PASSWORD;
}
if (webPasswordInputType) {
mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION))
| EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD;
}
} else if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_NUMBER) {
if (numberPasswordInputType) {
mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION))
| EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD;
}
}
}
private void chooseSize(@NonNull PopupWindow pop, @NonNull CharSequence text,
@NonNull TextView tv) {
final int wid = tv.getPaddingLeft() + tv.getPaddingRight();
final int ht = tv.getPaddingTop() + tv.getPaddingBottom();
final int defaultWidthInPixels = mTextView.getResources().getDimensionPixelSize(
com.android.internal.R.dimen.textview_error_popup_default_width);
final StaticLayout l = StaticLayout.Builder.obtain(text, 0, text.length(), tv.getPaint(),
defaultWidthInPixels)
.setUseLineSpacingFromFallbacks(tv.isFallbackLineSpacingForStaticLayout())
.build();
float max = 0;
for (int i = 0; i < l.getLineCount(); i++) {
max = Math.max(max, l.getLineWidth(i));
}
/*
* Now set the popup size to be big enough for the text plus the border capped
* to DEFAULT_MAX_POPUP_WIDTH
*/
pop.setWidth(wid + (int) Math.ceil(max));
pop.setHeight(ht + l.getHeight());
}
void setFrame() {
if (mErrorPopup != null) {
TextView tv = (TextView) mErrorPopup.getContentView();
chooseSize(mErrorPopup, mError, tv);
mErrorPopup.update(mTextView, getErrorX(), getErrorY(),
mErrorPopup.getWidth(), mErrorPopup.getHeight());
}
}
private int getWordStart(int offset) {
// FIXME - For this and similar methods we're not doing anything to check if there's
// a LocaleSpan in the text, this may be something we should try handling or checking for.
int retOffset = getWordIteratorWithText().prevBoundary(offset);
if (getWordIteratorWithText().isOnPunctuation(retOffset)) {
// On punctuation boundary or within group of punctuation, find punctuation start.
retOffset = getWordIteratorWithText().getPunctuationBeginning(offset);
} else {
// Not on a punctuation boundary, find the word start.
retOffset = getWordIteratorWithText().getPrevWordBeginningOnTwoWordsBoundary(offset);
}
if (retOffset == BreakIterator.DONE) {
return offset;
}
return retOffset;
}
private int getWordEnd(int offset) {
int retOffset = getWordIteratorWithText().nextBoundary(offset);
if (getWordIteratorWithText().isAfterPunctuation(retOffset)) {
// On punctuation boundary or within group of punctuation, find punctuation end.
retOffset = getWordIteratorWithText().getPunctuationEnd(offset);
} else {
// Not on a punctuation boundary, find the word end.
retOffset = getWordIteratorWithText().getNextWordEndOnTwoWordBoundary(offset);
}
if (retOffset == BreakIterator.DONE) {
return offset;
}
return retOffset;
}
private boolean needsToSelectAllToSelectWordOrParagraph() {
if (mTextView.hasPasswordTransformationMethod()) {
// Always select all on a password field.
// Cut/copy menu entries are not available for passwords, but being able to select all
// is however useful to delete or paste to replace the entire content.
return true;
}
int inputType = mTextView.getInputType();
int klass = inputType & InputType.TYPE_MASK_CLASS;
int variation = inputType & InputType.TYPE_MASK_VARIATION;
// Specific text field types: select the entire text for these
if (klass == InputType.TYPE_CLASS_NUMBER
|| klass == InputType.TYPE_CLASS_PHONE
|| klass == InputType.TYPE_CLASS_DATETIME
|| variation == InputType.TYPE_TEXT_VARIATION_URI
|| variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
|| variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS
|| variation == InputType.TYPE_TEXT_VARIATION_FILTER) {
return true;
}
return false;
}
/**
* Adjusts selection to the word under last touch offset. Return true if the operation was
* successfully performed.
*/
boolean selectCurrentWord() {
if (!mTextView.canSelectText()) {
return false;
}
if (needsToSelectAllToSelectWordOrParagraph()) {
return mTextView.selectAllText();
}
long lastTouchOffsets = getLastTouchOffsets();
final int minOffset = TextUtils.unpackRangeStartFromLong(lastTouchOffsets);
final int maxOffset = TextUtils.unpackRangeEndFromLong(lastTouchOffsets);
// Safety check in case standard touch event handling has been bypassed
if (minOffset < 0 || minOffset > mTextView.getText().length()) return false;
if (maxOffset < 0 || maxOffset > mTextView.getText().length()) return false;
int selectionStart, selectionEnd;
// If a URLSpan (web address, email, phone...) is found at that position, select it.
URLSpan[] urlSpans =
((Spanned) mTextView.getText()).getSpans(minOffset, maxOffset, URLSpan.class);
if (urlSpans.length >= 1) {
URLSpan urlSpan = urlSpans[0];
selectionStart = ((Spanned) mTextView.getText()).getSpanStart(urlSpan);
selectionEnd = ((Spanned) mTextView.getText()).getSpanEnd(urlSpan);
} else {
// FIXME - We should check if there's a LocaleSpan in the text, this may be
// something we should try handling or checking for.
final WordIterator wordIterator = getWordIterator();
wordIterator.setCharSequence(mTextView.getText(), minOffset, maxOffset);
selectionStart = wordIterator.getBeginning(minOffset);
selectionEnd = wordIterator.getEnd(maxOffset);
if (selectionStart == BreakIterator.DONE || selectionEnd == BreakIterator.DONE
|| selectionStart == selectionEnd) {
// Possible when the word iterator does not properly handle the text's language
long range = getCharClusterRange(minOffset);
selectionStart = TextUtils.unpackRangeStartFromLong(range);
selectionEnd = TextUtils.unpackRangeEndFromLong(range);
}
}
Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd);
return selectionEnd > selectionStart;
}
/**
* Adjusts selection to the paragraph under last touch offset. Return true if the operation was
* successfully performed.
*/
private boolean selectCurrentParagraph() {
if (!mTextView.canSelectText()) {
return false;
}
if (needsToSelectAllToSelectWordOrParagraph()) {
return mTextView.selectAllText();
}
long lastTouchOffsets = getLastTouchOffsets();
final int minLastTouchOffset = TextUtils.unpackRangeStartFromLong(lastTouchOffsets);
final int maxLastTouchOffset = TextUtils.unpackRangeEndFromLong(lastTouchOffsets);
final long paragraphsRange = getParagraphsRange(minLastTouchOffset, maxLastTouchOffset);
final int start = TextUtils.unpackRangeStartFromLong(paragraphsRange);
final int end = TextUtils.unpackRangeEndFromLong(paragraphsRange);
if (start < end) {
Selection.setSelection((Spannable) mTextView.getText(), start, end);
return true;
}
return false;
}
/**
* Get the minimum range of paragraphs that contains startOffset and endOffset.
*/
private long getParagraphsRange(int startOffset, int endOffset) {
final int startOffsetTransformed = mTextView.originalToTransformed(startOffset,
OffsetMapping.MAP_STRATEGY_CURSOR);
final int endOffsetTransformed = mTextView.originalToTransformed(endOffset,
OffsetMapping.MAP_STRATEGY_CURSOR);
final Layout layout = mTextView.getLayout();
if (layout == null) {
return TextUtils.packRangeInLong(-1, -1);
}
final CharSequence text = layout.getText();
int minLine = layout.getLineForOffset(startOffsetTransformed);
// Search paragraph start.
while (minLine > 0) {
final int prevLineEndOffset = layout.getLineEnd(minLine - 1);
if (text.charAt(prevLineEndOffset - 1) == '\n') {
break;
}
minLine--;
}
int maxLine = layout.getLineForOffset(endOffsetTransformed);
// Search paragraph end.
while (maxLine < layout.getLineCount() - 1) {
final int lineEndOffset = layout.getLineEnd(maxLine);
if (text.charAt(lineEndOffset - 1) == '\n') {
break;
}
maxLine++;
}
final int paragraphStart = mTextView.transformedToOriginal(layout.getLineStart(minLine),
OffsetMapping.MAP_STRATEGY_CURSOR);
final int paragraphEnd = mTextView.transformedToOriginal(layout.getLineEnd(maxLine),
OffsetMapping.MAP_STRATEGY_CURSOR);
return TextUtils.packRangeInLong(paragraphStart, paragraphEnd);
}
void onLocaleChanged() {
// Will be re-created on demand in getWordIterator and getWordIteratorWithText with the
// proper new locale
mWordIterator = null;
mWordIteratorWithText = null;
}
public WordIterator getWordIterator() {
if (mWordIterator == null) {
mWordIterator = new WordIterator(mTextView.getTextServicesLocale());
}
return mWordIterator;
}
private WordIterator getWordIteratorWithText() {
if (mWordIteratorWithText == null) {
mWordIteratorWithText = new WordIterator(mTextView.getTextServicesLocale());
mUpdateWordIteratorText = true;
}
if (mUpdateWordIteratorText) {
// FIXME - Shouldn't copy all of the text as only the area of the text relevant
// to the user's selection is needed. A possible solution would be to
// copy some number N of characters near the selection and then when the
// user approaches N then we'd do another copy of the next N characters.
CharSequence text = mTextView.getText();
mWordIteratorWithText.setCharSequence(text, 0, text.length());
mUpdateWordIteratorText = false;
}
return mWordIteratorWithText;
}
private int getNextCursorOffset(int offset, boolean findAfterGivenOffset) {
final Layout layout = mTextView.getLayout();
if (layout == null) return offset;
final int offsetTransformed =
mTextView.originalToTransformed(offset, OffsetMapping.MAP_STRATEGY_CURSOR);
final int nextCursor;
if (findAfterGivenOffset == layout.isRtlCharAt(offsetTransformed)) {
nextCursor = layout.getOffsetToLeftOf(offsetTransformed);
} else {
nextCursor = layout.getOffsetToRightOf(offsetTransformed);
}
return mTextView.transformedToOriginal(nextCursor, OffsetMapping.MAP_STRATEGY_CURSOR);
}
private long getCharClusterRange(int offset) {
final int textLength = mTextView.getText().length();
if (offset < textLength) {
final int clusterEndOffset = getNextCursorOffset(offset, true);
return TextUtils.packRangeInLong(
getNextCursorOffset(clusterEndOffset, false), clusterEndOffset);
}
if (offset - 1 >= 0) {
final int clusterStartOffset = getNextCursorOffset(offset, false);
return TextUtils.packRangeInLong(clusterStartOffset,
getNextCursorOffset(clusterStartOffset, true));
}
return TextUtils.packRangeInLong(offset, offset);
}
private boolean touchPositionIsInSelection() {
int selectionStart = mTextView.getSelectionStart();
int selectionEnd = mTextView.getSelectionEnd();
if (selectionStart == selectionEnd) {
return false;
}
if (selectionStart > selectionEnd) {
int tmp = selectionStart;
selectionStart = selectionEnd;
selectionEnd = tmp;
Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd);
}
SelectionModifierCursorController selectionController = getSelectionController();
int minOffset = selectionController.getMinTouchOffset();
int maxOffset = selectionController.getMaxTouchOffset();
return ((minOffset >= selectionStart) && (maxOffset < selectionEnd));
}
private PositionListener getPositionListener() {
if (mPositionListener == null) {
mPositionListener = new PositionListener();
}
return mPositionListener;
}
private interface TextViewPositionListener {
public void updatePosition(int parentPositionX, int parentPositionY,
boolean parentPositionChanged, boolean parentScrolled);
}
private boolean isOffsetVisible(int offset) {
Layout layout = mTextView.getLayout();
if (layout == null) return false;
final int offsetTransformed =
mTextView.originalToTransformed(offset, OffsetMapping.MAP_STRATEGY_CURSOR);
final int line = layout.getLineForOffset(offsetTransformed);
final int lineBottom = layout.getLineBottom(line);
final int primaryHorizontal = (int) layout.getPrimaryHorizontal(offsetTransformed);
return mTextView.isPositionVisible(
primaryHorizontal + mTextView.viewportToContentHorizontalOffset(),
lineBottom + mTextView.viewportToContentVerticalOffset());
}
/** Returns true if the screen coordinates position (x,y) corresponds to a character displayed
* in the view. Returns false when the position is in the empty space of left/right of text.
*/
private boolean isPositionOnText(float x, float y) {
Layout layout = mTextView.getLayout();
if (layout == null) return false;
final int line = mTextView.getLineAtCoordinate(y);
x = mTextView.convertToLocalHorizontalCoordinate(x);
if (x < layout.getLineLeft(line)) return false;
if (x > layout.getLineRight(line)) return false;
return true;
}
private void startDragAndDrop() {
getSelectionActionModeHelper().onSelectionDrag();
// TODO: Fix drag and drop in full screen extracted mode.
if (mTextView.isInExtractedMode()) {
return;
}
final int start = mTextView.getSelectionStart();
final int end = mTextView.getSelectionEnd();
CharSequence selectedText = mTextView.getTransformedText(start, end);
ClipData data = ClipData.newPlainText(null, selectedText);
DragLocalState localState = new DragLocalState(mTextView, start, end);
mTextView.startDragAndDrop(data, getTextThumbnailBuilder(start, end), localState,
View.DRAG_FLAG_GLOBAL);
stopTextActionMode();
if (hasSelectionController()) {
getSelectionController().resetTouchOffsets();
}
}
public boolean performLongClick(boolean handled) {
if (TextView.DEBUG_CURSOR) {
logCursor("performLongClick", "handled=%s", handled);
}
if (mIsBeingLongClickedByAccessibility) {
if (!handled) {
toggleInsertionActionMode();
}
return true;
}
// Long press in empty space moves cursor and starts the insertion action mode.
if (!handled && !isPositionOnText(mTouchState.getLastDownX(), mTouchState.getLastDownY())
&& !mTouchState.isOnHandle() && mInsertionControllerEnabled) {
final int offset = mTextView.getOffsetForPosition(mTouchState.getLastDownX(),
mTouchState.getLastDownY());
Selection.setSelection((Spannable) mTextView.getText(), offset);
getInsertionController().show();
mIsInsertionActionModeStartPending = true;
handled = true;
MetricsLogger.action(
mTextView.getContext(),
MetricsEvent.TEXT_LONGPRESS,
TextViewMetrics.SUBTYPE_LONG_PRESS_OTHER);
}
if (!handled && mTextActionMode != null) {
if (touchPositionIsInSelection()) {
startDragAndDrop();
MetricsLogger.action(
mTextView.getContext(),
MetricsEvent.TEXT_LONGPRESS,
TextViewMetrics.SUBTYPE_LONG_PRESS_DRAG_AND_DROP);
} else {
stopTextActionMode();
selectCurrentWordAndStartDrag();
MetricsLogger.action(
mTextView.getContext(),
MetricsEvent.TEXT_LONGPRESS,
TextViewMetrics.SUBTYPE_LONG_PRESS_SELECTION);
}
handled = true;
}
// Start a new selection
if (!handled) {
handled = selectCurrentWordAndStartDrag();
if (handled) {
MetricsLogger.action(
mTextView.getContext(),
MetricsEvent.TEXT_LONGPRESS,
TextViewMetrics.SUBTYPE_LONG_PRESS_SELECTION);
}
}
return handled;
}
private void toggleInsertionActionMode() {
if (mTextActionMode != null) {
stopTextActionMode();
} else {
startInsertionActionMode();
}
}
float getLastUpPositionX() {
return mTouchState.getLastUpX();
}
float getLastUpPositionY() {
return mTouchState.getLastUpY();
}
private long getLastTouchOffsets() {
SelectionModifierCursorController selectionController = getSelectionController();
final int minOffset = selectionController.getMinTouchOffset();
final int maxOffset = selectionController.getMaxTouchOffset();
return TextUtils.packRangeInLong(minOffset, maxOffset);
}
void onFocusChanged(boolean focused, int direction) {
if (TextView.DEBUG_CURSOR) {
logCursor("onFocusChanged", "focused=%s", focused);
}
mShowCursor = SystemClock.uptimeMillis();
ensureEndedBatchEdit();
if (focused) {
int selStart = mTextView.getSelectionStart();
int selEnd = mTextView.getSelectionEnd();
// SelectAllOnFocus fields are highlighted and not selected. Do not start text selection
// mode for these, unless there was a specific selection already started.
final boolean isFocusHighlighted = mSelectAllOnFocus && selStart == 0
&& selEnd == mTextView.getText().length();
mCreatedWithASelection = mFrozenWithFocus && mTextView.hasSelection()
&& !isFocusHighlighted;
if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) {
// If a tap was used to give focus to that view, move cursor at tap position.
// Has to be done before onTakeFocus, which can be overloaded.
final int lastTapPosition = getLastTapPosition();
if (lastTapPosition >= 0) {
if (TextView.DEBUG_CURSOR) {
logCursor("onFocusChanged", "setting cursor position: %d", lastTapPosition);
}
Selection.setSelection((Spannable) mTextView.getText(), lastTapPosition);
}
// Note this may have to be moved out of the Editor class
MovementMethod mMovement = mTextView.getMovementMethod();
if (mMovement != null) {
mMovement.onTakeFocus(mTextView, (Spannable) mTextView.getText(), direction);
}
// The DecorView does not have focus when the 'Done' ExtractEditText button is
// pressed. Since it is the ViewAncestor's mView, it requests focus before
// ExtractEditText clears focus, which gives focus to the ExtractEditText.
// This special case ensure that we keep current selection in that case.
// It would be better to know why the DecorView does not have focus at that time.
if (((mTextView.isInExtractedMode()) || mSelectionMoved)
&& selStart >= 0 && selEnd >= 0) {
/*
* Someone intentionally set the selection, so let them
* do whatever it is that they wanted to do instead of
* the default on-focus behavior. We reset the selection
* here instead of just skipping the onTakeFocus() call
* because some movement methods do something other than
* just setting the selection in theirs and we still
* need to go through that path.
*/
Selection.setSelection((Spannable) mTextView.getText(), selStart, selEnd);
}
if (mSelectAllOnFocus) {
mTextView.selectAllText();
}
mTouchFocusSelected = true;
}
mFrozenWithFocus = false;
mSelectionMoved = false;
if (mError != null) {
showError();
}
makeBlink();
} else {
if (mError != null) {
hideError();
}
// Don't leave us in the middle of a batch edit.
mTextView.onEndBatchEdit();
if (mTextView.isInExtractedMode()) {
hideCursorAndSpanControllers();
stopTextActionModeWithPreservingSelection();
} else {
hideCursorAndSpanControllers();
if (mTextView.isTemporarilyDetached()) {
stopTextActionModeWithPreservingSelection();
} else {
stopTextActionMode();
}
downgradeEasyCorrectionSpans();
}
// No need to create the controller
if (mSelectionModifierCursorController != null) {
mSelectionModifierCursorController.resetTouchOffsets();
}
if (mInsertModeController != null) {
mInsertModeController.exitInsertMode();
}
ensureNoSelectionIfNonSelectable();
}
}
private void ensureNoSelectionIfNonSelectable() {
// This could be the case if a TextLink has been tapped.
if (!mTextView.textCanBeSelected() && mTextView.hasSelection()) {
Selection.setSelection((Spannable) mTextView.getText(),
mTextView.length(), mTextView.length());
}
}
/**
* Downgrades to simple suggestions all the easy correction spans that are not a spell check
* span.
*/
private void downgradeEasyCorrectionSpans() {
CharSequence text = mTextView.getText();
if (text instanceof Spannable) {
Spannable spannable = (Spannable) text;
SuggestionSpan[] suggestionSpans = spannable.getSpans(0,
spannable.length(), SuggestionSpan.class);
for (int i = 0; i < suggestionSpans.length; i++) {
int flags = suggestionSpans[i].getFlags();
if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0
&& (flags & FLAG_MISSPELLED_OR_GRAMMAR_ERROR) == 0) {
flags &= ~SuggestionSpan.FLAG_EASY_CORRECT;
suggestionSpans[i].setFlags(flags);
}
}
}
}
void sendOnTextChanged(int start, int before, int after) {
getSelectionActionModeHelper().onTextChanged(start, start + before);
updateSpellCheckSpans(start, start + after, false);
// Flip flag to indicate the word iterator needs to have the text reset.
mUpdateWordIteratorText = true;
// Hide the controllers as soon as text is modified (typing, procedural...)
// We do not hide the span controllers, since they can be added when a new text is
// inserted into the text view (voice IME).
hideCursorControllers();
// Reset drag accelerator.
if (mSelectionModifierCursorController != null) {
mSelectionModifierCursorController.resetTouchOffsets();
}
stopTextActionMode();
}
private int getLastTapPosition() {
// No need to create the controller at that point, no last tap position saved
if (mSelectionModifierCursorController != null) {
int lastTapPosition = mSelectionModifierCursorController.getMinTouchOffset();
if (lastTapPosition >= 0) {
// Safety check, should not be possible.
if (lastTapPosition > mTextView.getText().length()) {
lastTapPosition = mTextView.getText().length();
}
return lastTapPosition;
}
}
return -1;
}
void onWindowFocusChanged(boolean hasWindowFocus) {
if (hasWindowFocus) {
resumeBlink();
if (mTextView.hasSelection() && !extractedTextModeWillBeStarted()) {
refreshTextActionMode();
}
} else {
suspendBlink();
if (mInputContentType != null) {
mInputContentType.enterDown = false;
}
// Order matters! Must be done before onParentLostFocus to rely on isShowingUp
hideCursorAndSpanControllers();
stopTextActionModeWithPreservingSelection();
if (mSuggestionsPopupWindow != null) {
mSuggestionsPopupWindow.onParentLostFocus();
}
// Don't leave us in the middle of a batch edit. Same as in onFocusChanged
ensureEndedBatchEdit();
ensureNoSelectionIfNonSelectable();
}
}
private boolean shouldFilterOutTouchEvent(MotionEvent event) {
if (!event.isFromSource(InputDevice.SOURCE_MOUSE)) {
return false;
}
final boolean primaryButtonStateChanged =
((mLastButtonState ^ event.getButtonState()) & MotionEvent.BUTTON_PRIMARY) != 0;
final int action = event.getActionMasked();
if ((action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP)
&& !primaryButtonStateChanged) {
return true;
}
if (action == MotionEvent.ACTION_MOVE
&& !event.isButtonPressed(MotionEvent.BUTTON_PRIMARY)) {
return true;
}
return false;
}
/**
* Handles touch events on an editable text view, implementing cursor movement, selection, etc.
*/
@VisibleForTesting
public void onTouchEvent(MotionEvent event) {
final boolean filterOutEvent = shouldFilterOutTouchEvent(event);
mLastButtonState = event.getButtonState();
if (filterOutEvent) {
if (event.getActionMasked() == MotionEvent.ACTION_UP) {
mDiscardNextActionUp = true;
}
return;
}
ViewConfiguration viewConfiguration = ViewConfiguration.get(mTextView.getContext());
mTouchState.update(event, viewConfiguration);
updateFloatingToolbarVisibility(event);
if (hasInsertionController()) {
getInsertionController().onTouchEvent(event);
}
if (hasSelectionController()) {
getSelectionController().onTouchEvent(event);
}
if (mShowSuggestionRunnable != null) {
mTextView.removeCallbacks(mShowSuggestionRunnable);
mShowSuggestionRunnable = null;
}
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
// Reset this state; it will be re-set if super.onTouchEvent
// causes focus to move to the view.
mTouchFocusSelected = false;
mIgnoreActionUpEvent = false;
}
}
private void updateFloatingToolbarVisibility(MotionEvent event) {
if (mTextActionMode != null) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_MOVE:
hideFloatingToolbar(ActionMode.DEFAULT_HIDE_DURATION);
break;
case MotionEvent.ACTION_UP: // fall through
case MotionEvent.ACTION_CANCEL:
showFloatingToolbar();
}
}
}
void hideFloatingToolbar(int duration) {
if (mTextActionMode != null) {
mTextView.removeCallbacks(mShowFloatingToolbar);
mTextActionMode.hide(duration);
}
}
private void showFloatingToolbar() {
if (mTextActionMode != null && mTextView.showUIForTouchScreen()) {
// Delay "show" so it doesn't interfere with click confirmations
// or double-clicks that could "dismiss" the floating toolbar.
int delay = ViewConfiguration.getDoubleTapTimeout();
mTextView.postDelayed(mShowFloatingToolbar, delay);
// This classifies the text and most likely returns before the toolbar is actually
// shown. If not, it will update the toolbar with the result when classification
// returns. We would rather not wait for a long running classification process.
invalidateActionModeAsync();
}
}
private InputMethodManager getInputMethodManager() {
return mTextView.getContext().getSystemService(InputMethodManager.class);
}
public void beginBatchEdit() {
mInBatchEditControllers = true;
final InputMethodState ims = mInputMethodState;
if (ims != null) {
int nesting = ++ims.mBatchEditNesting;
if (nesting == 1) {
ims.mCursorChanged = false;
ims.mChangedDelta = 0;
if (ims.mContentChanged) {
// We already have a pending change from somewhere else,
// so turn this into a full update.
ims.mChangedStart = 0;
ims.mChangedEnd = mTextView.getText().length();
} else {
ims.mChangedStart = EXTRACT_UNKNOWN;
ims.mChangedEnd = EXTRACT_UNKNOWN;
ims.mContentChanged = false;
}
mUndoInputFilter.beginBatchEdit();
mTextView.onBeginBatchEdit();
}
}
}
public void endBatchEdit() {
mInBatchEditControllers = false;
final InputMethodState ims = mInputMethodState;
if (ims != null) {
int nesting = --ims.mBatchEditNesting;
if (nesting == 0) {
finishBatchEdit(ims);
}
}
}
void ensureEndedBatchEdit() {
final InputMethodState ims = mInputMethodState;
if (ims != null && ims.mBatchEditNesting != 0) {
ims.mBatchEditNesting = 0;
finishBatchEdit(ims);
}
}
void finishBatchEdit(final InputMethodState ims) {
mTextView.onEndBatchEdit();
mUndoInputFilter.endBatchEdit();
if (ims.mContentChanged || ims.mSelectionModeChanged) {
mTextView.updateAfterEdit();
reportExtractedText();
} else if (ims.mCursorChanged) {
// Cheesy way to get us to report the current cursor location.
mTextView.invalidateCursor();
}
// sendUpdateSelection knows to avoid sending if the selection did
// not actually change.
sendUpdateSelection();
// Show drag handles if they were blocked by batch edit mode.
if (mTextActionMode != null) {
final CursorController cursorController = mTextView.hasSelection()
? getSelectionController() : getInsertionController();
if (cursorController != null && !cursorController.isActive()
&& !cursorController.isCursorBeingModified()
&& mTextView.showUIForTouchScreen()) {
cursorController.show();
}
}
}
/**
* Called from {@link TextView#setText(CharSequence, TextView.BufferType, boolean, int)} to
* schedule {@link InputMethodManager#restartInput(View)}.
*/
void scheduleRestartInputForSetText() {
mHasPendingRestartInputForSetText = true;
}
/**
* Called from {@link TextView#setText(CharSequence, TextView.BufferType, boolean, int)} to
* actually call {@link InputMethodManager#restartInput(View)} if it's scheduled. Does nothing
* otherwise.
*/
void maybeFireScheduledRestartInputForSetText() {
if (mHasPendingRestartInputForSetText) {
final InputMethodManager imm = getInputMethodManager();
if (imm != null) {
imm.invalidateInput(mTextView);
}
mHasPendingRestartInputForSetText = false;
}
}
static final int EXTRACT_NOTHING = -2;
static final int EXTRACT_UNKNOWN = -1;
boolean extractText(ExtractedTextRequest request, ExtractedText outText) {
return extractTextInternal(request, EXTRACT_UNKNOWN, EXTRACT_UNKNOWN,
EXTRACT_UNKNOWN, outText);
}
private boolean extractTextInternal(@Nullable ExtractedTextRequest request,
int partialStartOffset, int partialEndOffset, int delta,
@Nullable ExtractedText outText) {
if (request == null || outText == null) {
return false;
}
final CharSequence content = mTextView.getText();
if (content == null) {
return false;
}
if (partialStartOffset != EXTRACT_NOTHING) {
final int N = content.length();
if (partialStartOffset < 0) {
outText.partialStartOffset = outText.partialEndOffset = -1;
partialStartOffset = 0;
partialEndOffset = N;
} else {
// Now use the delta to determine the actual amount of text
// we need.
partialEndOffset += delta;
// Adjust offsets to ensure we contain full spans.
if (content instanceof Spanned) {
Spanned spanned = (Spanned) content;
Object[] spans = spanned.getSpans(partialStartOffset,
partialEndOffset, ParcelableSpan.class);
int i = spans.length;
while (i > 0) {
i--;
int j = spanned.getSpanStart(spans[i]);
if (j < partialStartOffset) partialStartOffset = j;
j = spanned.getSpanEnd(spans[i]);
if (j > partialEndOffset) partialEndOffset = j;
}
}
outText.partialStartOffset = partialStartOffset;
outText.partialEndOffset = partialEndOffset - delta;
if (partialStartOffset > N) {
partialStartOffset = N;
} else if (partialStartOffset < 0) {
partialStartOffset = 0;
}
if (partialEndOffset > N) {
partialEndOffset = N;
} else if (partialEndOffset < 0) {
partialEndOffset = 0;
}
}
if ((request.flags & InputConnection.GET_TEXT_WITH_STYLES) != 0) {
outText.text = content.subSequence(partialStartOffset,
partialEndOffset);
} else {
outText.text = TextUtils.substring(content, partialStartOffset,
partialEndOffset);
}
} else {
outText.partialStartOffset = 0;
outText.partialEndOffset = 0;
outText.text = "";
}
outText.flags = 0;
if (MetaKeyKeyListener.getMetaState(content, MetaKeyKeyListener.META_SELECTING) != 0) {
outText.flags |= ExtractedText.FLAG_SELECTING;
}
if (mTextView.isSingleLine()) {
outText.flags |= ExtractedText.FLAG_SINGLE_LINE;
}
outText.startOffset = 0;
outText.selectionStart = mTextView.getSelectionStart();
outText.selectionEnd = mTextView.getSelectionEnd();
outText.hint = mTextView.getHint();
return true;
}
boolean reportExtractedText() {
final Editor.InputMethodState ims = mInputMethodState;
if (ims == null) {
return false;
}
final boolean wasContentChanged = ims.mContentChanged;
if (!wasContentChanged && !ims.mSelectionModeChanged) {
return false;
}
ims.mContentChanged = false;
ims.mSelectionModeChanged = false;
final ExtractedTextRequest req = ims.mExtractedTextRequest;
if (req == null) {
return false;
}
final InputMethodManager imm = getInputMethodManager();
if (imm == null) {
return false;
}
if (TextView.DEBUG_EXTRACT) {
Log.v(TextView.LOG_TAG, "Retrieving extracted start="
+ ims.mChangedStart
+ " end=" + ims.mChangedEnd
+ " delta=" + ims.mChangedDelta);
}
if (ims.mChangedStart < 0 && !wasContentChanged) {
ims.mChangedStart = EXTRACT_NOTHING;
}
if (extractTextInternal(req, ims.mChangedStart, ims.mChangedEnd,
ims.mChangedDelta, ims.mExtractedText)) {
if (TextView.DEBUG_EXTRACT) {
Log.v(TextView.LOG_TAG,
"Reporting extracted start="
+ ims.mExtractedText.partialStartOffset
+ " end=" + ims.mExtractedText.partialEndOffset
+ ": " + ims.mExtractedText.text);
}
imm.updateExtractedText(mTextView, req.token, ims.mExtractedText);
ims.mChangedStart = EXTRACT_UNKNOWN;
ims.mChangedEnd = EXTRACT_UNKNOWN;
ims.mChangedDelta = 0;
ims.mContentChanged = false;
return true;
}
return false;
}
private void sendUpdateSelection() {
if (null != mInputMethodState && mInputMethodState.mBatchEditNesting <= 0
&& !mHasPendingRestartInputForSetText) {
final InputMethodManager imm = getInputMethodManager();
if (null != imm) {
final int selectionStart = mTextView.getSelectionStart();
final int selectionEnd = mTextView.getSelectionEnd();
int candStart = -1;
int candEnd = -1;
if (mTextView.getText() instanceof Spannable) {
final Spannable sp = (Spannable) mTextView.getText();
candStart = EditableInputConnection.getComposingSpanStart(sp);
candEnd = EditableInputConnection.getComposingSpanEnd(sp);
}
// InputMethodManager#updateSelection skips sending the message if
// none of the parameters have changed since the last time we called it.
imm.updateSelection(mTextView,
selectionStart, selectionEnd, candStart, candEnd);
}
}
}
void onDraw(Canvas canvas, Layout layout,
List highlightPaths,
List highlightPaints,
Path selectionHighlight, Paint selectionHighlightPaint,
int cursorOffsetVertical) {
final int selectionStart = mTextView.getSelectionStart();
final int selectionEnd = mTextView.getSelectionEnd();
final InputMethodState ims = mInputMethodState;
if (ims != null && ims.mBatchEditNesting == 0
&& (ims.mContentChanged || ims.mSelectionModeChanged)) {
InputMethodManager imm = getInputMethodManager();
if (imm != null && imm.hasActiveInputConnection(mTextView)) {
// We are in extract mode and the content has changed
// in some way... just report complete new text to the
// input method.
reportExtractedText();
}
}
boolean shouldDrawHighlightsOnTop = highContrastTextSmallTextRect()
&& canvas.isHighContrastTextEnabled();
// If high contrast text is drawing background rectangles behind the text, those cover up
// the cursor and correction highlighter etc. So just draw the text first, then draw the
// others on top of the text. If high contrast text isn't enabled: draw text last, as usual.
if (shouldDrawHighlightsOnTop) {
drawLayout(canvas, layout, highlightPaths, highlightPaints, selectionHighlight,
selectionHighlightPaint, cursorOffsetVertical, shouldDrawHighlightsOnTop);
}
if (mCorrectionHighlighter != null) {
mCorrectionHighlighter.draw(canvas, cursorOffsetVertical);
}
if (selectionHighlight != null && selectionStart == selectionEnd
&& mDrawableForCursor != null
&& !mTextView.hasGesturePreviewHighlight()) {
drawCursor(canvas, cursorOffsetVertical);
// Rely on the drawable entirely, do not draw the cursor line.
// Has to be done after the IMM related code above which relies on the highlight.
selectionHighlight = null;
}
if (mSelectionActionModeHelper != null) {
mSelectionActionModeHelper.onDraw(canvas);
if (mSelectionActionModeHelper.isDrawingHighlight()) {
selectionHighlight = null;
}
}
if (mInsertModeController != null) {
mInsertModeController.onDraw(canvas);
}
if (!shouldDrawHighlightsOnTop) {
drawLayout(canvas, layout, highlightPaths, highlightPaints, selectionHighlight,
selectionHighlightPaint, cursorOffsetVertical, shouldDrawHighlightsOnTop);
}
}
private void drawLayout(Canvas canvas, Layout layout, List highlightPaths,
List highlightPaints, Path selectionHighlight, Paint selectionHighlightPaint,
int cursorOffsetVertical, boolean shouldDrawHighlightsOnTop) {
if (mTextView.canHaveDisplayList() && canvas.isHardwareAccelerated()) {
drawHardwareAccelerated(canvas, layout, highlightPaths, highlightPaints,
selectionHighlight, selectionHighlightPaint, cursorOffsetVertical,
shouldDrawHighlightsOnTop);
} else {
layout.draw(canvas, highlightPaths, highlightPaints, selectionHighlight,
selectionHighlightPaint, cursorOffsetVertical);
}
}
private void drawHardwareAccelerated(Canvas canvas, Layout layout,
List highlightPaths, List highlightPaints,
Path selectionHighlight, Paint selectionHighlightPaint, int cursorOffsetVertical,
boolean shouldDrawHighlightsOnTop) {
final long lineRange = layout.getLineRangeForDraw(canvas);
int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
if (lastLine < 0) return;
if (!shouldDrawHighlightsOnTop) {
layout.drawWithoutText(canvas, highlightPaths, highlightPaints, selectionHighlight,
selectionHighlightPaint, cursorOffsetVertical, firstLine, lastLine);
} else {
layout.drawBackground(canvas, firstLine, lastLine);
}
if (layout instanceof DynamicLayout) {
if (mTextRenderNodes == null) {
mTextRenderNodes = ArrayUtils.emptyArray(TextRenderNode.class);
}
DynamicLayout dynamicLayout = (DynamicLayout) layout;
int[] blockEndLines = dynamicLayout.getBlockEndLines();
int[] blockIndices = dynamicLayout.getBlockIndices();
final int numberOfBlocks = dynamicLayout.getNumberOfBlocks();
final int indexFirstChangedBlock = dynamicLayout.getIndexFirstChangedBlock();
final ArraySet blockSet = dynamicLayout.getBlocksAlwaysNeedToBeRedrawn();
if (blockSet != null) {
for (int i = 0; i < blockSet.size(); i++) {
final int blockIndex = dynamicLayout.getBlockIndex(blockSet.valueAt(i));
if (blockIndex != DynamicLayout.INVALID_BLOCK_INDEX
&& mTextRenderNodes[blockIndex] != null) {
mTextRenderNodes[blockIndex].needsToBeShifted = true;
}
}
}
int startBlock = Arrays.binarySearch(blockEndLines, 0, numberOfBlocks, firstLine);
if (startBlock < 0) {
startBlock = -(startBlock + 1);
}
startBlock = Math.min(indexFirstChangedBlock, startBlock);
int startIndexToFindAvailableRenderNode = 0;
int lastIndex = numberOfBlocks;
for (int i = startBlock; i < numberOfBlocks; i++) {
final int blockIndex = blockIndices[i];
if (i >= indexFirstChangedBlock
&& blockIndex != DynamicLayout.INVALID_BLOCK_INDEX
&& mTextRenderNodes[blockIndex] != null) {
mTextRenderNodes[blockIndex].needsToBeShifted = true;
}
if (blockEndLines[i] < firstLine) {
// Blocks in [indexFirstChangedBlock, firstLine) are not redrawn here. They will
// be redrawn after they get scrolled into drawing range.
continue;
}
startIndexToFindAvailableRenderNode = drawHardwareAcceleratedInner(canvas, layout,
selectionHighlight, selectionHighlightPaint, cursorOffsetVertical,
blockEndLines, blockIndices, i, numberOfBlocks,
startIndexToFindAvailableRenderNode);
if (blockEndLines[i] >= lastLine) {
lastIndex = Math.max(indexFirstChangedBlock, i + 1);
break;
}
}
if (blockSet != null) {
for (int i = 0; i < blockSet.size(); i++) {
final int block = blockSet.valueAt(i);
final int blockIndex = dynamicLayout.getBlockIndex(block);
if (blockIndex == DynamicLayout.INVALID_BLOCK_INDEX
|| mTextRenderNodes[blockIndex] == null
|| mTextRenderNodes[blockIndex].needsToBeShifted) {
startIndexToFindAvailableRenderNode = drawHardwareAcceleratedInner(canvas,
layout, selectionHighlight, selectionHighlightPaint,
cursorOffsetVertical, blockEndLines, blockIndices, block,
numberOfBlocks, startIndexToFindAvailableRenderNode);
}
}
}
dynamicLayout.setIndexFirstChangedBlock(lastIndex);
} else {
// Boring layout is used for empty and hint text
layout.drawText(canvas, firstLine, lastLine);
}
if (shouldDrawHighlightsOnTop) {
layout.drawHighlights(canvas, highlightPaths, highlightPaints, selectionHighlight,
selectionHighlightPaint, cursorOffsetVertical, firstLine, lastLine);
}
}
private int drawHardwareAcceleratedInner(Canvas canvas, Layout layout, Path highlight,
Paint highlightPaint, int cursorOffsetVertical, int[] blockEndLines,
int[] blockIndices, int blockInfoIndex, int numberOfBlocks,
int startIndexToFindAvailableRenderNode) {
final int blockEndLine = blockEndLines[blockInfoIndex];
int blockIndex = blockIndices[blockInfoIndex];
final boolean blockIsInvalid = blockIndex == DynamicLayout.INVALID_BLOCK_INDEX;
if (blockIsInvalid) {
blockIndex = getAvailableDisplayListIndex(blockIndices, numberOfBlocks,
startIndexToFindAvailableRenderNode);
// Note how dynamic layout's internal block indices get updated from Editor
blockIndices[blockInfoIndex] = blockIndex;
if (mTextRenderNodes[blockIndex] != null) {
mTextRenderNodes[blockIndex].isDirty = true;
}
startIndexToFindAvailableRenderNode = blockIndex + 1;
}
if (mTextRenderNodes[blockIndex] == null) {
mTextRenderNodes[blockIndex] = new TextRenderNode("Text " + blockIndex);
}
final boolean blockDisplayListIsInvalid = mTextRenderNodes[blockIndex].needsRecord();
RenderNode blockDisplayList = mTextRenderNodes[blockIndex].renderNode;
if (mTextRenderNodes[blockIndex].needsToBeShifted || blockDisplayListIsInvalid) {
final int blockBeginLine = blockInfoIndex == 0 ?
0 : blockEndLines[blockInfoIndex - 1] + 1;
final int top = layout.getLineTop(blockBeginLine);
final int bottom = layout.getLineBottom(blockEndLine);
int left = 0;
int right = mTextView.getWidth();
if (mTextView.getHorizontallyScrolling()) {
float min = Float.MAX_VALUE;
float max = Float.MIN_VALUE;
for (int line = blockBeginLine; line <= blockEndLine; line++) {
min = Math.min(min, layout.getLineLeft(line));
max = Math.max(max, layout.getLineRight(line));
}
left = (int) min;
right = (int) (max + 0.5f);
}
// Rebuild display list if it is invalid
if (blockDisplayListIsInvalid) {
final RecordingCanvas recordingCanvas = blockDisplayList.beginRecording(
right - left, bottom - top);
try {
// drawText is always relative to TextView's origin, this translation
// brings this range of text back to the top left corner of the viewport
recordingCanvas.translate(-left, -top);
layout.drawText(recordingCanvas, blockBeginLine, blockEndLine);
mTextRenderNodes[blockIndex].isDirty = false;
// No need to untranslate, previous context is popped after
// drawDisplayList
} finally {
blockDisplayList.endRecording();
// Same as drawDisplayList below, handled by our TextView's parent
blockDisplayList.setClipToBounds(false);
}
}
// Valid display list only needs to update its drawing location.
blockDisplayList.setLeftTopRightBottom(left, top, right, bottom);
mTextRenderNodes[blockIndex].needsToBeShifted = false;
}
((RecordingCanvas) canvas).drawRenderNode(blockDisplayList);
return startIndexToFindAvailableRenderNode;
}
private int getAvailableDisplayListIndex(int[] blockIndices, int numberOfBlocks,
int searchStartIndex) {
int length = mTextRenderNodes.length;
for (int i = searchStartIndex; i < length; i++) {
boolean blockIndexFound = false;
for (int j = 0; j < numberOfBlocks; j++) {
if (blockIndices[j] == i) {
blockIndexFound = true;
break;
}
}
if (blockIndexFound) continue;
return i;
}
// No available index found, the pool has to grow
mTextRenderNodes = GrowingArrayUtils.append(mTextRenderNodes, length, null);
return length;
}
private void drawCursor(Canvas canvas, int cursorOffsetVertical) {
final boolean translate = cursorOffsetVertical != 0;
if (translate) canvas.translate(0, cursorOffsetVertical);
if (mDrawableForCursor != null) {
mDrawableForCursor.draw(canvas);
}
if (translate) canvas.translate(0, -cursorOffsetVertical);
}
void invalidateHandlesAndActionMode() {
if (mSelectionModifierCursorController != null) {
mSelectionModifierCursorController.invalidateHandles();
}
if (mInsertionPointCursorController != null) {
mInsertionPointCursorController.invalidateHandle();
}
if (mTextActionMode != null) {
invalidateActionMode();
}
}
/**
* Invalidates all the sub-display lists that overlap the specified character range
*/
void invalidateTextDisplayList(Layout layout, int start, int end) {
if (mTextRenderNodes != null && layout instanceof DynamicLayout) {
if (Flags.insertModeCrashWhenDelete()
&& mTextView.isOffsetMappingAvailable()) {
// Text is transformed with an OffsetMapping, and we can't know the changed range
// on the transformed text. Invalidate the all display lists instead.
invalidateTextDisplayList();
return;
}
final int startTransformed =
mTextView.originalToTransformed(start, OffsetMapping.MAP_STRATEGY_CHARACTER);
final int endTransformed =
mTextView.originalToTransformed(end, OffsetMapping.MAP_STRATEGY_CHARACTER);
final int firstLine = layout.getLineForOffset(startTransformed);
final int lastLine = layout.getLineForOffset(endTransformed);
DynamicLayout dynamicLayout = (DynamicLayout) layout;
int[] blockEndLines = dynamicLayout.getBlockEndLines();
int[] blockIndices = dynamicLayout.getBlockIndices();
final int numberOfBlocks = dynamicLayout.getNumberOfBlocks();
int i = 0;
// Skip the blocks before firstLine
while (i < numberOfBlocks) {
if (blockEndLines[i] >= firstLine) break;
i++;
}
// Invalidate all subsequent blocks until lastLine is passed
while (i < numberOfBlocks) {
final int blockIndex = blockIndices[i];
if (blockIndex != DynamicLayout.INVALID_BLOCK_INDEX) {
mTextRenderNodes[blockIndex].isDirty = true;
}
if (blockEndLines[i] >= lastLine) break;
i++;
}
}
}
@UnsupportedAppUsage
void invalidateTextDisplayList() {
if (mTextRenderNodes != null) {
for (int i = 0; i < mTextRenderNodes.length; i++) {
if (mTextRenderNodes[i] != null) mTextRenderNodes[i].isDirty = true;
}
}
}
void updateCursorPosition() {
loadCursorDrawable();
if (mDrawableForCursor == null) {
return;
}
final Layout layout = mTextView.getLayout();
final int offset = mTextView.getSelectionStart();
final int transformedOffset = mTextView.originalToTransformed(offset,
OffsetMapping.MAP_STRATEGY_CURSOR);
final int line = layout.getLineForOffset(transformedOffset);
final int top = layout.getLineTop(line);
final int bottom = layout.getLineBottom(line, /* includeLineSpacing= */ false);
final boolean clamped = layout.shouldClampCursor(line);
updateCursorPosition(top, bottom, layout.getPrimaryHorizontal(transformedOffset, clamped));
}
void refreshTextActionMode() {
if (extractedTextModeWillBeStarted()) {
mRestartActionModeOnNextRefresh = false;
return;
}
final boolean hasSelection = mTextView.hasSelection();
final SelectionModifierCursorController selectionController = getSelectionController();
final InsertionPointCursorController insertionController = getInsertionController();
if ((selectionController != null && selectionController.isCursorBeingModified())
|| (insertionController != null && insertionController.isCursorBeingModified())) {
// ActionMode should be managed by the currently active cursor controller.
mRestartActionModeOnNextRefresh = false;
return;
}
if (hasSelection) {
hideInsertionPointCursorController();
if (mTextActionMode == null) {
if (mRestartActionModeOnNextRefresh) {
// To avoid distraction, newly start action mode only when selection action
// mode is being restarted.
startSelectionActionModeAsync(false);
}
} else if (selectionController == null || !selectionController.isActive()) {
// Insertion action mode is active. Avoid dismissing the selection.
stopTextActionModeWithPreservingSelection();
startSelectionActionModeAsync(false);
} else {
mTextActionMode.invalidateContentRect();
}
} else {
// Insertion action mode is started only when insertion controller is explicitly
// activated.
if (insertionController == null || !insertionController.isActive()) {
stopTextActionMode();
} else if (mTextActionMode != null) {
mTextActionMode.invalidateContentRect();
}
}
mRestartActionModeOnNextRefresh = false;
}
/**
* Start an Insertion action mode.
*/
void startInsertionActionMode() {
if (mInsertionActionModeRunnable != null) {
mTextView.removeCallbacks(mInsertionActionModeRunnable);
}
if (extractedTextModeWillBeStarted()) {
return;
}
stopTextActionMode();
ActionMode.Callback actionModeCallback =
new TextActionModeCallback(TextActionMode.INSERTION);
mTextActionMode = mTextView.startActionMode(
actionModeCallback, ActionMode.TYPE_FLOATING);
registerOnBackInvokedCallback();
if (mTextActionMode != null && getInsertionController() != null) {
getInsertionController().show();
}
}
@NonNull
TextView getTextView() {
return mTextView;
}
@Nullable
ActionMode getTextActionMode() {
return mTextActionMode;
}
void setRestartActionModeOnNextRefresh(boolean value) {
mRestartActionModeOnNextRefresh = value;
}
/**
* Asynchronously starts a selection action mode using the TextClassifier.
*/
void startSelectionActionModeAsync(boolean adjustSelection) {
getSelectionActionModeHelper().startSelectionActionModeAsync(adjustSelection);
}
void startLinkActionModeAsync(int start, int end) {
if (!(mTextView.getText() instanceof Spannable)) {
return;
}
stopTextActionMode();
mRequestingLinkActionMode = true;
getSelectionActionModeHelper().startLinkActionModeAsync(start, end);
}
/**
* Asynchronously invalidates an action mode using the TextClassifier.
*/
void invalidateActionModeAsync() {
getSelectionActionModeHelper().invalidateActionModeAsync();
}
/**
* Synchronously invalidates an action mode without the TextClassifier.
*/
private void invalidateActionMode() {
if (mTextActionMode != null) {
mTextActionMode.invalidate();
}
}
private SelectionActionModeHelper getSelectionActionModeHelper() {
if (mSelectionActionModeHelper == null) {
mSelectionActionModeHelper = new SelectionActionModeHelper(this);
}
return mSelectionActionModeHelper;
}
/**
* If the TextView allows text selection, selects the current word when no existing selection
* was available and starts a drag.
*
* @return true if the drag was started.
*/
private boolean selectCurrentWordAndStartDrag() {
if (mInsertionActionModeRunnable != null) {
mTextView.removeCallbacks(mInsertionActionModeRunnable);
}
if (extractedTextModeWillBeStarted()) {
return false;
}
if (!checkField()) {
return false;
}
if (!mTextView.hasSelection() && !selectCurrentWord()) {
// No selection and cannot select a word.
return false;
}
stopTextActionModeWithPreservingSelection();
getSelectionController().enterDrag(
SelectionModifierCursorController.DRAG_ACCELERATOR_MODE_WORD);
return true;
}
/**
* Checks whether a selection can be performed on the current TextView.
*
* @return true if a selection can be performed
*/
boolean checkField() {
if (!mTextView.canSelectText() || !mTextView.requestFocus()) {
Log.w(TextView.LOG_TAG,
"TextView does not support text selection. Selection cancelled.");
return false;
}
return true;
}
boolean startActionModeInternal(@TextActionMode int actionMode) {
if (extractedTextModeWillBeStarted()) {
return false;
}
if (mTextActionMode != null) {
// Text action mode is already started
invalidateActionMode();
return false;
}
if (actionMode != TextActionMode.TEXT_LINK
&& (!checkField() || !mTextView.hasSelection())) {
return false;
}
if (!mTextView.showUIForTouchScreen()) {
return false;
}
ActionMode.Callback actionModeCallback = new TextActionModeCallback(actionMode);
mTextActionMode = mTextView.startActionMode(actionModeCallback, ActionMode.TYPE_FLOATING);
registerOnBackInvokedCallback();
final boolean selectableText = mTextView.isTextEditable() || mTextView.isTextSelectable();
if (actionMode == TextActionMode.TEXT_LINK && !selectableText
&& mTextActionMode instanceof FloatingActionMode) {
// Make the toolbar outside-touchable so that it can be dismissed when the user clicks
// outside of it.
((FloatingActionMode) mTextActionMode).setOutsideTouchable(true,
() -> stopTextActionMode());
}
final boolean selectionStarted = mTextActionMode != null;
if (selectionStarted
&& mTextView.isTextEditable() && !mTextView.isTextSelectable()
&& mShowSoftInputOnFocus) {
// Show the IME to be able to replace text, except when selecting non editable text.
final InputMethodManager imm = getInputMethodManager();
if (imm != null) {
imm.showSoftInput(mTextView, 0, null);
}
}
return selectionStarted;
}
private boolean extractedTextModeWillBeStarted() {
if (!(mTextView.isInExtractedMode())) {
final InputMethodManager imm = getInputMethodManager();
return imm != null && imm.isFullscreenMode();
}
return false;
}
/**
* @return true
if it's reasonable to offer to show suggestions depending on
* the current cursor position or selection range. This method is consistent with the
* method to show suggestions {@link SuggestionsPopupWindow#updateSuggestions}.
*/
boolean shouldOfferToShowSuggestions() {
CharSequence text = mTextView.getText();
if (!(text instanceof Spannable)) return false;
final Spannable spannable = (Spannable) text;
final int selectionStart = mTextView.getSelectionStart();
final int selectionEnd = mTextView.getSelectionEnd();
final SuggestionSpan[] suggestionSpans = spannable.getSpans(selectionStart, selectionEnd,
SuggestionSpan.class);
if (suggestionSpans.length == 0) {
return false;
}
if (selectionStart == selectionEnd) {
// Spans overlap the cursor.
for (int i = 0; i < suggestionSpans.length; i++) {
if (suggestionSpans[i].getSuggestions().length > 0) {
return true;
}
}
return false;
}
int minSpanStart = mTextView.getText().length();
int maxSpanEnd = 0;
int unionOfSpansCoveringSelectionStartStart = mTextView.getText().length();
int unionOfSpansCoveringSelectionStartEnd = 0;
boolean hasValidSuggestions = false;
for (int i = 0; i < suggestionSpans.length; i++) {
final int spanStart = spannable.getSpanStart(suggestionSpans[i]);
final int spanEnd = spannable.getSpanEnd(suggestionSpans[i]);
minSpanStart = Math.min(minSpanStart, spanStart);
maxSpanEnd = Math.max(maxSpanEnd, spanEnd);
if (selectionStart < spanStart || selectionStart > spanEnd) {
// The span doesn't cover the current selection start point.
continue;
}
hasValidSuggestions =
hasValidSuggestions || suggestionSpans[i].getSuggestions().length > 0;
unionOfSpansCoveringSelectionStartStart =
Math.min(unionOfSpansCoveringSelectionStartStart, spanStart);
unionOfSpansCoveringSelectionStartEnd =
Math.max(unionOfSpansCoveringSelectionStartEnd, spanEnd);
}
if (!hasValidSuggestions) {
return false;
}
if (unionOfSpansCoveringSelectionStartStart >= unionOfSpansCoveringSelectionStartEnd) {
// No spans cover the selection start point.
return false;
}
if (minSpanStart < unionOfSpansCoveringSelectionStartStart
|| maxSpanEnd > unionOfSpansCoveringSelectionStartEnd) {
// There is a span that is not covered by the union. In this case, we soouldn't offer
// to show suggestions as it's confusing.
return false;
}
return true;
}
/**
* @return true
if the cursor is inside an {@link SuggestionSpan} with
* {@link SuggestionSpan#FLAG_EASY_CORRECT} set.
*/
private boolean isCursorInsideEasyCorrectionSpan() {
Spannable spannable = (Spannable) mTextView.getText();
SuggestionSpan[] suggestionSpans = spannable.getSpans(mTextView.getSelectionStart(),
mTextView.getSelectionEnd(), SuggestionSpan.class);
for (int i = 0; i < suggestionSpans.length; i++) {
if ((suggestionSpans[i].getFlags() & SuggestionSpan.FLAG_EASY_CORRECT) != 0) {
return true;
}
}
return false;
}
void onTouchUpEvent(MotionEvent event) {
if (TextView.DEBUG_CURSOR) {
logCursor("onTouchUpEvent", null);
}
if (getSelectionActionModeHelper().resetSelection(
getTextView().getOffsetForPosition(event.getX(), event.getY()))) {
return;
}
boolean selectAllGotFocus = mSelectAllOnFocus && mTextView.didTouchFocusSelect();
hideCursorAndSpanControllers();
stopTextActionMode();
CharSequence text = mTextView.getText();
if (!selectAllGotFocus && text.length() > 0) {
// Move cursor
final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY());
final boolean shouldInsertCursor = !mRequestingLinkActionMode;
if (shouldInsertCursor) {
Selection.setSelection((Spannable) text, offset);
if (mSpellChecker != null) {
// When the cursor moves, the word that was typed may need spell check
mSpellChecker.onSelectionChanged();
}
}
if (!extractedTextModeWillBeStarted()) {
if (isCursorInsideEasyCorrectionSpan()) {
// Cancel the single tap delayed runnable.
if (mInsertionActionModeRunnable != null) {
mTextView.removeCallbacks(mInsertionActionModeRunnable);
}
mShowSuggestionRunnable = this::replace;
// removeCallbacks is performed on every touch
mTextView.postDelayed(mShowSuggestionRunnable,
ViewConfiguration.getDoubleTapTimeout());
} else if (hasInsertionController()) {
if (shouldInsertCursor && mTextView.showUIForTouchScreen()) {
getInsertionController().show();
} else {
getInsertionController().hide();
}
}
}
}
}
/**
* Called when {@link TextView#mTextOperationUser} has changed.
*
* Any user-specific resources need to be refreshed here.
*/
final void onTextOperationUserChanged() {
if (mSpellChecker != null) {
mSpellChecker.resetSession();
}
}
protected void stopTextActionMode() {
if (mTextActionMode != null) {
// This will hide the mSelectionModifierCursorController
mTextActionMode.finish();
}
unregisterOnBackInvokedCallback();
}
void stopTextActionModeWithPreservingSelection() {
if (mTextActionMode != null) {
mRestartActionModeOnNextRefresh = true;
}
mPreserveSelection = true;
stopTextActionMode();
mPreserveSelection = false;
}
/**
* @return True if this view supports insertion handles.
*/
boolean hasInsertionController() {
return mInsertionControllerEnabled;
}
/**
* @return True if this view supports selection handles.
*/
boolean hasSelectionController() {
return mSelectionControllerEnabled;
}
/** Returns the controller for the insertion cursor. */
@VisibleForTesting
public @Nullable InsertionPointCursorController getInsertionController() {
if (!mInsertionControllerEnabled) {
return null;
}
if (mInsertionPointCursorController == null) {
mInsertionPointCursorController = new InsertionPointCursorController();
final ViewTreeObserver observer = mTextView.getViewTreeObserver();
observer.addOnTouchModeChangeListener(mInsertionPointCursorController);
}
return mInsertionPointCursorController;
}
/** Returns the controller for selection. */
@VisibleForTesting
public @Nullable SelectionModifierCursorController getSelectionController() {
if (!mSelectionControllerEnabled) {
return null;
}
if (mSelectionModifierCursorController == null) {
mSelectionModifierCursorController = new SelectionModifierCursorController();
final ViewTreeObserver observer = mTextView.getViewTreeObserver();
observer.addOnTouchModeChangeListener(mSelectionModifierCursorController);
}
return mSelectionModifierCursorController;
}
@VisibleForTesting
@Nullable
public Drawable getCursorDrawable() {
return mDrawableForCursor;
}
private void updateCursorPosition(int top, int bottom, float horizontal) {
loadCursorDrawable();
final int left = clampHorizontalPosition(mDrawableForCursor, horizontal);
final int width = mDrawableForCursor.getIntrinsicWidth();
if (TextView.DEBUG_CURSOR) {
logCursor("updateCursorPosition", "left=%s, top=%s", left, (top - mTempRect.top));
}
mDrawableForCursor.setBounds(left, top - mTempRect.top, left + width,
bottom + mTempRect.bottom);
}
/**
* Return clamped position for the drawable. If the drawable is within the boundaries of the
* view, then it is offset with the left padding of the cursor drawable. If the drawable is at
* the beginning or the end of the text then its drawable edge is aligned with left or right of
* the view boundary. If the drawable is null, horizontal parameter is aligned to left or right
* of the view.
*
* @param drawable Drawable. Can be null.
* @param horizontal Horizontal position for the drawable.
* @return The clamped horizontal position for the drawable.
*/
private int clampHorizontalPosition(@Nullable final Drawable drawable, float horizontal) {
horizontal = Math.max(0.5f, horizontal - 0.5f);
if (mTempRect == null) mTempRect = new Rect();
int drawableWidth = 0;
if (drawable != null) {
drawable.getPadding(mTempRect);
drawableWidth = drawable.getIntrinsicWidth();
} else {
mTempRect.setEmpty();
}
int scrollX = mTextView.getScrollX();
float horizontalDiff = horizontal - scrollX;
int viewClippedWidth = mTextView.getWidth() - mTextView.getCompoundPaddingLeft()
- mTextView.getCompoundPaddingRight();
final int left;
if (horizontalDiff >= (viewClippedWidth - 1f)) {
// at the rightmost position
left = viewClippedWidth + scrollX - (drawableWidth - mTempRect.right);
} else if (Math.abs(horizontalDiff) <= 1f
|| (TextUtils.isEmpty(mTextView.getText())
&& (TextView.VERY_WIDE - scrollX) <= (viewClippedWidth + 1f)
&& horizontal <= 1f)) {
// at the leftmost position
left = scrollX - mTempRect.left;
} else {
left = (int) horizontal - mTempRect.left;
}
return left;
}
/**
* Called by the framework in response to a text auto-correction (such as fixing a typo using a
* a dictionary) from the current input method, provided by it calling
* {@link InputConnection#commitCorrection} InputConnection.commitCorrection()}. The default
* implementation flashes the background of the corrected word to provide feedback to the user.
*
* @param info The auto correct info about the text that was corrected.
*/
public void onCommitCorrection(CorrectionInfo info) {
if (mCorrectionHighlighter == null) {
mCorrectionHighlighter = new CorrectionHighlighter();
} else {
mCorrectionHighlighter.invalidate(false);
}
mCorrectionHighlighter.highlight(info);
mUndoInputFilter.freezeLastEdit();
}
void onScrollChanged() {
if (mPositionListener != null) {
mPositionListener.onScrollChanged();
}
if (mTextActionMode != null) {
mTextActionMode.invalidateContentRect();
}
}
/**
* @return True when the TextView isFocused and has a valid zero-length selection (cursor).
*/
private boolean shouldBlink() {
if (!isCursorVisible() || !mTextView.isFocused()
|| mTextView.getWindowVisibility() != mTextView.VISIBLE) return false;
final int start = mTextView.getSelectionStart();
if (start < 0) return false;
final int end = mTextView.getSelectionEnd();
if (end < 0) return false;
return start == end;
}
void makeBlink() {
if (shouldBlink()) {
mShowCursor = SystemClock.uptimeMillis();
if (mBlink == null) mBlink = new Blink();
// Call uncancel as mBlink could have previously been cancelled and cursor will not
// resume blinking unless uncancelled.
mBlink.uncancel();
mTextView.removeCallbacks(mBlink);
mTextView.postDelayed(mBlink, BLINK);
} else {
if (mBlink != null) mTextView.removeCallbacks(mBlink);
}
}
/**
*
* @return whether the Blink runnable is blinking or not, if null return false.
* @hide
*/
@VisibleForTesting
public boolean isBlinking() {
if (mBlink == null) return false;
return !mBlink.mCancelled;
}
private class Blink implements Runnable {
private boolean mCancelled;
public void run() {
if (mCancelled) {
return;
}
mTextView.removeCallbacks(this);
if (shouldBlink()) {
if (mTextView.getLayout() != null) {
mTextView.invalidateCursorPath();
}
mTextView.postDelayed(this, BLINK);
}
}
void cancel() {
if (!mCancelled) {
mTextView.removeCallbacks(this);
mCancelled = true;
}
}
void uncancel() {
mCancelled = false;
}
}
private DragShadowBuilder getTextThumbnailBuilder(int start, int end) {
TextView shadowView = (TextView) View.inflate(mTextView.getContext(),
com.android.internal.R.layout.text_drag_thumbnail, null);
if (shadowView == null) {
throw new IllegalArgumentException("Unable to inflate text drag thumbnail");
}
if (end - start > DRAG_SHADOW_MAX_TEXT_LENGTH) {
final long range = getCharClusterRange(start + DRAG_SHADOW_MAX_TEXT_LENGTH);
end = TextUtils.unpackRangeEndFromLong(range);
}
final CharSequence text = mTextView.getTransformedText(start, end);
shadowView.setText(text);
shadowView.setTextColor(mTextView.getTextColors());
shadowView.setTextAppearance(R.styleable.Theme_textAppearanceLarge);
shadowView.setGravity(Gravity.CENTER);
shadowView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
final int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
shadowView.measure(size, size);
shadowView.layout(0, 0, shadowView.getMeasuredWidth(), shadowView.getMeasuredHeight());
shadowView.invalidate();
return new DragShadowBuilder(shadowView);
}
private static class DragLocalState {
public TextView sourceTextView;
public int start, end;
public DragLocalState(TextView sourceTextView, int start, int end) {
this.sourceTextView = sourceTextView;
this.start = start;
this.end = end;
}
}
void onDrop(DragEvent event) {
final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY());
Object localState = event.getLocalState();
DragLocalState dragLocalState = null;
if (localState instanceof DragLocalState) {
dragLocalState = (DragLocalState) localState;
}
boolean dragDropIntoItself = dragLocalState != null
&& dragLocalState.sourceTextView == mTextView;
if (dragDropIntoItself) {
if (offset >= dragLocalState.start && offset < dragLocalState.end) {
// A drop inside the original selection discards the drop.
return;
}
}
final DragAndDropPermissions permissions = DragAndDropPermissions.obtain(event);
if (permissions != null) {
permissions.takeTransient();
}
mTextView.beginBatchEdit();
mUndoInputFilter.freezeLastEdit();
try {
final int originalLength = mTextView.getText().length();
Selection.setSelection((Spannable) mTextView.getText(), offset);
final ClipData clip = event.getClipData();
final ContentInfo payload = new ContentInfo.Builder(clip, SOURCE_DRAG_AND_DROP)
.setDragAndDropPermissions(permissions)
.build();
mTextView.performReceiveContent(payload);
if (dragDropIntoItself) {
deleteSourceAfterLocalDrop(dragLocalState, offset, originalLength);
}
} finally {
mTextView.endBatchEdit();
mUndoInputFilter.freezeLastEdit();
}
}
private void deleteSourceAfterLocalDrop(@NonNull DragLocalState dragLocalState, int dropOffset,
int lengthBeforeDrop) {
int dragSourceStart = dragLocalState.start;
int dragSourceEnd = dragLocalState.end;
if (dropOffset <= dragSourceStart) {
// Inserting text before selection has shifted positions
final int shift = mTextView.getText().length() - lengthBeforeDrop;
dragSourceStart += shift;
dragSourceEnd += shift;
}
// Delete original selection
mTextView.deleteText_internal(dragSourceStart, dragSourceEnd);
// Make sure we do not leave two adjacent spaces.
final int prevCharIdx = Math.max(0, dragSourceStart - 1);
final int nextCharIdx = Math.min(mTextView.getText().length(), dragSourceStart + 1);
if (nextCharIdx > prevCharIdx + 1) {
CharSequence t = mTextView.getTransformedText(prevCharIdx, nextCharIdx);
if (Character.isSpaceChar(t.charAt(0)) && Character.isSpaceChar(t.charAt(1))) {
mTextView.deleteText_internal(prevCharIdx, prevCharIdx + 1);
}
}
}
public void addSpanWatchers(Spannable text) {
final int textLength = text.length();
if (mKeyListener != null) {
text.setSpan(mKeyListener, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
if (mSpanController == null) {
mSpanController = new SpanController();
}
text.setSpan(mSpanController, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
void setContextMenuAnchor(float x, float y) {
mContextMenuAnchorX = x;
mContextMenuAnchorY = y;
}
private void setAssistContextMenuItems(Menu menu) {
final TextClassification textClassification =
getSelectionActionModeHelper().getTextClassification();
if (textClassification == null) {
return;
}
final AssistantCallbackHelper helper =
new AssistantCallbackHelper(getSelectionActionModeHelper());
helper.updateAssistMenuItems(menu, (MenuItem item) -> {
getSelectionActionModeHelper()
.onSelectionAction(item.getItemId(), item.getTitle().toString());
if (mProcessTextIntentActionsHandler.performMenuItemAction(item)) {
return true;
}
if (item.getGroupId() == TextView.ID_ASSIST && helper.onAssistMenuItemClicked(item)) {
return true;
}
return mTextView.onTextContextMenuItem(item.getItemId());
});
}
/**
* Called when the context menu is created.
*/
public void onCreateContextMenu(ContextMenu menu) {
if (mIsBeingLongClicked || Float.isNaN(mContextMenuAnchorX)
|| Float.isNaN(mContextMenuAnchorY)) {
return;
}
final int offset = mTextView.getOffsetForPosition(mContextMenuAnchorX, mContextMenuAnchorY);
if (offset == -1) {
return;
}
stopTextActionModeWithPreservingSelection();
if (mTextView.canSelectText()) {
final boolean isOnSelection = mTextView.hasSelection()
&& offset >= mTextView.getSelectionStart()
&& offset <= mTextView.getSelectionEnd();
if (!isOnSelection) {
// Right clicked position is not on the selection. Remove the selection and move the
// cursor to the right clicked position.
Selection.setSelection((Spannable) mTextView.getText(), offset);
stopTextActionMode();
}
}
if (shouldOfferToShowSuggestions()) {
final SuggestionInfo[] suggestionInfoArray =
new SuggestionInfo[SuggestionSpan.SUGGESTIONS_MAX_SIZE];
for (int i = 0; i < suggestionInfoArray.length; i++) {
suggestionInfoArray[i] = new SuggestionInfo();
}
final SubMenu subMenu = menu.addSubMenu(Menu.NONE, Menu.NONE,
CONTEXT_MENU_ITEM_ORDER_REPLACE, com.android.internal.R.string.replace);
final int numItems = mSuggestionHelper.getSuggestionInfo(suggestionInfoArray, null);
for (int i = 0; i < numItems; i++) {
final SuggestionInfo info = suggestionInfoArray[i];
subMenu.add(Menu.NONE, Menu.NONE, i, info.mText)
.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
replaceWithSuggestion(info);
return true;
}
});
}
}
final int menuItemOrderUndo = 2;
final int menuItemOrderRedo = 3;
final int menuItemOrderCut = 4;
final int menuItemOrderCopy = 5;
final int menuItemOrderPaste = 6;
final int menuItemOrderPasteAsPlainText;
final int menuItemOrderSelectAll;
final int menuItemOrderShare;
final int menuItemOrderAutofill;
if (mUseNewContextMenu) {
menuItemOrderPasteAsPlainText = 7;
menuItemOrderSelectAll = 8;
menuItemOrderShare = 9;
menuItemOrderAutofill = 10;
menu.setOptionalIconsVisible(true);
menu.setGroupDividerEnabled(true);
setAssistContextMenuItems(menu);
final int keyboard = mTextView.getResources().getConfiguration().keyboard;
menu.setQwertyMode(keyboard == Configuration.KEYBOARD_QWERTY);
} else {
menuItemOrderShare = 7;
menuItemOrderSelectAll = 8;
menuItemOrderAutofill = 10;
menuItemOrderPasteAsPlainText = 11;
}
final TypedArray a = mTextView.getContext().obtainStyledAttributes(new int[] {
// TODO: Make Undo/Redo be public attribute.
com.android.internal.R.attr.actionModeUndoDrawable,
com.android.internal.R.attr.actionModeRedoDrawable,
android.R.attr.actionModeCutDrawable,
android.R.attr.actionModeCopyDrawable,
android.R.attr.actionModePasteDrawable,
android.R.attr.actionModeSelectAllDrawable,
android.R.attr.actionModeShareDrawable,
});
menu.add(CONTEXT_MENU_GROUP_UNDO_REDO, TextView.ID_UNDO, menuItemOrderUndo,
com.android.internal.R.string.undo)
.setAlphabeticShortcut('z')
.setOnMenuItemClickListener(mOnContextMenuItemClickListener)
.setIcon(a.getDrawable(0))
.setEnabled(mTextView.canUndo());
menu.add(CONTEXT_MENU_GROUP_UNDO_REDO, TextView.ID_REDO, menuItemOrderRedo,
com.android.internal.R.string.redo)
.setAlphabeticShortcut('z', KeyEvent.META_CTRL_ON | KeyEvent.META_SHIFT_ON)
.setOnMenuItemClickListener(mOnContextMenuItemClickListener)
.setIcon(a.getDrawable(1))
.setEnabled(mTextView.canRedo());
menu.add(CONTEXT_MENU_GROUP_CLIPBOARD, TextView.ID_CUT, menuItemOrderCut,
com.android.internal.R.string.cut)
.setAlphabeticShortcut('x')
.setOnMenuItemClickListener(mOnContextMenuItemClickListener)
.setIcon(a.getDrawable(2))
.setEnabled(mTextView.canCut());
menu.add(CONTEXT_MENU_GROUP_CLIPBOARD, TextView.ID_COPY, menuItemOrderCopy,
com.android.internal.R.string.copy)
.setAlphabeticShortcut('c')
.setOnMenuItemClickListener(mOnContextMenuItemClickListener)
.setIcon(a.getDrawable(3))
.setEnabled(mTextView.canCopy());
menu.add(CONTEXT_MENU_GROUP_CLIPBOARD, TextView.ID_PASTE, menuItemOrderPaste,
com.android.internal.R.string.paste)
.setAlphabeticShortcut('v')
.setEnabled(mTextView.canPaste())
.setIcon(a.getDrawable(4))
.setOnMenuItemClickListener(mOnContextMenuItemClickListener);
menu.add(CONTEXT_MENU_GROUP_CLIPBOARD, TextView.ID_PASTE_AS_PLAIN_TEXT,
menuItemOrderPasteAsPlainText,
com.android.internal.R.string.paste_as_plain_text)
.setAlphabeticShortcut('v', KeyEvent.META_CTRL_ON | KeyEvent.META_SHIFT_ON)
.setEnabled(mTextView.canPasteAsPlainText())
.setIcon(a.getDrawable(4))
.setOnMenuItemClickListener(mOnContextMenuItemClickListener);
menu.add(CONTEXT_MENU_GROUP_CLIPBOARD, TextView.ID_SELECT_ALL,
menuItemOrderSelectAll, com.android.internal.R.string.selectAll)
.setAlphabeticShortcut('a')
.setEnabled(mTextView.canSelectAllText())
.setIcon(a.getDrawable(5))
.setOnMenuItemClickListener(mOnContextMenuItemClickListener);
menu.add(CONTEXT_MENU_GROUP_MISC, TextView.ID_SHARE, menuItemOrderShare,
com.android.internal.R.string.share)
.setEnabled(mTextView.canShare())
.setIcon(a.getDrawable(6))
.setOnMenuItemClickListener(mOnContextMenuItemClickListener);
menu.add(CONTEXT_MENU_GROUP_MISC, TextView.ID_AUTOFILL, menuItemOrderAutofill,
android.R.string.autofill)
.setEnabled(mTextView.canRequestAutofill())
.setOnMenuItemClickListener(mOnContextMenuItemClickListener);
mPreserveSelection = true;
a.recycle();
// No-op for the old context menu because it doesn't have icons.
adjustIconSpacing(menu);
}
/**
* Adjust icon spacing to align the texts.
* @hide
*/
@VisibleForTesting
public void adjustIconSpacing(ContextMenu menu) {
int width = -1;
int height = -1;
for (int i = 0; i < menu.size(); ++i) {
final MenuItem item = menu.getItem(i);
final Drawable d = item.getIcon();
if (d == null) {
continue;
}
width = Math.max(width, d.getIntrinsicWidth());
height = Math.max(height, d.getIntrinsicHeight());
}
if (width < 0 || height < 0) {
return; // No menu has icon drawable.
}
GradientDrawable paddingDrawable = new GradientDrawable();
paddingDrawable.setSize(width, height);
for (int i = 0; i < menu.size(); ++i) {
final MenuItem item = menu.getItem(i);
final Drawable d = item.getIcon();
if (d == null) {
item.setIcon(paddingDrawable);
}
}
}
@Nullable
private SuggestionSpan findEquivalentSuggestionSpan(
@NonNull SuggestionSpanInfo suggestionSpanInfo) {
final Editable editable = (Editable) mTextView.getText();
if (editable.getSpanStart(suggestionSpanInfo.mSuggestionSpan) >= 0) {
// Exactly same span is found.
return suggestionSpanInfo.mSuggestionSpan;
}
// Suggestion span couldn't be found. Try to find a suggestion span that has the same
// contents.
final SuggestionSpan[] suggestionSpans = editable.getSpans(suggestionSpanInfo.mSpanStart,
suggestionSpanInfo.mSpanEnd, SuggestionSpan.class);
for (final SuggestionSpan suggestionSpan : suggestionSpans) {
final int start = editable.getSpanStart(suggestionSpan);
if (start != suggestionSpanInfo.mSpanStart) {
continue;
}
final int end = editable.getSpanEnd(suggestionSpan);
if (end != suggestionSpanInfo.mSpanEnd) {
continue;
}
if (suggestionSpan.equals(suggestionSpanInfo.mSuggestionSpan)) {
return suggestionSpan;
}
}
return null;
}
private void replaceWithSuggestion(@NonNull final SuggestionInfo suggestionInfo) {
final SuggestionSpan targetSuggestionSpan = findEquivalentSuggestionSpan(
suggestionInfo.mSuggestionSpanInfo);
if (targetSuggestionSpan == null) {
// Span has been removed
return;
}
final Editable editable = (Editable) mTextView.getText();
final int spanStart = editable.getSpanStart(targetSuggestionSpan);
final int spanEnd = editable.getSpanEnd(targetSuggestionSpan);
if (spanStart < 0 || spanEnd <= spanStart) {
// Span has been removed
return;
}
final String originalText = TextUtils.substring(editable, spanStart, spanEnd);
// SuggestionSpans are removed by replace: save them before
SuggestionSpan[] suggestionSpans = editable.getSpans(spanStart, spanEnd,
SuggestionSpan.class);
final int length = suggestionSpans.length;
int[] suggestionSpansStarts = new int[length];
int[] suggestionSpansEnds = new int[length];
int[] suggestionSpansFlags = new int[length];
for (int i = 0; i < length; i++) {
final SuggestionSpan suggestionSpan = suggestionSpans[i];
suggestionSpansStarts[i] = editable.getSpanStart(suggestionSpan);
suggestionSpansEnds[i] = editable.getSpanEnd(suggestionSpan);
suggestionSpansFlags[i] = editable.getSpanFlags(suggestionSpan);
// Remove potential misspelled flags
int suggestionSpanFlags = suggestionSpan.getFlags();
if ((suggestionSpanFlags & FLAG_MISSPELLED_OR_GRAMMAR_ERROR) != 0) {
suggestionSpanFlags &= ~SuggestionSpan.FLAG_MISSPELLED;
suggestionSpanFlags &= ~SuggestionSpan.FLAG_GRAMMAR_ERROR;
suggestionSpanFlags &= ~SuggestionSpan.FLAG_EASY_CORRECT;
suggestionSpan.setFlags(suggestionSpanFlags);
}
}
// Swap text content between actual text and Suggestion span
final int suggestionStart = suggestionInfo.mSuggestionStart;
final int suggestionEnd = suggestionInfo.mSuggestionEnd;
final String suggestion = suggestionInfo.mText.subSequence(
suggestionStart, suggestionEnd).toString();
mTextView.replaceText_internal(spanStart, spanEnd, suggestion);
String[] suggestions = targetSuggestionSpan.getSuggestions();
suggestions[suggestionInfo.mSuggestionIndex] = originalText;
// Restore previous SuggestionSpans
final int lengthDelta = suggestion.length() - (spanEnd - spanStart);
for (int i = 0; i < length; i++) {
// Only spans that include the modified region make sense after replacement
// Spans partially included in the replaced region are removed, there is no
// way to assign them a valid range after replacement
if (suggestionSpansStarts[i] <= spanStart && suggestionSpansEnds[i] >= spanEnd) {
mTextView.setSpan_internal(suggestionSpans[i], suggestionSpansStarts[i],
suggestionSpansEnds[i] + lengthDelta, suggestionSpansFlags[i]);
}
}
// Move cursor at the end of the replaced word
final int newCursorPosition = spanEnd + lengthDelta;
mTextView.setCursorPosition_internal(newCursorPosition, newCursorPosition);
}
private final MenuItem.OnMenuItemClickListener mOnContextMenuItemClickListener =
new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
if (mProcessTextIntentActionsHandler.performMenuItemAction(item)) {
return true;
}
return mTextView.onTextContextMenuItem(item.getItemId());
}
};
/**
* Controls the {@link EasyEditSpan} monitoring when it is added, and when the related
* pop-up should be displayed.
* Also monitors {@link Selection} to call back to the attached input method.
*/
private class SpanController implements SpanWatcher {
private static final int DISPLAY_TIMEOUT_MS = 3000; // 3 secs
private EasyEditPopupWindow mPopupWindow;
private Runnable mHidePopup;
// This function is pure but inner classes can't have static functions
private boolean isNonIntermediateSelectionSpan(final Spannable text,
final Object span) {
return (Selection.SELECTION_START == span || Selection.SELECTION_END == span)
&& (text.getSpanFlags(span) & Spanned.SPAN_INTERMEDIATE) == 0;
}
@Override
public void onSpanAdded(Spannable text, Object span, int start, int end) {
if (isNonIntermediateSelectionSpan(text, span)) {
sendUpdateSelection();
} else if (span instanceof EasyEditSpan) {
if (mPopupWindow == null) {
mPopupWindow = new EasyEditPopupWindow();
mHidePopup = new Runnable() {
@Override
public void run() {
hide();
}
};
}
// Make sure there is only at most one EasyEditSpan in the text
if (mPopupWindow.mEasyEditSpan != null) {
mPopupWindow.mEasyEditSpan.setDeleteEnabled(false);
}
mPopupWindow.setEasyEditSpan((EasyEditSpan) span);
mPopupWindow.setOnDeleteListener(new EasyEditDeleteListener() {
@Override
public void onDeleteClick(EasyEditSpan span) {
Editable editable = (Editable) mTextView.getText();
int start = editable.getSpanStart(span);
int end = editable.getSpanEnd(span);
if (start >= 0 && end >= 0) {
sendEasySpanNotification(EasyEditSpan.TEXT_DELETED, span);
mTextView.deleteText_internal(start, end);
}
editable.removeSpan(span);
}
});
if (mTextView.getWindowVisibility() != View.VISIBLE) {
// The window is not visible yet, ignore the text change.
return;
}
if (mTextView.getLayout() == null) {
// The view has not been laid out yet, ignore the text change
return;
}
if (extractedTextModeWillBeStarted()) {
// The input is in extract mode. Do not handle the easy edit in
// the original TextView, as the ExtractEditText will do
return;
}
mPopupWindow.show();
mTextView.removeCallbacks(mHidePopup);
mTextView.postDelayed(mHidePopup, DISPLAY_TIMEOUT_MS);
}
}
@Override
public void onSpanRemoved(Spannable text, Object span, int start, int end) {
if (isNonIntermediateSelectionSpan(text, span)) {
sendUpdateSelection();
} else if (mPopupWindow != null && span == mPopupWindow.mEasyEditSpan) {
hide();
}
}
@Override
public void onSpanChanged(Spannable text, Object span, int previousStart, int previousEnd,
int newStart, int newEnd) {
if (isNonIntermediateSelectionSpan(text, span)) {
sendUpdateSelection();
} else if (mPopupWindow != null && span instanceof EasyEditSpan) {
EasyEditSpan easyEditSpan = (EasyEditSpan) span;
sendEasySpanNotification(EasyEditSpan.TEXT_MODIFIED, easyEditSpan);
text.removeSpan(easyEditSpan);
}
}
public void hide() {
if (mPopupWindow != null) {
mPopupWindow.hide();
mTextView.removeCallbacks(mHidePopup);
}
}
private void sendEasySpanNotification(int textChangedType, EasyEditSpan span) {
try {
PendingIntent pendingIntent = span.getPendingIntent();
if (pendingIntent != null) {
Intent intent = new Intent();
intent.putExtra(EasyEditSpan.EXTRA_TEXT_CHANGED_TYPE, textChangedType);
pendingIntent.send(mTextView.getContext(), 0, intent);
}
} catch (CanceledException e) {
// This should not happen, as we should try to send the intent only once.
Log.w(TAG, "PendingIntent for notification cannot be sent", e);
}
}
}
/**
* Listens for the delete event triggered by {@link EasyEditPopupWindow}.
*/
private interface EasyEditDeleteListener {
/**
* Clicks the delete pop-up.
*/
void onDeleteClick(EasyEditSpan span);
}
/**
* Displays the actions associated to an {@link EasyEditSpan}. The pop-up is controlled
* by {@link SpanController}.
*/
private class EasyEditPopupWindow extends PinnedPopupWindow
implements OnClickListener {
private static final int POPUP_TEXT_LAYOUT =
com.android.internal.R.layout.text_edit_action_popup_text;
private TextView mDeleteTextView;
private EasyEditSpan mEasyEditSpan;
private EasyEditDeleteListener mOnDeleteListener;
@Override
protected void createPopupWindow() {
mPopupWindow = new PopupWindow(mTextView.getContext(), null,
com.android.internal.R.attr.textSelectHandleWindowStyle);
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
mPopupWindow.setClippingEnabled(true);
}
@Override
protected void initContentView() {
LinearLayout linearLayout = new LinearLayout(mTextView.getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
mContentView = linearLayout;
mContentView.setBackgroundResource(
com.android.internal.R.drawable.text_edit_side_paste_window);
LayoutInflater inflater = (LayoutInflater) mTextView.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams wrapContent = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mDeleteTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null);
mDeleteTextView.setLayoutParams(wrapContent);
mDeleteTextView.setText(com.android.internal.R.string.delete);
mDeleteTextView.setOnClickListener(this);
mContentView.addView(mDeleteTextView);
}
public void setEasyEditSpan(EasyEditSpan easyEditSpan) {
mEasyEditSpan = easyEditSpan;
}
private void setOnDeleteListener(EasyEditDeleteListener listener) {
mOnDeleteListener = listener;
}
@Override
public void onClick(View view) {
if (view == mDeleteTextView
&& mEasyEditSpan != null && mEasyEditSpan.isDeleteEnabled()
&& mOnDeleteListener != null) {
mOnDeleteListener.onDeleteClick(mEasyEditSpan);
}
}
@Override
public void hide() {
if (mEasyEditSpan != null) {
mEasyEditSpan.setDeleteEnabled(false);
}
mOnDeleteListener = null;
super.hide();
}
@Override
protected int getTextOffset() {
// Place the pop-up at the end of the span
Editable editable = (Editable) mTextView.getText();
return editable.getSpanEnd(mEasyEditSpan);
}
@Override
protected int getVerticalLocalPosition(int line) {
final Layout layout = mTextView.getLayout();
return layout.getLineBottom(line, /* includeLineSpacing= */ false);
}
@Override
protected int clipVertically(int positionY) {
// As we display the pop-up below the span, no vertical clipping is required.
return positionY;
}
}
private class PositionListener implements ViewTreeObserver.OnPreDrawListener {
// 3 handles
// 3 ActionPopup [replace, suggestion, easyedit] (suggestionsPopup first hides the others)
// 1 CursorAnchorInfoNotifier
private static final int MAXIMUM_NUMBER_OF_LISTENERS = 7;
private TextViewPositionListener[] mPositionListeners =
new TextViewPositionListener[MAXIMUM_NUMBER_OF_LISTENERS];
private boolean[] mCanMove = new boolean[MAXIMUM_NUMBER_OF_LISTENERS];
private boolean mPositionHasChanged = true;
// Absolute position of the TextView with respect to its parent window
private int mPositionX, mPositionY;
private int mPositionXOnScreen, mPositionYOnScreen;
private int mNumberOfListeners;
private boolean mScrollHasChanged;
final int[] mTempCoords = new int[2];
public void addSubscriber(TextViewPositionListener positionListener, boolean canMove) {
if (mNumberOfListeners == 0) {
updatePosition();
ViewTreeObserver vto = mTextView.getViewTreeObserver();
vto.addOnPreDrawListener(this);
}
int emptySlotIndex = -1;
for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) {
TextViewPositionListener listener = mPositionListeners[i];
if (listener == positionListener) {
return;
} else if (emptySlotIndex < 0 && listener == null) {
emptySlotIndex = i;
}
}
mPositionListeners[emptySlotIndex] = positionListener;
mCanMove[emptySlotIndex] = canMove;
mNumberOfListeners++;
}
public void removeSubscriber(TextViewPositionListener positionListener) {
for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) {
if (mPositionListeners[i] == positionListener) {
mPositionListeners[i] = null;
mNumberOfListeners--;
break;
}
}
if (mNumberOfListeners == 0) {
ViewTreeObserver vto = mTextView.getViewTreeObserver();
vto.removeOnPreDrawListener(this);
}
}
public int getPositionX() {
return mPositionX;
}
public int getPositionY() {
return mPositionY;
}
public int getPositionXOnScreen() {
return mPositionXOnScreen;
}
public int getPositionYOnScreen() {
return mPositionYOnScreen;
}
@Override
public boolean onPreDraw() {
updatePosition();
for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) {
if (mPositionHasChanged || mScrollHasChanged || mCanMove[i]) {
TextViewPositionListener positionListener = mPositionListeners[i];
if (positionListener != null) {
positionListener.updatePosition(mPositionX, mPositionY,
mPositionHasChanged, mScrollHasChanged);
}
}
}
mScrollHasChanged = false;
return true;
}
private void updatePosition() {
mTextView.getLocationInWindow(mTempCoords);
mPositionHasChanged = mTempCoords[0] != mPositionX || mTempCoords[1] != mPositionY;
mPositionX = mTempCoords[0];
mPositionY = mTempCoords[1];
mTextView.getLocationOnScreen(mTempCoords);
mPositionXOnScreen = mTempCoords[0];
mPositionYOnScreen = mTempCoords[1];
}
public void onScrollChanged() {
mScrollHasChanged = true;
}
}
private abstract class PinnedPopupWindow implements TextViewPositionListener {
protected PopupWindow mPopupWindow;
protected ViewGroup mContentView;
int mPositionX, mPositionY;
int mClippingLimitLeft, mClippingLimitRight;
protected abstract void createPopupWindow();
protected abstract void initContentView();
protected abstract int getTextOffset();
protected abstract int getVerticalLocalPosition(int line);
protected abstract int clipVertically(int positionY);
protected void setUp() {
}
public PinnedPopupWindow() {
// Due to calling subclass methods in base constructor, subclass constructor is not
// called before subclass methods, e.g. createPopupWindow or initContentView. To give
// a chance to initialize subclasses, call setUp() method here.
// TODO: It is good to extract non trivial initialization code from constructor.
setUp();
createPopupWindow();
mPopupWindow.setWindowLayoutType(
WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL);
mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
initContentView();
LayoutParams wrapContent = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
mContentView.setLayoutParams(wrapContent);
mPopupWindow.setContentView(mContentView);
}
public void show() {
getPositionListener().addSubscriber(this, false /* offset is fixed */);
computeLocalPosition();
final PositionListener positionListener = getPositionListener();
updatePosition(positionListener.getPositionX(), positionListener.getPositionY());
}
protected void measureContent() {
final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics();
mContentView.measure(
View.MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels,
View.MeasureSpec.AT_MOST),
View.MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels,
View.MeasureSpec.AT_MOST));
}
/* The popup window will be horizontally centered on the getTextOffset() and vertically
* positioned according to viewportToContentHorizontalOffset.
*
* This method assumes that mContentView has properly been measured from its content. */
private void computeLocalPosition() {
measureContent();
final int width = mContentView.getMeasuredWidth();
final int offset = getTextOffset();
final int transformedOffset = mTextView.originalToTransformed(offset,
OffsetMapping.MAP_STRATEGY_CURSOR);
final Layout layout = mTextView.getLayout();
mPositionX = (int) (layout.getPrimaryHorizontal(transformedOffset) - width / 2.0f);
mPositionX += mTextView.viewportToContentHorizontalOffset();
final int line = layout.getLineForOffset(transformedOffset);
mPositionY = getVerticalLocalPosition(line);
mPositionY += mTextView.viewportToContentVerticalOffset();
}
private void updatePosition(int parentPositionX, int parentPositionY) {
int positionX = parentPositionX + mPositionX;
int positionY = parentPositionY + mPositionY;
positionY = clipVertically(positionY);
// Horizontal clipping
final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics();
final int width = mContentView.getMeasuredWidth();
positionX = Math.min(
displayMetrics.widthPixels - width + mClippingLimitRight, positionX);
positionX = Math.max(-mClippingLimitLeft, positionX);
if (isShowing()) {
mPopupWindow.update(positionX, positionY, -1, -1);
} else {
mPopupWindow.showAtLocation(mTextView, Gravity.NO_GRAVITY,
positionX, positionY);
}
}
public void hide() {
if (!isShowing()) {
return;
}
mPopupWindow.dismiss();
getPositionListener().removeSubscriber(this);
}
@Override
public void updatePosition(int parentPositionX, int parentPositionY,
boolean parentPositionChanged, boolean parentScrolled) {
// Either parentPositionChanged or parentScrolled is true, check if still visible
if (isShowing() && isOffsetVisible(getTextOffset())) {
if (parentScrolled) computeLocalPosition();
updatePosition(parentPositionX, parentPositionY);
} else {
hide();
}
}
public boolean isShowing() {
return mPopupWindow.isShowing();
}
}
private static final class SuggestionInfo {
// Range of actual suggestion within mText
int mSuggestionStart, mSuggestionEnd;
// The SuggestionSpan that this TextView represents
final SuggestionSpanInfo mSuggestionSpanInfo = new SuggestionSpanInfo();
// The index of this suggestion inside suggestionSpan
int mSuggestionIndex;
final SpannableStringBuilder mText = new SpannableStringBuilder();
void clear() {
mSuggestionSpanInfo.clear();
mText.clear();
}
// Utility method to set attributes about a SuggestionSpan.
void setSpanInfo(SuggestionSpan span, int spanStart, int spanEnd) {
mSuggestionSpanInfo.mSuggestionSpan = span;
mSuggestionSpanInfo.mSpanStart = spanStart;
mSuggestionSpanInfo.mSpanEnd = spanEnd;
}
}
private static final class SuggestionSpanInfo {
// The SuggestionSpan;
@Nullable
SuggestionSpan mSuggestionSpan;
// The SuggestionSpan start position
int mSpanStart;
// The SuggestionSpan end position
int mSpanEnd;
void clear() {
mSuggestionSpan = null;
}
}
private class SuggestionHelper {
private final Comparator mSuggestionSpanComparator =
new SuggestionSpanComparator();
private final HashMap mSpansLengths =
new HashMap();
private class SuggestionSpanComparator implements Comparator {
public int compare(SuggestionSpan span1, SuggestionSpan span2) {
final int flag1 = span1.getFlags();
final int flag2 = span2.getFlags();
if (flag1 != flag2) {
// Compare so that the order will be: easy -> misspelled -> grammarError
int easy = compareFlag(SuggestionSpan.FLAG_EASY_CORRECT, flag1, flag2);
if (easy != 0) return easy;
int misspelled = compareFlag(SuggestionSpan.FLAG_MISSPELLED, flag1, flag2);
if (misspelled != 0) return misspelled;
int grammarError = compareFlag(SuggestionSpan.FLAG_GRAMMAR_ERROR, flag1, flag2);
if (grammarError != 0) return grammarError;
}
return mSpansLengths.get(span1).intValue() - mSpansLengths.get(span2).intValue();
}
/*
* Returns -1 if flags1 has flagToCompare but flags2 does not.
* Returns 1 if flags2 has flagToCompare but flags1 does not.
* Otherwise, returns 0.
*/
private int compareFlag(int flagToCompare, int flags1, int flags2) {
boolean hasFlag1 = (flags1 & flagToCompare) != 0;
boolean hasFlag2 = (flags2 & flagToCompare) != 0;
if (hasFlag1 == hasFlag2) return 0;
return hasFlag1 ? -1 : 1;
}
}
/**
* Returns the suggestion spans that cover the current cursor position. The suggestion
* spans are sorted according to the length of text that they are attached to.
*/
private SuggestionSpan[] getSortedSuggestionSpans() {
int pos = mTextView.getSelectionStart();
Spannable spannable = (Spannable) mTextView.getText();
SuggestionSpan[] suggestionSpans = spannable.getSpans(pos, pos, SuggestionSpan.class);
mSpansLengths.clear();
for (SuggestionSpan suggestionSpan : suggestionSpans) {
int start = spannable.getSpanStart(suggestionSpan);
int end = spannable.getSpanEnd(suggestionSpan);
mSpansLengths.put(suggestionSpan, Integer.valueOf(end - start));
}
// The suggestions are sorted according to their types (easy correction first,
// misspelled second, then grammar error) and to the length of the text that they cover
// (shorter first).
Arrays.sort(suggestionSpans, mSuggestionSpanComparator);
mSpansLengths.clear();
return suggestionSpans;
}
/**
* Gets the SuggestionInfo list that contains suggestion information at the current cursor
* position.
*
* @param suggestionInfos SuggestionInfo array the results will be set.
* @param misspelledSpanInfo a struct the misspelled SuggestionSpan info will be set.
* @return the number of suggestions actually fetched.
*/
public int getSuggestionInfo(SuggestionInfo[] suggestionInfos,
@Nullable SuggestionSpanInfo misspelledSpanInfo) {
final Spannable spannable = (Spannable) mTextView.getText();
final SuggestionSpan[] suggestionSpans = getSortedSuggestionSpans();
final int nbSpans = suggestionSpans.length;
if (nbSpans == 0) return 0;
int numberOfSuggestions = 0;
for (final SuggestionSpan suggestionSpan : suggestionSpans) {
final int spanStart = spannable.getSpanStart(suggestionSpan);
final int spanEnd = spannable.getSpanEnd(suggestionSpan);
if (misspelledSpanInfo != null
&& (suggestionSpan.getFlags() & FLAG_MISSPELLED_OR_GRAMMAR_ERROR) != 0) {
misspelledSpanInfo.mSuggestionSpan = suggestionSpan;
misspelledSpanInfo.mSpanStart = spanStart;
misspelledSpanInfo.mSpanEnd = spanEnd;
}
final String[] suggestions = suggestionSpan.getSuggestions();
final int nbSuggestions = suggestions.length;
suggestionLoop:
for (int suggestionIndex = 0; suggestionIndex < nbSuggestions; suggestionIndex++) {
final String suggestion = suggestions[suggestionIndex];
for (int i = 0; i < numberOfSuggestions; i++) {
final SuggestionInfo otherSuggestionInfo = suggestionInfos[i];
if (otherSuggestionInfo.mText.toString().equals(suggestion)) {
final int otherSpanStart =
otherSuggestionInfo.mSuggestionSpanInfo.mSpanStart;
final int otherSpanEnd =
otherSuggestionInfo.mSuggestionSpanInfo.mSpanEnd;
if (spanStart == otherSpanStart && spanEnd == otherSpanEnd) {
continue suggestionLoop;
}
}
}
SuggestionInfo suggestionInfo = suggestionInfos[numberOfSuggestions];
suggestionInfo.setSpanInfo(suggestionSpan, spanStart, spanEnd);
suggestionInfo.mSuggestionIndex = suggestionIndex;
suggestionInfo.mSuggestionStart = 0;
suggestionInfo.mSuggestionEnd = suggestion.length();
suggestionInfo.mText.replace(0, suggestionInfo.mText.length(), suggestion);
numberOfSuggestions++;
if (numberOfSuggestions >= suggestionInfos.length) {
return numberOfSuggestions;
}
}
}
return numberOfSuggestions;
}
}
private final class SuggestionsPopupWindow extends PinnedPopupWindow
implements OnItemClickListener {
private static final int MAX_NUMBER_SUGGESTIONS = SuggestionSpan.SUGGESTIONS_MAX_SIZE;
// Key of intent extras for inserting new word into user dictionary.
private static final String USER_DICTIONARY_EXTRA_WORD = "word";
private static final String USER_DICTIONARY_EXTRA_LOCALE = "locale";
private SuggestionInfo[] mSuggestionInfos;
private int mNumberOfSuggestions;
private boolean mCursorWasVisibleBeforeSuggestions;
private boolean mIsShowingUp = false;
private SuggestionAdapter mSuggestionsAdapter;
private TextAppearanceSpan mHighlightSpan; // TODO: Make mHighlightSpan final.
private TextView mAddToDictionaryButton;
private TextView mDeleteButton;
private ListView mSuggestionListView;
private final SuggestionSpanInfo mMisspelledSpanInfo = new SuggestionSpanInfo();
private int mContainerMarginWidth;
private int mContainerMarginTop;
private LinearLayout mContainerView;
private Context mContext; // TODO: Make mContext final.
private class CustomPopupWindow extends PopupWindow {
@Override
public void dismiss() {
if (!isShowing()) {
return;
}
super.dismiss();
getPositionListener().removeSubscriber(SuggestionsPopupWindow.this);
// Safe cast since show() checks that mTextView.getText() is an Editable
((Spannable) mTextView.getText()).removeSpan(mSuggestionRangeSpan);
mTextView.setCursorVisible(mCursorWasVisibleBeforeSuggestions);
if (hasInsertionController() && !extractedTextModeWillBeStarted()) {
getInsertionController().show();
}
}
}
public SuggestionsPopupWindow() {
mCursorWasVisibleBeforeSuggestions = mTextView.isCursorVisibleFromAttr();
}
@Override
protected void setUp() {
mContext = applyDefaultTheme(mTextView.getContext());
mHighlightSpan = new TextAppearanceSpan(mContext,
mTextView.mTextEditSuggestionHighlightStyle);
}
private Context applyDefaultTheme(Context originalContext) {
TypedArray a = originalContext.obtainStyledAttributes(
new int[]{com.android.internal.R.attr.isLightTheme});
boolean isLightTheme = a.getBoolean(0, true);
int themeId = isLightTheme ? R.style.ThemeOverlay_Material_Light
: R.style.ThemeOverlay_Material_Dark;
a.recycle();
return new ContextThemeWrapper(originalContext, themeId);
}
@Override
protected void createPopupWindow() {
mPopupWindow = new CustomPopupWindow();
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mPopupWindow.setFocusable(true);
mPopupWindow.setClippingEnabled(false);
}
@Override
protected void initContentView() {
final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mContentView = (ViewGroup) inflater.inflate(
mTextView.mTextEditSuggestionContainerLayout, null);
mContainerView = (LinearLayout) mContentView.findViewById(
com.android.internal.R.id.suggestionWindowContainer);
ViewGroup.MarginLayoutParams lp =
(ViewGroup.MarginLayoutParams) mContainerView.getLayoutParams();
mContainerMarginWidth = lp.leftMargin + lp.rightMargin;
mContainerMarginTop = lp.topMargin;
mClippingLimitLeft = lp.leftMargin;
mClippingLimitRight = lp.rightMargin;
mSuggestionListView = (ListView) mContentView.findViewById(
com.android.internal.R.id.suggestionContainer);
mSuggestionsAdapter = new SuggestionAdapter();
mSuggestionListView.setAdapter(mSuggestionsAdapter);
mSuggestionListView.setOnItemClickListener(this);
// Inflate the suggestion items once and for all.
mSuggestionInfos = new SuggestionInfo[MAX_NUMBER_SUGGESTIONS];
for (int i = 0; i < mSuggestionInfos.length; i++) {
mSuggestionInfos[i] = new SuggestionInfo();
}
mAddToDictionaryButton = (TextView) mContentView.findViewById(
com.android.internal.R.id.addToDictionaryButton);
mAddToDictionaryButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final SuggestionSpan misspelledSpan =
findEquivalentSuggestionSpan(mMisspelledSpanInfo);
if (misspelledSpan == null) {
// Span has been removed.
return;
}
final Editable editable = (Editable) mTextView.getText();
final int spanStart = editable.getSpanStart(misspelledSpan);
final int spanEnd = editable.getSpanEnd(misspelledSpan);
if (spanStart < 0 || spanEnd <= spanStart) {
return;
}
final String originalText = TextUtils.substring(editable, spanStart, spanEnd);
final Intent intent = new Intent(Settings.ACTION_USER_DICTIONARY_INSERT);
intent.putExtra(USER_DICTIONARY_EXTRA_WORD, originalText);
intent.putExtra(USER_DICTIONARY_EXTRA_LOCALE,
mTextView.getTextServicesLocale().toString());
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mTextView.startActivityAsTextOperationUserIfNecessary(intent);
// There is no way to know if the word was indeed added. Re-check.
// TODO The ExtractEditText should remove the span in the original text instead
editable.removeSpan(mMisspelledSpanInfo.mSuggestionSpan);
Selection.setSelection(editable, spanEnd);
updateSpellCheckSpans(spanStart, spanEnd, false);
hideWithCleanUp();
}
});
mDeleteButton = (TextView) mContentView.findViewById(
com.android.internal.R.id.deleteButton);
mDeleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final Editable editable = (Editable) mTextView.getText();
final int spanUnionStart = editable.getSpanStart(mSuggestionRangeSpan);
int spanUnionEnd = editable.getSpanEnd(mSuggestionRangeSpan);
if (spanUnionStart >= 0 && spanUnionEnd > spanUnionStart) {
// Do not leave two adjacent spaces after deletion, or one at beginning of
// text
if (spanUnionEnd < editable.length()
&& Character.isSpaceChar(editable.charAt(spanUnionEnd))
&& (spanUnionStart == 0
|| Character.isSpaceChar(
editable.charAt(spanUnionStart - 1)))) {
spanUnionEnd = spanUnionEnd + 1;
}
mTextView.deleteText_internal(spanUnionStart, spanUnionEnd);
}
hideWithCleanUp();
}
});
}
public boolean isShowingUp() {
return mIsShowingUp;
}
public void onParentLostFocus() {
mIsShowingUp = false;
}
private class SuggestionAdapter extends BaseAdapter {
private LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
@Override
public int getCount() {
return mNumberOfSuggestions;
}
@Override
public Object getItem(int position) {
return mSuggestionInfos[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) convertView;
if (textView == null) {
textView = (TextView) mInflater.inflate(mTextView.mTextEditSuggestionItemLayout,
parent, false);
}
final SuggestionInfo suggestionInfo = mSuggestionInfos[position];
textView.setText(suggestionInfo.mText);
return textView;
}
}
@Override
public void show() {
if (!(mTextView.getText() instanceof Editable)) return;
if (extractedTextModeWillBeStarted()) {
return;
}
if (updateSuggestions()) {
mCursorWasVisibleBeforeSuggestions = mTextView.isCursorVisibleFromAttr();
mTextView.setCursorVisible(false);
mIsShowingUp = true;
super.show();
}
mSuggestionListView.setVisibility(mNumberOfSuggestions == 0 ? View.GONE : View.VISIBLE);
}
@Override
protected void measureContent() {
final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics();
final int horizontalMeasure = View.MeasureSpec.makeMeasureSpec(
displayMetrics.widthPixels, View.MeasureSpec.AT_MOST);
final int verticalMeasure = View.MeasureSpec.makeMeasureSpec(
displayMetrics.heightPixels, View.MeasureSpec.AT_MOST);
int width = 0;
View view = null;
for (int i = 0; i < mNumberOfSuggestions; i++) {
view = mSuggestionsAdapter.getView(i, view, mContentView);
view.getLayoutParams().width = LayoutParams.WRAP_CONTENT;
view.measure(horizontalMeasure, verticalMeasure);
width = Math.max(width, view.getMeasuredWidth());
}
if (mAddToDictionaryButton.getVisibility() != View.GONE) {
mAddToDictionaryButton.measure(horizontalMeasure, verticalMeasure);
width = Math.max(width, mAddToDictionaryButton.getMeasuredWidth());
}
mDeleteButton.measure(horizontalMeasure, verticalMeasure);
width = Math.max(width, mDeleteButton.getMeasuredWidth());
width += mContainerView.getPaddingLeft() + mContainerView.getPaddingRight()
+ mContainerMarginWidth;
// Enforce the width based on actual text widths
mContentView.measure(
View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
verticalMeasure);
Drawable popupBackground = mPopupWindow.getBackground();
if (popupBackground != null) {
if (mTempRect == null) mTempRect = new Rect();
popupBackground.getPadding(mTempRect);
width += mTempRect.left + mTempRect.right;
}
mPopupWindow.setWidth(width);
}
@Override
protected int getTextOffset() {
return (mTextView.getSelectionStart() + mTextView.getSelectionStart()) / 2;
}
@Override
protected int getVerticalLocalPosition(int line) {
final Layout layout = mTextView.getLayout();
return layout.getLineBottom(line, /* includeLineSpacing= */ false)
- mContainerMarginTop;
}
@Override
protected int clipVertically(int positionY) {
final int height = mContentView.getMeasuredHeight();
final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics();
return Math.min(positionY, displayMetrics.heightPixels - height);
}
private void hideWithCleanUp() {
for (final SuggestionInfo info : mSuggestionInfos) {
info.clear();
}
mMisspelledSpanInfo.clear();
hide();
}
private boolean updateSuggestions() {
Spannable spannable = (Spannable) mTextView.getText();
mNumberOfSuggestions =
mSuggestionHelper.getSuggestionInfo(mSuggestionInfos, mMisspelledSpanInfo);
if (mNumberOfSuggestions == 0 && mMisspelledSpanInfo.mSuggestionSpan == null) {
return false;
}
int spanUnionStart = mTextView.getText().length();
int spanUnionEnd = 0;
for (int i = 0; i < mNumberOfSuggestions; i++) {
final SuggestionSpanInfo spanInfo = mSuggestionInfos[i].mSuggestionSpanInfo;
spanUnionStart = Math.min(spanUnionStart, spanInfo.mSpanStart);
spanUnionEnd = Math.max(spanUnionEnd, spanInfo.mSpanEnd);
}
if (mMisspelledSpanInfo.mSuggestionSpan != null) {
spanUnionStart = Math.min(spanUnionStart, mMisspelledSpanInfo.mSpanStart);
spanUnionEnd = Math.max(spanUnionEnd, mMisspelledSpanInfo.mSpanEnd);
}
for (int i = 0; i < mNumberOfSuggestions; i++) {
highlightTextDifferences(mSuggestionInfos[i], spanUnionStart, spanUnionEnd);
}
// Make "Add to dictionary" item visible if there is a span with the misspelled flag
int addToDictionaryButtonVisibility = View.GONE;
if (mMisspelledSpanInfo.mSuggestionSpan != null) {
if (mMisspelledSpanInfo.mSpanStart >= 0
&& mMisspelledSpanInfo.mSpanEnd > mMisspelledSpanInfo.mSpanStart) {
addToDictionaryButtonVisibility = View.VISIBLE;
}
}
mAddToDictionaryButton.setVisibility(addToDictionaryButtonVisibility);
if (mSuggestionRangeSpan == null) mSuggestionRangeSpan = new SuggestionRangeSpan();
final int underlineColor;
if (mNumberOfSuggestions != 0) {
underlineColor =
mSuggestionInfos[0].mSuggestionSpanInfo.mSuggestionSpan.getUnderlineColor();
} else {
underlineColor = mMisspelledSpanInfo.mSuggestionSpan.getUnderlineColor();
}
if (underlineColor == 0) {
// Fallback on the default highlight color when the first span does not provide one
mSuggestionRangeSpan.setBackgroundColor(mTextView.mHighlightColor);
} else {
final float BACKGROUND_TRANSPARENCY = 0.4f;
final int newAlpha = (int) (Color.alpha(underlineColor) * BACKGROUND_TRANSPARENCY);
mSuggestionRangeSpan.setBackgroundColor(
(underlineColor & 0x00FFFFFF) + (newAlpha << 24));
}
boolean sendAccessibilityEvent = mTextView.isVisibleToAccessibility();
CharSequence beforeText = sendAccessibilityEvent
? new SpannedString(spannable, true) : null;
spannable.setSpan(mSuggestionRangeSpan, spanUnionStart, spanUnionEnd,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
if (sendAccessibilityEvent) {
mTextView.sendAccessibilityEventTypeViewTextChanged(
beforeText, spanUnionStart, spanUnionEnd);
}
mSuggestionsAdapter.notifyDataSetChanged();
return true;
}
private void highlightTextDifferences(SuggestionInfo suggestionInfo, int unionStart,
int unionEnd) {
final Spannable text = (Spannable) mTextView.getText();
final int spanStart = suggestionInfo.mSuggestionSpanInfo.mSpanStart;
final int spanEnd = suggestionInfo.mSuggestionSpanInfo.mSpanEnd;
// Adjust the start/end of the suggestion span
suggestionInfo.mSuggestionStart = spanStart - unionStart;
suggestionInfo.mSuggestionEnd = suggestionInfo.mSuggestionStart
+ suggestionInfo.mText.length();
suggestionInfo.mText.setSpan(mHighlightSpan, 0, suggestionInfo.mText.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Add the text before and after the span.
final String textAsString = text.toString();
suggestionInfo.mText.insert(0, textAsString.substring(unionStart, spanStart));
suggestionInfo.mText.append(textAsString.substring(spanEnd, unionEnd));
}
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
SuggestionInfo suggestionInfo = mSuggestionInfos[position];
replaceWithSuggestion(suggestionInfo);
hideWithCleanUp();
}
}
/**
* Helper class for UI component (e.g. ActionMode and ContextMenu) with TextClassification.
* @hide
*/
@VisibleForTesting
public class AssistantCallbackHelper {
private final Map