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