Home | History | Annotate | Download | only in inputmethod
      1 /*
      2  * Copyright (C) 2017 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.tv.settings.inputmethod;
     18 
     19 import android.content.Intent;
     20 import android.os.Bundle;
     21 import android.support.v17.preference.LeanbackPreferenceFragment;
     22 import android.support.v7.preference.PreferenceScreen;
     23 import android.text.TextUtils;
     24 
     25 import com.android.settingslib.inputmethod.InputMethodAndSubtypeEnablerManager;
     26 
     27 /**
     28  * Fragment for android.settings.INPUT_METHOD_SUBTYPE_SETTINGS
     29  */
     30 public class InputMethodAndSubtypeEnablerFragment extends LeanbackPreferenceFragment {
     31     private InputMethodAndSubtypeEnablerManager mManager;
     32 
     33     /**
     34      * @return new instance of {@link InputMethodAndSubtypeEnablerFragment}
     35      */
     36     public static InputMethodAndSubtypeEnablerFragment newInstance() {
     37         return new InputMethodAndSubtypeEnablerFragment();
     38     }
     39 
     40     @Override
     41     public void onCreate(final Bundle icicle) {
     42         super.onCreate(icicle);
     43     }
     44 
     45     @Override
     46     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
     47         final PreferenceScreen root =
     48                 getPreferenceManager().createPreferenceScreen(getPreferenceManager().getContext());
     49 
     50         // Input method id should be available from an Intent when this preference is launched as a
     51         // single Activity (see InputMethodAndSubtypeEnablerActivity). It should be available
     52         // from a preference argument when the preference is launched as a part of the other
     53         // Activity (like a right pane of 2-pane Settings app)
     54         final String targetImi = getStringExtraFromIntentOrArguments(
     55                 android.provider.Settings.EXTRA_INPUT_METHOD_ID);
     56 
     57         final String title = getStringExtraFromIntentOrArguments(Intent.EXTRA_TITLE);
     58 
     59         mManager = new InputMethodAndSubtypeEnablerManager(this);
     60         mManager.init(this, targetImi, root);
     61         root.setTitle(title);
     62         setPreferenceScreen(root);
     63 
     64     }
     65 
     66     private String getStringExtraFromIntentOrArguments(final String name) {
     67         final Intent intent = getActivity().getIntent();
     68         final String fromIntent = intent.getStringExtra(name);
     69         if (fromIntent != null) {
     70             return fromIntent;
     71         }
     72         final Bundle arguments = getArguments();
     73         return (arguments == null) ? null : arguments.getString(name);
     74     }
     75 
     76     @Override
     77     public void onActivityCreated(final Bundle icicle) {
     78         super.onActivityCreated(icicle);
     79         final String title = getStringExtraFromIntentOrArguments(Intent.EXTRA_TITLE);
     80         if (!TextUtils.isEmpty(title)) {
     81             getActivity().setTitle(title);
     82         }
     83     }
     84 
     85     @Override
     86     public void onResume() {
     87         super.onResume();
     88         mManager.refresh(getContext(), this);
     89     }
     90 
     91     @Override
     92     public void onPause() {
     93         super.onPause();
     94         mManager.save(getContext(), this);
     95     }
     96 }
     97