Home | History | Annotate | Download | only in inputmethod
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.inputmethod;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.os.Bundle;
     23 import android.provider.Settings;
     24 import android.text.TextUtils;
     25 import android.view.View;
     26 import android.view.inputmethod.InputMethodInfo;
     27 import android.view.inputmethod.InputMethodManager;
     28 import android.widget.Button;
     29 import android.widget.LinearLayout;
     30 
     31 import java.util.ArrayList;
     32 import java.util.List;
     33 
     34 import static android.widget.LinearLayout.VERTICAL;
     35 
     36 /**
     37  * Demonstrates how to show the input method subtype enabler without relying on
     38  * {@link InputMethodManager#showInputMethodAndSubtypeEnabler(String)}, which is highly likely to be
     39  * broken.
     40  */
     41 public class ShowInputMethodAndSubtypeEnabler extends Activity {
     42 
     43     @Override
     44     protected void onCreate(Bundle savedInstanceState) {
     45         super.onCreate(savedInstanceState);
     46 
     47         final LinearLayout layout = new LinearLayout(this);
     48         layout.setOrientation(VERTICAL);
     49 
     50         {
     51             final Button button = new Button(this);
     52             button.setText("Show (All IMEs)");
     53             button.setOnClickListener(new View.OnClickListener() {
     54                 @Override
     55                 public void onClick(View v) {
     56                     showInputMethodAndSubtypeEnabler(ShowInputMethodAndSubtypeEnabler.this, null);
     57                 }
     58             });
     59             layout.addView(button);
     60         }
     61 
     62         for (InputMethodInfo imi : getEnabledInputMethodsThatHaveMultipleSubtypes()) {
     63             final Button button = new Button(this);
     64             final String id = imi.getId();
     65             button.setText("Show (" + id + ")");
     66             button.setOnClickListener(new View.OnClickListener() {
     67                 @Override
     68                 public void onClick(View v) {
     69                     showInputMethodAndSubtypeEnabler(ShowInputMethodAndSubtypeEnabler.this, id);
     70                 }
     71             });
     72             layout.addView(button);
     73         }
     74         setContentView(layout);
     75     }
     76 
     77 //BEGIN_INCLUDE(showInputMethodAndSubtypeEnabler)
     78     static void showInputMethodAndSubtypeEnabler(Context context, String inputMethodId) {
     79         final Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SUBTYPE_SETTINGS);
     80         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
     81                 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
     82                 | Intent.FLAG_ACTIVITY_CLEAR_TOP);
     83         if (!TextUtils.isEmpty(inputMethodId)) {
     84             intent.putExtra(Settings.EXTRA_INPUT_METHOD_ID, inputMethodId);
     85         }
     86         context.startActivity(intent, null);
     87     }
     88 //END_INCLUDE(showInputMethodAndSubtypeEnabler)
     89 
     90     private List<InputMethodInfo> getEnabledInputMethodsThatHaveMultipleSubtypes() {
     91         final InputMethodManager imm =
     92                 (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
     93         final List<InputMethodInfo> result = new ArrayList<>();
     94         if (imm == null) {
     95             return result;
     96         }
     97         for (InputMethodInfo imi : imm.getEnabledInputMethodList()) {
     98             if (imi.getSubtypeCount() > 1) {
     99                 result.add(imi);
    100             }
    101         }
    102         return result;
    103     }
    104 }
    105