1 /* 2 * Copyright (C) 2023 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.intentresolver; 18 19 import android.annotation.Nullable; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.provider.Settings; 23 import android.text.TextUtils; 24 25 import com.android.internal.annotations.VisibleForTesting; 26 27 /** 28 * Helper to look up the components available on this device to handle assorted built-in actions 29 * like "Edit" that may be displayed for certain content/preview types. The components are queried 30 * when this record is instantiated, and are then immutable for a given instance. 31 * 32 * Because this describes the app's external execution environment, test methods may prefer to 33 * provide explicit values to override the default lookup logic. 34 */ 35 public class ChooserIntegratedDeviceComponents { 36 @Nullable 37 private final ComponentName mEditSharingComponent; 38 39 @Nullable 40 private final ComponentName mNearbySharingComponent; 41 42 /** Look up the integrated components available on this device. */ get( Context context, SecureSettings secureSettings)43 public static ChooserIntegratedDeviceComponents get( 44 Context context, 45 SecureSettings secureSettings) { 46 return new ChooserIntegratedDeviceComponents( 47 getEditSharingComponent(context), 48 getNearbySharingComponent(context, secureSettings)); 49 } 50 51 @VisibleForTesting ChooserIntegratedDeviceComponents( ComponentName editSharingComponent, ComponentName nearbySharingComponent)52 ChooserIntegratedDeviceComponents( 53 ComponentName editSharingComponent, ComponentName nearbySharingComponent) { 54 mEditSharingComponent = editSharingComponent; 55 mNearbySharingComponent = nearbySharingComponent; 56 } 57 getEditSharingComponent()58 public ComponentName getEditSharingComponent() { 59 return mEditSharingComponent; 60 } 61 getNearbySharingComponent()62 public ComponentName getNearbySharingComponent() { 63 return mNearbySharingComponent; 64 } 65 getEditSharingComponent(Context context)66 private static ComponentName getEditSharingComponent(Context context) { 67 String editorComponent = context.getApplicationContext().getString( 68 R.string.config_systemImageEditor); 69 return TextUtils.isEmpty(editorComponent) 70 ? null : ComponentName.unflattenFromString(editorComponent); 71 } 72 getNearbySharingComponent(Context context, SecureSettings secureSettings)73 private static ComponentName getNearbySharingComponent(Context context, 74 SecureSettings secureSettings) { 75 String nearbyComponent = secureSettings.getString( 76 context.getContentResolver(), Settings.Secure.NEARBY_SHARING_COMPONENT); 77 if (TextUtils.isEmpty(nearbyComponent)) { 78 nearbyComponent = context.getString(R.string.config_defaultNearbySharingComponent); 79 } 80 return TextUtils.isEmpty(nearbyComponent) 81 ? null : ComponentName.unflattenFromString(nearbyComponent); 82 } 83 } 84