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 
27 import com.android.tv.settings.R;
28 
29 import java.util.List;
30 
31 @Keep
32 public class InputCustomNameFragment extends GuidedStepSupportFragment {
33 
34     private static final String ARG_CURRENT_NAME = "current_name";
35     private static final String ARG_DEFAULT_NAME = "default_name";
36 
37     private CharSequence mName;
38 
prepareArgs(@onNull Bundle args, CharSequence defaultName, CharSequence currentName)39     public static void prepareArgs(@NonNull Bundle args, CharSequence defaultName,
40             CharSequence currentName) {
41         args.putCharSequence(ARG_DEFAULT_NAME, defaultName);
42         args.putCharSequence(ARG_CURRENT_NAME, currentName);
43     }
44 
45     @Override
onCreate(Bundle savedInstanceState)46     public void onCreate(Bundle savedInstanceState) {
47         mName = getArguments().getCharSequence(ARG_CURRENT_NAME);
48 
49         super.onCreate(savedInstanceState);
50     }
51 
52     @NonNull
53     @Override
onCreateGuidance(Bundle savedInstanceState)54     public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
55         return new GuidanceStylist.Guidance(
56                 getString(R.string.inputs_custom_name),
57                 getString(R.string.inputs_custom_name_description_fmt,
58                         getArguments().getCharSequence(ARG_DEFAULT_NAME)),
59                 null,
60                 getContext().getDrawable(R.drawable.ic_input_132dp)
61         );
62     }
63 
64     @Override
onCreateButtonActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)65     public void onCreateButtonActions(@NonNull List<GuidedAction> actions,
66             Bundle savedInstanceState) {
67         actions.add(new GuidedAction.Builder(getContext())
68                 .clickAction(GuidedAction.ACTION_ID_OK)
69                 .build());
70         actions.add(new GuidedAction.Builder(getContext())
71                 .clickAction(GuidedAction.ACTION_ID_CANCEL)
72                 .build());
73     }
74 
75     @Override
onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)76     public void onCreateActions(@NonNull List<GuidedAction> actions,
77             Bundle savedInstanceState) {
78         actions.add(new GuidedAction.Builder(getContext())
79                 .title(mName)
80                 .editable(true)
81                 .build());
82     }
83 
84     @Override
onGuidedActionClicked(GuidedAction action)85     public void onGuidedActionClicked(GuidedAction action) {
86         if (action.getId() == GuidedAction.ACTION_ID_OK) {
87             ((Callback) getTargetFragment()).onSetCustomName(mName);
88             getFragmentManager().popBackStack();
89         } else if (action.getId() == GuidedAction.ACTION_ID_CANCEL) {
90             getFragmentManager().popBackStack();
91         }
92     }
93 
94     @Override
onGuidedActionEditedAndProceed(GuidedAction action)95     public long onGuidedActionEditedAndProceed(GuidedAction action) {
96         mName = action.getTitle();
97         return GuidedAction.ACTION_ID_OK;
98     }
99 
100     public interface Callback {
onSetCustomName(CharSequence name)101         void onSetCustomName(CharSequence name);
102     }
103 }
104