parts: move clock indicator to its own fragment

Change-Id: I53ad2ba34a2ba1820f18a8a2469a7448c84165e4
Signed-off-by: Joey <jbevilacqua@shiftphones.com>
diff --git a/res/values-de/strings.xml b/res/values-de/strings.xml
index 20b0a0a..9d3b0a9 100644
--- a/res/values-de/strings.xml
+++ b/res/values-de/strings.xml
@@ -92,7 +92,7 @@
     <string name="status_bar_icons_title">Statusleistensymbole</string>
     <string name="status_bar_system_icons_title">Systemsymbole</string>
     <string name="status_bar_system_icons_summary">Festlegen, welche Statusleistensymbole angezeigt werden</string>
-    <string name="status_bar_clock_title">Uhr</string>
+    <string name="status_bar_clock_title">Uhrzeitanzeige</string>
     <string name="status_bar_clock_show_seconds_title">Sekunden anzeigen</string>
     <string name="status_bar_clock_show_seconds_summary">Stunden, Minuten und Sekunden anzeigen</string>
     <string name="status_bar_clock_position_title">Position der Uhr</string>
diff --git a/res/values-it/strings.xml b/res/values-it/strings.xml
index 6f967d1..55f2a89 100644
--- a/res/values-it/strings.xml
+++ b/res/values-it/strings.xml
@@ -91,7 +91,7 @@
     <string name="status_bar_icons_title">Icone barra di stato</string>
     <string name="status_bar_system_icons_title">Icone di sistema</string>
     <string name="status_bar_system_icons_summary">Controlla quali icone vengono visualizzate nella barra di stato</string>
-    <string name="status_bar_clock_title">Orologio</string>
+    <string name="status_bar_clock_title">Indicatore orologio</string>
     <string name="status_bar_clock_show_seconds_title">Mostra secondi</string>
     <string name="status_bar_clock_show_seconds_summary">Mostra ore, minuti e secondi</string>
     <string name="status_bar_clock_position_title">Posizione orologio</string>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 63934ed..7ab7eb8 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -115,7 +115,7 @@
     <string name="status_bar_system_icons_summary">Control which status bar icons are shown</string>
 
     <!-- Status bar - Clock -->
-    <string name="status_bar_clock_title">Clock</string>
+    <string name="status_bar_clock_title">Clock indicator</string>
 
     <string name="status_bar_clock_show_seconds_title">Show seconds</string>
     <string name="status_bar_clock_show_seconds_summary">Show hours, minutes, and seconds</string>
diff --git a/res/xml/parts_catalog.xml b/res/xml/parts_catalog.xml
index 581cb97..1620116 100644
--- a/res/xml/parts_catalog.xml
+++ b/res/xml/parts_catalog.xml
@@ -45,6 +45,11 @@
           android:fragment="com.shiftos.shiftparts.statusbar.StatusBarSettings"
           shift:xmlRes="@xml/status_bar_settings" />
 
+    <part android:key="status_bar_clock_settings"
+          android:title="@string/status_bar_clock_title"
+          android:fragment="com.shiftos.shiftparts.statusbar.StatusBarClockSettings"
+          shift:xmlRes="@xml/status_bar_clock_settings" />
+
     <part android:key="power_menu"
           android:title="@string/power_menu_title"
           android:fragment="com.shiftos.shiftparts.input.PowerMenuActions"
diff --git a/res/xml/status_bar_clock_settings.xml b/res/xml/status_bar_clock_settings.xml
new file mode 100644
index 0000000..701c259
--- /dev/null
+++ b/res/xml/status_bar_clock_settings.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+     Copyright (C) 2014-2016 The CyanogenMod Project
+     Copyright (C) 2017-2018 The LineageOS Project
+     Copyright (C) 2020 Shift GmbH
+
+     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:shift="http://schemas.android.com/apk/res/shiftos.platform"
+    android:key="status_bar_clock_settings"
+    android:title="@string/status_bar_clock_title">
+
+    <shiftos.preference.ShiftSystemSettingListPreference
+        android:key="status_bar_clock"
+        android:title="@string/status_bar_clock_position_title"
+        android:summary="%s"
+        android:dialogTitle="@string/status_bar_clock_position_title"
+        android:entries="@array/status_bar_clock_position_entries"
+        android:entryValues="@array/status_bar_clock_position_values"
+        android:defaultValue="0" />
+
+    <shiftos.preference.SecureSettingSwitchPreference
+        android:key="clock_seconds"
+        android:title="@string/status_bar_clock_show_seconds_title"
+        android:summary="@string/status_bar_clock_show_seconds_summary"
+        android:defaultValue="false" />
+
+    <shiftos.preference.ShiftSystemSettingListPreference
+        android:key="status_bar_am_pm"
+        android:title="@string/status_bar_am_pm_title"
+        android:summary="%s"
+        android:dialogTitle="@string/status_bar_am_pm_title"
+        android:entries="@array/status_bar_am_pm_entries"
+        android:entryValues="@array/status_bar_am_pm_values"
+        android:defaultValue="2" />
+</PreferenceScreen>
diff --git a/res/xml/status_bar_settings.xml b/res/xml/status_bar_settings.xml
index a2d7d45..30ff7f5 100644
--- a/res/xml/status_bar_settings.xml
+++ b/res/xml/status_bar_settings.xml
@@ -35,46 +35,17 @@
         android:title="@string/network_traffic_settings_title"
         android:summary="@string/network_traffic_settings_summary" />
 
