blob: 2435ffadd8797fcb03a4df57ff6968fce4d7f589 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -070017#define LOG_NDEBUG 0
Mathias Agopianbc726112009-09-23 15:44:05 -070018#define LOG_TAG "BootAnimation"
19
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#include <stdint.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080021#include <inttypes.h>
Damien Bargiacchi97480862016-03-29 14:55:55 -070022#include <sys/inotify.h>
23#include <sys/poll.h>
24#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <math.h>
27#include <fcntl.h>
28#include <utils/misc.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070029#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070030#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Jason parksbd9a08d2011-01-31 15:04:34 -060032#include <cutils/properties.h>
33
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080034#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070035#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include <utils/Atomic.h>
37#include <utils/Errors.h>
38#include <utils/Log.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080039#include <utils/SystemClock.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Keun-young Parkb5938422017-03-23 13:46:24 -070041#include <android-base/properties.h>
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043#include <ui/PixelFormat.h>
44#include <ui/Rect.h>
45#include <ui/Region.h>
46#include <ui/DisplayInfo.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Jeff Brown0b722fe2012-08-24 22:40:14 -070048#include <gui/ISurfaceComposer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080049#include <gui/Surface.h>
50#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080051
Andreas Gampecfedceb2014-09-30 21:48:18 -070052// TODO: Fix Skia.
53#pragma GCC diagnostic push
54#pragma GCC diagnostic ignored "-Wunused-parameter"
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050055#include <SkBitmap.h>
Matt Sarett8898c162016-03-24 16:11:01 -040056#include <SkImage.h>
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050057#include <SkStream.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070058#pragma GCC diagnostic pop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60#include <GLES/gl.h>
61#include <GLES/glext.h>
62#include <EGL/eglext.h>
63
64#include "BootAnimation.h"
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -070065#include "audioplay.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
67namespace android {
68
Damien Bargiacchi97480862016-03-29 14:55:55 -070069static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
70static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
71static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070072static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
73static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
74
Damien Bargiacchi97480862016-03-29 14:55:55 -070075static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
76static const char SYSTEM_TIME_DIR_NAME[] = "time";
77static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070078static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
79static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070080static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
81static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
82static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
83static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070084static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070085// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
86static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070087static constexpr char FONT_BEGIN_CHAR = ' ';
88static constexpr char FONT_END_CHAR = '~' + 1;
89static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
90static constexpr size_t FONT_NUM_COLS = 16;
91static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
92static const int TEXT_CENTER_VALUE = INT_MAX;
93static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -070094static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Geoffrey Pitsch30508792016-07-22 17:04:21 -040095static const char PLAY_SOUND_PROP_NAME[] = "persist.sys.bootanim.play_sound";
Narayan Kamathafd31e02013-12-03 13:16:03 +000096static const int ANIM_ENTRY_NAME_MAX = 256;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070097static constexpr size_t TEXT_POS_LEN_MAX = 16;
Geoffrey Pitsch290c4352016-08-09 14:35:10 -040098static const char BOOT_COMPLETED_PROP_NAME[] = "sys.boot_completed";
99static const char BOOTREASON_PROP_NAME[] = "ro.boot.bootreason";
100// bootreasons list in "system/core/bootstat/bootstat.cpp".
101static const std::vector<std::string> PLAY_SOUND_BOOTREASON_BLACKLIST {
102 "kernel_panic",
103 "Panic",
104 "Watchdog",
105};
Narayan Kamathafd31e02013-12-03 13:16:03 +0000106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107// ---------------------------------------------------------------------------
108
Damien Bargiacchi97480862016-03-29 14:55:55 -0700109BootAnimation::BootAnimation() : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700110 mTimeFormat12Hour(false), mTimeCheckThread(NULL) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700111 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400112
113 // If the system has already booted, the animation is not being used for a boot.
Keun-young Parkb5938422017-03-23 13:46:24 -0700114 mSystemBoot = !android::base::GetBoolProperty(BOOT_COMPLETED_PROP_NAME, false);
115 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
116 if (powerCtl.empty()) {
117 mShuttingDown = false;
118 } else {
119 mShuttingDown = true;
120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121}
122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700124 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +0000125 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700126 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -0700127 run("BootAnimation", PRIORITY_DISPLAY);
128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129}
130
Mathias Agopianbc726112009-09-23 15:44:05 -0700131sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 return mSession;
133}
134
Mathias Agopianbc726112009-09-23 15:44:05 -0700135
Narayan Kamathafd31e02013-12-03 13:16:03 +0000136void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700137{
138 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +0000139 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700140
141 // calling requestExit() is not enough here because the Surface code
142 // might be blocked on a condition variable that will never be updated.
143 kill( getpid(), SIGKILL );
144 requestExit();
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700145 audioplay::destroy();
Mathias Agopianbc726112009-09-23 15:44:05 -0700146}
147
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
149 const char* name) {
150 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700151 if (asset == NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 return NO_INIT;
153 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400154 sk_sp<SkData> data = SkData::MakeWithoutCopy(asset->getBuffer(false),
155 asset->getLength());
156 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
157 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 asset->close();
159 delete asset;
160
161 // ensure we can call getPixels(). No need to call unlock, since the
162 // bitmap will go out of scope when we return from this method.
163 bitmap.lockPixels();
164
165 const int w = bitmap.width();
166 const int h = bitmap.height();
167 const void* p = bitmap.getPixels();
168
169 GLint crop[4] = { 0, h, w, -h };
170 texture->w = w;
171 texture->h = h;
172
173 glGenTextures(1, &texture->name);
174 glBindTexture(GL_TEXTURE_2D, texture->name);
175
Mike Reed42a1d082014-07-07 18:06:18 -0400176 switch (bitmap.colorType()) {
177 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
179 GL_UNSIGNED_BYTE, p);
180 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400181 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
183 GL_UNSIGNED_SHORT_4_4_4_4, p);
184 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400185 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
187 GL_UNSIGNED_BYTE, p);
188 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400189 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
191 GL_UNSIGNED_SHORT_5_6_5, p);
192 break;
193 default:
194 break;
195 }
196
197 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
198 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
199 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
200 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
201 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 return NO_ERROR;
204}
205
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700206status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700207{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700208 SkBitmap bitmap;
Damien Bargiacchif67b3172016-09-07 20:17:14 -0700209 sk_sp<SkData> data = SkData::MakeWithoutCopy(map->getDataPtr(),
210 map->getDataLength());
Matt Sarett8898c162016-03-24 16:11:01 -0400211 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
212 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700213
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200214 // FileMap memory is never released until application exit.
215 // Release it now as the texture is already loaded and the memory used for
216 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700217 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200218
Mathias Agopiana8826d62009-10-01 03:10:14 -0700219 // ensure we can call getPixels(). No need to call unlock, since the
220 // bitmap will go out of scope when we return from this method.
221 bitmap.lockPixels();
222
223 const int w = bitmap.width();
224 const int h = bitmap.height();
225 const void* p = bitmap.getPixels();
226
227 GLint crop[4] = { 0, h, w, -h };
228 int tw = 1 << (31 - __builtin_clz(w));
229 int th = 1 << (31 - __builtin_clz(h));
230 if (tw < w) tw <<= 1;
231 if (th < h) th <<= 1;
232
Mike Reed42a1d082014-07-07 18:06:18 -0400233 switch (bitmap.colorType()) {
234 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530235 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700236 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
237 GL_UNSIGNED_BYTE, 0);
238 glTexSubImage2D(GL_TEXTURE_2D, 0,
239 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
240 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530241 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700242 GL_UNSIGNED_BYTE, p);
243 }
244 break;
245
Mike Reed42a1d082014-07-07 18:06:18 -0400246 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530247 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700248 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
249 GL_UNSIGNED_SHORT_5_6_5, 0);
250 glTexSubImage2D(GL_TEXTURE_2D, 0,
251 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
252 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530253 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700254 GL_UNSIGNED_SHORT_5_6_5, p);
255 }
256 break;
257 default:
258 break;
259 }
260
261 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
262
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700263 *width = w;
264 *height = h;
265
Mathias Agopiana8826d62009-10-01 03:10:14 -0700266 return NO_ERROR;
267}
268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269status_t BootAnimation::readyToRun() {
270 mAssets.addDefaultAssets();
271
Jeff Brown0b722fe2012-08-24 22:40:14 -0700272 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
273 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700275 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 if (status)
277 return -1;
278
279 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700280 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
281 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700282
283 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700284 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700285 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286
Mathias Agopian17f638b2009-04-16 20:04:08 -0700287 sp<Surface> s = control->getSurface();
288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700290 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700291 EGL_RED_SIZE, 8,
292 EGL_GREEN_SIZE, 8,
293 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700294 EGL_DEPTH_SIZE, 0,
295 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700296 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700297 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 EGLint numConfigs;
299 EGLConfig config;
300 EGLSurface surface;
301 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700304
305 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700306 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700307 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 context = eglCreateContext(display, config, NULL, NULL);
309 eglQuerySurface(display, surface, EGL_WIDTH, &w);
310 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700311
Mathias Agopianabac0102009-07-31 14:47:00 -0700312 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
313 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 mDisplay = display;
316 mContext = context;
317 mSurface = surface;
318 mWidth = w;
319 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700320 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 mFlingerSurface = s;
322
Elliott Hughesc367d482013-10-29 13:12:55 -0700323 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600324 // of being encrypted we show the encrypted boot animation.
325 char decrypt[PROPERTY_VALUE_MAX];
326 property_get("vold.decrypt", decrypt, "");
327
Keun-young Parkb5938422017-03-23 13:46:24 -0700328 bool encryptedAnimation = atoi(decrypt) != 0 ||
329 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600330
Keun-young Parkb5938422017-03-23 13:46:24 -0700331 if (!mShuttingDown && encryptedAnimation &&
332 (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0)) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700333 mZipFileName = SYSTEM_ENCRYPTED_BOOTANIMATION_FILE;
Keun-young Parkb5938422017-03-23 13:46:24 -0700334 return NO_ERROR;
Jason parksbd9a08d2011-01-31 15:04:34 -0600335 }
Keun-young Parkb5938422017-03-23 13:46:24 -0700336 static const char* bootFiles[] = {OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE};
337 static const char* shutdownFiles[] =
338 {OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE};
339
340 for (const char* f : (!mShuttingDown ? bootFiles : shutdownFiles)) {
341 if (access(f, R_OK) == 0) {
342 mZipFileName = f;
343 return NO_ERROR;
344 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 return NO_ERROR;
347}
348
Mathias Agopiana8826d62009-10-01 03:10:14 -0700349bool BootAnimation::threadLoop()
350{
351 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000352 // We have no bootanimation file, so we use the stock android logo
353 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700354 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700355 r = android();
356 } else {
357 r = movie();
358 }
359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
361 eglDestroyContext(mDisplay, mContext);
362 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700363 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700364 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700365 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530366 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700367 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 return r;
369}
370
Mathias Agopiana8826d62009-10-01 03:10:14 -0700371bool BootAnimation::android()
372{
Wei Wangdbdda2b2017-02-07 18:59:58 -0800373 ALOGD("BootAnimationShownTiming start time: %" PRId64 "ms", elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700374 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
375 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376
377 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700378 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700379 glDisable(GL_DITHER);
380 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700381 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 glClear(GL_COLOR_BUFFER_BIT);
383 eglSwapBuffers(mDisplay, mSurface);
384
Mathias Agopiana8826d62009-10-01 03:10:14 -0700385 glEnable(GL_TEXTURE_2D);
386 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
387
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700388 const GLint xc = (mWidth - mAndroid[0].w) / 2;
389 const GLint yc = (mHeight - mAndroid[0].h) / 2;
390 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
393 updateRect.height());
394
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700395 // Blend state
396 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
397 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 const nsecs_t startTime = systemTime();
400 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700401 nsecs_t now = systemTime();
402 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700403 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
404 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
405 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406
Mathias Agopian81668642009-07-28 11:41:30 -0700407 glDisable(GL_SCISSOR_TEST);
408 glClear(GL_COLOR_BUFFER_BIT);
409
410 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700413 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
414 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700416 glEnable(GL_BLEND);
417 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
418 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419
Mathias Agopian627e7b52009-05-21 19:21:59 -0700420 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
421 if (res == EGL_FALSE)
422 break;
423
Mathias Agopian13796652009-03-24 22:49:21 -0700424 // 12fps: don't animate too fast to preserve CPU
425 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
426 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700427 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700428
429 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 } while (!exitPending());
431
432 glDeleteTextures(1, &mAndroid[0].name);
433 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 return false;
435}
436
Kevin Hesterd3782b22012-04-26 10:38:55 -0700437void BootAnimation::checkExit() {
438 // Allow surface flinger to gracefully request shutdown
439 char value[PROPERTY_VALUE_MAX];
440 property_get(EXIT_PROP_NAME, value, "0");
441 int exitnow = atoi(value);
442 if (exitnow) {
443 requestExit();
444 }
445}
446
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700447bool BootAnimation::validClock(const Animation::Part& part) {
448 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
449}
450
451bool parseTextCoord(const char* str, int* dest) {
452 if (strcmp("c", str) == 0) {
453 *dest = TEXT_CENTER_VALUE;
454 return true;
455 }
456
457 char* end;
458 int val = (int) strtol(str, &end, 0);
459 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
460 return false;
461 }
462 *dest = val;
463 return true;
464}
465
466// Parse two position coordinates. If only string is non-empty, treat it as the y value.
467void parsePosition(const char* str1, const char* str2, int* x, int* y) {
468 bool success = false;
469 if (strlen(str1) == 0) { // No values were specified
470 // success = false
471 } else if (strlen(str2) == 0) { // we have only one value
472 if (parseTextCoord(str1, y)) {
473 *x = TEXT_CENTER_VALUE;
474 success = true;
475 }
476 } else {
477 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
478 success = true;
479 }
480 }
481
482 if (!success) {
483 *x = TEXT_MISSING_VALUE;
484 *y = TEXT_MISSING_VALUE;
485 }
486}
487
Jesse Hall083b84c2014-09-22 10:51:09 -0700488// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
489// characters in str is a hex number in [0, 255], which are converted to
490// floating point values in the range [0.0, 1.0] and placed in the
491// corresponding elements of color.
492//
493// If the input string isn't valid, parseColor returns false and color is
494// left unchanged.
495static bool parseColor(const char str[7], float color[3]) {
496 float tmpColor[3];
497 for (int i = 0; i < 3; i++) {
498 int val = 0;
499 for (int j = 0; j < 2; j++) {
500 val *= 16;
501 char c = str[2*i + j];
502 if (c >= '0' && c <= '9') val += c - '0';
503 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
504 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
505 else return false;
506 }
507 tmpColor[i] = static_cast<float>(val) / 255.0f;
508 }
509 memcpy(color, tmpColor, sizeof(tmpColor));
510 return true;
511}
512
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700513
514static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700515{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700516 ZipEntryRO entry = zip->findEntryByName(name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700517 ALOGE_IF(!entry, "couldn't find %s", name);
518 if (!entry) {
519 return false;
520 }
521
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700522 FileMap* entryMap = zip->createEntryFileMap(entry);
523 zip->releaseEntry(entry);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700524 ALOGE_IF(!entryMap, "entryMap is null");
525 if (!entryMap) {
526 return false;
527 }
528
529 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000530 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700531 return true;
532}
533
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700534// The font image should be a 96x2 array of character images. The
535// columns are the printable ASCII characters 0x20 - 0x7f. The
536// top row is regular text; the bottom row is bold.
537status_t BootAnimation::initFont(Font* font, const char* fallback) {
538 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800539
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700540 if (font->map != nullptr) {
541 glGenTextures(1, &font->texture.name);
542 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800543
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700544 status = initTexture(font->map, &font->texture.w, &font->texture.h);
545
546 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
547 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
548 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
549 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
550 } else if (fallback != nullptr) {
551 status = initTexture(&font->texture, mAssets, fallback);
552 } else {
553 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800554 }
555
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700556 if (status == NO_ERROR) {
557 font->char_width = font->texture.w / FONT_NUM_COLS;
558 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
559 }
560
561 return status;
562}
563
564void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
565 glEnable(GL_BLEND); // Allow us to draw on top of the animation
566 glBindTexture(GL_TEXTURE_2D, font.texture.name);
567
568 const int len = strlen(str);
569 const int strWidth = font.char_width * len;
570
571 if (*x == TEXT_CENTER_VALUE) {
572 *x = (mWidth - strWidth) / 2;
573 } else if (*x < 0) {
574 *x = mWidth + *x - strWidth;
575 }
576 if (*y == TEXT_CENTER_VALUE) {
577 *y = (mHeight - font.char_height) / 2;
578 } else if (*y < 0) {
579 *y = mHeight + *y - font.char_height;
580 }
581
582 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
583
584 for (int i = 0; i < len; i++) {
585 char c = str[i];
586
587 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
588 c = '?';
589 }
590
591 // Crop the texture to only the pixels in the current glyph
592 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
593 const int row = charPos / FONT_NUM_COLS;
594 const int col = charPos % FONT_NUM_COLS;
595 cropRect[0] = col * font.char_width; // Left of column
596 cropRect[1] = row * font.char_height * 2; // Top of row
597 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
598 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
599 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
600
601 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
602
603 *x += font.char_width;
604 }
605
606 glDisable(GL_BLEND); // Return to the animation's default behaviour
607 glBindTexture(GL_TEXTURE_2D, 0);
608}
609
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700610// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700611void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700612 static constexpr char TIME_FORMAT_12[] = "%l:%M";
613 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700614 static constexpr int TIME_LENGTH = 6;
615
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800616 time_t rawtime;
617 time(&rawtime);
618 struct tm* timeInfo = localtime(&rawtime);
619
620 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700621 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
622 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800623
624 if (length != TIME_LENGTH - 1) {
625 ALOGE("Couldn't format time; abandoning boot animation clock");
626 mClockEnabled = false;
627 return;
628 }
629
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800630 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700631 int x = xPos;
632 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800633 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800634}
635
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700636bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700637{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700638 String8 desString;
639
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700640 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000641 return false;
642 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700643 char const* s = desString.string();
644
Mathias Agopiana8826d62009-10-01 03:10:14 -0700645 // Parse the description file
646 for (;;) {
647 const char* endl = strstr(s, "\n");
Andreas Gampecfedceb2014-09-30 21:48:18 -0700648 if (endl == NULL) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700649 String8 line(s, endl - s);
650 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800651 int fps = 0;
652 int width = 0;
653 int height = 0;
654 int count = 0;
655 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000656 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700657 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700658 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
659 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700660
Kevin Hesterd3782b22012-04-26 10:38:55 -0700661 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700662 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Jesse Hall083b84c2014-09-22 10:51:09 -0700663 // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700664 animation.width = width;
665 animation.height = height;
666 animation.fps = fps;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700667 } else if (sscanf(l, " %c %d %d %s #%6s %16s %16s",
668 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
669 //ALOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
670 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700671 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700672 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700673 part.count = count;
674 part.pause = pause;
675 part.path = path;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700676 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700677 part.animation = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700678 if (!parseColor(color, part.backgroundColor)) {
679 ALOGE("> invalid color '#%s'", color);
680 part.backgroundColor[0] = 0.0f;
681 part.backgroundColor[1] = 0.0f;
682 part.backgroundColor[2] = 0.0f;
683 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700684 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700685 animation.parts.add(part);
686 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700687 else if (strcmp(l, "$SYSTEM") == 0) {
688 // ALOGD("> SYSTEM");
689 Animation::Part part;
690 part.playUntilComplete = false;
691 part.count = 1;
692 part.pause = 0;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700693 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700694 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
695 if (part.animation != NULL)
696 animation.parts.add(part);
697 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700698 s = ++endl;
699 }
700
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700701 return true;
702}
703
704bool BootAnimation::preloadZip(Animation& animation)
705{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700706 // read all the data structures
707 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000708 void *cookie = NULL;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400709 ZipFileRO* zip = animation.zip;
710 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000711 return false;
712 }
713
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400714 Animation::Part* partWithAudio = NULL;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000715 ZipEntryRO entry;
716 char name[ANIM_ENTRY_NAME_MAX];
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400717 while ((entry = zip->nextEntry(cookie)) != NULL) {
718 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000719 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
720 ALOGE("Error fetching entry file name");
721 continue;
722 }
723
724 const String8 entryName(name);
725 const String8 path(entryName.getPathDir());
726 const String8 leaf(entryName.getPathLeaf());
727 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700728 if (entryName == CLOCK_FONT_ZIP_NAME) {
729 FileMap* map = zip->createEntryFileMap(entry);
730 if (map) {
731 animation.clockFont.map = map;
732 }
733 continue;
734 }
735
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400736 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000737 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100738 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000739 // supports only stored png files
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400740 if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000741 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400742 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000743 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000744 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700745 if (leaf == "audio.wav") {
746 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700747 part.audioData = (uint8_t *)map->getDataPtr();
748 part.audioLength = map->getDataLength();
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400749 partWithAudio = &part;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400750 } else if (leaf == "trim.txt") {
751 part.trimData.setTo((char const*)map->getDataPtr(),
752 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700753 } else {
754 Animation::Frame frame;
755 frame.name = leaf;
756 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400757 frame.trimWidth = animation.width;
758 frame.trimHeight = animation.height;
759 frame.trimX = 0;
760 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700761 part.frames.add(frame);
762 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700763 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700764 } else {
765 ALOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700766 }
767 }
768 }
769 }
770 }
771 }
772
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400773 // If there is trimData present, override the positioning defaults.
774 for (Animation::Part& part : animation.parts) {
775 const char* trimDataStr = part.trimData.string();
776 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
777 const char* endl = strstr(trimDataStr, "\n");
778 // No more trimData for this part.
779 if (endl == NULL) {
780 break;
781 }
782 String8 line(trimDataStr, endl - trimDataStr);
783 const char* lineStr = line.string();
784 trimDataStr = ++endl;
785 int width = 0, height = 0, x = 0, y = 0;
786 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
787 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
788 frame.trimWidth = width;
789 frame.trimHeight = height;
790 frame.trimX = x;
791 frame.trimY = y;
792 } else {
793 ALOGE("Error parsing trim.txt, line: %s", lineStr);
794 break;
795 }
796 }
797 }
798
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700799 // Create and initialize audioplay if there is a wav file in any of the animations.
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400800 // Do it on a separate thread so we don't hold up the animation intro.
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400801 if (partWithAudio != NULL) {
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700802 ALOGD("found audio.wav, creating playback engine");
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400803 mInitAudioThread = new InitAudioThread(partWithAudio->audioData,
804 partWithAudio->audioLength);
805 mInitAudioThread->run("BootAnimation::InitAudioThread", PRIORITY_NORMAL);
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700806 }
807
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400808 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000809
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700810 return true;
811}
812
813bool BootAnimation::movie()
814{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700815 Animation* animation = loadAnimation(mZipFileName);
816 if (animation == NULL)
817 return false;
818
Damien Bargiacchi97480862016-03-29 14:55:55 -0700819 bool anyPartHasClock = false;
820 for (size_t i=0; i < animation->parts.size(); i++) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700821 if(validClock(animation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700822 anyPartHasClock = true;
823 break;
824 }
825 }
826 if (!anyPartHasClock) {
827 mClockEnabled = false;
828 }
829
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530830 // Check if npot textures are supported
831 mUseNpotTextures = false;
832 String8 gl_extensions;
833 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
834 if (!exts) {
835 glGetError();
836 } else {
837 gl_extensions.setTo(exts);
838 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
839 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
840 mUseNpotTextures = true;
841 }
842 }
843
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800844 // Blend required to draw time on top of animation frames.
845 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700846 glShadeModel(GL_FLAT);
847 glDisable(GL_DITHER);
848 glDisable(GL_SCISSOR_TEST);
849 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700850
851 glBindTexture(GL_TEXTURE_2D, 0);
852 glEnable(GL_TEXTURE_2D);
853 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
854 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
855 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
856 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
857 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
858
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700859 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800860 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700861 clockFontInitialized =
862 (initFont(&animation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
863 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800864 }
865
Damien Bargiacchi97480862016-03-29 14:55:55 -0700866 if (mClockEnabled && !updateIsTimeAccurate()) {
867 mTimeCheckThread = new TimeCheckThread(this);
868 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
869 }
870
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700871 playAnimation(*animation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700872
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400873 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700874 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400875 mTimeCheckThread = nullptr;
876 }
877
Wei Wanga90c54c2017-02-02 11:35:21 -0800878 // We should have joined mInitAudioThread thread in playAnimation
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400879 if (mInitAudioThread != nullptr) {
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400880 mInitAudioThread = nullptr;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700881 }
882
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700883 releaseAnimation(animation);
884
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700885 if (clockFontInitialized) {
886 glDeleteTextures(1, &animation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700887 }
888
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700889 return false;
890}
891
892bool BootAnimation::playAnimation(const Animation& animation)
893{
894 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700895 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400896 const int animationX = (mWidth - animation.width) / 2;
897 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800898
Wei Wangdbdda2b2017-02-07 18:59:58 -0800899 ALOGD("BootAnimationShownTiming start time: %" PRId64 "ms", elapsedRealtime());
Narayan Kamathafd31e02013-12-03 13:16:03 +0000900 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700901 const Animation::Part& part(animation.parts[i]);
902 const size_t fcount = part.frames.size();
903 glBindTexture(GL_TEXTURE_2D, 0);
904
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700905 // Handle animation package
906 if (part.animation != NULL) {
907 playAnimation(*part.animation);
908 if (exitPending())
909 break;
910 continue; //to next part
911 }
912
Mathias Agopiana8826d62009-10-01 03:10:14 -0700913 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700914 // Exit any non playuntil complete parts immediately
915 if(exitPending() && !part.playUntilComplete)
916 break;
917
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700918 // only play audio file the first time we animate the part
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400919 if (r == 0 && part.audioData && playSoundsAllowed()) {
920 ALOGD("playing clip for part%d, size=%d", (int) i, part.audioLength);
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400921 // Block until the audio engine is finished initializing.
922 if (mInitAudioThread != nullptr) {
923 mInitAudioThread->join();
924 }
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400925 audioplay::playClip(part.audioData, part.audioLength);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700926 }
927
Jesse Hall083b84c2014-09-22 10:51:09 -0700928 glClearColor(
929 part.backgroundColor[0],
930 part.backgroundColor[1],
931 part.backgroundColor[2],
932 1.0f);
933
Narayan Kamathafd31e02013-12-03 13:16:03 +0000934 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700935 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700936 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700937
938 if (r > 0) {
939 glBindTexture(GL_TEXTURE_2D, frame.tid);
940 } else {
941 if (part.count != 1) {
942 glGenTextures(1, &frame.tid);
943 glBindTexture(GL_TEXTURE_2D, frame.tid);
944 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
945 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
946 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700947 int w, h;
948 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700949 }
950
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400951 const int xc = animationX + frame.trimX;
952 const int yc = animationY + frame.trimY;
953 Region clearReg(Rect(mWidth, mHeight));
954 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800955 if (!clearReg.isEmpty()) {
956 Region::const_iterator head(clearReg.begin());
957 Region::const_iterator tail(clearReg.end());
958 glEnable(GL_SCISSOR_TEST);
959 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700960 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400961 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800962 glClear(GL_COLOR_BUFFER_BIT);
963 }
964 glDisable(GL_SCISSOR_TEST);
965 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400966 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
967 // which is equivalent to mHeight - (yc + frame.trimHeight)
968 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
969 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700970 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
971 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800972 }
973
Mathias Agopiana8826d62009-10-01 03:10:14 -0700974 eglSwapBuffers(mDisplay, mSurface);
975
976 nsecs_t now = systemTime();
977 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700978 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700979 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700980
981 if (delay > 0) {
982 struct timespec spec;
983 spec.tv_sec = (now + delay) / 1000000000;
984 spec.tv_nsec = (now + delay) % 1000000000;
985 int err;
986 do {
987 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
988 } while (err<0 && errno == EINTR);
989 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700990
991 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700992 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700993
Mathias Agopiana8826d62009-10-01 03:10:14 -0700994 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700995
996 // For infinite parts, we've now played them at least once, so perhaps exit
997 if(exitPending() && !part.count)
998 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700999 }
1000
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001001 }
1002
1003 // Free textures created for looping parts now that the animation is done.
1004 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001005 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001006 const size_t fcount = part.frames.size();
1007 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001008 const Animation::Frame& frame(part.frames[j]);
1009 glDeleteTextures(1, &frame.tid);
1010 }
1011 }
1012 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001013
1014 // we've finally played everything we're going to play
1015 audioplay::setPlaying(false);
1016 audioplay::destroy();
1017
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001018 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001019}
1020
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001021void BootAnimation::releaseAnimation(Animation* animation) const
1022{
1023 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1024 e = animation->parts.end(); it != e; ++it) {
1025 if (it->animation)
1026 releaseAnimation(it->animation);
1027 }
1028 if (animation->zip)
1029 delete animation->zip;
1030 delete animation;
1031}
1032
1033BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1034{
1035 if (mLoadedFiles.indexOf(fn) >= 0) {
1036 ALOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
1037 fn.string());
1038 return NULL;
1039 }
1040 ZipFileRO *zip = ZipFileRO::open(fn);
1041 if (zip == NULL) {
1042 ALOGE("Failed to open animation zip \"%s\": %s",
1043 fn.string(), strerror(errno));
1044 return NULL;
1045 }
1046
1047 Animation *animation = new Animation;
1048 animation->fileName = fn;
1049 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001050 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001051 mLoadedFiles.add(animation->fileName);
1052
1053 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001054 if (!preloadZip(*animation)) {
1055 return NULL;
1056 }
1057
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001058
1059 mLoadedFiles.remove(fn);
1060 return animation;
1061}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001062
Geoffrey Pitsch290c4352016-08-09 14:35:10 -04001063bool BootAnimation::playSoundsAllowed() const {
1064 // Only play sounds for system boots, not runtime restarts.
1065 if (!mSystemBoot) {
1066 return false;
1067 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001068 if (mShuttingDown) { // no audio while shutting down
1069 return false;
1070 }
Geoffrey Pitsch290c4352016-08-09 14:35:10 -04001071 // Read the system property to see if we should play the sound.
1072 // If it's not present, default to allowed.
1073 if (!property_get_bool(PLAY_SOUND_PROP_NAME, 1)) {
1074 return false;
1075 }
1076
1077 // Don't play sounds if this is a reboot due to an error.
1078 char bootreason[PROPERTY_VALUE_MAX];
1079 if (property_get(BOOTREASON_PROP_NAME, bootreason, nullptr) > 0) {
1080 for (const auto& str : PLAY_SOUND_BOOTREASON_BLACKLIST) {
1081 if (strcasecmp(str.c_str(), bootreason) == 0) {
1082 return false;
1083 }
1084 }
1085 }
1086 return true;
1087}
1088
Damien Bargiacchi97480862016-03-29 14:55:55 -07001089bool BootAnimation::updateIsTimeAccurate() {
1090 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1091 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1092
1093 if (mTimeIsAccurate) {
1094 return true;
1095 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001096 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001097 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001098
1099 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1100 mTimeFormat12Hour = true;
1101 }
1102
Damien Bargiacchi97480862016-03-29 14:55:55 -07001103 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1104 mTimeIsAccurate = true;
1105 return true;
1106 }
1107
1108 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
1109 if (file != NULL) {
1110 long long lastChangedTime = 0;
1111 fscanf(file, "%lld", &lastChangedTime);
1112 fclose(file);
1113 if (lastChangedTime > 0) {
1114 struct timespec now;
1115 clock_gettime(CLOCK_REALTIME, &now);
1116 // Match the Java timestamp format
1117 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001118 if (ACCURATE_TIME_EPOCH < rtcNow
1119 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1120 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001121 mTimeIsAccurate = true;
1122 }
1123 }
1124 }
1125
1126 return mTimeIsAccurate;
1127}
1128
1129BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1130 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1131
1132BootAnimation::TimeCheckThread::~TimeCheckThread() {
1133 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1134 close(mInotifyFd);
1135}
1136
1137bool BootAnimation::TimeCheckThread::threadLoop() {
1138 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1139 && mBootAnimation->mClockEnabled;
1140 if (!shouldLoop) {
1141 close(mInotifyFd);
1142 mInotifyFd = -1;
1143 }
1144 return shouldLoop;
1145}
1146
1147bool BootAnimation::TimeCheckThread::doThreadLoop() {
1148 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1149
1150 // Poll instead of doing a blocking read so the Thread can exit if requested.
1151 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1152 ssize_t pollResult = poll(&pfd, 1, 1000);
1153
1154 if (pollResult == 0) {
1155 return true;
1156 } else if (pollResult < 0) {
1157 ALOGE("Could not poll inotify events");
1158 return false;
1159 }
1160
1161 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1162 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1163 if (length == 0) {
1164 return true;
1165 } else if (length < 0) {
1166 ALOGE("Could not read inotify events");
1167 return false;
1168 }
1169
1170 const struct inotify_event *event;
1171 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1172 event = (const struct inotify_event *) ptr;
1173 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1174 addTimeDirWatch();
1175 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1176 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1177 return !mBootAnimation->updateIsTimeAccurate();
1178 }
1179 }
1180
1181 return true;
1182}
1183
1184void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1185 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1186 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1187 if (mTimeWd > 0) {
1188 // No need to watch for the time directory to be created if it already exists
1189 inotify_rm_watch(mInotifyFd, mSystemWd);
1190 mSystemWd = -1;
1191 }
1192}
1193
1194status_t BootAnimation::TimeCheckThread::readyToRun() {
1195 mInotifyFd = inotify_init();
1196 if (mInotifyFd < 0) {
1197 ALOGE("Could not initialize inotify fd");
1198 return NO_INIT;
1199 }
1200
1201 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1202 if (mSystemWd < 0) {
1203 close(mInotifyFd);
1204 mInotifyFd = -1;
1205 ALOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
1206 return NO_INIT;
1207 }
1208
1209 addTimeDirWatch();
1210
1211 if (mBootAnimation->updateIsTimeAccurate()) {
1212 close(mInotifyFd);
1213 mInotifyFd = -1;
1214 return ALREADY_EXISTS;
1215 }
1216
1217 return NO_ERROR;
1218}
1219
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001220BootAnimation::InitAudioThread::InitAudioThread(uint8_t* exampleAudioData, int exampleAudioLength)
1221 : Thread(false),
1222 mExampleAudioData(exampleAudioData),
1223 mExampleAudioLength(exampleAudioLength) {}
1224
1225bool BootAnimation::InitAudioThread::threadLoop() {
1226 audioplay::create(mExampleAudioData, mExampleAudioLength);
1227 // Exit immediately
1228 return false;
1229}
1230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231// ---------------------------------------------------------------------------
1232
1233}
1234; // namespace android