Use new app header in more screens.

- This covers all subclasses of AppInfoWithHeader: app launch settings,
  app storage settings, draw over settings, write setting details,
  usage  access settings.

Bug: 32442716
Test: RunSettingsRoboTests
Change-Id: I8e5831a4fcce5f98c3a6cc6b0fd66bc4a53d1330
diff --git a/src/com/android/settings/applications/AppInfoWithHeader.java b/src/com/android/settings/applications/AppInfoWithHeader.java
index 004ce0c..c482892 100644
--- a/src/com/android/settings/applications/AppInfoWithHeader.java
+++ b/src/com/android/settings/applications/AppInfoWithHeader.java
@@ -16,10 +16,16 @@
 
 package com.android.settings.applications;
 
+import android.app.Activity;
 import android.os.Bundle;
+import android.support.v7.preference.Preference;
 import android.util.Log;
+import android.view.View;
 
 import com.android.settings.AppHeader;
+import com.android.settings.overlay.FeatureFactory;
+
+import static com.android.settings.applications.AppHeaderController.ActionType;
 
 public abstract class AppInfoWithHeader extends AppInfoBase {
 
@@ -34,8 +40,27 @@
         }
         mCreated = true;
         if (mPackageInfo == null) return;
-        AppHeader.createAppHeader(this, mPackageInfo.applicationInfo.loadIcon(mPm),
-                mPackageInfo.applicationInfo.loadLabel(mPm), mPackageName,
-                mPackageInfo.applicationInfo.uid, 0);
+        final Activity activity = getActivity();
+        if (!FeatureFactory.getFactory(activity)
+                .getDashboardFeatureProvider(activity).isEnabled()) {
+            AppHeader.createAppHeader(this, mPackageInfo.applicationInfo.loadIcon(mPm),
+                    mPackageInfo.applicationInfo.loadLabel(mPm), mPackageName,
+                    mPackageInfo.applicationInfo.uid, 0);
+        } else {
+            final View appHeader = FeatureFactory.getFactory(activity)
+                    .getApplicationFeatureProvider(activity)
+                    .newAppHeaderController(this, null /* appHeader */)
+                    .setIcon(mPackageInfo.applicationInfo.loadIcon(mPm))
+                    .setLabel(mPackageInfo.applicationInfo.loadLabel(mPm))
+                    .setSummary(mPackageInfo)
+                    .setPackageName(mPackageName)
+                    .setUid(mPackageInfo.applicationInfo.uid)
+                    .setButtonActions(ActionType.ACTION_APP_INFO, ActionType.ACTION_NONE)
+                    .done();
+            final Preference appHeaderPref = new LayoutPreference(getPrefContext(), appHeader);
+            // Makes sure it's the first preference onscreen.
+            appHeaderPref.setOrder(-1000);
+            getPreferenceScreen().addPreference(appHeaderPref);
+        }
     }
 }
diff --git a/src/com/android/settings/applications/ProcessStatsDetail.java b/src/com/android/settings/applications/ProcessStatsDetail.java
index bc65a7f..524b663 100644
--- a/src/com/android/settings/applications/ProcessStatsDetail.java
+++ b/src/com/android/settings/applications/ProcessStatsDetail.java
@@ -16,6 +16,7 @@
 
 package com.android.settings.applications;
 
+import android.app.Activity;
 import android.app.ActivityManager;
 import android.app.ActivityManager.RunningServiceInfo;
 import android.app.AlertDialog;
@@ -31,6 +32,7 @@
 import android.graphics.drawable.ColorDrawable;
 import android.os.Bundle;
 import android.os.Process;
+import android.os.UserHandle;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceCategory;
 import android.text.format.Formatter;
@@ -40,6 +42,7 @@
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
+
 import com.android.internal.logging.MetricsProto.MetricsEvent;
 import com.android.settings.AppHeader;
 import com.android.settings.CancellablePreference;
@@ -48,6 +51,7 @@
 import com.android.settings.SettingsPreferenceFragment;
 import com.android.settings.SummaryPreference;
 import com.android.settings.applications.ProcStatsEntry.Service;
+import com.android.settings.overlay.FeatureFactory;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -55,6 +59,8 @@
 import java.util.HashMap;
 import java.util.List;
 
+import static com.android.settings.applications.AppHeaderController.ActionType;
+
 public class ProcessStatsDetail extends SettingsPreferenceFragment {
 
     private static final String TAG = "ProcessStatsDetail";
@@ -119,9 +125,32 @@
             finish();
             return;
         }
