OSDN Git Service

Extract a FakeNativeWrapper to share between tests
[android-x86/frameworks-base.git] / cmds / bootanimation / BootAnimationUtil.cpp
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
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     return atoi(value) > 0;
53 }
54
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;
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
77 bool 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
104 }  // namespace android