Home | History | Annotate | Download | only in handheld
      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.handheld;
     17 
     18 import android.app.ActionBar;
     19 import android.content.ActivityNotFoundException;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.os.Bundle;
     23 import android.preference.Preference;
     24 import android.preference.PreferenceScreen;
     25 import android.util.ArraySet;
     26 import android.util.Log;
     27 
     28 import com.android.packageinstaller.R;
     29 import com.android.packageinstaller.permission.model.PermissionApps.PmCache;
     30 import com.android.packageinstaller.permission.model.PermissionGroup;
     31 import com.android.packageinstaller.permission.model.PermissionGroups;
     32 import com.android.packageinstaller.permission.utils.Utils;
     33 
     34 import java.util.List;
     35 
     36 /**
     37  * Superclass for fragments allowing the user to manage permissions.
     38  */
     39 abstract class ManagePermissionsFragment extends PermissionsFrameFragment
     40         implements PermissionGroups.PermissionsGroupsChangeCallback,
     41         Preference.OnPreferenceClickListener {
     42     private static final String LOG_TAG = "ManagePermissionsFragment";
     43 
     44     static final String OS_PKG = "android";
     45 
     46     private ArraySet<String> mLauncherPkgs;
     47 
     48     private PermissionGroups mPermissions;
     49 
     50     @Override
     51     public void onCreate(Bundle icicle) {
     52         super.onCreate(icicle);
     53         setLoading(true /* loading */, false /* animate */);
     54         setHasOptionsMenu(true);
     55         final ActionBar ab = getActivity().getActionBar();
     56         if (ab != null) {
     57             ab.setDisplayHomeAsUpEnabled(true);
     58         }
     59         mLauncherPkgs = Utils.getLauncherPackages(getContext());
     60         mPermissions = new PermissionGroups(getContext(), getLoaderManager(), this);
     61     }
     62 
     63     @Override
     64     public boolean onPreferenceClick(Preference preference) {
     65         String key = preference.getKey();
     66 
     67         PermissionGroup group = mPermissions.getGroup(key);
     68         if (group == null) {
     69             return false;
     70         }
     71 
     72         Intent intent = new Intent(Intent.ACTION_MANAGE_PERMISSION_APPS)
     73                 .putExtra(Intent.EXTRA_PERMISSION_NAME, key);
     74         try {
     75             getActivity().startActivity(intent);
     76         } catch (ActivityNotFoundException e) {
     77             Log.w(LOG_TAG, "No app to handle " + intent);
     78         }
     79 
     80         return true;
     81     }
     82 
     83     /**
     84      * @return the permissions
     85      */
     86     protected PermissionGroups getPermissions() {
     87         return mPermissions;
     88     }
     89 
     90     @Override
     91     public void onPermissionGroupsChanged() {
     92         updatePermissionsUi();
     93     }
     94 
     95     /**
     96      * Update the preferences to show the new {@link #getPermissions() permissions}.
     97      */
     98     protected abstract void updatePermissionsUi();
     99 
    100     /**
    101      * Add preferences for all permissions of a type to the preference screen.
    102      *
    103      * @param addSystemPermissions If the permissions added should be system permissions or not
    104      *
    105      * @return The preference screen the permissions were added to
    106      */
    107     protected PreferenceScreen updatePermissionsUi(boolean addSystemPermissions) {
    108         Context context = getActivity();
    109         if (context == null) {
    110             return null;
    111         }
    112 
    113         List<PermissionGroup> groups = mPermissions.getGroups();
    114         PreferenceScreen screen = getPreferenceScreen();
    115         if (screen == null) {
    116             screen = getPreferenceManager().createPreferenceScreen(getActivity());
    117             setPreferenceScreen(screen);
    118         } else {
    119             screen.removeAll();
    120         }
    121 
    122         // Use this to speed up getting the info for all of the PermissionApps below.
    123         // Create a new one for each refresh to make sure it has fresh data.
    124         PmCache cache = new PmCache(getContext().getPackageManager());
    125         for (PermissionGroup group : groups) {
    126             boolean isSystemPermission = group.getDeclaringPackage().equals(OS_PKG);
    127 
    128             if (addSystemPermissions == isSystemPermission) {
    129                 Preference preference = findPreference(group.getName());
    130 
    131                 if (preference == null) {
    132                     preference = new Preference(context);
    133                     preference.setOnPreferenceClickListener(this);
    134                     preference.setKey(group.getName());
    135                     preference.setIcon(Utils.applyTint(context, group.getIcon(),
    136                             android.R.attr.colorControlNormal));
    137                     preference.setTitle(group.getLabel());
    138                     // Set blank summary so that no resizing/jumping happens when the summary is
    139                     // loaded.
    140                     preference.setSummary(" ");
    141                     preference.setPersistent(false);
    142                     screen.addPreference(preference);
    143                 }
    144                 preference.setSummary(getString(R.string.app_permissions_group_summary,
    145                         group.getGranted(), group.getTotal()));
    146             }
    147         }
    148         if (screen.getPreferenceCount() != 0) {
    149             setLoading(false /* loading */, true /* animate */);
    150         }
    151 
    152         return screen;
    153     }
    154 }
    155