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