Merge "Add additional permissions button to manage perms" into mnc-dev
diff --git a/res/drawable/ic_toc.xml b/res/drawable/ic_toc.xml
new file mode 100644
index 0000000..66c476b
--- /dev/null
+++ b/res/drawable/ic_toc.xml
@@ -0,0 +1,24 @@
+<!--
+    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.
+-->
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+        android:width="24.0dp"
+        android:height="24.0dp"
+        android:viewportWidth="48.0"
+        android:viewportHeight="48.0">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M6.0,18.0l28.0,0.0l0.0,-4.0L6.0,14.0l0.0,4.0zm0.0,8.0l28.0,0.0l0.0,-4.0L6.0,22.0l0.0,4.0zm0.0,8.0l28.0,0.0l0.0,-4.0L6.0,30.0l0.0,4.0zm32.0,0.0l4.0,0.0l0.0,-4.0l-4.0,0.0l0.0,4.0zm0.0,-20.0l0.0,4.0l4.0,0.0l0.0,-4.0l-4.0,0.0zm0.0,12.0l4.0,0.0l0.0,-4.0l-4.0,0.0l0.0,4.0z"/>
+</vector>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index ebf1fb4..bd16faa 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -6592,4 +6592,10 @@
     <!-- Turn on settings for system ui tuner [CHAR LIMIT=40] -->
     <string name="system_ui_settings">Show SystemUI Tuner</string>
 
+    <!-- Label for button that leads to more permissions [CHAR LIMIT=40] -->
+    <string name="additional_permissions">Additional permissions</string>
+
+    <!-- Description of how many more permissions to view on next page [CHAR LIMIT=30] -->
+    <string name="additional_permissions_more"><xliff:g id="count" example="2">%1$d</xliff:g> more</string>
+
 </resources>
diff --git a/src/com/android/settings/applications/ManagePermissions.java b/src/com/android/settings/applications/ManagePermissions.java
index e8f9d0f..862868a 100644
--- a/src/com/android/settings/applications/ManagePermissions.java
+++ b/src/com/android/settings/applications/ManagePermissions.java
@@ -15,16 +15,22 @@
  */
 package com.android.settings.applications;
 
+import android.app.Activity;
+import android.app.FragmentTransaction;
 import android.content.ActivityNotFoundException;
 import android.content.Context;
 import android.content.Intent;
+import android.content.res.Resources;
+import android.content.res.Resources.Theme;
 import android.os.Bundle;
 import android.preference.Preference;
 import android.preference.Preference.OnPreferenceClickListener;
 import android.preference.PreferenceScreen;
 import android.util.Log;
+import android.view.View;
 
 import com.android.internal.logging.MetricsLogger;
+import com.android.settings.AppHeader;
 import com.android.settings.R;
 import com.android.settings.SettingsPreferenceFragment;
 import com.android.settingslib.applications.PermissionsInfo;
@@ -33,12 +39,16 @@
 import java.util.List;
 
 public class ManagePermissions extends SettingsPreferenceFragment
-        implements PermissionsInfo.Callback, OnPreferenceClickListener {
+        implements PermissionsInfo.Callback {
 
     private static final String TAG = "ManagePermissions";
 
+    private static final String OS_PKG = "android";
+
     private PermissionsInfo mPermissionsInfo;
 
+    private PreferenceScreen mExtraScreen;
+
     @Override
     public void onResume() {
         super.onResume();
@@ -54,25 +64,66 @@
     }
 
     private void refreshUi() {
+        Activity activity = getActivity();
         PreferenceScreen screen = getPreferenceScreen();
         if (screen == null) {
-            screen = getPreferenceManager().createPreferenceScreen(getActivity());
+            screen = getPreferenceManager().createPreferenceScreen(activity);
             setPreferenceScreen(screen);
         } else {
             screen.removeAll();
+            if (mExtraScreen != null) {
+                mExtraScreen.removeAll();
+            }
         }
         final int count = screen.getPreferenceCount();
         if (count == 0) {
+            final Preference extraScreenPreference = new Preference(activity);
+            extraScreenPreference.setIcon(R.drawable.ic_toc);
+            extraScreenPreference.setTitle(R.string.additional_permissions);
             List<PermissionGroup> groups = mPermissionsInfo.getGroups();
             for (PermissionGroup group : groups) {
                 if (group.possibleApps.size() == 0) continue;
-                PermissionPreference pref = new PermissionPreference(getActivity(), group);
+                PermissionPreference pref = new PermissionPreference(activity, group);
                 pref.refreshUi();
-                screen.addPreference(pref);
+                if (group.packageName.equals(OS_PKG)) {
+                    screen.addPreference(pref);
+                } else {
+                    if (mExtraScreen == null) {
+                        mExtraScreen = getPreferenceManager().createPreferenceScreen(activity);
+                    }
+                    mExtraScreen.addPreference(pref);
+                }
+            }
+            if (mExtraScreen != null) {
+                extraScreenPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
+                    @Override
+                    public boolean onPreferenceClick(Preference preference) {
+                        AdditionalPermissionsFragment frag = new AdditionalPermissionsFragment();
+                        frag.setTargetFragment(ManagePermissions.this, 0);
+                        FragmentTransaction ft = getFragmentManager().beginTransaction();
+                        ft.replace(R.id.main_content, frag);
+                        ft.addToBackStack("AdditionalPerms");
+                        ft.commit();
+                        return true;
+                    }
+                });
+                extraScreenPreference.setSummary(getString(R.string.additional_permissions_more,
+                        mExtraScreen.getPreferenceCount()));
+                screen.addPreference(extraScreenPreference);
             }
         } else {
-            for (int i = 0; i < count; i++) {
-                ((PermissionPreference) screen.getPreference(i)).refreshUi();
+            updatePrefs(screen);
+            if (mExtraScreen != null) {
+                updatePrefs(mExtraScreen);
+            }
+        }
+    }
+
+    private void updatePrefs(PreferenceScreen screen) {
+        for (int i = 0; i < screen.getPreferenceCount(); i++) {
+            Preference pref = screen.getPreference(i);
+            if (pref instanceof PermissionPreference) {
+                ((PermissionPreference) pref).refreshUi();
             }
         }
     }
@@ -83,11 +134,6 @@
     }
 
     @Override
-    public boolean onPreferenceClick(Preference preference) {
-        return true;
-    }
-
-    @Override
     protected int getMetricsCategory() {
         return MetricsLogger.MANAGE_PERMISSIONS;
     }
@@ -121,4 +167,26 @@
         }
     }
 
+    public static class AdditionalPermissionsFragment extends SettingsPreferenceFragment {
+        @Override
+        public void onCreate(Bundle icicle) {
+            super.onCreate(icicle);
+            setPreferenceScreen(((ManagePermissions) getTargetFragment()).mExtraScreen);
+        }
+
+        @Override
+        public void onViewCreated(View view, Bundle savedInstanceState) {
+            super.onViewCreated(view, savedInstanceState);
+            Resources resources = getResources();
+            Theme theme = getActivity().getTheme();
+            AppHeader.createAppHeader(this, resources.getDrawable(R.drawable.ic_toc, theme),
+                    getString(R.string.additional_permissions), null, android.R.color.white);
+        }
+
+        @Override
+        protected int getMetricsCategory() {
+            return MetricsLogger.MANAGE_PERMISSIONS;
+        }
+    }
+
 }