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.app.tvsettings.TvSettingsEnums; 21 import android.content.pm.ActivityInfo; 22 import android.content.pm.PackageInfo; 23 import android.content.pm.PackageManager; 24 import android.os.Bundle; 25 import android.util.Log; 26 27 import androidx.annotation.Keep; 28 import androidx.annotation.NonNull; 29 import androidx.preference.Preference; 30 import androidx.preference.PreferenceGroup; 31 import androidx.preference.SwitchPreference; 32 import androidx.preference.TwoStatePreference; 33 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, 0) 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.setSummaryOn(R.string.app_permission_summary_allowed); 126 switchPref.setSummaryOff(R.string.app_permission_summary_not_allowed); 127 return switchPref; 128 } 129 130 @NonNull 131 @Override createAppPreference()132 public Preference createAppPreference() { 133 return new SwitchPreference(getPreferenceManager().getContext()); 134 } 135 136 @NonNull 137 @Override getEmptyPreference()138 public Preference getEmptyPreference() { 139 final Preference empty = new Preference(getPreferenceManager().getContext()); 140 empty.setKey("empty"); 141 empty.setTitle(R.string.picture_in_picture_empty_text); 142 empty.setEnabled(false); 143 return empty; 144 } 145 146 @NonNull 147 @Override getAppPreferenceGroup()148 public PreferenceGroup getAppPreferenceGroup() { 149 return getPreferenceScreen(); 150 } 151 152 @Override getPageId()153 protected int getPageId() { 154 return TvSettingsEnums.APPS_SPECIAL_APP_ACCESS_PICTURE_IN_PICTURE; 155 } 156 } 157