1 /* 2 * Copyright (C) 2022 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.ui.settings; 18 19 import static com.android.providers.media.photopicker.util.CloudProviderUtils.getProviderLabel; 20 21 import android.content.Context; 22 import android.content.pm.PackageManager; 23 import android.content.pm.ProviderInfo; 24 import android.graphics.drawable.Drawable; 25 26 import androidx.annotation.NonNull; 27 28 import com.android.providers.media.photopicker.data.CloudProviderInfo; 29 import com.android.providers.media.photopicker.data.model.UserId; 30 31 /** 32 * Class that represents a single radio button cloud provider option displayed in the 33 * SettingsCloudMediaSelectFragment. 34 */ 35 class CloudMediaProviderOption { 36 @NonNull private final String mKey; 37 @NonNull private final CharSequence mLabel; 38 @NonNull private final Drawable mIcon; 39 40 /** 41 * Creates and returns a new CloudMediaProviderOption object from given cloud provider info. 42 * 43 * @param cloudProviderInfo contains the details of the cloud provider. 44 * @param context is the application context that is used to get application info from 45 * PackageManager. 46 * @param userId is the userId creating the cloud provider option. If the userId is for a 47 * managed profile, the icon and label may be different from the one for personal 48 * profile. 49 * @return a new CloudMediaProviderOption object that will be displayed as a radio button item 50 * on the Photo Picker Settings page. 51 */ 52 @NonNull fromCloudProviderInfo( @onNull CloudProviderInfo cloudProviderInfo, @NonNull Context context, @NonNull UserId userId)53 static CloudMediaProviderOption fromCloudProviderInfo( 54 @NonNull CloudProviderInfo cloudProviderInfo, 55 @NonNull Context context, 56 @NonNull UserId userId) { 57 try { 58 // Get Package Manager as the user in the selected tab. This will ensure that for a 59 // managed profile, icons are displayed with a badge. The label of an app in managed 60 // profile may also be different. 61 final PackageManager packageManager = userId.getPackageManager(context); 62 final ProviderInfo providerInfo = packageManager.resolveContentProvider( 63 cloudProviderInfo.authority, /* flags */ 0); 64 65 final CharSequence label = getProviderLabel(packageManager, providerInfo); 66 final Drawable icon = providerInfo.loadIcon(packageManager); 67 final String key = cloudProviderInfo.authority; 68 return new CloudMediaProviderOption(key , label, icon); 69 } catch (PackageManager.NameNotFoundException e) { 70 throw new IllegalArgumentException( 71 "Package name not found: " + cloudProviderInfo.packageName, e); 72 } 73 } 74 CloudMediaProviderOption(@onNull String key, @NonNull CharSequence label, @NonNull Drawable icon)75 CloudMediaProviderOption(@NonNull String key, @NonNull CharSequence label, 76 @NonNull Drawable icon) { 77 mKey = key; 78 mLabel = label; 79 mIcon = icon; 80 } 81 82 @NonNull getKey()83 String getKey() { 84 return mKey; 85 } 86 87 @NonNull getLabel()88 CharSequence getLabel() { 89 return mLabel; 90 } 91 92 @NonNull getIcon()93 Drawable getIcon() { 94 return mIcon; 95 } 96 } 97