1 /*
2  * Copyright (C) 2021 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.providers.media.cloudproviders;
18 
19 import static android.provider.CloudMediaProviderContract.EXTRA_PAGE_TOKEN;
20 
21 import static com.android.providers.media.PickerProviderMediaGenerator.MediaGenerator;
22 
23 import android.content.res.AssetFileDescriptor;
24 import android.database.Cursor;
25 import android.graphics.Point;
26 import android.os.Bundle;
27 import android.os.CancellationSignal;
28 import android.os.ParcelFileDescriptor;
29 import android.provider.CloudMediaProvider;
30 
31 import com.android.providers.media.PickerProviderMediaGenerator;
32 import com.android.providers.media.photopicker.data.CloudProviderQueryExtras;
33 
34 import java.io.FileNotFoundException;
35 
36 /**
37  * Implements a cloud {@link CloudMediaProvider} interface over items generated with
38  * {@link MediaGenerator}
39  */
40 public class CloudProviderPrimary extends CloudMediaProvider {
41     public static final String AUTHORITY =
42             "com.android.providers.media.photopicker.tests.cloud_primary";
43 
44     private final MediaGenerator mMediaGenerator =
45             PickerProviderMediaGenerator.getMediaGenerator(AUTHORITY);
46 
47     @Override
onCreate()48     public boolean onCreate() {
49         return true;
50     }
51 
52     @Override
onQueryMedia(Bundle extras)53     public Cursor onQueryMedia(Bundle extras) {
54         final CloudProviderQueryExtras queryExtras =
55                 CloudProviderQueryExtras.fromCloudMediaBundle(extras);
56 
57         String pageToken = extras.getString(EXTRA_PAGE_TOKEN, null);
58 
59         return mMediaGenerator.getMedia(queryExtras.getGeneration(), queryExtras.getAlbumId(),
60                 queryExtras.getMimeTypes(), queryExtras.getSizeBytes(), pageToken,
61                 queryExtras.getPageSize());
62     }
63 
64     @Override
onQueryDeletedMedia(Bundle extras)65     public Cursor onQueryDeletedMedia(Bundle extras) {
66         final CloudProviderQueryExtras queryExtras =
67                 CloudProviderQueryExtras.fromCloudMediaBundle(extras);
68 
69         String pageToken = extras.getString(EXTRA_PAGE_TOKEN, null);
70         return mMediaGenerator.getDeletedMedia(queryExtras.getGeneration(), pageToken);
71     }
72 
73     @Override
onQueryAlbums(Bundle extras)74     public Cursor onQueryAlbums(Bundle extras) {
75         final CloudProviderQueryExtras queryExtras =
76                 CloudProviderQueryExtras.fromCloudMediaBundle(extras);
77 
78         String pageToken = extras.getString(EXTRA_PAGE_TOKEN, null);
79         return mMediaGenerator.getAlbums(queryExtras.getMimeTypes(), queryExtras.getSizeBytes(),
80                 /* isLocal */ false, pageToken);
81     }
82 
83     @Override
onOpenPreview(String mediaId, Point size, Bundle extras, CancellationSignal signal)84     public AssetFileDescriptor onOpenPreview(String mediaId, Point size, Bundle extras,
85             CancellationSignal signal) throws FileNotFoundException {
86         throw new UnsupportedOperationException("onOpenPreview not supported");
87     }
88 
89     @Override
onOpenMedia(String mediaId, Bundle extras, CancellationSignal signal)90     public ParcelFileDescriptor onOpenMedia(String mediaId, Bundle extras,
91             CancellationSignal signal) throws FileNotFoundException {
92         throw new UnsupportedOperationException("onOpenMedia not supported");
93     }
94 
95     @Override
onGetMediaCollectionInfo(Bundle extras)96     public Bundle onGetMediaCollectionInfo(Bundle extras) {
97         return mMediaGenerator.getMediaCollectionInfo();
98     }
99 }
100