-    <PreferenceCategory
-        android:title="@string/status_bar_icons_title">
+    <PreferenceScreen
+        android:key="status_bar_icons"
+        android:title="@string/status_bar_system_icons_title"
+        android:summary="@string/status_bar_system_icons_summary">
+        <intent android:action="com.android.settings.action.STATUS_BAR_TUNER" />
+    </PreferenceScreen>
 
-        <PreferenceScreen
-            android:key="status_bar_icons"
-            android:title="@string/status_bar_system_icons_title"
-            android:summary="@string/status_bar_system_icons_summary">
-            <intent android:action="com.android.settings.action.STATUS_BAR_TUNER" />
-        </PreferenceScreen>
-
-    </PreferenceCategory>
-
-    <PreferenceCategory
+    <PreferenceScreen
+        android:fragment="com.shiftos.shiftparts.statusbar.StatusBarClockSettings"
         android:key="status_bar_clock_key"
-        android:title="@string/status_bar_clock_title">
-
-        <shiftos.preference.ShiftSystemSettingListPreference
-            android:key="status_bar_clock"
-            android:title="@string/status_bar_clock_position_title"
-            android:summary="%s"
-            android:dialogTitle="@string/status_bar_clock_position_title"
-            android:entries="@array/status_bar_clock_position_entries"
-            android:entryValues="@array/status_bar_clock_position_values"
-            android:defaultValue="0" />
-
-        <shiftos.preference.SecureSettingSwitchPreference
-            android:key="clock_seconds"
-            android:title="@string/status_bar_clock_show_seconds_title"
-            android:summary="@string/status_bar_clock_show_seconds_summary"
-            android:defaultValue="false" />
-
-        <shiftos.preference.ShiftSystemSettingListPreference
-            android:key="status_bar_am_pm"
-            android:title="@string/status_bar_am_pm_title"
-            android:summary="%s"
-            android:dialogTitle="@string/status_bar_am_pm_title"
-            android:entries="@array/status_bar_am_pm_entries"
-            android:entryValues="@array/status_bar_am_pm_values"
-            android:defaultValue="2" />
-    </PreferenceCategory>
+        android:title="@string/status_bar_clock_title" />
 
     <PreferenceCategory
         android:title="@string/status_bar_brightness_category">
