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