FilteringCursorWrapper.java revision d182bb641f228b2d28527a6aa86075f6358ab838
1/*
2 * Copyright (C) 2013 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
17package com.android.documentsui;
18
19import static com.android.documentsui.DocumentsActivity.TAG;
20
21import android.database.AbstractCursor;
22import android.database.Cursor;
23import android.os.Bundle;
24import android.provider.DocumentsContract.Document;
25import android.util.Log;
26
27/**
28 * Cursor wrapper that filters MIME types not matching given list.
29 */
30public class FilteringCursorWrapper extends AbstractCursor {
31    private final Cursor mCursor;
32
33    private final int[] mPosition;
34    private int mCount;
35
36    public FilteringCursorWrapper(Cursor cursor, String[] acceptMimes) {
37        mCursor = cursor;
38
39        final int count = cursor.getCount();
40        mPosition = new int[count];
41
42        cursor.moveToPosition(-1);
43        while (cursor.moveToNext()) {
44            final String mimeType = cursor.getString(
45                    cursor.getColumnIndex(Document.COLUMN_MIME_TYPE));
46            if (MimePredicate.mimeMatches(acceptMimes, mimeType)) {
47                mPosition[mCount++] = cursor.getPosition();
48            }
49        }
50
51        Log.d(TAG, "Before filtering " + cursor.getCount() + ", after " + mCount);
52    }
53
54    @Override
55    public Bundle getExtras() {
56        return mCursor.getExtras();
57    }
58
59    @Override
60    public void close() {
61        super.close();
62        mCursor.close();
63    }
64
65    @Override
66    public boolean onMove(int oldPosition, int newPosition) {
67        return mCursor.moveToPosition(mPosition[newPosition]);
68    }
69
70    @Override
71    public String[] getColumnNames() {
72        return mCursor.getColumnNames();
73    }
74
75    @Override
76    public int getCount() {
77        return mCount;
78    }
79
80    @Override
81    public double getDouble(int column) {
82        return mCursor.getDouble(column);
83    }
84
85    @Override
86    public float getFloat(int column) {
87        return mCursor.getFloat(column);
88    }
89
90    @Override
91    public int getInt(int column) {
92        return mCursor.getInt(column);
93    }
94
95    @Override
96    public long getLong(int column) {
97        return mCursor.getLong(column);
98    }
99
100    @Override
101    public short getShort(int column) {
102        return mCursor.getShort(column);
103    }
104
105    @Override
106    public String getString(int column) {
107        return mCursor.getString(column);
108    }
109
110    @Override
111    public int getType(int column) {
112        return mCursor.getType(column);
113    }
114
115    @Override
116    public boolean isNull(int column) {
117        return mCursor.isNull(column);
118    }
119}
120