diff --git a/src/com/shiftos/shiftparts/statusbar/StatusBarClockSettings.java b/src/com/shiftos/shiftparts/statusbar/StatusBarClockSettings.java
new file mode 100644
index 0000000..aa8c9ee
--- /dev/null
+++ b/src/com/shiftos/shiftparts/statusbar/StatusBarClockSettings.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2020 Shift GmbH
+ *
+ * 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.shiftos.shiftparts.statusbar;
+
+import android.content.Context;
+import android.os.Bundle;
+import android.provider.Settings;
+import android.text.format.DateFormat;
+import android.text.TextUtils;
+import android.util.ArraySet;
+import android.view.View;
+
+import shiftos.preference.ShiftSystemSettingListPreference;
+import shiftos.preference.SecureSettingSwitchPreference;
+import shiftos.providers.ShiftSettings;
+
+import com.shiftos.shiftparts.R;
+import com.shiftos.shiftparts.SettingsPreferenceFragment;
+import com.shiftos.shiftparts.search.BaseSearchIndexProvider;
+import com.shiftos.shiftparts.search.Searchable;
+import com.shiftos.shiftparts.utils.DeviceUtils;
+
+import java.util.Set;
+
+public class StatusBarClockSettings extends SettingsPreferenceFragment
+        implements Searchable {
+
+    private static final String ICON_BLACKLIST = "icon_blacklist";
+    private static final String STATUS_BAR_CLOCK_STYLE = "status_bar_clock";
+    private static final String STATUS_BAR_CLOCK_SECONDS = "clock_seconds";
+    private static final String STATUS_BAR_AM_PM = "status_bar_am_pm";
+
+    private ShiftSystemSettingListPreference mStatusBarClock;
+    private SecureSettingSwitchPreference mStatusBarClockSeconds;
+    private ShiftSystemSettingListPreference mStatusBarAmPm;
+
+    private boolean mHasNotch;
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        addPreferencesFromResource(R.xml.status_bar_clock_settings);
+
+        mHasNotch = DeviceUtils.hasNotch(getActivity());
+
+        mStatusBarAmPm =
+                (ShiftSystemSettingListPreference) findPreference(STATUS_BAR_AM_PM);
+        mStatusBarClockSeconds =
+                (SecureSettingSwitchPreference) findPreference(STATUS_BAR_CLOCK_SECONDS);
+        mStatusBarClock =
+                (ShiftSystemSettingListPreference) findPreference(STATUS_BAR_CLOCK_STYLE);
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        updatePrefsStatus();
+    }
+
+    private void updatePrefsStatus() {
+        final String curIconBlacklist = Settings.Secure.getString(getContext().getContentResolver(),
+                ICON_BLACKLIST);
+
+        final boolean available = !TextUtils.delimitedStringContains(
+                curIconBlacklist, ',', "clock");
+        mStatusBarClock.setEnabled(available);
+        mStatusBarClockSeconds.setEnabled(available);
+        final boolean is24h = DateFormat.is24HourFormat(getActivity());
+        mStatusBarAmPm.setEnabled(available && !is24h);
+        if (is24h) {
+            mStatusBarAmPm.setSummary(R.string.status_bar_am_pm_info);
+        }
+
+        final boolean disallowCenteredClock = mHasNotch || getNetworkTrafficStatus() != 0;
+
+        // Adjust status bar preferences for RTL
+        if (getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
+            if (disallowCenteredClock) {
+                mStatusBarClock.setEntries(R.array.status_bar_clock_position_entries_notch_rtl);
+                mStatusBarClock.setEntryValues(R.array.status_bar_clock_position_values_notch_rtl);
+            } else {
+                mStatusBarClock.setEntries(R.array.status_bar_clock_position_entries_rtl);
+                mStatusBarClock.setEntryValues(R.array.status_bar_clock_position_values_rtl);
+            }
+        } else if (disallowCenteredClock) {
+            mStatusBarClock.setEntries(R.array.status_bar_clock_position_entries_notch);
+            mStatusBarClock.setEntryValues(R.array.status_bar_clock_position_values_notch);
+        } else {
+            mStatusBarClock.setEntries(R.array.status_bar_clock_position_entries);
+            mStatusBarClock.setEntryValues(R.array.status_bar_clock_position_values);
+        }
+    }
+
+    private int getNetworkTrafficStatus() {
+        return ShiftSettings.Secure.getInt(getActivity().getContentResolver(),
+                ShiftSettings.Secure.NETWORK_TRAFFIC_MODE, 0);
+    }
+
+    public static final Searchable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
+            new BaseSearchIndexProvider() {
+
+        @Override
+        public Set<String> getNonIndexableKeys(Context context) {
+            return new ArraySet<String>();
+        }
+    };
+}
diff --git a/src/com/shiftos/shiftparts/statusbar/StatusBarSettings.java b/src/com/shiftos/shiftparts/statusbar/StatusBarSettings.java
index a45ef95..0c8b2ae 100644
--- a/src/com/shiftos/shiftparts/statusbar/StatusBarSettings.java
+++ b/src/com/shiftos/shiftparts/statusbar/StatusBarSettings.java
@@ -19,13 +19,11 @@
 import android.content.Context;
 import android.os.Bundle;
 import android.provider.Settings;
-import android.text.format.DateFormat;
 import android.text.TextUtils;
 import android.util.ArraySet;
 import android.view.View;
 
 import androidx.preference.Preference;
-import androidx.preference.PreferenceCategory;
 import androidx.preference.PreferenceScreen;
 
 import shiftos.preference.ShiftSystemSettingListPreference;
