Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.documentsui; |
| 18 | |
Steve McKay | 6c8bfb7 | 2015-10-05 12:32:46 -0700 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 20 | import static com.android.documentsui.Shared.TAG; |
Jeff Sharkey | b7807e9 | 2013-10-29 11:48:26 -0700 | [diff] [blame] | 21 | import static com.android.documentsui.model.DocumentInfo.getCursorLong; |
| 22 | import static com.android.documentsui.model.DocumentInfo.getCursorString; |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 23 | |
| 24 | import android.database.AbstractCursor; |
| 25 | import android.database.Cursor; |
| 26 | import android.os.Bundle; |
| 27 | import android.provider.DocumentsContract.Document; |
| 28 | import android.util.Log; |
| 29 | |
| 30 | /** |
| 31 | * Cursor wrapper that filters MIME types not matching given list. |
| 32 | */ |
| 33 | public 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 Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 40 | this(cursor, acceptMimes, null, Long.MIN_VALUE); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | public FilteringCursorWrapper(Cursor cursor, String[] acceptMimes, String[] rejectMimes) { |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 44 | this(cursor, acceptMimes, rejectMimes, Long.MIN_VALUE); |
| 45 | } |
| 46 | |
| 47 | public FilteringCursorWrapper( |
| 48 | Cursor cursor, String[] acceptMimes, String[] rejectMimes, long rejectBefore) { |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 49 | mCursor = cursor; |
| 50 | |
| 51 | final int count = cursor.getCount(); |
| 52 | mPosition = new int[count]; |
| 53 | |
| 54 | cursor.moveToPosition(-1); |
Jeff Sharkey | f73d81a | 2013-11-18 17:41:33 -0800 | [diff] [blame] | 55 | while (cursor.moveToNext() && mCount < count) { |
Jeff Sharkey | b7807e9 | 2013-10-29 11:48:26 -0700 | [diff] [blame] | 56 | final String mimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE); |
| 57 | final long lastModified = getCursorLong(cursor, Document.COLUMN_LAST_MODIFIED); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 58 | if (rejectMimes != null && MimePredicate.mimeMatches(rejectMimes, mimeType)) { |
| 59 | continue; |
| 60 | } |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 61 | if (lastModified < rejectBefore) { |
| 62 | continue; |
| 63 | } |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 64 | if (MimePredicate.mimeMatches(acceptMimes, mimeType)) { |
| 65 | mPosition[mCount++] = cursor.getPosition(); |
| 66 | } |
| 67 | } |
| 68 | |
Steve McKay | 6c8bfb7 | 2015-10-05 12:32:46 -0700 | [diff] [blame] | 69 | if (DEBUG && mCount != cursor.getCount()) { |
| 70 | Log.d(TAG, "Before filtering " + cursor.getCount() + ", after " + mCount); |
| 71 | } |
Jeff Sharkey | a4d1f22 | 2013-09-07 14:45:03 -0700 | [diff] [blame] | 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 | } |