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