@@ -42,26 +40,20 @@
 public class StatusBarSettings extends SettingsPreferenceFragment
         implements Preference.OnPreferenceChangeListener, Searchable {
 
-    private static final String CATEGORY_CLOCK = "status_bar_clock_key";
-
     private static final String ICON_BLACKLIST = "icon_blacklist";
-
     private static final String STATUS_BAR_CLOCK_STYLE = "status_bar_clock";
-    private static final String STATUS_BAR_AM_PM = "status_bar_am_pm";
     private static final String STATUS_BAR_QUICK_QS_PULLDOWN = "qs_quick_pulldown";
 
-
     private static final int PULLDOWN_DIR_NONE = 0;
     private static final int PULLDOWN_DIR_RIGHT = 1;
     private static final int PULLDOWN_DIR_LEFT = 2;
 
     private static final String NETWORK_TRAFFIC_SETTINGS = "network_traffic_settings";
+    private static final String STATUS_BAR_CLOCK_SETTINGS = "status_bar_clock_key";
 
     private ShiftSystemSettingListPreference mQuickPulldown;
-    private ShiftSystemSettingListPreference mStatusBarClock;
-    private ShiftSystemSettingListPreference mStatusBarAmPm;
-    private PreferenceCategory mStatusBarClockCategory;
     private PreferenceScreen mNetworkTrafficPref;
+    private PreferenceScreen mClockIndicatorPref;
 
     private boolean mHasNotch;
 
@@ -71,21 +63,13 @@
         addPreferencesFromResource(R.xml.status_bar_settings);
 
         mNetworkTrafficPref = (PreferenceScreen) findPreference(NETWORK_TRAFFIC_SETTINGS);
+        mClockIndicatorPref = (PreferenceScreen) findPreference(STATUS_BAR_CLOCK_SETTINGS);
 
         mHasNotch = DeviceUtils.hasNotch(getActivity());
         if (mHasNotch) {
             getPreferenceScreen().removePreference(mNetworkTrafficPref);
         }
 
-        mStatusBarAmPm =
-                (ShiftSystemSettingListPreference) findPreference(STATUS_BAR_AM_PM);
-        mStatusBarClock =
-                (ShiftSystemSettingListPreference) findPreference(STATUS_BAR_CLOCK_STYLE);
-        mStatusBarClock.setOnPreferenceChangeListener(this);
-
-        mStatusBarClockCategory =
-                (PreferenceCategory) getPreferenceScreen().findPreference(CATEGORY_CLOCK);
-
         mQuickPulldown =
                 (ShiftSystemSettingListPreference) findPreference(STATUS_BAR_QUICK_QS_PULLDOWN);
         mQuickPulldown.setOnPreferenceChangeListener(this);
@@ -99,38 +83,13 @@
         final String curIconBlacklist = Settings.Secure.getString(getContext().getContentResolver(),
                 ICON_BLACKLIST);
 
-        if (TextUtils.delimitedStringContains(curIconBlacklist, ',', "clock")) {
-            getPreferenceScreen().removePreference(mStatusBarClockCategory);
-        } else {
-            getPreferenceScreen().addPreference(mStatusBarClockCategory);
-        }
+        mClockIndicatorPref.setEnabled(
+                !TextUtils.delimitedStringContains(curIconBlacklist, ',', "clock"));
 
-        if (DateFormat.is24HourFormat(getActivity())) {
-            mStatusBarAmPm.setEnabled(false);
-            mStatusBarAmPm.setSummary(R.string.status_bar_am_pm_info);
-        }
-
-        final boolean disallowCenteredClock = mHasNotch || getNetworkTrafficStatus() != 0;
-
-        // Adjust status bar preferences for RTL
         if (getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
-            if (disallowCenteredClock) {
-                mStatusBarClock.setEntries(R.array.status_bar_clock_position_entries_notch_rtl);
-                mStatusBarClock.setEntryValues(R.array.status_bar_clock_position_values_notch_rtl);
-            } else {
-                mStatusBarClock.setEntries(R.array.status_bar_clock_position_entries_rtl);
-                mStatusBarClock.setEntryValues(R.array.status_bar_clock_position_values_rtl);
-            }
             mQuickPulldown.setEntries(R.array.status_bar_quick_qs_pulldown_entries_rtl);
             mQuickPulldown.setEntryValues(R.array.status_bar_quick_qs_pulldown_values_rtl);
-        } else if (disallowCenteredClock) {
-            mStatusBarClock.setEntries(R.array.status_bar_clock_position_entries_notch);
-            mStatusBarClock.setEntryValues(R.array.status_bar_clock_position_values_notch);
-        } else {
-            mStatusBarClock.setEntries(R.array.status_bar_clock_position_entries);
-            mStatusBarClock.setEntryValues(R.array.status_bar_clock_position_values);
         }
-
         // Disable network traffic preferences if clock is centered in the status bar
         updateNetworkTrafficStatus(getClockPosition());
     }
@@ -143,9 +102,6 @@
             case STATUS_BAR_QUICK_QS_PULLDOWN:
                 updateQuickPulldownSummary(value);
                 break;
-            case STATUS_BAR_CLOCK_STYLE:
-                updateNetworkTrafficStatus(value);
-                break;
         }
         return true;
     }
@@ -184,11 +140,6 @@
         ));
     }
 
-    private int getNetworkTrafficStatus() {
-        return ShiftSettings.Secure.getInt(getActivity().getContentResolver(),
-                ShiftSettings.Secure.NETWORK_TRAFFIC_MODE, 0);
-    }
-
     private int getClockPosition() {
         return ShiftSettings.System.getInt(getActivity().getContentResolver(),
                 STATUS_BAR_CLOCK_STYLE, 2);