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.inputmethod.CompletionInfo;
22 import android.view.inputmethod.EditorInfo;
23 import android.view.inputmethod.InputConnection;
24 import android.view.inputmethod.InputConnectionWrapper;
25 import android.view.inputmethod.InputMethodManager;
26 
27 import com.android.launcher3.ExtendedEditText;
28 
29 import java.util.List;
30 
31 /**
32  * Handles additional edit text functionality to better support folder name suggestion.
33  * First, makes suggestion to the InputMethodManager via {@link #displayCompletions(List)}
34  * Second, intercepts whether user accepted the suggestion or manually edited their
35  * folder names.
36  */
37 public class FolderNameEditText extends ExtendedEditText {
38     private static final String TAG = "FolderNameEditText";
39     private static final boolean DEBUG = false;
40 
41     private boolean mEnteredCompose = false;
42 
FolderNameEditText(Context context)43     public FolderNameEditText(Context context) {
44         super(context);
45     }
46 
FolderNameEditText(Context context, AttributeSet attrs)47     public FolderNameEditText(Context context, AttributeSet attrs) {
48         // ctor chaining breaks the touch handling
49         super(context, attrs);
50     }
51 
FolderNameEditText(Context context, AttributeSet attrs, int defStyleAttr)52     public FolderNameEditText(Context context, AttributeSet attrs, int defStyleAttr) {
53         super(context, attrs, defStyleAttr);
54     }
55 
56     @Override
onCreateInputConnection(EditorInfo outAttrs)57     public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
58         InputConnection con = super.onCreateInputConnection(outAttrs);
59         FolderNameEditTextInputConnection connectionWrapper =
60                 new FolderNameEditTextInputConnection(con, true);
61         return connectionWrapper;
62     }
63 
64     /**
65      * Send strings in @param suggestList to the IME to show up as suggestions.
66      */
displayCompletions(List<String> suggestList)67     public void displayCompletions(List<String> suggestList) {
68         int cnt = Math.min(suggestList.size(), FolderNameProvider.SUGGEST_MAX);
69         CompletionInfo[] cInfo = new CompletionInfo[cnt];
70         for (int i = 0; i < cnt; i++) {
71             cInfo[i] = new CompletionInfo(i, i, suggestList.get(i));
72         }
73         // post it to future frame so that onSelectionChanged, onFocusChanged, all other
74         // TextView flag change and IME animation has settled. Ideally, there should be IMM
75         // callback to notify when the IME animation and state handling is finished.
76         postDelayed(() -> getContext().getSystemService(InputMethodManager.class)
77                 .displayCompletions(this, cInfo), 40 /* 2~3 frame delay */);
78     }
79 
80     /**
81      * Within 's', the 'count' characters beginning at 'start' have just replaced
82      * old text 'before'
83      */
84     @Override
onTextChanged(CharSequence s, int start, int before, int count)85     public void onTextChanged(CharSequence s, int start, int before, int count) {
86         String reason = "unknown";
87         if (start == 0 && count == 0 && before > 0) {
88             reason = "suggestion was rejected";
89             mEnteredCompose = true;
90         }
91         if (DEBUG) {
92             Log.d(TAG, "onTextChanged " + start + "," + before + "," + count
93                     + ", " + reason);
94         }
95     }
96 
97     @Override
onCommitCompletion(CompletionInfo text)98     public void onCommitCompletion(CompletionInfo text) {
99         setText(text.getText());
100         setSelection(text.getText().length());
101         mEnteredCompose = false;
102     }
103 
setEnteredCompose(boolean value)104     protected void setEnteredCompose(boolean value) {
105         mEnteredCompose = value;
106     }
107 
108     private class FolderNameEditTextInputConnection extends InputConnectionWrapper {
109 
FolderNameEditTextInputConnection(InputConnection target, boolean mutable)110         FolderNameEditTextInputConnection(InputConnection target, boolean mutable) {
111             super(target, mutable);
112         }
113 
114         @Override
setComposingText(CharSequence cs, int newCursorPos)115         public boolean setComposingText(CharSequence cs, int newCursorPos) {
116             mEnteredCompose = true;
117             return super.setComposingText(cs, newCursorPos);
118         }
119     }
120 }
121