• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.Nullable;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.graphics.drawable.Icon;
28 import android.os.AsyncTask;
29 import android.os.Bundle;
30 import android.os.PowerManager;
31 import android.view.View;
32 
33 import androidx.annotation.Keep;
34 import androidx.annotation.NonNull;
35 import androidx.fragment.app.FragmentActivity;
36 import androidx.leanback.app.GuidedStepSupportFragment;
37 import androidx.leanback.widget.GuidanceStylist;
38 import androidx.leanback.widget.GuidedAction;
39 
40 import com.android.tv.settings.FullScreenDialogFragment;
41 import com.android.tv.settings.R;
42 import com.android.tv.settings.overlay.FlavorUtils;
43 import com.android.tv.settings.widget.SettingsGuidedStepFragment;
44 
45 import java.util.List;
46 
47 /** Activity to confirm rebooting the device */
48 public class RebootConfirmActivity extends FragmentActivity {
49 
50     private static final String ARG_SAFE_MODE = "RebootConfirmFragment.safe_mode";
51 
52     /** generate an Intent to start this Activity */
getIntent(Context context, boolean safeMode)53     public static Intent getIntent(Context context, boolean safeMode) {
54         return new Intent(context, RebootConfirmActivity.class)
55                 .putExtra(ARG_SAFE_MODE, safeMode);
56     }
57 
reboot(Context context, boolean toSafeMode)58     protected static void reboot(Context context, boolean toSafeMode) {
59         final PowerManager pm = context.getSystemService(PowerManager.class);
60 
61         new AsyncTask<Void, Void, Void>() {
62             @Override
63             protected Void doInBackground(Void... params) {
64                 if (toSafeMode) {
65                     pm.rebootSafeMode();
66                 } else {
67                     pm.reboot(null);
68                 }
69                 return null;
70             }
71         }.execute();
72     }
73 
isToSafeMode(Bundle arguments)74     protected static boolean isToSafeMode(Bundle arguments) {
75         boolean toSafeMode = false;
76         if (arguments != null) {
77             toSafeMode = arguments.getBoolean(ARG_SAFE_MODE, false);
78         }
79         return toSafeMode;
80     }
81 
82     @Override
onCreate(@ullable Bundle savedInstanceState)83     protected void onCreate(@Nullable Bundle savedInstanceState) {
84         super.onCreate(savedInstanceState);
85 
86         if (savedInstanceState == null) {
87             boolean toSafeMode = isToSafeMode(getIntent().getExtras());
88             boolean twoPanel = FlavorUtils.isTwoPanel(getApplicationContext());
89 
90             if (!twoPanel) {
91                 setTheme(R.style.Theme_Leanback_GuidedStep);
92                 GuidedStepSupportFragment.addAsRoot(
93                         this,
94                         GuidedStepRebootConfirmFragment.newInstance(toSafeMode),
95                         android.R.id.content);
96             } else {
97                 setTheme(R.style.TvSettingsDialog_FullScreen);
98                 FullScreenDialogRebootConfirmFragment dialogFragment =
99                         FullScreenDialogRebootConfirmFragment.newInstance(getApplicationContext(),
100                                 toSafeMode);
101                 getSupportFragmentManager()
102                         .beginTransaction()
103                         .add(android.R.id.content, dialogFragment)
104                         .commitAllowingStateLoss();
105             }
106         }
107     }
108 
109     /** Confirmation page in the full screen dialog style */
110     public static class FullScreenDialogRebootConfirmFragment extends FullScreenDialogFragment {
111 
newInstance(Context context, boolean safeMode)112         public static FullScreenDialogRebootConfirmFragment newInstance(Context context,
113                 boolean safeMode) {
114             Bundle args = new FullScreenDialogFragment.DialogBuilder()
115                     .setIcon(Icon.createWithResource(context, R.drawable.ic_warning_132dp))
116                     .setTitle(safeMode ? context.getString(R.string.reboot_safemode_confirm)
117                             : context.getString(R.string.system_reboot_confirm))
118                     .setMessage(safeMode ? context.getString(R.string.reboot_safemode_desc) : null)
119                     .setPositiveButton(safeMode ? context.getString(R.string.reboot_safemode_action)
120                             : context.getString(R.string.restart_button_label))
121                     .setNegativeButton(context.getString(R.string.settings_cancel))
122                     .setInitialFocusOnNegativeButton(true)
123                     .build();
124 
125             args.putBoolean(ARG_SAFE_MODE, safeMode);
126             FullScreenDialogRebootConfirmFragment fragment =
127                     new FullScreenDialogRebootConfirmFragment();
128             fragment.setArguments(args);
129             return fragment;
130         }
131 
132         @Override
onButtonPressed(int action)133         public void onButtonPressed(int action) {
134             if (action == ACTION_POSITIVE) {
135                 reboot(requireContext(), isToSafeMode(requireArguments()));
136             } else {
137                 requireActivity().finish();
138             }
139         }
140     }
141 
142     /** Confirmation page in the guided step style */
143     @Keep
144     public static class GuidedStepRebootConfirmFragment extends SettingsGuidedStepFragment {
145 
newInstance( boolean safeMode)146         public static GuidedStepRebootConfirmFragment newInstance(
147                 boolean safeMode) {
148             Bundle args = new Bundle(1);
149             args.putBoolean(ARG_SAFE_MODE, safeMode);
150 
151             GuidedStepRebootConfirmFragment
152                     fragment = new GuidedStepRebootConfirmFragment();
153             fragment.setArguments(args);
154             return fragment;
155         }
156 
157         @Override
onViewCreated(View view, Bundle savedInstanceState)158         public void onViewCreated(View view, Bundle savedInstanceState) {
159             super.onViewCreated(view, savedInstanceState);
160             setSelectedActionPosition(1);
161         }
162 
163         @Override
164         public @NonNull
onCreateGuidance(Bundle savedInstanceState)165         GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
166             if (getArguments().getBoolean(ARG_SAFE_MODE, false)) {
167                 return new GuidanceStylist.Guidance(
168                         getString(R.string.reboot_safemode_confirm),
169                         getString(R.string.reboot_safemode_desc),
170                         null,
171                         getActivity().getDrawable(R.drawable.ic_warning_132dp)
172                 );
173             } else {
174                 return new GuidanceStylist.Guidance(
175                         getString(R.string.system_reboot_confirm),
176                         null,
177                         null,
178                         getActivity().getDrawable(R.drawable.ic_warning_132dp)
179                 );
180             }
181         }
182 
183         @Override
onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)184         public void onCreateActions(@NonNull List<GuidedAction> actions,
185                 Bundle savedInstanceState) {
186             final Context context = getActivity();
187             if (getArguments().getBoolean(ARG_SAFE_MODE, false)) {
188                 actions.add(new GuidedAction.Builder(context)
189                         .id(GuidedAction.ACTION_ID_OK)
190                         .title(R.string.reboot_safemode_action)
191                         .build());
192             } else {
193                 actions.add(new GuidedAction.Builder(context)
194                         .id(GuidedAction.ACTION_ID_OK)
195                         .title(R.string.restart_button_label)
196                         .build());
197             }
198             actions.add(new GuidedAction.Builder(context)
199                     .clickAction(GuidedAction.ACTION_ID_CANCEL)
200                     .build());
201         }
202 
203         @Override
onCreateGuidanceStylist()204         public GuidanceStylist onCreateGuidanceStylist() {
205             return new GuidanceStylist() {
206                 @Override
207                 public int onProvideLayoutId() {
208                     switch (FlavorUtils.getFlavor(getContext())) {
209                         case FLAVOR_CLASSIC:
210                         case FLAVOR_TWO_PANEL:
211                             return R.layout.confirm_guidance;
212                         case FLAVOR_X:
213                         case FLAVOR_VENDOR:
214                             return R.layout.confirm_guidance_x;
215                         default:
216                             return R.layout.confirm_guidance;
217                     }
218                 }
219             };
220         }
221 
222         @Override
223         public void onGuidedActionClicked(GuidedAction action) {
224             if (action.getId() == GuidedAction.ACTION_ID_OK) {
225                 reboot(requireContext(), isToSafeMode(requireArguments()));
226             } else {
227                 requireActivity().finish();
228             }
229         }
230     }
231 }
232