blob: 7426af56ecb1e6809cad3b5e708879d6dbc9e666 [file] [log] [blame]
Jeff Sharkeya4d1f222013-09-07 14:45:03 -07001/*
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
Steve McKay6c8bfb72015-10-05 12:32:46 -070019import static com.android.documentsui.Shared.DEBUG;
Steve McKayfefcd702015-08-20 16:19:38 +000020import static com.android.documentsui.Shared.TAG;
Jeff Sharkeyb7807e92013-10-29 11:48:26 -070021import static com.android.documentsui.model.DocumentInfo.getCursorLong;
22import static com.android.documentsui.model.DocumentInfo.getCursorString;
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070023
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) {
Jeff Sharkey9dd02622013-09-27 16:44:11 -070040 this(cursor, acceptMimes, null, Long.MIN_VALUE);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070041 }
42
43 public FilteringCursorWrapper(Cursor cursor, String[] acceptMimes, String[] rejectMimes) {
Jeff Sharkey9dd02622013-09-27 16:44:11 -070044 this(cursor, acceptMimes, rejectMimes, Long.MIN_VALUE);
45 }
46
47 public FilteringCursorWrapper(
48 Cursor cursor, String[] acceptMimes, String[] rejectMimes, long rejectBefore) {
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070049 mCursor = cursor;
50
51 final int count = cursor.getCount();
52 mPosition = new int[count];
53
54 cursor.moveToPosition(-1);
Jeff Sharkeyf73d81a2013-11-18 17:41:33 -080055 while (cursor.moveToNext() && mCount < count) {
Jeff Sharkeyb7807e92013-10-29 11:48:26 -070056 final String mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
57 final long lastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED);
Jeff Sharkeya35ac2d2013-09-10 12:04:26 -070058 if (rejectMimes != null && MimePredicate.mimeMatches(rejectMimes, mimeType)) {
59 continue;
60 }
Jeff Sharkey9dd02622013-09-27 16:44:11 -070061 if (lastModified < rejectBefore) {
62 continue;
63 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070064 if (MimePredicate.mimeMatches(acceptMimes, mimeType)) {
65 mPosition[mCount++] = cursor.getPosition();
66 }
67 }
68
Steve McKay6c8bfb72015-10-05 12:32:46 -070069 if (DEBUG && mCount != cursor.getCount()) {
70 Log.d(TAG, "Before filtering " + cursor.getCount() + ", after " + mCount);
71 }
Jeff Sharkeya4d1f222013-09-07 14:45:03 -070072 }
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}