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 com.android.settings.connecteddevice.virtual; 18 19 import static com.android.settings.spa.app.appinfo.AppInfoSettingsProvider.startAppInfoSettings; 20 21 import android.app.Application; 22 import android.content.Context; 23 import android.os.UserHandle; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 import androidx.preference.PreferenceFragmentCompat; 28 import androidx.preference.PreferenceScreen; 29 30 import com.android.settings.core.BasePreferenceController; 31 import com.android.settingslib.Utils; 32 import com.android.settingslib.applications.ApplicationsState; 33 import com.android.settingslib.widget.AppPreference; 34 35 import java.util.Objects; 36 37 /** This class adds the details about the virtual device companion app. */ 38 public class VirtualDeviceDetailsCompanionAppController extends BasePreferenceController { 39 40 private static final String KEY_VIRTUAL_DEVICE_COMPANION_APP = "virtual_device_companion_app"; 41 42 @Nullable 43 private PreferenceFragmentCompat mFragment; 44 @Nullable 45 private String mPackageName; 46 VirtualDeviceDetailsCompanionAppController(@onNull Context context)47 public VirtualDeviceDetailsCompanionAppController(@NonNull Context context) { 48 super(context, KEY_VIRTUAL_DEVICE_COMPANION_APP); 49 } 50 51 /** One-time initialization when the controller is first created. */ init(@onNull PreferenceFragmentCompat fragment, @NonNull String packageName)52 void init(@NonNull PreferenceFragmentCompat fragment, @NonNull String packageName) { 53 mFragment = fragment; 54 mPackageName = packageName; 55 } 56 57 @Override displayPreference(@onNull PreferenceScreen screen)58 public void displayPreference(@NonNull PreferenceScreen screen) { 59 ApplicationsState applicationsState = ApplicationsState.getInstance( 60 (Application) mContext.getApplicationContext()); 61 final ApplicationsState.AppEntry appEntry = applicationsState.getEntry( 62 mPackageName, UserHandle.myUserId()); 63 64 final AppPreference preference = screen.findPreference(getPreferenceKey()); 65 66 preference.setTitle(appEntry.label); 67 preference.setIcon(Utils.getBadgedIcon(mContext, appEntry.info)); 68 preference.setOnPreferenceClickListener(pref -> { 69 startAppInfoSettings(Objects.requireNonNull(mPackageName), appEntry.info.uid, 70 Objects.requireNonNull(mFragment), /* request= */ 1001, 71 getMetricsCategory()); 72 return true; 73 }); 74 } 75 76 @Override getAvailabilityStatus()77 public int getAvailabilityStatus() { 78 return AVAILABLE; 79 } 80 } 81