1 /* 2 * Copyright (C) 2015 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 package com.android.packageinstaller.permission.ui.television; 17 18 import android.app.ActionBar; 19 import android.app.FragmentTransaction; 20 import android.content.ActivityNotFoundException; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.os.Bundle; 24 import android.support.annotation.Nullable; 25 import android.support.v7.preference.Preference; 26 import android.support.v7.preference.Preference.OnPreferenceClickListener; 27 import android.support.v7.preference.PreferenceScreen; 28 import android.util.ArraySet; 29 import android.util.Log; 30 import android.view.MenuItem; 31 import android.view.View; 32 33 import com.android.packageinstaller.R; 34 import com.android.packageinstaller.permission.model.PermissionApps.PmCache; 35 import com.android.packageinstaller.permission.model.PermissionGroup; 36 import com.android.packageinstaller.permission.model.PermissionGroups; 37 import com.android.packageinstaller.permission.utils.Utils; 38 39 import java.util.List; 40 41 public final class ManagePermissionsFragment extends SettingsWithHeader 42 implements PermissionGroups.PermissionsGroupsChangeCallback, OnPreferenceClickListener { 43 private static final String LOG_TAG = "ManagePermissionsFragment"; 44 45 private static final String OS_PKG = "android"; 46 47 private static final String EXTRA_PREFS_KEY = "extra_prefs_key"; 48 49 private ArraySet<String> mLauncherPkgs; 50 51 private PermissionGroups mPermissions; 52 53 private PreferenceScreen mExtraScreen; 54 55 public static ManagePermissionsFragment newInstance() { 56 return new ManagePermissionsFragment(); 57 } 58 59 @Override 60 public void onCreate(Bundle icicle) { 61 super.onCreate(icicle); 62 setLoading(true /* loading */, false /* animate */); 63 setHasOptionsMenu(true); 64 final ActionBar ab = getActivity().getActionBar(); 65 if (ab != null) { 66 ab.setDisplayHomeAsUpEnabled(true); 67 } 68 mLauncherPkgs = Utils.getLauncherPackages(getContext()); 69 mPermissions = new PermissionGroups(getContext(), getLoaderManager(), this); 70 } 71 72 @Override 73 public boolean onOptionsItemSelected(MenuItem item) { 74 if (item.getItemId() == android.R.id.home) { 75 getActivity().finish(); 76 return true; 77 } 78 return super.onOptionsItemSelected(item); 79 } 80 81 @Override 82 public boolean onPreferenceClick(Preference preference) { 83 String key = preference.getKey(); 84 85 PermissionGroup group = mPermissions.getGroup(key); 86 if (group == null) { 87 return false; 88 } 89 90 Intent intent = new Intent(Intent.ACTION_MANAGE_PERMISSION_APPS) 91 .putExtra(Intent.EXTRA_PERMISSION_NAME, key); 92 try { 93 getActivity().startActivity(intent); 94 } catch (ActivityNotFoundException e) { 95 Log.w(LOG_TAG, "No app to handle " + intent); 96 } 97 98 return true; 99 } 100 101 @Override 102 public void onPermissionGroupsChanged() { 103 updatePermissionsUi(); 104 } 105 106 @Override 107 public void onViewCreated(View view, Bundle savedInstanceState) { 108 super.onViewCreated(view, savedInstanceState); 109 bindPermissionUi(this, getView()); 110 } 111 112 private static void bindPermissionUi(SettingsWithHeader fragment, @Nullable View rootView) { 113 if (fragment == null || rootView == null) { 114 return; 115 } 116 fragment.setHeader(null, null, null, fragment.getString( 117 R.string.manage_permissions_decor_title)); 118 } 119 120 private void updatePermissionsUi() { 121 Context context = getPreferenceManager().getContext(); 122 if (context == null) { 123 return; 124 } 125 126 List<PermissionGroup> groups = mPermissions.getGroups(); 127 PreferenceScreen screen = getPreferenceScreen(); 128 129 // Use this to speed up getting the info for all of the PermissionApps below. 130 // Create a new one for each refresh to make sure it has fresh data. 131 PmCache cache = new PmCache(getContext().getPackageManager()); 132 for (PermissionGroup group : groups) { 133 boolean isSystemPermission = group.getDeclaringPackage().equals(OS_PKG); 134 135 Preference preference = findPreference(group.getName()); 136 if (preference == null && mExtraScreen != null) { 137 preference = mExtraScreen.findPreference(group.getName()); 138 } 139 if (preference == null) { 140 preference = new Preference(context); 141 preference.setOnPreferenceClickListener(this); 142 preference.setKey(group.getName()); 143 preference.setIcon(Utils.applyTint(context, group.getIcon(), 144 android.R.attr.colorControlNormal)); 145 preference.setTitle(group.getLabel()); 146 // Set blank summary so that no resizing/jumping happens when the summary is loaded. 147 preference.setSummary(" "); 148 preference.setPersistent(false); 149 if (isSystemPermission) { 150 screen.addPreference(preference); 151 } else { 152 if (mExtraScreen == null) { 153 mExtraScreen = getPreferenceManager().createPreferenceScreen(context); 154 } 155 mExtraScreen.addPreference(preference); 156 } 157 } 158 159 preference.setSummary(getString(R.string.app_permissions_group_summary, 160 group.getGranted(), group.getTotal())); 161 } 162 163 if (mExtraScreen != null && mExtraScreen.getPreferenceCount() > 0 164 && screen.findPreference(EXTRA_PREFS_KEY) == null) { 165 Preference extraScreenPreference = new Preference(context); 166 extraScreenPreference.setKey(EXTRA_PREFS_KEY); 167 extraScreenPreference.setIcon(Utils.applyTint(context, 168 R.drawable.ic_more_items, 169 android.R.attr.colorControlNormal)); 170 extraScreenPreference.setTitle(R.string.additional_permissions); 171 extraScreenPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() { 172 @Override 173 public boolean onPreferenceClick(Preference preference) { 174 AdditionalPermissionsFragment frag = new AdditionalPermissionsFragment(); 175 frag.setTargetFragment(ManagePermissionsFragment.this, 0); 176 FragmentTransaction ft = getFragmentManager().beginTransaction(); 177 ft.replace(android.R.id.content, frag); 178 ft.addToBackStack(null); 179 ft.commit(); 180 return true; 181 } 182 }); 183 int count = mExtraScreen.getPreferenceCount(); 184 extraScreenPreference.setSummary(getResources().getQuantityString( 185 R.plurals.additional_permissions_more, count, count)); 186 screen.addPreference(extraScreenPreference); 187 } 188 if (screen.getPreferenceCount() != 0) { 189 setLoading(false /* loading */, true /* animate */); 190 } 191 } 192 193 public static class AdditionalPermissionsFragment extends SettingsWithHeader { 194 @Override 195 public void onCreate(Bundle icicle) { 196 setLoading(true /* loading */, false /* animate */); 197 super.onCreate(icicle); 198 getActivity().setTitle(R.string.additional_permissions); 199 setHasOptionsMenu(true); 200 } 201 202 @Override 203 public void onDestroy() { 204 getActivity().setTitle(R.string.app_permissions); 205 super.onDestroy(); 206 } 207 208 @Override 209 public boolean onOptionsItemSelected(MenuItem item) { 210 switch (item.getItemId()) { 211 case android.R.id.home: 212 getFragmentManager().popBackStack(); 213 return true; 214 } 215 return super.onOptionsItemSelected(item); 216 } 217 218 @Override 219 public void onViewCreated(View view, Bundle savedInstanceState) { 220 super.onViewCreated(view, savedInstanceState); 221 bindPermissionUi(this, getView()); 222 } 223 224 private static void bindPermissionUi(SettingsWithHeader fragment, @Nullable View rootView) { 225 if (fragment == null || rootView == null) { 226 return; 227 } 228 fragment.setHeader(null, null, null, 229 fragment.getString(R.string.additional_permissions_decor_title)); 230 } 231 232 @Override 233 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 234 setPreferenceScreen(((ManagePermissionsFragment) getTargetFragment()).mExtraScreen); 235 setLoading(false /* loading */, true /* animate */); 236 } 237 } 238 } 239