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.fragment.app.FragmentTransaction; 30 import androidx.leanback.app.GuidedStepSupportFragment; 31 import androidx.leanback.widget.GuidanceStylist; 32 import androidx.leanback.widget.GuidedAction; 33 import androidx.leanback.widget.GuidedActionsStylist; 34 35 import com.android.tv.settings.R; 36 import com.android.tv.settings.name.setup.DeviceNameFlowStartActivity; 37 import com.android.tv.settings.util.GuidedActionsAlignUtil; 38 39 import java.util.List; 40 41 /** 42 * Fragment responsible for adding new device name. 43 */ 44 public class DeviceNameSetCustomFragment extends GuidedStepSupportFragment { 45 46 private GuidedAction mEditAction; 47 newInstance()48 public static DeviceNameSetCustomFragment newInstance() { 49 return new DeviceNameSetCustomFragment(); 50 } 51 52 @Override onCreateGuidanceStylist()53 public GuidanceStylist onCreateGuidanceStylist() { 54 return GuidedActionsAlignUtil.createGuidanceStylist(); 55 } 56 57 @Override onCreateActionsStylist()58 public GuidedActionsStylist onCreateActionsStylist() { 59 return new GuidedActionsStylist() { 60 @Override 61 public int onProvideItemLayoutId() { 62 return R.layout.guided_step_input_action; 63 } 64 }; 65 } 66 67 @Override 68 public View onCreateView(LayoutInflater inflater, ViewGroup container, 69 Bundle savedInstanceState) { 70 View view = super.onCreateView(inflater, container, savedInstanceState); 71 return GuidedActionsAlignUtil.createView(view, this); 72 } 73 74 @NonNull 75 @Override 76 public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 77 return new GuidanceStylist.Guidance( 78 getString(R.string.select_device_name_title, Build.MODEL), 79 getString(R.string.select_device_name_description), 80 null, 81 null); 82 } 83 84 @Override 85 public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) { 86 mEditAction = new GuidedAction.Builder(getContext()) 87 .title("") 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 // Overriding this method removes the unpreferable enter transition animation of this fragment. 100 @Override 101 protected void onProvideFragmentTransitions() { 102 setEnterTransition(null); 103 } 104 105 @Override 106 public long onGuidedActionEditedAndProceed(GuidedAction action) { 107 final CharSequence name = action.getTitle(); 108 if (TextUtils.isGraphic(name)) { 109 DeviceManager.setDeviceName(getActivity(), name.toString()); 110 getActivity().setResult(Activity.RESULT_OK); 111 112 // Set the flag for the appropriate exit animation for setup. 113 if (getActivity() instanceof DeviceNameFlowStartActivity) { 114 ((DeviceNameFlowStartActivity) getActivity()).setResultOk(true); 115 } 116 DeviceNameSuggestionStatus.getInstance( 117 getActivity().getApplicationContext()).setFinished(); 118 getActivity().finish(); 119 return super.onGuidedActionEditedAndProceed(action); 120 } else { 121 popBackStackToGuidedStepSupportFragment( 122 DeviceNameSetCustomFragment.class, FragmentManager.POP_BACK_STACK_INCLUSIVE); 123 return GuidedAction.ACTION_ID_CANCEL; 124 } 125 } 126 127 @Override 128 protected void onAddSharedElementTransition( 129 FragmentTransaction ft, GuidedStepSupportFragment disappearing) { 130 // no-op 131 } 132 133 @Override 134 public void onGuidedActionEditCanceled(GuidedAction action) { 135 // We need to "pop to" current fragment with INCLUSIVE flag instead of popping to previous 136 // fragment because DeviceNameSetFragment was set to be root and not added on backstack. 137 popBackStackToGuidedStepSupportFragment( 138 DeviceNameSetCustomFragment.class, FragmentManager.POP_BACK_STACK_INCLUSIVE); 139 } 140 } 141