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