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