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.os.Build;
20 import android.os.Bundle;
21 import android.support.annotation.NonNull;
22 import android.support.v17.leanback.app.GuidedStepFragment;
23 import android.support.v17.leanback.widget.GuidanceStylist;
24 import android.support.v17.leanback.widget.GuidedAction;
25 
26 import com.android.tv.settings.R;
27 
28 import java.util.List;
29 
30 public class DeviceNameSetFragment extends GuidedStepFragment {
31 
newInstance()32     public static DeviceNameSetFragment newInstance() {
33         return new DeviceNameSetFragment();
34     }
35 
36     @NonNull
37     @Override
onCreateGuidance(Bundle savedInstanceState)38     public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
39         return new GuidanceStylist.Guidance(
40                 getString(R.string.select_device_name_title, Build.MODEL),
41                 getString(R.string.select_device_name_description, Build.MODEL),
42                 null,
43                 null);
44     }
45 
46     @Override
onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)47     public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
48         final String[] options = getResources().getStringArray(R.array.rooms);
49         final int length = options.length;
50         for (int i = 0; i < length; i++) {
51             actions.add(new GuidedAction.Builder()
52                     .title(options[i])
53                     .id(i)
54                     .build());
55         }
56         actions.add(new GuidedAction.Builder()
57                 .title(getString(R.string.custom_room))
58                 .id(options.length)
59                 .build());
60         super.onCreateActions(actions, savedInstanceState);
61     }
62 
63     @Override
onGuidedActionClicked(GuidedAction action)64     public void onGuidedActionClicked(GuidedAction action) {
65         final long id = action.getId();
66         final String[] options = getResources().getStringArray(R.array.rooms);
67         if (id < 0 || id > options.length) {
68             throw new IllegalStateException("Unknown action ID");
69         } else if (id < options.length) {
70             DeviceManager.setDeviceName(getActivity(), options[(int)id]);
71             getActivity().finish();
72         } else if (id == options.length) {
73             GuidedStepFragment.add(getFragmentManager(), DeviceNameSetCustomFragment.newInstance());
74         }
75     }
76 }
77