1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.inputmethod.leanback;
18 
19 import android.inputmethodservice.InputMethodService;
20 import android.text.TextUtils;
21 import android.util.Log;
22 import android.view.inputmethod.CompletionInfo;
23 import android.view.inputmethod.EditorInfo;
24 import android.view.inputmethod.InputConnection;
25 
26 import java.util.ArrayList;
27 
28 /**
29  * This class is used to get suggestions from LatinIme's suggestion engine based
30  * on the current composing word
31  */
32 public class LeanbackSuggestionsFactory {
33 
34     private static final String TAG = "LbSuggestionsFactory";
35     private static final boolean DEBUG = Log.isLoggable(TAG, Log.VERBOSE);
36 
37     // mode for suggestions
38     private static final int MODE_DEFAULT = 0;
39     private static final int MODE_DOMAIN = 1;
40     private static final int MODE_AUTO_COMPLETE = 2;
41 
42     private InputMethodService mContext;
43     private int mNumSuggestions;
44     private int mMode;
45 
46     // current active suggestions
47     private final ArrayList<String> mSuggestions = new ArrayList<String>();
48 
LeanbackSuggestionsFactory(InputMethodService context, int maxSuggestions)49     public LeanbackSuggestionsFactory(InputMethodService context, int maxSuggestions) {
50         mContext = context;
51         mNumSuggestions = maxSuggestions;
52     }
53 
onStartInput(EditorInfo attribute)54     public void onStartInput(EditorInfo attribute) {
55         mMode = MODE_DEFAULT;
56 
57         if ((attribute.inputType & EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE) != 0) {
58             mMode = MODE_AUTO_COMPLETE;
59         }
60 
61         switch (LeanbackUtils.getInputTypeClass(attribute)) {
62             case EditorInfo.TYPE_CLASS_TEXT:
63                 switch (LeanbackUtils.getInputTypeVariation(attribute)) {
64                     case EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS:
65                     case EditorInfo.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS:
66                         mMode = MODE_DOMAIN;
67                         break;
68                 }
69                 break;
70         }
71     }
72 
73     /**
74      * call this method in {@link InputMethodService#onDisplayCompletions} to
75      * insert completions provided by the app in front of the dictionary
76      * suggestions
77      */
onDisplayCompletions(CompletionInfo[] completions)78     public void onDisplayCompletions(CompletionInfo[] completions) {
79         createSuggestions();
80 
81         // insert completions to the front of suggestions
82         final int totalCompletions = completions == null ? 0 : completions.length;
83         for (int i = 0; i < totalCompletions && mSuggestions.size() < mNumSuggestions; i++) {
84             if (TextUtils.isEmpty(completions[i].getText())) {
85                 break;
86             }
87             mSuggestions.add(i, completions[i].getText().toString());
88         }
89 
90         if (Log.isLoggable(TAG, Log.VERBOSE)) {
91             for (int i = 0; i < mSuggestions.size(); i++) {
92                 Log.d(TAG, "completion " + i + ": " + mSuggestions.get(i));
93             }
94         }
95     }
96 
shouldSuggestionsAmend()97     public boolean shouldSuggestionsAmend() {
98         return (mMode == MODE_DOMAIN);
99     }
100 
getSuggestions()101     public ArrayList<String> getSuggestions() {
102         return mSuggestions;
103     }
104 
clearSuggestions()105     public void clearSuggestions() {
106         mSuggestions.clear();
107     }
108 
createSuggestions()109     public void createSuggestions() {
110         clearSuggestions();
111 
112         if (mMode == MODE_DOMAIN) {
113             String[] commonDomains =
114                     mContext.getResources().getStringArray(R.array.common_domains);
115             for (String domain : commonDomains) {
116                 mSuggestions.add(domain);
117             }
118         }
119     }
120 }
121