| /* |
| * Copyright (C) 2016 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.packageinstaller.permission.ui.handheld; |
| |
| import android.app.FragmentTransaction; |
| import android.preference.Preference; |
| import android.preference.PreferenceScreen; |
| import android.view.MenuItem; |
| |
| import com.android.packageinstaller.R; |
| import com.android.packageinstaller.permission.model.PermissionGroup; |
| import com.android.packageinstaller.permission.utils.Utils; |
| |
| import java.util.List; |
| |
| /** |
| * Fragment that allows the user to manage standard permissions. |
| */ |
| public final class ManageStandardPermissionsFragment extends ManagePermissionsFragment { |
| private static final String EXTRA_PREFS_KEY = "extra_prefs_key"; |
| |
| /** |
| * @return A new fragment |
| */ |
| public static ManageStandardPermissionsFragment newInstance() { |
| return new ManageStandardPermissionsFragment(); |
| } |
| |
| @Override |
| public void onStart() { |
| super.onStart(); |
| |
| getActivity().setTitle(com.android.packageinstaller.R.string.app_permissions); |
| } |
| |
| |
| @Override |
| public boolean onOptionsItemSelected(MenuItem item) { |
| if (item.getItemId() == android.R.id.home) { |
| getActivity().finish(); |
| return true; |
| } |
| return super.onOptionsItemSelected(item); |
| } |
| |
| @Override |
| protected void updatePermissionsUi() { |
| PreferenceScreen screen = updatePermissionsUi(true); |
| if (screen == null) { |
| return; |
| } |
| |
| // Check if we need an additional permissions preference |
| List<PermissionGroup> groups = getPermissions().getGroups(); |
| int numExtraPermissions = 0; |
| for (PermissionGroup group : groups) { |
| if (!group.getDeclaringPackage().equals(ManagePermissionsFragment.OS_PKG)) { |
| numExtraPermissions++; |
| } |
| } |
| |
| Preference additionalPermissionsPreference = screen.findPreference(EXTRA_PREFS_KEY); |
| if (numExtraPermissions == 0) { |
| if (additionalPermissionsPreference != null) { |
| screen.removePreference(additionalPermissionsPreference); |
| } |
| } else { |
| if (additionalPermissionsPreference == null) { |
| additionalPermissionsPreference = new Preference(getActivity()); |
| additionalPermissionsPreference.setKey(EXTRA_PREFS_KEY); |
| additionalPermissionsPreference.setIcon(Utils.applyTint(getActivity(), |
| R.drawable.ic_more_items, |
| android.R.attr.colorControlNormal)); |
| additionalPermissionsPreference.setTitle(R.string.additional_permissions); |
| additionalPermissionsPreference.setOnPreferenceClickListener(preference -> { |
| ManageCustomPermissionsFragment frag = |
| new ManageCustomPermissionsFragment(); |
| frag.setTargetFragment(ManageStandardPermissionsFragment.this, 0); |
| FragmentTransaction ft = getFragmentManager().beginTransaction(); |
| ft.replace(android.R.id.content, frag); |
| ft.addToBackStack(null); |
| ft.commit(); |
| return true; |
| }); |
| |
| screen.addPreference(additionalPermissionsPreference); |
| } |
| |
| additionalPermissionsPreference.setSummary(getResources().getQuantityString( |
| R.plurals.additional_permissions_more, numExtraPermissions, |
| numExtraPermissions)); |
| } |
| } |
| } |