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