/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.permissioncontroller.permission.ui.handheld; import static com.android.permissioncontroller.Constants.EXTRA_SESSION_ID; import static com.android.permissioncontroller.Constants.INVALID_SESSION_ID; import static com.android.permissioncontroller.PermissionControllerStatsLog.APP_PERMISSIONS_FRAGMENT_VIEWED; import static com.android.permissioncontroller.PermissionControllerStatsLog.APP_PERMISSIONS_FRAGMENT_VIEWED__CATEGORY__ALLOWED; import static com.android.permissioncontroller.PermissionControllerStatsLog.APP_PERMISSIONS_FRAGMENT_VIEWED__CATEGORY__ALLOWED_FOREGROUND; import static com.android.permissioncontroller.PermissionControllerStatsLog.APP_PERMISSIONS_FRAGMENT_VIEWED__CATEGORY__DENIED; import static com.android.permissioncontroller.permission.ui.handheld.UtilsKt.pressBack; import android.app.ActionBar; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.drawable.Drawable; import android.icu.text.ListFormatter; import android.net.Uri; import android.os.Bundle; import android.os.UserHandle; import android.provider.Settings; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.annotation.StringRes; import androidx.lifecycle.ViewModelProvider; import androidx.preference.Preference; import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceScreen; import androidx.preference.SwitchPreference; import com.android.permissioncontroller.PermissionControllerStatsLog; import com.android.permissioncontroller.R; import com.android.permissioncontroller.permission.model.livedatatypes.AutoRevokeState; import com.android.permissioncontroller.permission.ui.Category; import com.android.permissioncontroller.permission.ui.model.AppPermissionGroupsViewModel; import com.android.permissioncontroller.permission.ui.model.AppPermissionGroupsViewModel.GroupUiInfo; import com.android.permissioncontroller.permission.ui.model.AppPermissionGroupsViewModel.PermSubtitle; import com.android.permissioncontroller.permission.ui.model.AppPermissionGroupsViewModelFactory; import com.android.permissioncontroller.permission.utils.KotlinUtils; import com.android.permissioncontroller.permission.utils.Utils; import com.android.settingslib.HelpUtils; import java.text.Collator; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Random; /** * Show and manage permission groups for an app. * *
Shows the list of permission groups the app has requested at one permission for.
*/
public final class AppPermissionGroupsFragment extends SettingsWithLargeHeader {
private static final String LOG_TAG = AppPermissionGroupsFragment.class.getSimpleName();
private static final String IS_SYSTEM_PERMS_SCREEN = "_is_system_screen";
private static final String AUTO_REVOKE_CATEGORY_KEY = "_AUTO_REVOKE_KEY";
private static final String AUTO_REVOKE_SWITCH_KEY = "_AUTO_REVOKE_SWITCH_KEY";
private static final String AUTO_REVOKE_SUMMARY_KEY = "_AUTO_REVOKE_SUMMARY_KEY";
static final String EXTRA_HIDE_INFO_BUTTON = "hideInfoButton";
private AppPermissionGroupsViewModel mViewModel;
private boolean mIsSystemPermsScreen;
private boolean mIsFirstLoad;
private String mPackageName;
private UserHandle mUser;
private Collator mCollator;
/**
* Create a bundle with the arguments needed by this fragment
*
* @param packageName The name of the package
* @param userHandle The user of this package
* @param sessionId The current session ID
* @param isSystemPermsScreen Whether or not this screen is the system permission screen, or
* the extra permissions screen
*
* @return A bundle with all of the args placed
*/
public static Bundle createArgs(@NonNull String packageName, @NonNull UserHandle userHandle,
long sessionId, boolean isSystemPermsScreen) {
Bundle arguments = new Bundle();
arguments.putString(Intent.EXTRA_PACKAGE_NAME, packageName);
arguments.putParcelable(Intent.EXTRA_USER, userHandle);
arguments.putLong(EXTRA_SESSION_ID, sessionId);
arguments.putBoolean(IS_SYSTEM_PERMS_SCREEN, isSystemPermsScreen);
return arguments;
}
/**
* Create a bundle for a system permissions fragment
*
* @param packageName The name of the package
* @param userHandle The user of this package
* @param sessionId The current session ID
*
* @return A bundle with all of the args placed
*/
public static Bundle createArgs(@NonNull String packageName, @NonNull UserHandle userHandle,
long sessionId) {
return createArgs(packageName, userHandle, sessionId, true);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
mIsFirstLoad = true;
final ActionBar ab = getActivity().getActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
}
mPackageName = getArguments().getString(Intent.EXTRA_PACKAGE_NAME);
mUser = getArguments().getParcelable(Intent.EXTRA_USER);
mIsSystemPermsScreen = getArguments().getBoolean(IS_SYSTEM_PERMS_SCREEN, true);
AppPermissionGroupsViewModelFactory factory =
new AppPermissionGroupsViewModelFactory(mPackageName, mUser,
getArguments().getLong(EXTRA_SESSION_ID, 0));
mViewModel = new ViewModelProvider(this, factory).get(AppPermissionGroupsViewModel.class);
mViewModel.getPackagePermGroupsLiveData().observe(this, this::updatePreferences);
mViewModel.getAutoRevokeLiveData().observe(this, this::setAutoRevokeToggleState);
mCollator = Collator.getInstance(
getContext().getResources().getConfiguration().getLocales().get(0));
if (mViewModel.getPackagePermGroupsLiveData().getValue() != null) {
updatePreferences(mViewModel.getPackagePermGroupsLiveData().getValue());
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().setTitle(R.string.app_permissions);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
pressBack(this);
return true;
}
case MENU_ALL_PERMS: {
mViewModel.showAllPermissions(this, AllAppPermissionsFragment.createArgs(
mPackageName, mUser));
return true;
}
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
if (mIsSystemPermsScreen) {
menu.add(Menu.NONE, MENU_ALL_PERMS, Menu.NONE, R.string.all_permissions);
HelpUtils.prepareHelpMenuItem(getActivity(), menu, R.string.help_app_permissions,
getClass().getName());
}
}
private static void bindUi(SettingsWithLargeHeader fragment, String packageName,
UserHandle user) {
Activity activity = fragment.getActivity();
Intent infoIntent = null;
if (!activity.getIntent().getBooleanExtra(EXTRA_HIDE_INFO_BUTTON, false)) {
infoIntent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
.setData(Uri.fromParts("package", packageName, null));
}
Drawable icon = KotlinUtils.INSTANCE.getBadgedPackageIcon(activity.getApplication(),
packageName, user);
fragment.setHeader(icon, KotlinUtils.INSTANCE.getPackageLabel(activity.getApplication(),
packageName, user), infoIntent, user, false);
}
private void createPreferenceScreenIfNeeded() {
if (getPreferenceScreen() == null) {
addPreferencesFromResource(R.xml.allowed_denied);
addAutoRevokePreferences(getPreferenceScreen());
bindUi(this, mPackageName, mUser);
}
}
private void updatePreferences(Map