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