summaryrefslogtreecommitdiffstats
blob: d24148d2a4f89f438a7ecd501d546ff6d87370f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.car.kitchensink.power;

import android.car.CarNotConnectedException;
import android.car.hardware.power.CarPowerManager;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.google.android.car.kitchensink.KitchenSinkActivity;
import com.google.android.car.kitchensink.R;

import java.util.concurrent.Executor;

public class PowerTestFragment extends Fragment {
    private final boolean DBG = false;
    private final String TAG = "PowerTestFragment";
    private CarPowerManager mCarPowerManager;
    private TextView mTvBootReason;
    private Executor mExecutor;

    private class ThreadPerTaskExecutor implements Executor {
        public void execute(Runnable r) {
            new Thread(r).start();
        }
    }

    private final CarPowerManager.CarPowerStateListener mPowerListener =
            new CarPowerManager.CarPowerStateListener () {
                @Override
                public void onStateChanged(int state) {
                    Log.i(TAG, "onStateChanged() state = " + state);
                }
            };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        final Runnable r = () -> {
            mCarPowerManager = ((KitchenSinkActivity) getActivity()).getPowerManager();
            mExecutor = new ThreadPerTaskExecutor();
            try {
                mCarPowerManager.setListener(mPowerListener, mExecutor);
            } catch (CarNotConnectedException e) {
                Log.e(TAG, "Car is not connected!");
            } catch (IllegalStateException e) {
                Log.e(TAG, "CarPowerManager listener was not cleared");
            }
        };
        ((KitchenSinkActivity) getActivity()).requestRefreshManager(r,
                new Handler(getContext().getMainLooper()));
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mCarPowerManager.clearListener();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
        View v = inflater.inflate(R.layout.power_test, container, false);

        Button b = v.findViewById(R.id.btnPwrGetBootReason);
        b.setOnClickListener(this::getBootReasonBtn);

        b = v.findViewById(R.id.btnPwrRequestShutdown);
        b.setOnClickListener(this::requestShutdownBtn);

        b = v.findViewById(R.id.btnPwrShutdown);
        b.setOnClickListener(this::shutdownBtn);

        b = v.findViewById(R.id.btnPwrSleep);
        b.setOnClickListener(this::sleepBtn);

        mTvBootReason = v.findViewById(R.id.tvPowerBootReason);

        if(DBG) {
            Log.d(TAG, "Starting PowerTestFragment");
        }

        return v;
    }

    private void getBootReasonBtn(View v) {
        try {
            int bootReason = mCarPowerManager.getBootReason();
            mTvBootReason.setText(String.valueOf(bootReason));
        } catch (CarNotConnectedException e) {
            Log.e(TAG, "Failed to getBootReason()", e);
        }
    }

    private void requestShutdownBtn(View v) {
        try {
            mCarPowerManager.requestShutdownOnNextSuspend();
        } catch (CarNotConnectedException e) {
            Log.e(TAG, "Failed to set requestShutdownOnNextSuspend()", e);
        }
    }

    private void shutdownBtn(View v) {
        if(DBG) {
            Log.d(TAG, "Calling shutdown method");
        }
        PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
        pm.shutdown(/* confirm */ false, /* reason */ null, /* wait */ false);
        Log.d(TAG, "shutdown called!");
    }

    private void sleepBtn(View v) {
        if(DBG) {
            Log.d(TAG, "Calling sleep method");
        }
        // NOTE:  This doesn't really work to sleep the device.  Actual sleep is implemented via
        //  SystemInterface via libsuspend::force_suspend()
        PowerManager pm = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
        pm.goToSleep(SystemClock.uptimeMillis(), PowerManager.GO_TO_SLEEP_REASON_DEVICE_ADMIN,
                     PowerManager.GO_TO_SLEEP_FLAG_NO_DOZE);
    }
}