summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/theme/util/BootAnimationHelper.java
blob: 1bd4ee7182fd9852e03c59c599e50a55dfd65c0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (C) 2016 Cyanogen, Inc.
 * Copyright (C) 2016 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cyanogenmod.theme.util;

import android.app.ActivityManager;
import android.content.Context;
import android.content.res.ThemeConfig;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class BootAnimationHelper {
    private static final String TAG = BootAnimationHelper.class.getSimpleName();
    private static final int MAX_REPEAT_COUNT = 3;

    public static final String THEME_INTERNAL_BOOT_ANI_PATH =
            "assets/bootanimation/bootanimation.zip";
    public static final String SYSTEM_BOOT_ANI_PATH = "/system/media/bootanimation.zip";
    public static final String CACHED_SUFFIX = "_bootanimation.zip";

    public static final int NUM_FIRST_LINE_PARAMETERS = 3;
    public static final int NUM_PART_LINE_PARAMETERS = 4;

    public static class AnimationPart {
        /**
         * Number of times to play this part
         */
        public int playCount;
        /**
         * If non-zero, pause for the given # of seconds before moving on to next part.
         */
        public int pause;
        /**
         * The name of this part
         */
        public String partName;
        /**
         * Time each frame is displayed
         */
        public int frameRateMillis;
        /**
         * List of file names for the given frames in this part
         */
        public List<String> frames;
        /**
         * width of the animation
         */
        public int width;
        /**
         * height of the animation
         */
        public int height;

        public AnimationPart(int playCount, int pause, String partName, int frameRateMillis,
                             int width, int height) {
            this.playCount = playCount == 0 ? MAX_REPEAT_COUNT : playCount;
            this.pause = pause;
            this.partName = partName;
            this.frameRateMillis = frameRateMillis;
            this.width = width;
            this.height = height;
            frames = new ArrayList<String>();
        }

        public void addFrame(String frame) {
            frames.add(frame);
        }
    }

    /**
     * Gather up all the details for the given boot animation
     * @param zip The bootanimation.zip
     * @return A list of AnimationPart if successful, null if not.
     * @throws IOException
     */
    public static List<AnimationPart> parseAnimation(ZipFile zip)
            throws IOException, BootAnimationException {
        if (zip == null) {
            // To make tracking down boot animation problems we'll throw a BootAnimationException
            // instead of an IllegalArgumentException.
            throw new BootAnimationException("Boot animation ZipFile cannot be null");
        }
        List<AnimationPart> animationParts = null;

        ZipEntry ze = zip.getEntry("desc.txt");
        if (ze != null) {
            animationParts = parseDescription(zip.getInputStream(ze));
        } else {
            throw new BootAnimationException("Missing desc.txt in root of bootanimation.zip");
        }

        if (animationParts == null) {
            // We really should not end up here but in case we do here's an exception for ya!
            throw new BootAnimationException("Unable to load boot animation.");
        }

        Iterator<AnimationPart> iterator = animationParts.iterator();
        while(iterator.hasNext()) {
            AnimationPart a = iterator.next();
            for (Enumeration<? extends ZipEntry> e = zip.entries();e.hasMoreElements();) {
                ze = e.nextElement();
                if (!ze.isDirectory() && ze.getName().contains(a.partName)) {
                    a.addFrame(ze.getName());
                }
            }
            if (a.frames.size() > 0) {
                Collections.sort(a.frames);
            } else {
                // This boot animation may be salvageable if there are still some other parts
                // that are good.  We'll remove this part and if there are no parts left by
                // the time we have iterated over all the parts then we can throw an exception.
                Log.w(TAG, String.format("No frames in part %s, removing from animation",
                        a.partName));
                iterator.remove();
            }
        }
        if (animationParts.size() == 0) {
            throw new BootAnimationException("Boot animation must have at least one part.");
        }

        return animationParts;
    }

    /**
     * Parses the desc.txt of the boot animation
     * @param in InputStream to the desc.txt
     * @return A list of the parts as given in desc.txt
     * @throws IOException
     */
    private static List<AnimationPart> parseDescription(InputStream in)
            throws IOException, BootAnimationException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        // read in suggested width, height, and frame rate from first line
        String line = reader.readLine();
        String[] details = line.split(" ");
        if (details.length != NUM_FIRST_LINE_PARAMETERS) {
            throw new BootAnimationException(String.format(
                    "Invalid # of parameters on first line of desc.txt; exptected %d, read %d  " +
                            "(\"%s\")",
                    NUM_FIRST_LINE_PARAMETERS, details.length, line));
        }

        // The items should be in the following order: width, height, frame rate
        final int width = Integer.parseInt(details[0]);
        final int height = Integer.parseInt(details[1]);
        final int frameRateMillis = 1000 / Integer.parseInt(details[2]);

        List<AnimationPart> animationParts = new ArrayList<AnimationPart>();
        while ((line = reader.readLine()) != null) {
            // trim off any leading and trailing spaces
            line = line.trim();
            // if the line is empty continue on to the next
            if (TextUtils.isEmpty(line)) continue;

            String[] info = line. split(" ");
            if (info.length != NUM_PART_LINE_PARAMETERS) {
                Log.w(TAG, String.format(
                        "Invalid # of part parameters; exptected %d, read %d  (\"%s\")",
                        NUM_PART_LINE_PARAMETERS, info.length, line));
                // let's continue in case there are parts that are valid
                continue;
            }
            if (!info[0].equals("p") && !info[0].equals("c")) {
                Log.w(TAG, String.format(
                   "Unknown part type; expected 'p' or 'c', read %s  (\"%s\")", info[0], line));

                // let's continue in case there are parts that are valid
                continue;
            }
            int playCount = Integer.parseInt(info[1]);
            int pause = Integer.parseInt(info[2]);
            String name = info[3];
            AnimationPart ap = new AnimationPart(playCount, pause, name, frameRateMillis,
                    width, height);
            animationParts.add(ap);
        }

        return animationParts;
    }

    public static String getPreviewFrameEntryName(InputStream is) throws IOException {
        ZipInputStream zis = (is instanceof ZipInputStream) ? (ZipInputStream) is
                : new ZipInputStream(new BufferedInputStream(is));
        ZipEntry ze;
        // First thing to do is iterate over all the entries and the zip and store them
        // for building the animations afterwards
        String previewName = null;
        while ((ze = zis.getNextEntry()) != null) {
            final String entryName = ze.getName();
            if (entryName.contains("/")
                    && (entryName.endsWith(".png") || entryName.endsWith(".jpg"))) {
                previewName = entryName;
            }
        }

        return previewName;
    }

    public static Bitmap loadPreviewFrame(Context context, InputStream is, String previewName)
            throws IOException {
        ZipInputStream zis = (is instanceof ZipInputStream) ? (ZipInputStream) is
                : new ZipInputStream(new BufferedInputStream(is));
        ZipEntry ze;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = am.isLowRamDevice() ? 4 : 2;
        opts.inPreferredConfig = Bitmap.Config.RGB_565;
        // First thing to do is iterate over all the entries and the zip and store them
        // for building the animations afterwards
        Bitmap preview = null;
        while ((ze = zis.getNextEntry()) != null && preview == null) {
            final String entryName = ze.getName();
            if (entryName.equals(previewName)) {
                preview = BitmapFactory.decodeStream(zis, null, opts);
            }
        }
        zis.close();

        return preview;
    }

    public static void clearBootAnimationCache(Context context) {
        File cache = context.getCacheDir();
        if (cache.exists()) {
            for(File f : cache.listFiles()) {
                // volley stores stuff in cache so don't delete the volley directory
                if(!f.isDirectory() && f.getName().endsWith(CACHED_SUFFIX)) f.delete();
            }
        }
    }

    public static class LoadBootAnimationImage extends AsyncTask<Object, Void, Bitmap> {
        private ImageView imv;
        private String path;
        private Context context;

        public LoadBootAnimationImage(ImageView imv, Context context, String path) {
            this.imv = imv;
            this.context = context;
            this.path = path;
        }

        @Override
        protected Bitmap doInBackground(Object... params) {
            Bitmap bitmap = null;
            String previewName = null;
            // this is ugly, ugly, ugly.  Did I mention this is ugly?
            try {
                if (ThemeConfig.SYSTEM_DEFAULT.equals(path)) {
                    previewName = getPreviewFrameEntryName(
                            new FileInputStream(SYSTEM_BOOT_ANI_PATH));
                    bitmap = loadPreviewFrame(
                            context, new FileInputStream(SYSTEM_BOOT_ANI_PATH), previewName);
                } else {
                    final Context themeCtx = context.createPackageContext(path, 0);
                    previewName = getPreviewFrameEntryName(
                            themeCtx.getAssets().open("bootanimation/bootanimation.zip"));
                    bitmap = loadPreviewFrame(context,
                            themeCtx.getAssets().open("bootanimation/bootanimation.zip"),
                            previewName);
                }
            } catch (Exception e) {
                // don't care since a null bitmap will be returned
                e.printStackTrace();
            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            if (result != null && imv != null) {
                imv.setVisibility(View.VISIBLE);
                imv.setImageBitmap(result);
            }
        }
    }

    public static class BootAnimationException extends Exception {
        public BootAnimationException(String detailMessage) {
            super(detailMessage);
        }
    }
}