SystemUI: Bringup tuner navbar and statusbar changes

Based on the following changes:

Author: Steve Kondik <steve@cyngn.com>
Date:   Mon Sep 12 12:49:59 2016 -0700

    systemui: Enable navbar tuner

     * And add an action for it.

    Change-Id: I23a51cc8e33f3498deec9ed60281423c66bbc4ca

Author: Steve Kondik <steve@cyngn.com>
Date:   Wed Sep 14 05:08:02 2016 -0700

    systemui: Fix a few issues with Tuner

     * Allow launching PreferenceScreens directly by setting the
       "tuner" extra to a valid key.
     * Remove the silly warning. There are no dragons here.
     * Move CM stuff to CM manifest.
     * Fix the ActionBar title when navigating.

    Change-Id: Ideac31dbfd71d7c0aa7fc3c20395c24128c598fc

Author: Joey Rizzoli <joey@cyanogenmoditalia.it>
Date:   Tue Nov 8 15:44:25 2016 +0100

    Tuner: allow power notif controls to be launched externally

    Change-Id: I8a9e1a2080640938c99125ab04986dfc99e004bf
    Signed-off-by: Joey Rizzoli <joey@cyanogenmoditalia.it>

Author: Gabriele M <moto.falcon.git@gmail.com>
Date:   Sat Sep 16 19:35:28 2017 +0200

    SystemUI: Revert some Tuner changes

    These changes were done to start the status bar tuner directly without
    showing the main Tuner screen. However, they negatively affect the
    management of the fragments. Revert them so that a different solution
    can be implemented in a follow-up change.

    This reverts the following commits:

     - 338de6728d8910d450f8962a44bbe69feba1ced4
       "Handle onOptionsItemSelected for TunerActivity"

     - b3eba6af8e3253efc1332b55821fd08e4fbf77b0
       "SystemUI: Remove nav bar and status bar options from SystemUI Tuner"

     - 82b9f95f761e7b5cc94ea2542b30460e48f8fb02 (partially)
       "systemui: Fix a few issues with Tuner"

    BUGBASH-661

    Change-Id: I35182248566ff31ed08d2836d66d5bd3f947ee9a

Author: Gabriele M <moto.falcon.git@gmail.com>
Date:   Sat Sep 16 19:35:38 2017 +0200

    SystemUI: Add separate pref screen for the status bar tuner

    Keep the status bar preferences separate and allow to start them
    directly without loading the main tuner fragment.

    Change-Id: Ie1a0d9395b7bc01d585616a1976adeda3c457a0d

Change-Id: Icc84575db18931d3a43bd9e640f2a55a51b460c1
Signed-off-by: Aryan Sinha <sinha.aryan03@gmail.com>
diff --git a/packages/SystemUI/Android.bp b/packages/SystemUI/Android.bp
index 3a1f22f..6fbbfd4 100644
--- a/packages/SystemUI/Android.bp
+++ b/packages/SystemUI/Android.bp
@@ -99,6 +99,7 @@
         "themelib",
     ],
     manifest: "AndroidManifest.xml",
+    additional_manifests: ["LineageManifest.xml"],
 
     kotlincflags: ["-Xjvm-default=enable"],
 
