aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/filemanager/service/MimeTypeIndexService.java
blob: 7f92692071b340e6b6a1ad49106d366e8b0a7664 (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
/*
* Copyright (C) 2014 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 com.cyanogenmod.filemanager.service;

import android.app.IntentService;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import com.cyanogenmod.filemanager.providers.MimeTypeIndexProvider;
import com.cyanogenmod.filemanager.util.MimeTypeHelper;
import com.cyanogenmod.filemanager.util.MimeTypeHelper.MimeTypeCategory;

import java.io.File;
import java.io.FileFilter;
import java.util.HashMap;
import java.util.Map;

/**
 * MimeTypeIndexService
 * <pre>
 *    Service intended to index space used by mime type
 * </pre>
 *
 * @see {@link android.app.IntentService}
 */
public class MimeTypeIndexService extends IntentService {

    // Constants
    private static final String TAG = MimeTypeIndexService.class.getSimpleName();
    public static final String ACTION_START_INDEX = "com.cyanogenmod.filemanager" +
            ".ACTION_START_INDEX";
    public static final String EXTRA_FILE_ROOT = "extra_file_root";

    /**
     * Constructor
     */
    public MimeTypeIndexService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.v(TAG, "onHandleIntent(" + intent + ")");
        if (intent == null) {
            Log.w(TAG, "Intent passed was null");
            return;
        }
        String action = intent.getAction();
        Log.d(TAG, "Action: " + action);
        if (TextUtils.isEmpty(action)) {
            Log.w(TAG, "Failed to parse action");
            return;
        }
        String fileRoot = intent.getStringExtra(EXTRA_FILE_ROOT);
        if (TextUtils.isEmpty(fileRoot)) {
            Log.w(TAG, "Empty file root, bailing out");
            return;
        }
        if (ACTION_START_INDEX.equalsIgnoreCase(action)) {
            performIndexAction(fileRoot);
        }
    }

    private void performIndexAction(String fileRoot) {
        Log.v(TAG, "performIndexAction(" + fileRoot + ")");
        if (TextUtils.isEmpty(fileRoot)) {
            Log.w(TAG, "Empty or null file root '" + fileRoot + "'");
            return;
        }
        Log.i(TAG, "Starting mime type usage indexing on '" + fileRoot + "'");
        fileRoot = fileRoot.trim();
        File rootFile = new File(fileRoot);
        Map<MimeTypeCategory, Long> spaceCalculationMap =
                new HashMap<MimeTypeCategory, Long>();
        calculateUsageByType(rootFile, spaceCalculationMap);
        ContentValues[] valuesList = new ContentValues[spaceCalculationMap.keySet().size()];
        int i = 0;
        for (MimeTypeCategory category : spaceCalculationMap.keySet()) {
            Log.d(TAG, "" + category + " = " + spaceCalculationMap.get(category));
            ContentValues values = new ContentValues();
            values.put(MimeTypeIndexProvider.COLUMN_FILE_ROOT, fileRoot);
            values.put(MimeTypeIndexProvider.COLUMN_CATEGORY, category.name());
            values.put(MimeTypeIndexProvider.COLUMN_SIZE, spaceCalculationMap.get(category));
            valuesList[i] = values;
            i++;
        }
        MimeTypeIndexProvider.clearMountPointUsages(this, fileRoot); // Clear old data
        getContentResolver().bulkInsert(MimeTypeIndexProvider.getContentUri(), valuesList);
    }

    private class FileOnlyFileFilter implements FileFilter {
        @Override
        public boolean accept(File file) {
            return file != null && !file.isDirectory() && file.isFile();
        }
    }

    private class DirectoryOnlyFileFilter implements FileFilter {
        @Override
        public boolean accept(File file) {
            return file != null && file.isDirectory();
        }
    }

    private void calculateUsageByType(File root, Map<MimeTypeCategory, Long> groupUsageMap) {
        File[] dirs = root.listFiles(new DirectoryOnlyFileFilter());
        File[] files = root.listFiles(new FileOnlyFileFilter());
        if (dirs != null) {
            // Recurse directories
            for (File dir : dirs) {
                calculateUsageByType(dir, groupUsageMap);
            }
        }
        if (files != null) {
            // Iterate every file
            for (File file : files) {
                MimeTypeCategory category = MimeTypeHelper.getCategory(this, file);
                long size = file.length();
                if (!groupUsageMap.containsKey(category)) {
                    groupUsageMap.put(category, size);
                } else {
                    long newSum = groupUsageMap.get(category) + size;
                    groupUsageMap.put(category, newSum);
                }
            }
        }
    }

    /**
     * Kick off an indexing job for the provided file root or mount point root
     *
     * @param context  {@link android.content.Context}
     * @param fileRoot {@link java.lang.String}
     *
     * @throws IllegalArgumentException {@link java.lang.IllegalArgumentException}
     */
    public static void indexFileRoot(Context context, String fileRoot) throws
            IllegalArgumentException {
        if (context == null) {
            throw new IllegalArgumentException("'context' cannot be null");
        }
        // Start indexing the external storage
        Intent intent = new Intent(context, MimeTypeIndexService.class);
        intent.setAction(MimeTypeIndexService.ACTION_START_INDEX);
        intent.putExtra(MimeTypeIndexService.EXTRA_FILE_ROOT, fileRoot);
        context.startService(intent);
    }

}