• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 "AnimationParser.h"
18 
19 #include <android-base/stringprintf.h>
20 #include <android-base/strings.h>
21 
22 #include <cutils/klog.h>
23 
24 #include "animation.h"
25 
26 #define LOGE(x...) do { KLOG_ERROR("charger", x); } while (0)
27 #define LOGW(x...) do { KLOG_WARNING("charger", x); } while (0)
28 #define LOGV(x...) do { KLOG_DEBUG("charger", x); } while (0)
29 
30 namespace android {
31 
32 // Lines consisting of only whitespace or whitespace followed by '#' can be ignored.
can_ignore_line(const char * str)33 bool can_ignore_line(const char* str) {
34     for (int i = 0; str[i] != '\0' && str[i] != '#'; i++) {
35         if (!isspace(str[i])) return false;
36     }
37     return true;
38 }
39 
remove_prefix(const std::string & line,const char * prefix,const char ** rest)40 bool remove_prefix(const std::string& line, const char* prefix, const char** rest) {
41     const char* str = line.c_str();
42     int start;
43     char c;
44 
45     std::string format = base::StringPrintf(" %s%%n%%c", prefix);
46     if (sscanf(str, format.c_str(), &start, &c) != 1) {
47         return false;
48     }
49 
50     *rest = &str[start];
51     return true;
52 }
53 
parse_text_field(const char * in,animation::text_field * field)54 bool parse_text_field(const char* in, animation::text_field* field) {
55     int* x = &field->pos_x;
56     int* y = &field->pos_y;
57     int* r = &field->color_r;
58     int* g = &field->color_g;
59     int* b = &field->color_b;
60     int* a = &field->color_a;
61 
62     int start = 0, end = 0;
63 
64     if (sscanf(in, "c c %d %d %d %d %n%*s%n", r, g, b, a, &start, &end) == 4) {
65         *x = CENTER_VAL;
66         *y = CENTER_VAL;
67     } else if (sscanf(in, "c %d %d %d %d %d %n%*s%n", y, r, g, b, a, &start, &end) == 5) {
68         *x = CENTER_VAL;
69     } else if (sscanf(in, "%d c %d %d %d %d %n%*s%n", x, r, g, b, a, &start, &end) == 5) {
70         *y = CENTER_VAL;
71     } else if (sscanf(in, "%d %d %d %d %d %d %n%*s%n", x, y, r, g, b, a, &start, &end) != 6) {
72         return false;
73     }
74 
75     if (end == 0) return false;
76 
77     field->font_file.assign(&in[start], end - start);
78 
79     return true;
80 }
81 
parse_animation_desc(const std::string & content,animation * anim)82 bool parse_animation_desc(const std::string& content, animation* anim) {
83     static constexpr const char* animation_prefix = "animation: ";
84     static constexpr const char* fail_prefix = "fail: ";
85     static constexpr const char* clock_prefix = "clock_display: ";
86     static constexpr const char* percent_prefix = "percent_display: ";
87 
88     std::vector<animation::frame> frames;
89 
90     for (const auto& line : base::Split(content, "\n")) {
91         animation::frame frame;
92         const char* rest;
93 
94         if (can_ignore_line(line.c_str())) {
95             continue;
96         } else if (remove_prefix(line, animation_prefix, &rest)) {
97             int start = 0, end = 0;
98             if (sscanf(rest, "%d %d %n%*s%n", &anim->num_cycles, &anim->first_frame_repeats,
99                     &start, &end) != 2 ||
100                 end == 0) {
101                 LOGE("Bad animation format: %s\n", line.c_str());
102                 return false;
103             } else {
104                 anim->animation_file.assign(&rest[start], end - start);
105             }
106         } else if (remove_prefix(line, fail_prefix, &rest)) {
107             anim->fail_file.assign(rest);
108         } else if (remove_prefix(line, clock_prefix, &rest)) {
109             if (!parse_text_field(rest, &anim->text_clock)) {
110                 LOGE("Bad clock_display format: %s\n", line.c_str());
111                 return false;
112             }
113         } else if (remove_prefix(line, percent_prefix, &rest)) {
114             if (!parse_text_field(rest, &anim->text_percent)) {
115                 LOGE("Bad percent_display format: %s\n", line.c_str());
116                 return false;
117             }
118         } else if (sscanf(line.c_str(), " frame: %d %d %d",
119                 &frame.disp_time, &frame.min_level, &frame.max_level) == 3) {
120             frames.push_back(std::move(frame));
121         } else {
122             LOGE("Malformed animation description line: %s\n", line.c_str());
123             return false;
124         }
125     }
126 
127     if (anim->animation_file.empty() || frames.empty()) {
128         LOGE("Bad animation description. Provide the 'animation: ' line and at least one 'frame: ' "
129              "line.\n");
130         return false;
131     }
132 
133     anim->num_frames = frames.size();
134     anim->frames = new animation::frame[frames.size()];
135     std::copy(frames.begin(), frames.end(), anim->frames);
136 
137     return true;
138 }
139 
140 }  // namespace android
141