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.SystemApi;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.provider.MediaCognitionService.ProcessingTypes;
24 
25 import androidx.annotation.NonNull;
26 
27 import com.android.providers.media.flags.Flags;
28 
29 import java.util.Arrays;
30 
31 
32 /**
33  * A class containing versions of different cognition processing methods that can be
34  * requested by MediaProvider. See {@link MediaCognitionService.ProcessingTypes}
35  *
36  * This will be sent by the implementation of MediaCognitionService in response of
37  * {@link MediaCognitionService#onGetProcessingVersions}.
38  *
39  * @hide
40  */
41 @SystemApi
42 @FlaggedApi(Flags.FLAG_MEDIA_COGNITION_SERVICE)
43 public final class MediaCognitionProcessingVersions implements Parcelable {
44     private int[] mProcessingVersions;
45 
46     /**
47      * @hide
48      */
MediaCognitionProcessingVersions(int[] processingVersions)49     MediaCognitionProcessingVersions(int[] processingVersions) {
50         mProcessingVersions = processingVersions;
51     }
52 
53     /**
54      * This will create an empty instance. Use {@link #setProcessingVersion} to set versions.
55      */
MediaCognitionProcessingVersions()56     public MediaCognitionProcessingVersions() {
57         mProcessingVersions = new int[ProcessingTypes.class.getDeclaredFields().length];
58         Arrays.fill(mProcessingVersions, -1);
59     }
60 
61     /**
62      * This function is used to set versions of different processing types
63      * implemented in MediaCognitionService. (like {@code IMAGE_LABEL})
64      *
65      * @param processingType Any one of the processing type
66      *                       from {@link MediaCognitionService.ProcessingTypes}
67      * @param version The current version of the processing.
68      */
setProcessingVersion(@ediaCognitionService.ProcessingType int processingType, int version)69     public void setProcessingVersion(@MediaCognitionService.ProcessingType int processingType,
70             int version) {
71         final int index = Integer.numberOfTrailingZeros(processingType);
72         if (!checkProcessingTypeCorrectness(processingType, index)) {
73             throw new IllegalArgumentException("Wrong Processing Type");
74         }
75         mProcessingVersions[index] = version;
76     }
77 
78     /**
79      * Get the versions of processing types set by {@link #setProcessingVersion(int, int)}
80      * If not set, default value is -1
81      *
82      * @param processingType Any one of the processing type
83      *                       from {@link MediaCognitionService.ProcessingTypes}
84      */
getProcessingVersion(@ediaCognitionService.ProcessingType int processingType)85     public int getProcessingVersion(@MediaCognitionService.ProcessingType  int processingType) {
86         final int index = Integer.numberOfTrailingZeros(processingType);
87         if (!checkProcessingTypeCorrectness(processingType, index)) {
88             throw new IllegalArgumentException("Wrong Processing Type");
89         }
90         return mProcessingVersions[index];
91     }
92 
checkProcessingTypeCorrectness( @ediaCognitionService.ProcessingType int processingType, int index)93     private boolean checkProcessingTypeCorrectness(
94             @MediaCognitionService.ProcessingType  int processingType, int index) {
95         if (Integer.bitCount(processingType) != 1 || index >= mProcessingVersions.length) {
96             return false;
97         }
98         return true;
99     }
100     /**
101      * {@inheritDoc}
102      */
103     @Override
describeContents()104     public int describeContents() {
105         return 0;
106     }
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
writeToParcel(@onNull Parcel dest, int flags)112     public void writeToParcel(@NonNull Parcel dest, int flags) {
113         dest.writeIntArray(mProcessingVersions);
114     }
115 
116     @NonNull
117     public static final Creator<MediaCognitionProcessingVersions> CREATOR =
118             new Creator<MediaCognitionProcessingVersions>() {
119                 @Override
120                 public MediaCognitionProcessingVersions createFromParcel(Parcel source) {
121                     int[] processingVersions =
122                             new int[ProcessingTypes.class.getDeclaredFields().length];
123                     source.readIntArray(processingVersions);
124                     return new MediaCognitionProcessingVersions(processingVersions);
125                 }
126 
127                 @Override
128                 public MediaCognitionProcessingVersions[] newArray(int size) {
129                     return new MediaCognitionProcessingVersions[size];
130                 }
131             };
132 }
133