2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "BootAnimationUtil.h"
22 #include <binder/IServiceManager.h>
23 #include <cutils/properties.h>
24 #include <utils/Log.h>
25 #include <utils/SystemClock.h>
26 #include <android-base/properties.h>
31 static constexpr char PLAY_SOUND_PROP_NAME[] = "persist.sys.bootanim.play_sound";
32 static constexpr char BOOT_COMPLETED_PROP_NAME[] = "sys.boot_completed";
33 static constexpr char POWER_CTL_PROP_NAME[] = "sys.powerctl";
34 static constexpr char BOOTREASON_PROP_NAME[] = "ro.boot.bootreason";
35 static const std::vector<std::string> PLAY_SOUND_BOOTREASON_BLACKLIST {
44 bool bootAnimationDisabled() {
45 char value[PROPERTY_VALUE_MAX];
46 property_get("debug.sf.nobootanimation", value, "0");
47 if (atoi(value) > 0) {
51 property_get("ro.boot.quiescent", value, "0");
52 return atoi(value) > 0;
55 void 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;
63 while (sm->checkService(name) == nullptr) {
65 if ((retry % LOG_PER_RETRIES) == 0) {
66 ALOGW("Waiting for SurfaceFlinger, waited for %" PRId64 " ms",
67 elapsedRealtime() - waitStartTime);
69 usleep(SERVICE_WAIT_SLEEP_MS * 1000);
71 int64_t totalWaited = elapsedRealtime() - waitStartTime;
72 if (totalWaited > SERVICE_WAIT_SLEEP_MS) {
73 ALOGI("Waiting for SurfaceFlinger took %" PRId64 " ms", totalWaited);
77 bool playSoundsAllowed() {
78 // Only play sounds for system boots, not runtime restarts.
79 if (android::base::GetBoolProperty(BOOT_COMPLETED_PROP_NAME, false)) {
82 // no audio while shutting down
83 if (!android::base::GetProperty(POWER_CTL_PROP_NAME, "").empty()) {
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)) {
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) {
104 } // namespace android