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 #include "BootAnimationUtil.h"
18 
19 #include <vector>
20 #include <inttypes.h>
21 
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>
27 
28 namespace android {
29 namespace {
30 
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 {
36   "kernel_panic",
37   "Panic",
38   "Watchdog",
39 };
40 
41 }  // namespace
42 
43 
bootAnimationDisabled()44 bool bootAnimationDisabled() {
45     char value[PROPERTY_VALUE_MAX];
46     property_get("debug.sf.nobootanimation", value, "0");
47     if (atoi(value) > 0) {
48         return true;
49     }
50 
51     property_get("ro.boot.quiescent", value, "0");
52     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;
60 }
61 
waitForSurfaceFlinger()62 void 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 
playSoundsAllowed()84 bool 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 
111 }  // namespace android
112