1 /*
2  * Copyright (C) 2015 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.name;
18 
19 import android.app.Activity;
20 import android.app.FragmentManager;
21 import android.os.Build;
22 import android.os.Bundle;
23 import android.text.TextUtils;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 
28 import androidx.annotation.NonNull;
29 import androidx.leanback.app.GuidedStepFragment;
30 import androidx.leanback.widget.GuidanceStylist;
31 import androidx.leanback.widget.GuidedAction;
32 import androidx.leanback.widget.GuidedActionsStylist;
33 
34 import com.android.tv.settings.R;
35 import com.android.tv.settings.name.setup.DeviceNameFlowStartActivity;
36 import com.android.tv.settings.util.GuidedActionsAlignUtil;
37 
38 import java.util.List;
39 
40 /**
41  * Fragment responsible for adding new device name.
42  */
43 public class DeviceNameSetCustomFragment extends GuidedStepFragment {
44 
45     private GuidedAction mEditAction;
46 
newInstance()47     public static DeviceNameSetCustomFragment newInstance() {
48         return new DeviceNameSetCustomFragment();
49     }
50 
51     @Override
onCreateGuidanceStylist()52     public GuidanceStylist onCreateGuidanceStylist() {
53         return GuidedActionsAlignUtil.createGuidanceStylist();
54     }
55 
56     @Override
onCreateActionsStylist()57     public GuidedActionsStylist onCreateActionsStylist() {
58         return GuidedActionsAlignUtil.createNoBackgroundGuidedActionsStylist();
59     }
60 
61     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)62     public View onCreateView(LayoutInflater inflater, ViewGroup container,
63             Bundle savedInstanceState) {
64         View view = super.onCreateView(inflater, container, savedInstanceState);
65         return GuidedActionsAlignUtil.createView(view, this);
66     }
67 
68     @NonNull
69     @Override
onCreateGuidance(Bundle savedInstanceState)70     public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
71         return new GuidanceStylist.Guidance(
72                 getString(R.string.select_device_name_title, Build.MODEL),
73                 getString(R.string.select_device_name_description),
74                 null,
75                 null);
76     }
77 
78     @Override
onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)79     public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
80         mEditAction = new GuidedAction.Builder(getContext())
81                 .title("")
82                 .editable(true)
83                 .build();
84         actions.add(mEditAction);
85     }
86 
87     @Override
onResume()88     public void onResume() {
89         super.onResume();
90         openInEditMode(mEditAction);
91     }
92 
93     // Overriding this method removes the unpreferable enter transition animation of this fragment.
94     @Override
onProvideFragmentTransitions()95     protected void onProvideFragmentTransitions() {
96         setEnterTransition(null);
97     }
98 
99     @Override
onGuidedActionEditedAndProceed(GuidedAction action)100     public long onGuidedActionEditedAndProceed(GuidedAction action) {
101         final CharSequence name = action.getTitle();
102         if (TextUtils.isGraphic(name)) {
103             DeviceManager.setDeviceName(getActivity(), name.toString());
104             getActivity().setResult(Activity.RESULT_OK);
105 
106             // Set the flag for the appropriate exit animation for setup.
107             if (getActivity() instanceof DeviceNameFlowStartActivity) {
108                 ((DeviceNameFlowStartActivity) getActivity()).setResultOk(true);
109             }
110             DeviceNameSuggestionStatus.getInstance(
111                     getActivity().getApplicationContext()).setFinished();
112             getActivity().finish();
113             return super.onGuidedActionEditedAndProceed(action);
114         } else {
115             popBackStackToGuidedStepFragment(
116                     DeviceNameSetCustomFragment.class, FragmentManager.POP_BACK_STACK_INCLUSIVE);
117             return GuidedAction.ACTION_ID_CANCEL;
118         }
119     }
120 
121     @Override
onGuidedActionEditCanceled(GuidedAction action)122     public void onGuidedActionEditCanceled(GuidedAction action) {
123         // We need to "pop to" current fragment with INCLUSIVE flag instead of popping to previous
124         // fragment because DeviceNameSetFragment was set to be root and not added on backstack.
125         popBackStackToGuidedStepFragment(
126                 DeviceNameSetCustomFragment.class, FragmentManager.POP_BACK_STACK_INCLUSIVE);
127     }
128 }
129