1 /* 2 * Copyright (C) 2018 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.applications.specialaccess.pictureinpicture; 18 19 import android.content.Context; 20 import android.content.pm.PackageInfo; 21 import android.content.pm.PackageManager; 22 import android.os.UserHandle; 23 import android.util.Log; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.Preference; 27 28 import com.android.settings.SettingsPreferenceFragment; 29 import com.android.settings.applications.appinfo.AppInfoPreferenceControllerBase; 30 31 public class PictureInPictureDetailPreferenceController extends AppInfoPreferenceControllerBase { 32 33 private static final String TAG = "PicInPicDetailControl"; 34 35 private final PackageManager mPackageManager; 36 37 private String mPackageName; 38 PictureInPictureDetailPreferenceController(Context context, String key)39 public PictureInPictureDetailPreferenceController(Context context, String key) { 40 super(context, key); 41 mPackageManager = context.getPackageManager(); 42 } 43 44 @Override getAvailabilityStatus()45 public int getAvailabilityStatus() { 46 if (!mContext.getPackageManager().hasSystemFeature( 47 PackageManager.FEATURE_PICTURE_IN_PICTURE)) { 48 return UNSUPPORTED_ON_DEVICE; 49 } 50 return hasPictureInPictureActivites() ? AVAILABLE : DISABLED_FOR_USER; 51 } 52 53 @Override updateState(Preference preference)54 public void updateState(Preference preference) { 55 preference.setSummary(getPreferenceSummary()); 56 } 57 58 @Override getDetailFragmentClass()59 protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() { 60 return PictureInPictureDetails.class; 61 } 62 63 @VisibleForTesting hasPictureInPictureActivites()64 boolean hasPictureInPictureActivites() { 65 // Get the package info with the activities 66 PackageInfo packageInfoWithActivities = null; 67 try { 68 packageInfoWithActivities = mPackageManager.getPackageInfoAsUser(mPackageName, 69 PackageManager.GET_ACTIVITIES, UserHandle.myUserId()); 70 } catch (Exception e) { 71 // Catch Exception to avoid DeadObjectException thrown with binder transaction 72 // failures, since the explicit request of DeadObjectException has compiler errors. 73 Log.e(TAG, "Exception while retrieving the package info of " + mPackageName, e); 74 } 75 76 return packageInfoWithActivities != null 77 && PictureInPictureSettings.checkPackageHasPictureInPictureActivities( 78 packageInfoWithActivities.packageName, 79 packageInfoWithActivities.activities); 80 } 81 82 @VisibleForTesting getPreferenceSummary()83 int getPreferenceSummary() { 84 return PictureInPictureDetails.getPreferenceSummary(mContext, 85 mParent.getPackageInfo().applicationInfo.uid, mPackageName); 86 } 87 setPackageName(String packageName)88 public void setPackageName(String packageName) { 89 mPackageName = packageName; 90 } 91 } 92