1 /* 2 * Copyright (C) 2024 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 package com.android.documentsui.loaders 17 18 import android.os.Bundle 19 import java.time.Duration 20 21 /** 22 * The constant to be used for the maxResults parameter, if we wish to get all (unlimited) results. 23 */ 24 const val ALL_RESULTS: Int = -1 25 26 /** 27 * Common query options. These are: 28 * - maximum number to return; pass ALL_RESULTS to impose no limits. 29 * - maximum lastModified delta in milliseconds: the delta from now used to reject files that were 30 * not modified in the specified milliseconds; pass null for no limits. 31 * - maximum time the query should return, including empty, results; pass null for no limits. 32 * - whether or not to show hidden files. 33 * - A list of MIME types used to filter returned files. 34 * - "Other" query arguments not covered by the above. 35 * 36 * The "other" query arguments are added as due to existing code communicating information such 37 * as acceptable file kind (images, videos, etc.) is done via Bundle arguments. This could be 38 * and should be changed if this code ever is rewritten. 39 * TODO(b:397095797): Merge otherQueryArgs with acceptableMimeTypes and maxLastModifiedDelta. 40 */ 41 data class QueryOptions( 42 val maxResults: Int, 43 val maxLastModifiedDelta: Duration?, 44 val maxQueryTime: Duration?, 45 val showHidden: Boolean, 46 val acceptableMimeTypes: Array<String>, 47 val otherQueryArgs: Bundle, 48 ) { 49 equalsnull50 override fun equals(other: Any?): Boolean { 51 if (this === other) return true 52 if (javaClass != other?.javaClass) return false 53 54 other as QueryOptions 55 56 return maxResults == other.maxResults && 57 maxLastModifiedDelta == other.maxLastModifiedDelta && 58 maxQueryTime == other.maxQueryTime && 59 showHidden == other.showHidden && 60 acceptableMimeTypes.contentEquals(other.acceptableMimeTypes) 61 } 62 63 /** 64 * Helper method that computes the earliest valid last modified timestamp. Converts last 65 * modified duration to milliseconds past now. If the maxLastModifiedDelta is negative 66 * this method returns 0L. 67 */ getRejectBeforeTimestampnull68 fun getRejectBeforeTimestamp() = 69 if (maxLastModifiedDelta == null) { 70 0L 71 } else { 72 System.currentTimeMillis() - maxLastModifiedDelta.toMillis() 73 } 74 75 /** 76 * Helper function that indicates if query time is unlimited. Due to internal reliance on 77 * Java's Duration class it assumes anything larger than 60 seconds has unlimited waiting 78 * time. 79 */ isQueryTimeUnlimitednull80 fun isQueryTimeUnlimited() = maxQueryTime == null 81 82 override fun hashCode(): Int { 83 var result = maxResults 84 result = 31 * result + maxLastModifiedDelta.hashCode() 85 result = 31 * result + maxQueryTime.hashCode() 86 result = 31 * result + showHidden.hashCode() 87 result = 31 * result + acceptableMimeTypes.contentHashCode() 88 return result 89 } 90 } 91