diff --git a/packages/SystemUI/LineageManifest.xml b/packages/SystemUI/LineageManifest.xml
new file mode 100644
index 0000000..6ed6b93
--- /dev/null
+++ b/packages/SystemUI/LineageManifest.xml
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+ * Copyright (c) 2017 The LineageOS 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.
+ */
+-->
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+        package="com.android.systemui">
+
+    <!-- SystemUI Tuner -->
+    <application>
+        <activity-alias
+            android:name=".tuner.StatusBarTuner"
+            android:targetActivity=".tuner.TunerActivity"
+            android:icon="@drawable/tuner"
+            android:theme="@style/TunerSettings"
+            android:label="@string/status_bar"
+            android:process=":tuner"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="com.android.settings.action.STATUS_BAR_TUNER" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity-alias>
+
+        <activity-alias
+            android:name=".tuner.NavBarTuner"
+            android:targetActivity=".tuner.TunerActivity"
+            android:icon="@drawable/tuner"
+            android:theme="@style/TunerSettings"
+            android:label="@string/nav_bar"
+            android:process=":tuner"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="com.android.settings.action.NAV_BAR_TUNER" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity-alias>
+
+        <activity-alias
+            android:name=".tuner.TunerActivity"
+            android:targetActivity=".tuner.TunerActivity"
+            android:icon="@drawable/tuner"
+            android:theme="@style/TunerSettings"
+            android:label="@string/tuner_full_importance_settings"
+            android:process=":tuner"
+            android:exported="true">
+            <intent-filter>
+                <action android:name="com.android.settings.action.POWER_NOTIF_CONTROLS" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+        </activity-alias>
+    </application>
+
+</manifest>
diff --git a/packages/SystemUI/res/xml/status_bar_prefs.xml b/packages/SystemUI/res/xml/status_bar_prefs.xml
new file mode 100644
index 0000000..c1aafc9
--- /dev/null
+++ b/packages/SystemUI/res/xml/status_bar_prefs.xml
@@ -0,0 +1,105 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:sysui="http://schemas.android.com/apk/res-auto"
+    android:key="status_bar"
+    android:title="@string/status_bar">
+
+    <com.android.systemui.tuner.StatusBarSwitch
+        android:key="rotate"
+        android:title="@string/status_bar_settings_auto_rotation" />
+
+    <com.android.systemui.tuner.StatusBarSwitch
+        android:key="headset"
+        android:title="@string/headset" />
+
+    <com.android.systemui.tuner.StatusBarSwitch
+        android:key="managed_profile"
+        android:title="@string/status_bar_work" />
+
+    <!-- ime -->
+    <!-- sync_failing -->
+    <!-- sync_active -->
+
+    <com.android.systemui.tuner.StatusBarSwitch
+        android:key="cast"
+        android:title="@string/quick_settings_cast_title" />
+
+    <com.android.systemui.tuner.StatusBarSwitch
+        android:key="hotspot"
+        android:title="@string/quick_settings_hotspot_label" />
+
+    <com.android.systemui.tuner.StatusBarSwitch
+        android:key="bluetooth"
+        android:title="@string/quick_settings_bluetooth_label" />
+    <com.android.systemui.tuner.StatusBarSwitch
+        android:key="cameratoggle"
+        android:title="@string/quick_settings_camera_label" />
+
+    <!-- nfc -->
+    <!-- tty -->
+    <!-- speakerphone -->
+
+    <com.android.systemui.tuner.StatusBarSwitch
+        android:key="zen"
+        android:title="@string/quick_settings_dnd_label" />
+
+    <!-- mute -->
+
+    <com.android.systemui.tuner.StatusBarSwitch
+        android:key="volume"
+        android:title="@*android:string/volume_unknown" />
+
+    <com.android.systemui.tuner.StatusBarSwitch
+        android:key="wifi"
+        android:title="@string/quick_settings_wifi_label" />
+
+    <com.android.systemui.tuner.StatusBarSwitch
+        android:key="ethernet"
+        android:title="@string/status_bar_ethernet" />
+
+    <com.android.systemui.tuner.StatusBarSwitch
+        android:key="mobile"
+        android:title="@string/quick_settings_cellular_detail_title" />
+
+    <com.android.systemui.tuner.StatusBarSwitch
+        android:key="airplane"
+        android:title="@string/status_bar_airplane" />
+
+    <!-- other weird signal stuff -->
+
+    <com.android.systemui.tuner.BatteryPreference
+        android:title="@string/battery"
+        android:summary="%s"
+        android:entries="@array/battery_options" />
+
+    <com.android.systemui.tuner.StatusBarSwitch
+        android:key="alarm_clock"
+        android:title="@string/status_bar_alarm" />
+
+    <!-- secure -->
+
+    <com.android.systemui.tuner.ClockPreference
+        android:title="@string/tuner_time"
+        android:summary="%s"
+        android:entries="@array/clock_options" />
+
+    <com.android.systemui.tuner.TunerSwitch
+        android:key="low_priority"
+        android:title="@string/tuner_low_priority"
+        sysui:defValue="false" />
+
+</PreferenceScreen>
diff --git a/packages/SystemUI/res/xml/tuner_prefs.xml b/packages/SystemUI/res/xml/tuner_prefs.xml
index 135fc43..b15fde2 100644
--- a/packages/SystemUI/res/xml/tuner_prefs.xml
+++ b/packages/SystemUI/res/xml/tuner_prefs.xml
@@ -20,94 +20,8 @@
 
     <PreferenceScreen
         android:key="status_bar"
