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.tv.settings.device.apps.specialaccess; 18 19 import android.app.AppOpsManager; 20 import android.content.pm.ActivityInfo; 21 import android.content.pm.PackageInfo; 22 import android.content.pm.PackageManager; 23 import android.os.Bundle; 24 import android.util.Log; 25 26 import androidx.annotation.Keep; 27 import androidx.annotation.NonNull; 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceGroup; 30 import androidx.preference.SwitchPreference; 31 import androidx.preference.TwoStatePreference; 32 33 import com.android.internal.logging.nano.MetricsProto; 34 import com.android.settingslib.applications.ApplicationsState; 35 import com.android.tv.settings.R; 36 import com.android.tv.settings.SettingsPreferenceFragment; 37 38 /** 39 * Fragment for managing which apps are granted PIP access 40 */ 41 @Keep 42 public class PictureInPicture extends SettingsPreferenceFragment 43 implements ManageApplicationsController.Callback { 44 private static final String TAG = "PictureInPicture"; 45 46 private ManageApplicationsController mManageApplicationsController; 47 private AppOpsManager mAppOpsManager; 48 49 private final ApplicationsState.AppFilter mFilter = 50 new ApplicationsState.CompoundFilter( 51 new ApplicationsState.CompoundFilter( 52 ApplicationsState.FILTER_WITHOUT_DISABLED_UNTIL_USED, 53 ApplicationsState.FILTER_ALL_ENABLED), 54 55 new ApplicationsState.AppFilter() { 56 @Override 57 public void init() {} 58 59 @Override 60 public boolean filterApp(ApplicationsState.AppEntry info) { 61 info.extraInfo = mAppOpsManager.checkOpNoThrow( 62 AppOpsManager.OP_PICTURE_IN_PICTURE, 63 info.info.uid, 64 info.info.packageName) == AppOpsManager.MODE_ALLOWED; 65 return !ManageAppOp.shouldIgnorePackage( 66 getContext(), info.info.packageName) 67 && checkPackageHasPipActivities(info.info.packageName); 68 } 69 }); 70 71 @Override onCreate(Bundle savedInstanceState)72 public void onCreate(Bundle savedInstanceState) { 73 super.onCreate(savedInstanceState); 74 mAppOpsManager = getContext().getSystemService(AppOpsManager.class); 75 mManageApplicationsController = new ManageApplicationsController(getContext(), this, 76 getLifecycle(), mFilter, ApplicationsState.ALPHA_COMPARATOR); 77 } 78 79 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)80 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 81 setPreferencesFromResource(R.xml.picture_in_picture, null); 82 } 83 84 @Override onResume()85 public void onResume() { 86 super.onResume(); 87 mManageApplicationsController.updateAppList(); 88 } 89 checkPackageHasPipActivities(String packageName)90 private boolean checkPackageHasPipActivities(String packageName) { 91 try { 92 final PackageInfo packageInfo = getContext().getPackageManager().getPackageInfo( 93 packageName, PackageManager.GET_ACTIVITIES); 94 if (packageInfo.activities == null) { 95 return false; 96 } 97 for (ActivityInfo info : packageInfo.activities) { 98 if (info.supportsPictureInPicture()) { 99 return true; 100 } 101 } 102 } catch (PackageManager.NameNotFoundException e) { 103 Log.e(TAG, "Exception while fetching package info for " + packageName, e); 104 return false; 105 } 106 return false; 107 } 108 109 @NonNull 110 @Override bindPreference(@onNull Preference preference, ApplicationsState.AppEntry entry)111 public Preference bindPreference(@NonNull Preference preference, 112 ApplicationsState.AppEntry entry) { 113 final TwoStatePreference switchPref = (SwitchPreference) preference; 114 switchPref.setTitle(entry.label); 115 switchPref.setKey(entry.info.packageName); 116 switchPref.setIcon(entry.icon); 117 switchPref.setChecked((Boolean) entry.extraInfo); 118 switchPref.setOnPreferenceChangeListener((pref, newValue) -> { 119 mAppOpsManager.setMode(AppOpsManager.OP_PICTURE_IN_PICTURE, 120 entry.info.uid, 121 entry.info.packageName, 122 (Boolean) newValue ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_ERRORED); 123 return true; 124 }); 125 switchPref.setSummary((Boolean) entry.extraInfo 126 ? R.string.app_permission_summary_allowed 127 : R.string.app_permission_summary_not_allowed); 128 return switchPref; 129 } 130 131 @NonNull 132 @Override createAppPreference()133 public Preference createAppPreference() { 134 return new SwitchPreference(getPreferenceManager().getContext()); 135 } 136 137 @NonNull 138 @Override getEmptyPreference()139 public Preference getEmptyPreference() { 140 final Preference empty = new Preference(getPreferenceManager().getContext()); 141 empty.setKey("empty"); 142 empty.setTitle(R.string.picture_in_picture_empty_text); 143 empty.setEnabled(false); 144 return empty; 145 } 146 147 @NonNull 148 @Override getAppPreferenceGroup()149 public PreferenceGroup getAppPreferenceGroup() { 150 return getPreferenceScreen(); 151 } 152 153 @Override getMetricsCategory()154 public int getMetricsCategory() { 155 return MetricsProto.MetricsEvent.SETTINGS_MANAGE_PICTURE_IN_PICTURE; 156 } 157 } 158