-        AppHeader.createAppHeader(this,
-                mApp.mUiTargetApp != null ? mApp.mUiTargetApp.loadIcon(mPm) : new ColorDrawable(0),
-                mApp.mUiLabel, mApp.mPackage, mApp.mUiTargetApp.uid);
+        final Activity activity = getActivity();
+        if (!FeatureFactory.getFactory(activity)
+                .getDashboardFeatureProvider(activity).isEnabled()) {
+            AppHeader.createAppHeader(this, mApp.mUiTargetApp != null
+                            ? mApp.mUiTargetApp.loadIcon(mPm)
+                            : new ColorDrawable(0),
+                    mApp.mUiLabel, mApp.mPackage, mApp.mUiTargetApp.uid);
+        } else {
+            final View appHeader = FeatureFactory.getFactory(activity)
+                    .getApplicationFeatureProvider(activity)
+                    .newAppHeaderController(this, null /* appHeader */)
+                    .setIcon(mApp.mUiTargetApp != null
+                            ? mApp.mUiTargetApp.loadIcon(mPm)
+                            : new ColorDrawable(0))
+                    .setLabel(mApp.mUiLabel)
+                    .setPackageName(mApp.mPackage)
+                    .setUid(mApp.mUiTargetApp != null
+                            ? mApp.mUiTargetApp.uid
+                            : UserHandle.USER_NULL)
+                    .setButtonActions(ActionType.ACTION_APP_INFO, ActionType.ACTION_NONE)
+                    .done();
+            final Preference appHeaderPref = new LayoutPreference(getPrefContext(), appHeader);
+            // Makes sure it's the first preference onscreen.
+            appHeaderPref.setOrder(-1000);
+            getPreferenceScreen().addPreference(appHeaderPref);
+        }
     }
 
     @Override
diff --git a/tests/robotests/src/com/android/settings/applications/AppInfoWithHeaderTest.java b/tests/robotests/src/com/android/settings/applications/AppInfoWithHeaderTest.java
new file mode 100644
index 0000000..744bc0f
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/applications/AppInfoWithHeaderTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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.settings.applications;
+
+import android.app.AlertDialog;
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageInfo;
+import android.content.pm.PackageManager;
+import android.support.v7.preference.PreferenceManager;
+import android.support.v7.preference.PreferenceScreen;
+
+import com.android.settings.SettingsRobolectricTestRunner;
+import com.android.settings.TestConfig;
+import com.android.settings.testutils.FakeFeatureFactory;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.annotation.Config;
+import org.robolectric.shadows.ShadowApplication;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class AppInfoWithHeaderTest {
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private Context mContext;
+
+    private FakeFeatureFactory mFactory;
+    private TestFragment mAppInfoWithHeader;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        FakeFeatureFactory.setupForTest(mContext);
+
+        mFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
+
+        mAppInfoWithHeader = new TestFragment();
+    }
+
+    @Test
+    public void testAppHeaderIsAdded() {
+        when(mFactory.dashboardFeatureProvider.isEnabled()).thenReturn(true);
+        when(mFactory.applicationFeatureProvider.newAppHeaderController(mAppInfoWithHeader, null))
+                .thenReturn(new AppHeaderController(
+                        ShadowApplication.getInstance().getApplicationContext(),
+                        mAppInfoWithHeader,
+                        null));
+        mAppInfoWithHeader.onActivityCreated(null);
+
+        verify(mAppInfoWithHeader.mScreen).addPreference(any(LayoutPreference.class));
+    }
+
+    public static class TestFragment extends AppInfoWithHeader {
+
+        PreferenceManager mManager;
+        PreferenceScreen mScreen;
+
+        public TestFragment() {
+            mPm = mock(PackageManager.class);
+            mManager = mock(PreferenceManager.class);
+            mScreen = mock(PreferenceScreen.class);
+            mPackageInfo = new PackageInfo();
+            mPackageInfo.applicationInfo = new ApplicationInfo();
+
+            when(mManager.getContext())
+                    .thenReturn(ShadowApplication.getInstance().getApplicationContext());
+        }
+
+        @Override
+        public int getMetricsCategory() {
+            return 0;
+        }
+
+        @Override
+        protected boolean refreshUi() {
+            return false;
+        }
+
+        @Override
+        protected AlertDialog createDialog(int id, int errorCode) {
+            return null;
+        }
+
+        @Override
+        public PreferenceScreen getPreferenceScreen() {
+            return mScreen;
+        }
+
+        @Override
+        public PreferenceManager getPreferenceManager() {
+            return mManager;
+        }
+    }
+
+}