1 /* 2 * Copyright (C) 2016 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.system; 18 19 import android.os.Bundle; 20 21 import androidx.annotation.Keep; 22 import androidx.annotation.NonNull; 23 import androidx.leanback.app.GuidedStepSupportFragment; 24 import androidx.leanback.widget.GuidanceStylist; 25 import androidx.leanback.widget.GuidedAction; 26 import androidx.leanback.widget.GuidedActionsStylist; 27 28 import com.android.tv.settings.R; 29 import com.android.tv.settings.util.AccessibilityHelper; 30 import com.android.tv.settings.widget.SettingsGuidedStepFragment; 31 32 import java.util.List; 33 34 @Keep 35 public class InputCustomNameFragment extends SettingsGuidedStepFragment { 36 37 private static final String ARG_CURRENT_NAME = "current_name"; 38 private static final String ARG_DEFAULT_NAME = "default_name"; 39 40 private CharSequence mName; 41 private GuidedAction mEditAction; 42 prepareArgs(@onNull Bundle args, CharSequence defaultName, CharSequence currentName)43 public static void prepareArgs(@NonNull Bundle args, CharSequence defaultName, 44 CharSequence currentName) { 45 args.putCharSequence(ARG_DEFAULT_NAME, defaultName); 46 args.putCharSequence(ARG_CURRENT_NAME, currentName); 47 } 48 49 @Override onCreate(Bundle savedInstanceState)50 public void onCreate(Bundle savedInstanceState) { 51 mName = getArguments().getCharSequence(ARG_CURRENT_NAME); 52 53 super.onCreate(savedInstanceState); 54 } 55 56 @NonNull 57 @Override onCreateGuidance(Bundle savedInstanceState)58 public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 59 return new GuidanceStylist.Guidance( 60 getString(R.string.inputs_custom_name), 61 getString(R.string.inputs_custom_name_description_fmt, 62 getArguments().getCharSequence(ARG_DEFAULT_NAME)), 63 null, 64 getContext().getDrawable(R.drawable.ic_input_132dp) 65 ); 66 } 67 68 @Override onCreateActionsStylist()69 public GuidedActionsStylist onCreateActionsStylist() { 70 return new GuidedActionsStylist() { 71 @Override 72 public int onProvideItemLayoutId() { 73 return R.layout.guided_step_input_action; 74 } 75 @Override 76 protected void setupImeOptions(GuidedActionsStylist.ViewHolder vh, 77 GuidedAction action) { 78 // keep defaults 79 } 80 }; 81 } 82 83 @Override 84 public void onCreateActions(@NonNull List<GuidedAction> actions, 85 Bundle savedInstanceState) { 86 mEditAction = new GuidedAction.Builder(getContext()) 87 .title(mName) 88 .editable(true) 89 .build(); 90 actions.add(mEditAction); 91 } 92 93 @Override 94 public void onResume() { 95 super.onResume(); 96 openInEditMode(mEditAction); 97 } 98 99 @Override 100 public long onGuidedActionEditedAndProceed(GuidedAction action) { 101 mName = action.getTitle(); 102 ((Callback) getTargetFragment()).onSetCustomName(mName); 103 getFragmentManager().popBackStack(); 104 return GuidedAction.ACTION_ID_OK; 105 } 106 107 @Override 108 public void onGuidedActionEditCanceled(GuidedAction action) { 109 // We need to ensure the IME is closed before navigating back. See b/233207859. 110 AccessibilityHelper.dismissKeyboard(getActivity(), getView()); 111 getFragmentManager().popBackStack(); 112 } 113 114 public interface Callback { 115 void onSetCustomName(CharSequence name); 116 } 117 } 118