-        android:title="@string/status_bar" >
-
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="rotate"
-            android:title="@string/status_bar_settings_auto_rotation" />
-
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="headset"
-            android:title="@string/headset" />
-
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="managed_profile"
-            android:title="@string/status_bar_work" />
-
-        <!-- ime -->
-        <!-- sync_failing -->
-        <!-- sync_active -->
-
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="cast"
-            android:title="@string/quick_settings_cast_title" />
-
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="hotspot"
-            android:title="@string/quick_settings_hotspot_label" />
-
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="bluetooth"
-            android:title="@string/quick_settings_bluetooth_label" />
-
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="cameratoggle"
-            android:title="@string/quick_settings_camera_label" />
-
-        <!-- nfc -->
-        <!-- tty -->
-        <!-- speakerphone -->
-
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="zen"
-            android:title="@string/quick_settings_dnd_label" />
-
-        <!-- mute -->
-
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="volume"
-            android:title="@*android:string/volume_unknown" />
-
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="wifi"
-            android:title="@string/quick_settings_wifi_label" />
-
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="ethernet"
-            android:title="@string/status_bar_ethernet" />
-
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="mobile"
-            android:title="@string/quick_settings_cellular_detail_title" />
-
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="airplane"
-            android:title="@string/status_bar_airplane" />
-
-        <!-- other weird signal stuff -->
-
-        <com.android.systemui.tuner.BatteryPreference
-            android:title="@string/battery"
-            android:summary="%s"
-            android:entries="@array/battery_options" />
-
-        <com.android.systemui.tuner.StatusBarSwitch
-            android:key="alarm_clock"
-            android:title="@string/status_bar_alarm" />
-
-        <!-- secure -->
-
-        <com.android.systemui.tuner.ClockPreference
-            android:title="@string/tuner_time"
-            android:summary="%s"
-            android:entries="@array/clock_options" />
-
-        <com.android.systemui.tuner.TunerSwitch
-            android:key="low_priority"
-            android:title="@string/tuner_low_priority"
-            sysui:defValue="false" />
-
-    </PreferenceScreen>
+        android:title="@string/status_bar"
+        android:fragment="com.android.systemui.tuner.StatusBarTuner" />
 
     <PreferenceScreen
         android:key="volume_and_do_not_disturb"
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/StatusBarTuner.java b/packages/SystemUI/src/com/android/systemui/tuner/StatusBarTuner.java
new file mode 100644
index 0000000..3552ea0
--- /dev/null
+++ b/packages/SystemUI/src/com/android/systemui/tuner/StatusBarTuner.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2017 The LineageOS 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.systemui.tuner;
+
+import android.os.Bundle;
+import android.view.MenuItem;
+
+import androidx.preference.PreferenceFragment;
+
+import com.android.internal.logging.MetricsLogger;
+import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
+import com.android.systemui.R;
+
+public class StatusBarTuner extends PreferenceFragment {
+
+    @Override
+    public void onActivityCreated(Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
+        setHasOptionsMenu(true);
+        getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
+    }
+
+    @Override
+    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
+        addPreferencesFromResource(R.xml.status_bar_prefs);
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        MetricsLogger.visibility(getContext(), MetricsEvent.TUNER, true);
+    }
+
+    @Override
+    public void onPause() {
+        super.onPause();
+        MetricsLogger.visibility(getContext(), MetricsEvent.TUNER, false);
+    }
+
+    @Override
+    public boolean onOptionsItemSelected(MenuItem item) {
+        if (item.getItemId() == android.R.id.home) {
+            getActivity().onBackPressed();
+            return true;
+        }
+        return false;
+    }
+}
diff --git a/packages/SystemUI/src/com/android/systemui/tuner/TunerActivity.java b/packages/SystemUI/src/com/android/systemui/tuner/TunerActivity.java
index 5b66216..f87ec1a 100644
--- a/packages/SystemUI/src/com/android/systemui/tuner/TunerActivity.java
+++ b/packages/SystemUI/src/com/android/systemui/tuner/TunerActivity.java
@@ -65,11 +65,19 @@
 
         if (getFragmentManager().findFragmentByTag(TAG_TUNER) == null) {
             final String action = getIntent().getAction();
-            boolean showDemoMode = action != null && action.equals(
-                    "com.android.settings.action.DEMO_MODE");
-            final PreferenceFragment fragment = showDemoMode
-                    ? new DemoModeFragment(mDemoModeController)
-                    : new TunerFragment(mTunerService);
+            final Fragment fragment;
+            if ("com.android.settings.action.DEMO_MODE".equals(action)) {
+                fragment = new DemoModeFragment(mDemoModeController);
+            } else if ("com.android.settings.action.NAV_BAR_TUNER".equals(action)) {
+                fragment = new NavBarTuner();
+            } else if ("com.android.settings.action.POWER_NOTIF_CONTROLS".equals(action)) {
+                fragment = new PowerNotificationControlsFragment();
+            } else if ("com.android.settings.action.STATUS_BAR_TUNER".equals(action)) {
+                fragment = new StatusBarTuner();
+            } else {
+                fragment = new TunerFragment(mTunerService);
+            }
+
             getFragmentManager().beginTransaction().replace(R.id.content_frame,
                     fragment, TAG_TUNER).commit();
         }