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
17package com.android.tv.settings.name;
18
19import android.content.Context;
20import android.os.Build;
21import android.os.Bundle;
22import android.support.annotation.NonNull;
23import android.support.v17.leanback.app.GuidedStepFragment;
24import android.support.v17.leanback.widget.GuidanceStylist;
25import android.support.v17.leanback.widget.GuidedAction;
26import android.support.v17.leanback.widget.GuidedActionsStylist;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30
31import com.android.tv.settings.R;
32import com.android.tv.settings.util.GuidedActionsAlignUtil;
33
34import java.util.List;
35
36/**
37 * Fragment responsible for showing the device name summary.
38 */
39public class DeviceNameSummaryFragment extends GuidedStepFragment {
40
41    public static DeviceNameSummaryFragment newInstance() {
42        return new DeviceNameSummaryFragment();
43    }
44
45    @Override
46    public GuidanceStylist onCreateGuidanceStylist() {
47        return GuidedActionsAlignUtil.createGuidanceStylist();
48    }
49
50    @Override
51    public GuidedActionsStylist onCreateActionsStylist() {
52        return GuidedActionsAlignUtil.createGuidedActionsStylist();
53    }
54
55    @Override
56    public View onCreateView(LayoutInflater inflater, ViewGroup container,
57            Bundle savedInstanceState) {
58        View view = super.onCreateView(inflater, container, savedInstanceState);
59        return GuidedActionsAlignUtil.createView(view, this);
60    }
61
62    @NonNull
63    @Override
64    public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
65        return new GuidanceStylist.Guidance(
66                getString(R.string.device_rename_title, Build.MODEL),
67                getString(R.string.device_rename_description, Build.MODEL,
68                        DeviceManager.getDeviceName(getActivity())),
69                null,
70                null);
71    }
72
73    @Override
74    public void onCreateActions(@NonNull List<GuidedAction> actions, Bundle savedInstanceState) {
75        final Context context = getActivity();
76        actions.add(new GuidedAction.Builder(context)
77                .id(GuidedAction.ACTION_ID_CONTINUE)
78                .title(R.string.change_setting)
79                .build());
80        actions.add(new GuidedAction.Builder(context)
81                .id(GuidedAction.ACTION_ID_CANCEL)
82                .title(R.string.keep_settings)
83                .build());
84    }
85
86    @Override
87    public void onGuidedActionClicked(GuidedAction action) {
88        final long actionId = action.getId();
89        if (actionId == GuidedAction.ACTION_ID_CONTINUE) {
90            GuidedStepFragment.add(getFragmentManager(), DeviceNameSetFragment.newInstance());
91        } else if (actionId == GuidedAction.ACTION_ID_CANCEL) {
92            getActivity().finish();
93        } else {
94            throw new IllegalStateException("Unknown action");
95        }
96    }
97}
98