1 /* 2 * Copyright (C) 2017 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.google.android.car.kitchensink.power; 18 19 import android.car.hardware.power.CarPowerManager; 20 import android.content.Context; 21 import android.os.Bundle; 22 import android.os.Handler; 23 import android.os.PowerManager; 24 import android.os.SystemClock; 25 import android.util.Log; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.Button; 30 31 import androidx.fragment.app.Fragment; 32 33 import com.google.android.car.kitchensink.KitchenSinkActivity; 34 import com.google.android.car.kitchensink.R; 35 36 public class PowerTestFragment extends Fragment { 37 private final boolean DBG = false; 38 private final String TAG = "PowerTestFragment"; 39 private CarPowerManager mCarPowerManager; 40 41 private final CarPowerManager.CarPowerStateListener mPowerListener = 42 (state) -> { 43 Log.i(TAG, "onStateChanged() state = " + state); 44 }; 45 46 @Override onCreate(Bundle savedInstanceState)47 public void onCreate(Bundle savedInstanceState) { 48 final Runnable r = () -> { 49 mCarPowerManager = ((KitchenSinkActivity) getActivity()).getPowerManager(); 50 try { 51 mCarPowerManager.setListener(mPowerListener); 52 } catch (IllegalStateException e) { 53 Log.e(TAG, "CarPowerManager listener was not cleared"); 54 } 55 }; 56 ((KitchenSinkActivity) getActivity()).requestRefreshManager(r, 57 new Handler(getContext().getMainLooper())); 58 super.onCreate(savedInstanceState); 59 } 60 61 @Override onDestroy()62 public void onDestroy() { 63 super.onDestroy(); 64 mCarPowerManager.clearListener(); 65 } 66 67 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)68 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) { 69 View v = inflater.inflate(R.layout.power_test, container, false); 70 71 Button b = v.findViewById(R.id.btnPwrRequestShutdown); 72 b.setOnClickListener(this::requestShutdownBtn); 73 74 b = v.findViewById(R.id.btnPwrShutdown); 75 b.setOnClickListener(this::shutdownBtn); 76 77 b = v.findViewById(R.id.btnPwrSleep); 78 b.setOnClickListener(this::sleepBtn); 79 80 if(DBG) { 81 Log.d(TAG, "Starting PowerTestFragment"); 82 } 83 84 return v; 85 } 86 requestShutdownBtn(View v)87 private void requestShutdownBtn(View v) { 88 mCarPowerManager.requestShutdownOnNextSuspend(); 89 } 90 shutdownBtn(View v)91 private void shutdownBtn(View v) { 92 if(DBG) { 93 Log.d(TAG, "Calling shutdown method"); 94 } 95 PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE); 96 pm.shutdown(/* confirm */ false, /* reason */ null, /* wait */ false); 97 Log.d(TAG, "shutdown called!"); 98 } 99 sleepBtn(View v)100 private void sleepBtn(View v) { 101 if(DBG) { 102 Log.d(TAG, "Calling sleep method"); 103 } 104 // NOTE: This doesn't really work to sleep the device. Actual sleep is implemented via 105 // SystemInterface via libsuspend::force_suspend() 106 PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE); 107 pm.goToSleep(SystemClock.uptimeMillis(), PowerManager.GO_TO_SLEEP_REASON_DEVICE_ADMIN, 108 PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE); 109 } 110 } 111