Update picture-in-picture settings to match mocks.

- Removing switches in top-level picture-in-picture settings, adding
  details screen for each app which contains the switch and some
  additional information.

Bug: 35957404
Test: make -j40 RunSettingsRoboTests
Change-Id: Ib22df8a52ab857e071ec43dd3e5d1f5282db35cb
diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java
index f2d6452..8f645f8 100644
--- a/src/com/android/settings/Settings.java
+++ b/src/com/android/settings/Settings.java
@@ -95,6 +95,7 @@
     public static class NotificationAccessSettingsActivity extends SettingsActivity { /* empty */ }
     public static class VrListenersSettingsActivity extends SettingsActivity { /* empty */ }
     public static class PictureInPictureSettingsActivity extends SettingsActivity { /* empty */ }
+    public static class AppPictureInPictureSettingsActivity extends SettingsActivity { /* empty */ }
     public static class ZenAccessSettingsActivity extends SettingsActivity { /* empty */ }
     public static class ConditionProviderSettingsActivity extends SettingsActivity { /* empty */ }
     public static class UsbSettingsActivity extends SettingsActivity { /* empty */ }
diff --git a/src/com/android/settings/applications/InstalledAppDetails.java b/src/com/android/settings/applications/InstalledAppDetails.java
index 08be7f7..e00ba92 100755
--- a/src/com/android/settings/applications/InstalledAppDetails.java
+++ b/src/com/android/settings/applications/InstalledAppDetails.java
@@ -52,7 +52,6 @@
 import android.os.UserHandle;
 import android.os.UserManager;
 import android.support.annotation.VisibleForTesting;
-import android.support.v14.preference.SwitchPreference;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.Preference.OnPreferenceClickListener;
 import android.support.v7.preference.PreferenceCategory;
@@ -1041,17 +1040,16 @@
                 category.addPreference(pref);
             }
             if (hasPictureInPictureActivities) {
-                final SwitchPreference pref = new SwitchPreference(getPrefContext());
-                pref.setPersistent(false);
+                Preference pref = new Preference(getPrefContext());
                 pref.setTitle(R.string.picture_in_picture_app_detail_title);
-                pref.setSummary(R.string.picture_in_picture_app_detail_summary);
-                pref.setChecked(PictureInPictureSettings.getEnterPipOnHideStateForPackage(
-                        getContext(), mPackageInfo.applicationInfo.uid, mPackageName));
-                pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+                pref.setKey("picture_in_picture");
+                pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                     @Override
-                    public boolean onPreferenceChange(Preference preference, Object newValue) {
-                        PictureInPictureSettings.setEnterPipOnHideStateForPackage(getContext(),
-                                mPackageInfo.applicationInfo.uid, mPackageName, (Boolean) newValue);
+                    public boolean onPreferenceClick(Preference preference) {
+                        AppInfoBase.startAppInfoFragment(PictureInPictureDetails.class,
+                                R.string.picture_in_picture_app_detail_title, mPackageName,
+                                mPackageInfo.applicationInfo.uid, InstalledAppDetails.this,
+                                -1, getMetricsCategory());
                         return true;
                     }
                 });
@@ -1164,6 +1162,11 @@
         if (pref != null) {
             pref.setSummary(DrawOverlayDetails.getSummary(getContext(), mAppEntry));
         }
