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.GuidedStepFragment;
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 GuidedStepFragment {
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                     .build());
103         }
104         actions.add(new GuidedAction.Builder()
105                 .title(getString(R.string.custom_room))
106                 .id(mDeviceNames.size())
107                 .build());
108         super.onCreateActions(actions, savedInstanceState);
109     }
110 
111     @Override
onResume()112     public void onResume() {
113         super.onResume();
114         int currentNamePosition = mDeviceNames.indexOf(DeviceManager.getDeviceName(getActivity()));
115         if (currentNamePosition != -1) {
116             setSelectedActionPosition(currentNamePosition);
117         }
118     }
119 
120     @Override
onGuidedActionClicked(GuidedAction action)121     public void onGuidedActionClicked(GuidedAction action) {
122         final long id = action.getId();
123         if (id < 0 || id > mDeviceNames.size()) {
124             throw new IllegalStateException("Unknown action ID");
125         } else if (id < mDeviceNames.size()) {
126             DeviceManager.setDeviceName(getActivity(), mDeviceNames.get((int) id));
127 
128             // Set the flag for the appropriate exit animation for setup.
129             if (getActivity() instanceof DeviceNameFlowStartActivity) {
130                 ((DeviceNameFlowStartActivity) getActivity()).setResultOk(true);
131             }
132             DeviceNameSuggestionStatus.getInstance(
133                     getActivity().getApplicationContext()).setFinished();
134             getActivity().setResult(Activity.RESULT_OK);
135             getActivity().finish();
136         } else if (id == mDeviceNames.size()) {
137             GuidedStepFragment.add(getFragmentManager(), DeviceNameSetCustomFragment.newInstance());
138         }
139     }
140 
141     // Overriding this method removes the unpreferable exit transition animation of this fragment,
142     // which is currently only applied before showing DeviceNameSetCustomFragment.
143     // Be sure not to remove this method or leave its body empty as it is also used on Settings
144     // (not during Setup) and we need its default enter transition animation in that case.
145     @Override
onProvideFragmentTransitions()146     protected void onProvideFragmentTransitions() {
147         super.onProvideFragmentTransitions();
148         setExitTransition(null);
149     }
150 }
151