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.os.Build; 21 import android.os.Bundle; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.view.ViewGroup; 25 26 import androidx.annotation.NonNull; 27 import androidx.leanback.app.GuidedStepSupportFragment; 28 import androidx.leanback.widget.GuidanceStylist; 29 import androidx.leanback.widget.GuidedAction; 30 import androidx.leanback.widget.GuidedActionsStylist; 31 32 import com.android.tv.settings.R; 33 import com.android.tv.settings.name.setup.DeviceNameFlowStartActivity; 34 import com.android.tv.settings.util.GuidedActionsAlignUtil; 35 36 import java.util.ArrayList; 37 import java.util.Arrays; 38 import java.util.List; 39 40 /** 41 * Fragment responsible for showing the device names list. 42 */ 43 public class DeviceNameSetFragment extends GuidedStepSupportFragment { 44 private ArrayList<String> mDeviceNames = new ArrayList<>(); 45 newInstance()46 public static DeviceNameSetFragment newInstance() { 47 return new DeviceNameSetFragment(); 48 } 49 50 @Override onCreateGuidanceStylist()51 public GuidanceStylist onCreateGuidanceStylist() { 52 return GuidedActionsAlignUtil.createGuidanceStylist(); 53 } 54 55 @Override onCreateActionsStylist()56 public GuidedActionsStylist onCreateActionsStylist() { 57 return GuidedActionsAlignUtil.createGuidedActionsStylist(); 58 } 59 60 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)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 onCreateGuidance(Bundle savedInstanceState)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), 73 null, 74 null); 75 } 76 77 @Override onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)78 public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) { 79 mDeviceNames.add(Build.MODEL); 80 mDeviceNames.addAll(Arrays.asList(getResources().getStringArray(R.array.rooms))); 81 // The strings added above are static names that should always be shown. 82 String currentDeviceName = DeviceManager.getDeviceName(getActivity()); 83 if (currentDeviceName == null) { 84 currentDeviceName = Build.MODEL; 85 } 86 // Ideally we don't want to have identical entries. (e.g., if a device was named 87 // "Android TV", then "Android TV" (from static names) will be pre-selected/highlighted 88 // instead of being added to top of the list. 89 // However, since "Enter Custom Name..." is not considered as an static name, if someone 90 // name his/her device to be "Enter Custom Name..." (same to the title of action to 91 // customize device name), this name will still show at top of list just like any other 92 // "normal" names, co-existing with the action button at bottom. 93 if (mDeviceNames.indexOf(currentDeviceName) == -1) { 94 mDeviceNames.add(0, currentDeviceName); 95 } 96 97 final int length = mDeviceNames.size(); 98 for (int i = 0; i < length; i++) { 99 actions.add(new GuidedAction.Builder() 100 .title(mDeviceNames.get(i)) 101 .id(i) 102 .multilineDescription(true) 103 .build()); 104 } 105 actions.add(new GuidedAction.Builder() 106 .title(getString(R.string.custom_room)) 107 .id(mDeviceNames.size()) 108 .multilineDescription(true) 109 .build()); 110 super.onCreateActions(actions, savedInstanceState); 111 } 112 113 @Override onResume()114 public void onResume() { 115 super.onResume(); 116 int currentNamePosition = mDeviceNames.indexOf(DeviceManager.getDeviceName(getActivity())); 117 if (currentNamePosition != -1) { 118 setSelectedActionPosition(currentNamePosition); 119 } 120 } 121 122 @Override onGuidedActionClicked(GuidedAction action)123 public void onGuidedActionClicked(GuidedAction action) { 124 final long id = action.getId(); 125 if (id < 0 || id > mDeviceNames.size()) { 126 throw new IllegalStateException("Unknown action ID"); 127 } else if (id < mDeviceNames.size()) { 128 DeviceManager.setDeviceName(getActivity(), mDeviceNames.get((int) id)); 129 130 // Set the flag for the appropriate exit animation for setup. 131 if (getActivity() instanceof DeviceNameFlowStartActivity) { 132 ((DeviceNameFlowStartActivity) getActivity()).setResultOk(true); 133 } 134 DeviceNameSuggestionStatus.getInstance( 135 getActivity().getApplicationContext()).setFinished(); 136 getActivity().setResult(Activity.RESULT_OK); 137 getActivity().finish(); 138 } else if (id == mDeviceNames.size()) { 139 GuidedStepSupportFragment.add( 140 getFragmentManager(), DeviceNameSetCustomFragment.newInstance()); 141 } 142 } 143 144 // Overriding this method removes the unpreferable exit transition animation of this fragment, 145 // which is currently only applied before showing DeviceNameSetCustomFragment. 146 // Be sure not to remove this method or leave its body empty as it is also used on Settings 147 // (not during Setup) and we need its default enter transition animation in that case. 148 @Override onProvideFragmentTransitions()149 protected void onProvideFragmentTransitions() { 150 super.onProvideFragmentTransitions(); 151 setExitTransition(null); 152 } 153 } 154