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.about;
18
19import android.content.Context;
20import android.os.AsyncTask;
21import android.os.Bundle;
22import android.os.PowerManager;
23import android.support.annotation.Keep;
24import android.support.annotation.NonNull;
25import android.support.v17.leanback.app.GuidedStepFragment;
26import android.support.v17.leanback.widget.GuidanceStylist;
27import android.support.v17.leanback.widget.GuidedAction;
28import android.view.View;
29
30import com.android.tv.settings.R;
31
32import java.util.List;
33
34@Keep
35public class RebootConfirmFragment extends GuidedStepFragment {
36
37    private static final String ARG_SAFE_MODE = "RebootConfirmFragment.safe_mode";
38
39    public static RebootConfirmFragment newInstance(boolean safeMode) {
40
41        Bundle args = new Bundle(1);
42        args.putBoolean(ARG_SAFE_MODE, safeMode);
43
44        RebootConfirmFragment fragment = new RebootConfirmFragment();
45        fragment.setArguments(args);
46        return fragment;
47    }
48
49    @Override
50    public void onViewCreated(View view, Bundle savedInstanceState) {
51        super.onViewCreated(view, savedInstanceState);
52        setSelectedActionPosition(1);
53    }
54
55    @Override
56    public @NonNull
57    GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
58        if (getArguments().getBoolean(ARG_SAFE_MODE, false)) {
59            return new GuidanceStylist.Guidance(
60                    getString(R.string.reboot_safemode_confirm),
61                    getString(R.string.reboot_safemode_desc),
62                    getString(R.string.about_preference),
63                    getActivity().getDrawable(R.drawable.ic_warning_132dp)
64            );
65        } else {
66            return new GuidanceStylist.Guidance(
67                    getString(R.string.system_reboot_confirm),
68                    null,
69                    getString(R.string.about_preference),
70                    getActivity().getDrawable(R.drawable.ic_warning_132dp)
71            );
72        }
73    }
74
75    @Override
76    public void onCreateActions(@NonNull List<GuidedAction> actions,
77            Bundle savedInstanceState) {
78        final Context context = getActivity();
79        if (getArguments().getBoolean(ARG_SAFE_MODE, false)) {
80            actions.add(new GuidedAction.Builder(context)
81                    .id(GuidedAction.ACTION_ID_OK)
82                    .title(R.string.reboot_safemode_action)
83                    .build());
84        } else {
85            actions.add(new GuidedAction.Builder(context)
86                    .id(GuidedAction.ACTION_ID_OK)
87                    .title(R.string.restart_button_label)
88                    .build());
89        }
90        actions.add(new GuidedAction.Builder(context)
91                .clickAction(GuidedAction.ACTION_ID_CANCEL)
92                .build());
93    }
94
95    @Override
96    public void onGuidedActionClicked(GuidedAction action) {
97        if (action.getId() == GuidedAction.ACTION_ID_OK) {
98            final boolean toSafeMode = getArguments().getBoolean(ARG_SAFE_MODE, false);
99            final PowerManager pm =
100                    (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
101
102            new AsyncTask<Void, Void, Void>() {
103                @Override
104                protected Void doInBackground(Void... params) {
105                    if (toSafeMode) {
106                        pm.rebootSafeMode();
107                    } else {
108                        pm.reboot(null);
109                    }
110                    return null;
111                }
112            }.execute();
113        } else {
114            getFragmentManager().popBackStack();
115        }
116    }
117}
118