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.tv.settings.device.apps.specialaccess; 18 19 import android.Manifest; 20 import android.app.AppOpsManager; 21 import android.app.tvsettings.TvSettingsEnums; 22 import android.os.Bundle; 23 import android.os.UserHandle; 24 import android.os.UserManager; 25 26 import androidx.annotation.Keep; 27 import androidx.annotation.NonNull; 28 import androidx.preference.Preference; 29 import androidx.preference.PreferenceGroup; 30 31 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 32 import com.android.settingslib.RestrictedLockUtilsInternal; 33 import com.android.settingslib.RestrictedSwitchPreference; 34 import com.android.settingslib.applications.ApplicationsState; 35 import com.android.tv.settings.R; 36 37 /** 38 * Fragment for controlling if apps can install other apps 39 */ 40 @Keep 41 public class ExternalSources extends ManageAppOp { 42 private AppOpsManager mAppOpsManager; 43 44 @Override onCreate(Bundle savedInstanceState)45 public void onCreate(Bundle savedInstanceState) { 46 mAppOpsManager = getContext().getSystemService(AppOpsManager.class); 47 super.onCreate(savedInstanceState); 48 } 49 50 @Override getAppOpsOpCode()51 public int getAppOpsOpCode() { 52 return AppOpsManager.OP_REQUEST_INSTALL_PACKAGES; 53 } 54 55 @Override getPermission()56 public String getPermission() { 57 return Manifest.permission.REQUEST_INSTALL_PACKAGES; 58 } 59 60 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)61 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 62 setPreferencesFromResource(R.xml.manage_external_sources, null); 63 } 64 65 @NonNull 66 @Override createAppPreference()67 public Preference createAppPreference() { 68 return new RestrictedSwitchPreference(getPreferenceManager().getContext()); 69 } 70 71 @NonNull 72 @Override bindPreference(@onNull Preference preference, ApplicationsState.AppEntry entry)73 public Preference bindPreference(@NonNull Preference preference, 74 ApplicationsState.AppEntry entry) { 75 final RestrictedSwitchPreference switchPref = (RestrictedSwitchPreference) preference; 76 switchPref.setTitle(entry.label); 77 switchPref.setKey(entry.info.packageName); 78 switchPref.setIcon(entry.icon); 79 switchPref.setOnPreferenceChangeListener((pref, newValue) -> { 80 findEntriesUsingPackageName(entry.info.packageName) 81 .forEach(packageEntry -> setCanInstallApps(packageEntry, (Boolean) newValue)); 82 return true; 83 }); 84 85 PermissionState state = (PermissionState) entry.extraInfo; 86 switchPref.setChecked(state.isAllowed()); 87 88 EnforcedAdmin admin = checkIfRestrictionEnforcedByAdmin(entry); 89 if (admin != null) { 90 switchPref.setDisabledByAdmin(admin); 91 } else { 92 switchPref.setEnabled(canChange(entry)); 93 switchPref.setSummary(getPreferenceSummary(entry)); 94 } 95 96 return switchPref; 97 } 98 checkIfRestrictionEnforcedByAdmin(ApplicationsState.AppEntry entry)99 private EnforcedAdmin checkIfRestrictionEnforcedByAdmin(ApplicationsState.AppEntry entry) { 100 int userId = UserHandle.getUserId(entry.info.uid); 101 EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(getContext(), 102 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, userId); 103 104 if (admin != null) { 105 return admin; 106 } 107 108 return RestrictedLockUtilsInternal.checkIfRestrictionEnforced(getContext(), 109 UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY, userId); 110 } 111 canChange(ApplicationsState.AppEntry entry)112 private boolean canChange(ApplicationsState.AppEntry entry) { 113 final UserHandle userHandle = UserHandle.getUserHandleForUid(entry.info.uid); 114 final UserManager um = UserManager.get(getContext()); 115 return !um.hasBaseUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES, userHandle) 116 && !um.hasBaseUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY, 117 userHandle); 118 } 119 getPreferenceSummary(ApplicationsState.AppEntry entry)120 private CharSequence getPreferenceSummary(ApplicationsState.AppEntry entry) { 121 if (!canChange(entry)) { 122 return getContext().getString(R.string.disabled); 123 } 124 125 return getContext().getString(((PermissionState) entry.extraInfo).isAllowed() 126 ? R.string.external_source_trusted 127 : R.string.external_source_untrusted); 128 } 129 setCanInstallApps(ApplicationsState.AppEntry entry, boolean newState)130 private void setCanInstallApps(ApplicationsState.AppEntry entry, boolean newState) { 131 mAppOpsManager.setMode(AppOpsManager.OP_REQUEST_INSTALL_PACKAGES, 132 entry.info.uid, entry.info.packageName, 133 newState ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_ERRORED); 134 updateAppList(); 135 } 136 137 @NonNull 138 @Override getAppPreferenceGroup()139 public PreferenceGroup getAppPreferenceGroup() { 140 return getPreferenceScreen(); 141 } 142 143 @Override getPageId()144 protected int getPageId() { 145 return TvSettingsEnums.APPS_SECURITY_RESTRICTIONS_UNKNOWN_SOURCES; 146 } 147 } 148