blob: 4f56e5a88c4cd550f7ed45f37353cacb7e3dffad [file] [log] [blame]
Ed Coyne7464ac92017-06-08 12:26:48 -07001/*
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#include "BootAnimationUtil.h"
18
Ed Coyne33f4b7d2018-04-10 13:39:09 -070019#include <vector>
Ed Coyne7464ac92017-06-08 12:26:48 -070020#include <inttypes.h>
21
22#include <binder/IServiceManager.h>
23#include <cutils/properties.h>
24#include <utils/Log.h>
25#include <utils/SystemClock.h>
Ed Coyne33f4b7d2018-04-10 13:39:09 -070026#include <android-base/properties.h>
Ed Coyne7464ac92017-06-08 12:26:48 -070027
28namespace android {
Ed Coyne33f4b7d2018-04-10 13:39:09 -070029namespace {
30
31static constexpr char PLAY_SOUND_PROP_NAME[] = "persist.sys.bootanim.play_sound";
32static constexpr char BOOT_COMPLETED_PROP_NAME[] = "sys.boot_completed";
33static constexpr char POWER_CTL_PROP_NAME[] = "sys.powerctl";
34static constexpr char BOOTREASON_PROP_NAME[] = "ro.boot.bootreason";
35static const std::vector<std::string> PLAY_SOUND_BOOTREASON_BLACKLIST {
36 "kernel_panic",
37 "Panic",
38 "Watchdog",
39};
40
41} // namespace
42
Ed Coyne7464ac92017-06-08 12:26:48 -070043
44bool bootAnimationDisabled() {
45 char value[PROPERTY_VALUE_MAX];
46 property_get("debug.sf.nobootanimation", value, "0");
47 if (atoi(value) > 0) {
Kalle Raita88efa562017-07-14 16:18:16 -070048 return true;
Ed Coyne7464ac92017-06-08 12:26:48 -070049 }
50
51 property_get("ro.boot.quiescent", value, "0");
Robert Horvath8c878d62021-04-12 16:51:56 +020052 if (atoi(value) > 0) {
53 // Only show the bootanimation for quiescent boots if this system property is set to enabled
54 if (!property_get_bool("ro.bootanim.quiescent.enabled", false)) {
55 return true;
56 }
57 }
58
59 return false;
Ed Coyne7464ac92017-06-08 12:26:48 -070060}
61
62void waitForSurfaceFlinger() {
63 // TODO: replace this with better waiting logic in future, b/35253872
64 int64_t waitStartTime = elapsedRealtime();
65 sp<IServiceManager> sm = defaultServiceManager();
66 const String16 name("SurfaceFlinger");
67 const int SERVICE_WAIT_SLEEP_MS = 100;
68 const int LOG_PER_RETRIES = 10;
69 int retry = 0;
70 while (sm->checkService(name) == nullptr) {
71 retry++;
72 if ((retry % LOG_PER_RETRIES) == 0) {
73 ALOGW("Waiting for SurfaceFlinger, waited for %" PRId64 " ms",
74 elapsedRealtime() - waitStartTime);
75 }
76 usleep(SERVICE_WAIT_SLEEP_MS * 1000);
77 };
78 int64_t totalWaited = elapsedRealtime() - waitStartTime;
79 if (totalWaited > SERVICE_WAIT_SLEEP_MS) {
80 ALOGI("Waiting for SurfaceFlinger took %" PRId64 " ms", totalWaited);
81 }
82}
83
Ed Coyne33f4b7d2018-04-10 13:39:09 -070084bool playSoundsAllowed() {
85 // Only play sounds for system boots, not runtime restarts.
86 if (android::base::GetBoolProperty(BOOT_COMPLETED_PROP_NAME, false)) {
87 return false;
88 }
89 // no audio while shutting down
90 if (!android::base::GetProperty(POWER_CTL_PROP_NAME, "").empty()) {
91 return false;
92 }
93 // Read the system property to see if we should play the sound.
94 // If it's not present, default to allowed.
95 if (!property_get_bool(PLAY_SOUND_PROP_NAME, 1)) {
96 return false;
97 }
98
99 // Don't play sounds if this is a reboot due to an error.
100 char bootreason[PROPERTY_VALUE_MAX];
101 if (property_get(BOOTREASON_PROP_NAME, bootreason, nullptr) > 0) {
102 for (const auto& str : PLAY_SOUND_BOOTREASON_BLACKLIST) {
103 if (strcasecmp(str.c_str(), bootreason) == 0) {
104 return false;
105 }
106 }
107 }
108 return true;
109}
110
Ed Coyne7464ac92017-06-08 12:26:48 -0700111} // namespace android