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