+        pref = findPreference("picture_in_picture");
+        if (pref != null) {
+            pref.setSummary(PictureInPictureDetails.getPreferenceSummary(getContext(),
+                    mPackageInfo.applicationInfo.uid, mPackageName));
+        }
         pref = findPreference("write_settings_apps");
         if (pref != null) {
             pref.setSummary(WriteSettingsDetails.getSummary(getContext(), mAppEntry));
diff --git a/src/com/android/settings/applications/PictureInPictureDetails.java b/src/com/android/settings/applications/PictureInPictureDetails.java
new file mode 100644
index 0000000..41f006a
--- /dev/null
+++ b/src/com/android/settings/applications/PictureInPictureDetails.java
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2017 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.settings.applications;
+
+import static android.app.AppOpsManager.MODE_ALLOWED;
+import static android.app.AppOpsManager.MODE_ERRORED;
+import static android.app.AppOpsManager.OP_PICTURE_IN_PICTURE;
+
+import android.app.AlertDialog;
+import android.app.AppOpsManager;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.provider.Settings;
+import android.support.v14.preference.SwitchPreference;
+import android.support.v7.preference.Preference;
+import android.support.v7.preference.Preference.OnPreferenceChangeListener;
+
+import com.android.internal.annotations.VisibleForTesting;
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.settings.R;
+import com.android.settings.overlay.FeatureFactory;
+
+public class PictureInPictureDetails extends AppInfoWithHeader
+        implements OnPreferenceChangeListener {
+
+    private static final String KEY_APP_OPS_SETTINGS_SWITCH = "app_ops_settings_switch";
+    private static final String KEY_APP_OPS_SETTINGS_PREFS = "app_ops_settings_preference";
+    private static final String KEY_APP_OPS_SETTINGS_DESC = "app_ops_settings_description";
+    private static final String LOG_TAG = "PictureInPictureDetails";
+
+    private SwitchPreference mSwitchPref;
+    private Preference mOverlayDesc;
+    private Intent mSettingsIntent;
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        // find preferences
+        addPreferencesFromResource(R.xml.app_ops_permissions_details);
+        mSwitchPref = (SwitchPreference) findPreference(KEY_APP_OPS_SETTINGS_SWITCH);
+        mOverlayDesc = findPreference(KEY_APP_OPS_SETTINGS_DESC);
+        getPreferenceScreen().removePreference(findPreference(KEY_APP_OPS_SETTINGS_PREFS));
+
+        // set title/summary for all of them
+        getPreferenceScreen().setTitle(R.string.picture_in_picture_app_detail_title);
+        mSwitchPref.setTitle(R.string.picture_in_picture_app_detail_switch);
+        mOverlayDesc.setSummary(R.string.picture_in_picture_app_detail_summary);
+
+        // install event listeners
+        mSwitchPref.setOnPreferenceChangeListener(this);
+
+        mSettingsIntent = new Intent(Intent.ACTION_MAIN)
+                .setAction(Settings.ACTION_PICTURE_IN_PICTURE_SETTINGS);
+    }
+
+    @Override
+    public boolean onPreferenceChange(Preference preference, Object newValue) {
+        if (preference == mSwitchPref) {
+            logSpecialPermissionChange((Boolean) newValue, mPackageName);
+            setEnterPipStateForPackage(getActivity(), mPackageInfo.applicationInfo.uid, mPackageName,
+                    (Boolean) newValue);
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    protected boolean refreshUi() {
+        boolean isAllowed = getEnterPipStateForPackage(getActivity(),
+                mPackageInfo.applicationInfo.uid, mPackageName);
+        mSwitchPref.setChecked(isAllowed);
+        return true;
+    }
+
+    @Override
+    protected AlertDialog createDialog(int id, int errorCode) {
+        return null;
+    }
+
+    @Override
+    public int getMetricsCategory() {
+        return MetricsEvent.SETTINGS_MANAGE_PICTURE_IN_PICTURE;
+    }
+
+    /**
+     * Sets whether the app associated with the given {@param packageName} is allowed to enter
+     * picture-in-picture.
+     */
+    static void setEnterPipStateForPackage(Context context, int uid, String packageName,
+            boolean value) {
+        final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
+        final int newMode = value ? MODE_ALLOWED : MODE_ERRORED;
+        appOps.setMode(OP_PICTURE_IN_PICTURE, uid, packageName, newMode);
+    }
+
+    /**
+     * @return whether the app associated with the given {@param packageName} is allowed to enter
+     *         picture-in-picture.
+     */
+    static boolean getEnterPipStateForPackage(Context context, int uid, String packageName) {
+        final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
+        return appOps.checkOpNoThrow(OP_PICTURE_IN_PICTURE, uid, packageName) == MODE_ALLOWED;
+    }
+
+    /**
+     * @return the summary for the current state of whether the app associated with the given
+     *         {@param packageName} is allowed to enter picture-in-picture.
+     */
+    static int getPreferenceSummary(Context context, int uid, String packageName) {
+        final boolean enabled = PictureInPictureDetails.getEnterPipStateForPackage(context, uid,
+                packageName);
+        return enabled ? R.string.picture_in_picture_on : R.string.picture_in_picture_off;
+    }
+
+    @VisibleForTesting
+    void logSpecialPermissionChange(boolean newState, String packageName) {
+        int logCategory = newState
+                ? MetricsEvent.APP_PICTURE_IN_PICTURE_ALLOW
+                : MetricsEvent.APP_PICTURE_IN_PICTURE_DENY;
+        FeatureFactory.getFactory(getContext())
+                .getMetricsFeatureProvider().action(getContext(), logCategory, packageName);
+    }
+}
diff --git a/src/com/android/settings/applications/PictureInPictureSettings.java b/src/com/android/settings/applications/PictureInPictureSettings.java
index df73572..b1c544a 100644
--- a/src/com/android/settings/applications/PictureInPictureSettings.java
+++ b/src/com/android/settings/applications/PictureInPictureSettings.java
@@ -15,13 +15,9 @@
  */
 package com.android.settings.applications;
 
-import static android.app.AppOpsManager.MODE_ALLOWED;
-import static android.app.AppOpsManager.MODE_ERRORED;
-import static android.app.AppOpsManager.OP_ENTER_PICTURE_IN_PICTURE_ON_HIDE;
 import static android.content.pm.PackageManager.GET_ACTIVITIES;
 
 import android.annotation.Nullable;
-import android.app.AppOpsManager;
 import android.content.Context;
 import android.content.pm.ActivityInfo;
 import android.content.pm.ApplicationInfo;
@@ -30,8 +26,8 @@
 import android.content.pm.PackageManager;
 import android.os.Bundle;
 import android.os.UserHandle;
-import android.support.v14.preference.SwitchPreference;
 import android.support.v7.preference.Preference;
+import android.support.v7.preference.Preference.OnPreferenceClickListener;
 import android.support.v7.preference.PreferenceScreen;
 import android.util.ArrayMap;
 import android.view.View;
@@ -40,7 +36,6 @@
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.R;
 import com.android.settings.notification.EmptyTextSettings;
-import com.android.settings.overlay.FeatureFactory;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -98,28 +93,6 @@
         return false;
     }
 
-    /**
-     * Sets whether the app associated with the given {@param packageName} is allowed to enter
-     * picture-in-picture when it is hidden.
-     */
-    static void setEnterPipOnHideStateForPackage(Context context, int uid, String packageName,
-            boolean value) {
-        final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
-        final int newMode = value ? MODE_ALLOWED : MODE_ERRORED;
-        appOps.setMode(OP_ENTER_PICTURE_IN_PICTURE_ON_HIDE,
-                uid, packageName, newMode);
-    }
-
-    /**
-     * @return whether the app associated with the given {@param packageName} is allowed to enter
-     *         picture-in-picture when it is hidden.
-     */
-    static boolean getEnterPipOnHideStateForPackage(Context context, int uid, String packageName) {
-        final AppOpsManager appOps = context.getSystemService(AppOpsManager.class);
-        return appOps.checkOpNoThrow(OP_ENTER_PICTURE_IN_PICTURE_ON_HIDE,
-                uid, packageName) == MODE_ALLOWED;
-    }
-
     @Override
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
@@ -147,8 +120,8 @@
             if (checkPackageHasPictureInPictureActivities(packageInfo.packageName,
                     packageInfo.activities)) {
                 final String packageName = packageInfo.applicationInfo.packageName;
-                final boolean state = getEnterPipOnHideStateForPackage(mContext,
-                        packageInfo.applicationInfo.uid, packageName);
+                final boolean state = PictureInPictureDetails.getEnterPipStateForPackage(
+                        mContext, packageInfo.applicationInfo.uid, packageName);
                 pipApps.add(packageInfo.applicationInfo);
                 packageToState.put(packageName, state);
             }
@@ -160,17 +133,18 @@
         for (final ApplicationInfo appInfo : pipApps) {
             final String packageName = appInfo.packageName;
             final CharSequence label = appInfo.loadLabel(mPackageManager);
-            final SwitchPreference pref = new SwitchPreference(prefContext);
-            pref.setPersistent(false);
+
+            final Preference pref = new Preference(prefContext);
             pref.setIcon(appInfo.loadIcon(mPackageManager));
             pref.setTitle(label);
-            pref.setChecked(packageToState.get(packageName));
-            pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
+            pref.setSummary(PictureInPictureDetails.getPreferenceSummary(prefContext,
+                    appInfo.uid, packageName));
+            pref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                 @Override
-                public boolean onPreferenceChange(Preference preference, Object newValue) {
-                    logSpecialPermissionChange((Boolean) newValue, packageName);
-                    setEnterPipOnHideStateForPackage(mContext, appInfo.uid, packageName,
-                            (Boolean) newValue);
+                public boolean onPreferenceClick(Preference preference) {
+                    AppInfoBase.startAppInfoFragment(PictureInPictureDetails.class,
+                            R.string.picture_in_picture_app_detail_title, packageName, appInfo.uid,
+                            PictureInPictureSettings.this, -1, getMetricsCategory());
                     return true;
                 }
             });
@@ -188,13 +162,4 @@
     public int getMetricsCategory() {
         return MetricsEvent.SETTINGS_MANAGE_PICTURE_IN_PICTURE;
     }
-
-    @VisibleForTesting
-    void logSpecialPermissionChange(boolean newState, String packageName) {
-        int logCategory = newState
-                ? MetricsEvent.APP_PICTURE_IN_PICTURE_ON_HIDE_ALLOW
-                : MetricsEvent.APP_PICTURE_IN_PICTURE_ON_HIDE_DENY;
-        FeatureFactory.getFactory(getContext())
-                .getMetricsFeatureProvider().action(getContext(), logCategory, packageName);
-    }
 }
diff --git a/src/com/android/settings/core/gateway/SettingsGateway.java b/src/com/android/settings/core/gateway/SettingsGateway.java
index fb2c9ce..467b848 100644
--- a/src/com/android/settings/core/gateway/SettingsGateway.java
+++ b/src/com/android/settings/core/gateway/SettingsGateway.java
@@ -52,6 +52,7 @@
 import com.android.settings.applications.ManageApplications;
 import com.android.settings.applications.ManageDomainUrls;
 import com.android.settings.applications.NotificationApps;
+import com.android.settings.applications.PictureInPictureDetails;
 import com.android.settings.applications.PictureInPictureSettings;
 import com.android.settings.applications.ProcessStatsSummary;
 import com.android.settings.applications.ProcessStatsUi;
@@ -218,6 +219,7 @@
             WallpaperTypeSettings.class.getName(),
             VrListenerSettings.class.getName(),
             PictureInPictureSettings.class.getName(),
+            PictureInPictureDetails.class.getName(),
             ManagedProfileSettings.class.getName(),
             ChooseAccountActivity.class.getName(),
             IccLockSettings.class.getName(),