blob: 1e417e938359b7fe3631e754551f963f3c454980 [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");
52 return atoi(value) > 0;
53}
54
55void waitForSurfaceFlinger() {
56 // TODO: replace this with better waiting logic in future, b/35253872
57 int64_t waitStartTime = elapsedRealtime();
58 sp<IServiceManager> sm = defaultServiceManager();
59 const String16 name("SurfaceFlinger");
60 const int SERVICE_WAIT_SLEEP_MS = 100;
61 const int LOG_PER_RETRIES = 10;
62 int retry = 0;
63 while (sm->checkService(name) == nullptr) {
64 retry++;
65 if ((retry % LOG_PER_RETRIES) == 0) {
66 ALOGW("Waiting for SurfaceFlinger, waited for %" PRId64 " ms",
67 elapsedRealtime() - waitStartTime);
68 }
69 usleep(SERVICE_WAIT_SLEEP_MS * 1000);
70 };
71 int64_t totalWaited = elapsedRealtime() - waitStartTime;
72 if (totalWaited > SERVICE_WAIT_SLEEP_MS) {
73 ALOGI("Waiting for SurfaceFlinger took %" PRId64 " ms", totalWaited);
74 }
75}
76
Ed Coyne33f4b7d2018-04-10 13:39:09 -070077bool playSoundsAllowed() {
78 // Only play sounds for system boots, not runtime restarts.
79 if (android::base::GetBoolProperty(BOOT_COMPLETED_PROP_NAME, false)) {
80 return false;
81 }
82 // no audio while shutting down
83 if (!android::base::GetProperty(POWER_CTL_PROP_NAME, "").empty()) {
84 return false;
85 }
86 // Read the system property to see if we should play the sound.
87 // If it's not present, default to allowed.
88 if (!property_get_bool(PLAY_SOUND_PROP_NAME, 1)) {
89 return false;
90 }
91
92 // Don't play sounds if this is a reboot due to an error.
93 char bootreason[PROPERTY_VALUE_MAX];
94 if (property_get(BOOTREASON_PROP_NAME, bootreason, nullptr) > 0) {
95 for (const auto& str : PLAY_SOUND_BOOTREASON_BLACKLIST) {
96 if (strcasecmp(str.c_str(), bootreason) == 0) {
97 return false;
98 }
99 }
100 }
101 return true;
102}
103
Ed Coyne7464ac92017-06-08 12:26:48 -0700104} // namespace android