aboutsummaryrefslogtreecommitdiffstats
path: root/cm/lib/main/java/org/cyanogenmod/platform/internal/IconCacheManagerService.java
blob: c8c6d3ef065eb2c9bdc0fd79309b613d2846b480 (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
/*
 * 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.platform.internal;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Binder;
import android.os.FileUtils;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.android.server.SystemService;
import cyanogenmod.app.CMContextConstants;

import org.cyanogenmod.internal.themes.IIconCacheManager;
import org.cyanogenmod.internal.util.ThemeUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Comparator;

/** @hide */
public class IconCacheManagerService extends CMSystemService {
    private static final String TAG = IconCacheManagerService.class.getSimpleName();

    private static final long MAX_ICON_CACHE_SIZE = 33554432L; // 32MB
    private static final long PURGED_ICON_CACHE_SIZE = 25165824L; // 24 MB

    private long mIconCacheSize = 0L;
    private Context mContext;

    public IconCacheManagerService(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public String getFeatureDeclaration() {
        return CMContextConstants.Features.THEMES;
    }

    @Override
    public void onStart() {
        Log.d(TAG, "registerIconCache cmiconcache: " + this);
        publishBinderService(CMContextConstants.CM_ICON_CACHE_SERVICE, mService);
    }

    private void purgeIconCache() {
        Log.d(TAG, "Purging icon cahe of size " + mIconCacheSize);
        File cacheDir = new File(ThemeUtils.SYSTEM_THEME_ICON_CACHE_DIR);
        File[] files = cacheDir.listFiles();
        Arrays.sort(files, mOldestFilesFirstComparator);
        for (File f : files) {
            if (!f.isDirectory()) {
                final long size = f.length();
                if(f.delete()) mIconCacheSize -= size;
            }
            if (mIconCacheSize <= PURGED_ICON_CACHE_SIZE) break;
        }
    }

    private Comparator<File> mOldestFilesFirstComparator = new Comparator<File>() {
        @Override
        public int compare(File lhs, File rhs) {
            return (int) (lhs.lastModified() - rhs.lastModified());
        }
    };

    private IBinder mService = new IIconCacheManager.Stub() {
        @Override
        public boolean cacheComposedIcon(Bitmap icon, String fileName) throws RemoteException {
            final long token = Binder.clearCallingIdentity();
            boolean success;
            FileOutputStream os;
            final File cacheDir = new File(ThemeUtils.SYSTEM_THEME_ICON_CACHE_DIR);
            if (cacheDir.listFiles().length == 0) {
                mIconCacheSize = 0;
            }
            try {
                File outFile = new File(cacheDir, fileName);
                os = new FileOutputStream(outFile);
                icon.compress(Bitmap.CompressFormat.PNG, 90, os);
                os.close();
                FileUtils.setPermissions(outFile,
                        FileUtils.S_IRWXU | FileUtils.S_IRWXG | FileUtils.S_IROTH,
                        -1, -1);
                mIconCacheSize += outFile.length();
                if (mIconCacheSize > MAX_ICON_CACHE_SIZE) {
                    purgeIconCache();
                }
                success = true;
            } catch (Exception e) {
                success = false;
                Log.w(TAG, "Unable to cache icon " + fileName, e);
            }
            Binder.restoreCallingIdentity(token);
            return success;
        }

    };
}