1 /* 2 * Copyright (C) 2017 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.appinfo; 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.support.annotation.VisibleForTesting; 24 import android.support.v7.preference.Preference; 25 import android.util.Log; 26 27 import com.android.settings.SettingsPreferenceFragment; 28 29 public class PictureInPictureDetailPreferenceController extends AppInfoPreferenceControllerBase { 30 31 private static final String TAG = "PicInPicDetailControl"; 32 33 private final PackageManager mPackageManager; 34 35 private String mPackageName; 36 37 public PictureInPictureDetailPreferenceController(Context context, String key) { 38 super(context, key); 39 mPackageManager = context.getPackageManager(); 40 } 41 42 @Override 43 public int getAvailabilityStatus() { 44 return hasPictureInPictureActivites() ? AVAILABLE : DISABLED_FOR_USER; 45 } 46 47 @Override 48 public void updateState(Preference preference) { 49 preference.setSummary(getPreferenceSummary()); 50 } 51 52 @Override 53 protected Class<? extends SettingsPreferenceFragment> getDetailFragmentClass() { 54 return PictureInPictureDetails.class; 55 } 56 57 @VisibleForTesting 58 boolean hasPictureInPictureActivites() { 59 // Get the package info with the activities 60 PackageInfo packageInfoWithActivities = null; 61 try { 62 packageInfoWithActivities = mPackageManager.getPackageInfoAsUser(mPackageName, 63 PackageManager.GET_ACTIVITIES, UserHandle.myUserId()); 64 } catch (PackageManager.NameNotFoundException e) { 65 Log.e(TAG, "Exception while retrieving the package info of " + mPackageName, e); 66 } 67 68 return packageInfoWithActivities != null 69 && PictureInPictureSettings.checkPackageHasPictureInPictureActivities( 70 packageInfoWithActivities.packageName, 71 packageInfoWithActivities.activities); 72 } 73 74 @VisibleForTesting 75 int getPreferenceSummary() { 76 return PictureInPictureDetails.getPreferenceSummary(mContext, 77 mParent.getPackageInfo().applicationInfo.uid, mPackageName); 78 } 79 80 public void setPackageName(String packageName) { 81 mPackageName = packageName; 82 } 83 } 84