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.photopicker.data;
18 
19 import android.content.ClipData;
20 import android.content.Intent;
21 import android.net.Uri;
22 
23 import androidx.annotation.NonNull;
24 
25 import com.android.providers.media.PickerUriResolver;
26 import com.android.providers.media.photopicker.data.model.Item;
27 import com.android.providers.media.photopicker.data.model.UserId;
28 
29 import com.google.common.annotations.VisibleForTesting;
30 
31 import java.util.ArrayList;
32 import java.util.List;
33 
34 /**
35  * This class is responsible for returning result to the caller of the PhotoPicker.
36  */
37 public class PickerResult {
38 
39     /**
40      * @return {@code Intent} which contains Uri that has been granted access on.
41      */
42     @NonNull
getPickerResponseIntent(String action, boolean canSelectMultiple, @NonNull List<Item> selectedItems)43     public static Intent getPickerResponseIntent(String action, boolean canSelectMultiple,
44             @NonNull List<Item> selectedItems) {
45         // 1. Get Picker Uris corresponding to the selected items
46         List<Uri> selectedUris = getPickerUrisForItems(action, selectedItems);
47 
48         // 2. Grant read access to picker Uris and return
49         Intent intent = new Intent();
50         final int size = selectedUris.size();
51         if (size < 1) {
52             // TODO (b/168783994): check if this is ever possible. If yes, handle properly,
53             // if not, remove this if block.
54             return intent;
55         }
56         if (!canSelectMultiple) {
57             intent.setData(selectedUris.get(0));
58         }
59         // TODO (b/169737761): use correct mime types
60         String[] mimeTypes = new String[]{"image/*", "video/*"};
61         final ClipData clipData = new ClipData(null /* label */, mimeTypes,
62                 new ClipData.Item(selectedUris.get(0)));
63         for (int i = 1; i < size; i++) {
64             clipData.addItem(new ClipData.Item(selectedUris.get(i)));
65         }
66         intent.setClipData(clipData);
67         intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
68         intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
69 
70         return intent;
71     }
72 
73     @VisibleForTesting
getPickerUri(String action, Uri uri)74     static Uri getPickerUri(String action, Uri uri) {
75         final String userInfo = uri.getUserInfo();
76         final String userId = userInfo == null ? UserId.CURRENT_USER.toString() : userInfo;
77         return PickerUriResolver.wrapProviderUri(uri, action, Integer.parseInt(userId));
78     }
79 
80     /**
81      * Returns list of PhotoPicker Uris corresponding to each {@link Item}
82      *
83      * @param action action name which opened PhotoPicker
84      * @param items list of Item for which we return uri list.
85      */
86     @NonNull
getPickerUrisForItems(String action, @NonNull List<Item> items)87     public static List<Uri> getPickerUrisForItems(String action, @NonNull List<Item> items) {
88         List<Uri> uris = new ArrayList<>();
89         for (Item item : items) {
90             uris.add(getPickerUri(action, item.getContentUri()));
91         }
92 
93         return uris;
94     }
95 
96 }
97