1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.launcher3.folder; 17 18 import android.content.Context; 19 import android.util.AttributeSet; 20 import android.util.Log; 21 import android.view.View; 22 import android.view.inputmethod.CompletionInfo; 23 import android.view.inputmethod.EditorInfo; 24 import android.view.inputmethod.InputConnection; 25 import android.view.inputmethod.InputConnectionWrapper; 26 import android.view.inputmethod.InputMethodManager; 27 28 import com.android.launcher3.ExtendedEditText; 29 30 import java.util.List; 31 32 /** 33 * Handles additional edit text functionality to better support folder name suggestion. 34 * First, makes suggestion to the InputMethodManager via {@link #displayCompletions(List)} 35 * Second, intercepts whether user accepted the suggestion or manually edited their 36 * folder names. 37 */ 38 public class FolderNameEditText extends ExtendedEditText { 39 private static final String TAG = "FolderNameEditText"; 40 private static final boolean DEBUG = false; 41 42 private boolean mEnteredCompose = false; 43 FolderNameEditText(Context context)44 public FolderNameEditText(Context context) { 45 super(context); 46 } 47 FolderNameEditText(Context context, AttributeSet attrs)48 public FolderNameEditText(Context context, AttributeSet attrs) { 49 // ctor chaining breaks the touch handling 50 super(context, attrs); 51 } 52 FolderNameEditText(Context context, AttributeSet attrs, int defStyleAttr)53 public FolderNameEditText(Context context, AttributeSet attrs, int defStyleAttr) { 54 super(context, attrs, defStyleAttr); 55 } 56 57 @Override onCreateInputConnection(EditorInfo outAttrs)58 public InputConnection onCreateInputConnection(EditorInfo outAttrs) { 59 InputConnection con = super.onCreateInputConnection(outAttrs); 60 FolderNameEditTextInputConnection connectionWrapper = 61 new FolderNameEditTextInputConnection(con, true); 62 return connectionWrapper; 63 } 64 65 /** 66 * Send strings in @param suggestList to the IME to show up as suggestions. 67 */ displayCompletions(List<String> suggestList)68 public void displayCompletions(List<String> suggestList) { 69 int cnt = Math.min(suggestList.size(), FolderNameProvider.SUGGEST_MAX); 70 CompletionInfo[] cInfo = new CompletionInfo[cnt]; 71 for (int i = 0; i < cnt; i++) { 72 cInfo[i] = new CompletionInfo(i, i, suggestList.get(i)); 73 } 74 // post it to future frame so that onSelectionChanged, onFocusChanged, all other 75 // TextView flag change and IME animation has settled. Ideally, there should be IMM 76 // callback to notify when the IME animation and state handling is finished. 77 postDelayed(() -> getContext().getSystemService(InputMethodManager.class) 78 .displayCompletions(this, cInfo), 40 /* 2~3 frame delay */); 79 } 80 81 /** 82 * Within 's', the 'count' characters beginning at 'start' have just replaced 83 * old text 'before' 84 */ 85 @Override onTextChanged(CharSequence s, int start, int before, int count)86 public void onTextChanged(CharSequence s, int start, int before, int count) { 87 String reason = "unknown"; 88 if (start == 0 && count == 0 && before > 0) { 89 reason = "suggestion was rejected"; 90 mEnteredCompose = true; 91 } 92 if (DEBUG) { 93 Log.d(TAG, "onTextChanged " + start + "," + before + "," + count 94 + ", " + reason); 95 } 96 } 97 98 @Override onCommitCompletion(CompletionInfo text)99 public void onCommitCompletion(CompletionInfo text) { 100 setText(text.getText()); 101 setSelection(text.getText().length()); 102 mEnteredCompose = false; 103 } 104 setEnteredCompose(boolean value)105 protected void setEnteredCompose(boolean value) { 106 mEnteredCompose = value; 107 } 108 109 private class FolderNameEditTextInputConnection extends InputConnectionWrapper { 110 FolderNameEditTextInputConnection(InputConnection target, boolean mutable)111 FolderNameEditTextInputConnection(InputConnection target, boolean mutable) { 112 super(target, mutable); 113 } 114 115 @Override setComposingText(CharSequence cs, int newCursorPos)116 public boolean setComposingText(CharSequence cs, int newCursorPos) { 117 mEnteredCompose = true; 118 return super.setComposingText(cs, newCursorPos); 119 } 120 } 121 122 @Override reset()123 public void reset() { 124 super.reset(); 125 if (isFocused()) { 126 View nextFocus = focusSearch(View.FOCUS_DOWN); 127 if (nextFocus != null) { 128 nextFocus.requestFocus(); 129 } 130 } 131 hideKeyboard(); 132 } 133 } 134