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 17 package android.provider; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.NonNull; 21 import android.annotation.SuppressLint; 22 import android.annotation.SystemApi; 23 import android.net.Uri; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 import android.provider.MediaCognitionService.ProcessingCombination; 27 import android.provider.MediaCognitionService.ProcessingType; 28 29 import com.android.providers.media.flags.Flags; 30 31 32 /** 33 * This is a request made by MediaProvider to the implementation of {@link MediaCognitionService}. 34 * This contains request for a single media. 35 * 36 * @hide 37 */ 38 @SystemApi 39 @FlaggedApi(Flags.FLAG_MEDIA_COGNITION_SERVICE) 40 public final class MediaCognitionProcessingRequest implements Parcelable { 41 private Uri mUri; 42 43 @ProcessingCombination 44 private int mProcessingCombination; 45 46 MediaCognitionProcessingRequest(Builder builder)47 private MediaCognitionProcessingRequest(Builder builder) { 48 mUri = builder.mUri; 49 mProcessingCombination = builder.mProcessingCombination; 50 } 51 52 /** 53 * Builder class to build {@link MediaCognitionProcessingRequest} instances. 54 */ 55 public static final class Builder { 56 private Uri mUri; // Required 57 @ProcessingCombination 58 private int mProcessingCombination; 59 60 Builder(@onNull Uri uri)61 public Builder(@NonNull Uri uri) { 62 this.mUri = uri; 63 } 64 65 /** 66 * set processing mode of the request. 67 * @param processingCombination bitmask of requests that can contain 68 * any combination of {@link MediaCognitionService.ProcessingTypes} 69 */ 70 @NonNull setProcessingCombination(@rocessingCombination int processingCombination)71 public Builder setProcessingCombination(@ProcessingCombination int processingCombination) { 72 this.mProcessingCombination = processingCombination; 73 return this; 74 } 75 76 /** 77 * add processing types in the request. 78 * @param processingRequest a single processing request that can contain any processing type 79 * from {@link MediaCognitionService.ProcessingTypes} 80 */ 81 @SuppressLint("MissingGetterMatchingBuilder") 82 @NonNull addProcessingRequest(@rocessingType int processingRequest)83 public Builder addProcessingRequest(@ProcessingType int processingRequest) { 84 this.mProcessingCombination |= processingRequest; 85 return this; 86 } 87 88 /** 89 * Instantiate a {@link MediaCognitionProcessingRequest} from this {@link Builder} 90 * @return {@link MediaCognitionProcessingRequest} representation of this builder 91 */ 92 @NonNull build()93 public MediaCognitionProcessingRequest build() { 94 return new MediaCognitionProcessingRequest(this); 95 } 96 } 97 98 /** 99 * Get {@link Uri} of the media for which the request of processing is made for 100 * @return {@link Uri} of the media 101 */ 102 @NonNull getUri()103 public Uri getUri() { 104 return mUri; 105 } 106 107 /** 108 * Use this function to check if processing is requested for a specific processing type 109 * @param processingType Any processing type from {@link MediaCognitionService.ProcessingTypes} 110 * @return true if processing requested, false otherwise 111 */ checkProcessingRequired(@rocessingCombination int processingType)112 public boolean checkProcessingRequired(@ProcessingCombination int processingType) { 113 return (mProcessingCombination & processingType) == processingType; 114 } 115 116 /** 117 * This function returns a bitmask for all the processing that has been requested for. 118 * 119 * @return bitmask of all processing requests 120 * based on {@link MediaCognitionService.ProcessingTypes} 121 */ 122 @ProcessingCombination getProcessingCombination()123 public int getProcessingCombination() { 124 return mProcessingCombination; 125 } 126 127 /** 128 * {@inheritDoc} 129 */ 130 @Override describeContents()131 public int describeContents() { 132 return 0; 133 } 134 135 /** 136 * {@inheritDoc} 137 */ 138 @Override writeToParcel(@onNull Parcel dest, int flags)139 public void writeToParcel(@NonNull Parcel dest, int flags) { 140 dest.writeString(mUri.toString()); 141 dest.writeInt(mProcessingCombination); 142 } 143 144 @NonNull 145 public static final Creator<MediaCognitionProcessingRequest> CREATOR = 146 new Creator<MediaCognitionProcessingRequest>() { 147 @Override 148 public MediaCognitionProcessingRequest createFromParcel(Parcel source) { 149 Uri uri = Uri.parse(source.readString()); 150 int processingCombination = source.readInt(); 151 152 return new MediaCognitionProcessingRequest.Builder(uri) 153 .setProcessingCombination(processingCombination) 154 .build(); 155 } 156 157 @Override 158 public MediaCognitionProcessingRequest[] newArray(int size) { 159 return new MediaCognitionProcessingRequest[size]; 160 } 161 }; 162 163 } 164