Messaging: change to androidx.preferences and use OmniPreferenceTheme

Change-Id: Idd2a7e39c4b47bc73c1b554b329bcbf286480633
diff --git a/Android.bp b/Android.bp
index fd0ed55..f3eecd3 100644
--- a/Android.bp
+++ b/Android.bp
@@ -63,12 +63,11 @@
         "androidx.core_core",
         "androidx.fragment_fragment",
         "androidx.media_media",
-        "androidx.legacy_legacy-support-core-utils",
-        "androidx.legacy_legacy-support-core-ui",
         "androidx.palette_palette",
         "androidx.recyclerview_recyclerview",
         "androidx.viewpager_viewpager",
         "androidx.legacy_legacy-support-v13",
+        "androidx.preference_preference",
         "colorpicker",
         "libchips",
         "libphotoviewer",
@@ -78,6 +77,7 @@
         "com.android.vcard",
         "guava",
         "libphonenumber",
+        "OmniPreferenceTheme"
     ],
 
     aaptflags: [
diff --git a/proguard.flags b/proguard.flags
index c5bbb42..f11fa05 100644
--- a/proguard.flags
+++ b/proguard.flags
@@ -37,7 +37,7 @@
 -keep public class * extends android.content.BroadcastReceiver
 -keep public class * extends android.content.ContentProvider
 -keep public class * extends android.app.backup.BackupAgentHelper
--keep public class * extends android.preference.Preference
+-keep public class * extends androidx.preference.Preference
 -keep public class * extends androidx.fragment.app.Fragment
 -keep public class com.android.vcard.* { *; }
 
diff --git a/res/values-night-v31/styles.xml b/res/values-night-v31/styles.xml
new file mode 100644
index 0000000..a83c8ff
--- /dev/null
+++ b/res/values-night-v31/styles.xml
@@ -0,0 +1,55 @@
+<?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.
+-->
+<resources>
+    <!--
+        Base application theme, dependent on API level. This theme is replaced
+        by BugleBaseTheme from res/values-vXX/styles.xml on newer devices.
+    -->
+    <style name="BugleBaseTheme" parent="Theme.AppCompat">
+        <!--
+            Theme customizations available in newer API levels can go in
+            res/values-vXX/styles.xml, while customizations related to
+            backward-compatibility can go here.
+        -->
+    </style>
+
+
+    <!-- Application theme. -->
+    <style name="BugleTheme" parent="BugleBaseTheme">
+        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
+         <item name="android:dropDownListViewStyle">@style/DropDownListViewStyle</item>
+         <item name="colorPrimary">@color/action_bar_background_color</item>
+         <item name="colorPrimaryDark">@color/action_bar_background_color</item>
+         <item name="colorAccent">@color/action_bar_background_color</item>
+         <item name="android:textColorHighlight">@color/text_highlight_color</item>
+         <item name="actionBarStyle">@style/BugleActionBar</item>
+         <item name="apnPreferenceStyle">@style/ApnPreference</item>
+         <item name="android:listViewStyle">@style/ListViewStyle</item>
+         <item name="actionBarPopupTheme">@style/PopupMenuStyleAppCompat</item>
+         <item name="android:actionBarPopupTheme">@style/PopupMenuStyleAppCompat</item>
+         <item name="alertDialogTheme">@style/BugleThemeAlertDialog</item>
+         <item name="android:alertDialogTheme">@style/BugleThemeAlertDialog</item>
+        <item name="preferenceTheme">@style/Omni.PreferenceTheme</item>
+        <item name="android:switchStyle">@style/Omni.SwitchStyle</item>
+        <item name="android:textAppearanceListItem">@style/Omni.PreferenceTitle</item>
+        <item name="android:navigationBarColor">@android:color/transparent</item>
+    </style>
+
+    <style name="BugleTheme.SettingsActivity" parent="BugleTheme">
+    </style>
+
+</resources>
diff --git a/res/values-v31/styles.xml b/res/values-v31/styles.xml
new file mode 100644
index 0000000..bc7e3fd
--- /dev/null
+++ b/res/values-v31/styles.xml
@@ -0,0 +1,54 @@
+<?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.
+-->
+<resources>
+    <!--
+        Base application theme, dependent on API level. This theme is replaced
+        by BugleBaseTheme from res/values-vXX/styles.xml on newer devices.
+    -->
+    <style name="BugleBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
+        <!--
+            Theme customizations available in newer API levels can go in
+            res/values-vXX/styles.xml, while customizations related to
+            backward-compatibility can go here.
+        -->
+    </style>
+
+
+    <!-- Application theme. -->
+    <style name="BugleTheme" parent="BugleBaseTheme">
+        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
+         <item name="android:dropDownListViewStyle">@style/DropDownListViewStyle</item>
+         <item name="colorPrimary">@color/action_bar_background_color</item>
+         <item name="colorPrimaryDark">@color/action_bar_background_color</item>
+         <item name="colorAccent">@color/action_bar_background_color</item>
+         <item name="android:textColorHighlight">@color/text_highlight_color</item>
+         <item name="actionBarStyle">@style/BugleActionBar</item>
+         <item name="apnPreferenceStyle">@style/ApnPreference</item>
+         <item name="android:listViewStyle">@style/ListViewStyle</item>
+         <item name="actionBarPopupTheme">@style/PopupMenuStyleAppCompat</item>
+         <item name="android:actionBarPopupTheme">@style/PopupMenuStyleAppCompat</item>
+         <item name="alertDialogTheme">@style/BugleThemeAlertDialog</item>
+         <item name="android:alertDialogTheme">@style/BugleThemeAlertDialog</item>
+        <item name="preferenceTheme">@style/Omni.PreferenceTheme</item>
+        <item name="android:switchStyle">@style/Omni.SwitchStyle</item>
+        <item name="android:textAppearanceListItem">@style/Omni.PreferenceTitle</item>
+        <item name="android:navigationBarColor">@android:color/transparent</item>
+    </style>
+
+    <style name="BugleTheme.SettingsActivity" parent="BugleTheme">
+    </style>
+</resources>
diff --git a/src/com/android/messaging/ui/appsettings/ApnEditorActivity.java b/src/com/android/messaging/ui/appsettings/ApnEditorActivity.java
index d174e81..322c760 100644
--- a/src/com/android/messaging/ui/appsettings/ApnEditorActivity.java
+++ b/src/com/android/messaging/ui/appsettings/ApnEditorActivity.java
@@ -25,9 +25,9 @@
 import android.database.sqlite.SQLiteDatabase;
 import android.os.AsyncTask;
 import android.os.Bundle;
-import android.preference.EditTextPreference;
-import android.preference.Preference;
-import android.preference.PreferenceFragment;
+import androidx.preference.EditTextPreference;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceFragment;
 import android.provider.Telephony;
 import androidx.core.app.NavUtils;
 import android.view.KeyEvent;
@@ -175,9 +175,7 @@
         }
 
         @Override
-        public void onActivityCreated(Bundle savedInstanceState) {
-            super.onActivityCreated(savedInstanceState);
-
+        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
             addPreferencesFromResource(R.xml.apn_editor);
 
             setHasOptionsMenu(true);
diff --git a/src/com/android/messaging/ui/appsettings/ApnPreference.java b/src/com/android/messaging/ui/appsettings/ApnPreference.java
index 64358eb..0d7d947 100644
--- a/src/com/android/messaging/ui/appsettings/ApnPreference.java
+++ b/src/com/android/messaging/ui/appsettings/ApnPreference.java
@@ -17,7 +17,6 @@
 package com.android.messaging.ui.appsettings;
 
 import android.content.Context;
-import android.preference.Preference;
 import android.util.AttributeSet;
 import android.util.Log;
 import android.view.View;
@@ -27,6 +26,10 @@
 import android.widget.RadioButton;
 import android.widget.RelativeLayout;
 
+import androidx.annotation.NonNull;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceViewHolder;
+
 import com.android.messaging.R;
 import com.android.messaging.datamodel.data.ParticipantData;
 import com.android.messaging.ui.UIIntents;
@@ -59,9 +62,9 @@
     private int mSubId = ParticipantData.DEFAULT_SELF_SUB_ID;
 
     @Override
-    public View getView(View convertView, ViewGroup parent) {
-        View view = super.getView(convertView, parent);
-
+    public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
+        super.onBindViewHolder(holder);
+        View view = holder.itemView;
         View widget = view.findViewById(R.id.apn_radiobutton);
         if ((widget != null) && widget instanceof RadioButton) {
             RadioButton rb = (RadioButton) widget;
@@ -87,8 +90,6 @@
         if ((textLayout != null) && textLayout instanceof RelativeLayout) {
             textLayout.setOnClickListener(this);
         }
-
-        return view;
     }
 
     public boolean isChecked() {
diff --git a/src/com/android/messaging/ui/appsettings/ApnSettingsActivity.java b/src/com/android/messaging/ui/appsettings/ApnSettingsActivity.java
index 13f775a..10fb997 100644
--- a/src/com/android/messaging/ui/appsettings/ApnSettingsActivity.java
+++ b/src/com/android/messaging/ui/appsettings/ApnSettingsActivity.java
@@ -30,10 +30,10 @@
 import android.os.Looper;
 import android.os.Message;
 import android.os.UserManager;
-import android.preference.Preference;
-import android.preference.PreferenceFragment;
-import android.preference.PreferenceGroup;
-import android.preference.PreferenceScreen;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceFragment;
+import androidx.preference.PreferenceGroup;
+import androidx.preference.PreferenceScreen;
 import android.provider.Telephony;
 import androidx.core.app.NavUtils;
 import android.view.Menu;
@@ -162,9 +162,7 @@
         }
 
         @Override
-        public void onActivityCreated(Bundle savedInstanceState) {
-            super.onActivityCreated(savedInstanceState);
-
+        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
             final ListView lv = (ListView) getView().findViewById(android.R.id.list);
             TextView empty = (TextView) getView().findViewById(android.R.id.empty);
             if (empty != null) {
@@ -303,8 +301,7 @@
         }
 
         @Override
-        public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
-                Preference preference) {
+        public boolean onPreferenceTreeClick(Preference preference) {
             startActivity(
                     UIIntents.get().getApnEditorIntent(getActivity(), preference.getKey(), mSubId));
             return true;
diff --git a/src/com/android/messaging/ui/appsettings/ApplicationSettingsActivity.java b/src/com/android/messaging/ui/appsettings/ApplicationSettingsActivity.java
index ac8e0b6..e8e7f88 100644
--- a/src/com/android/messaging/ui/appsettings/ApplicationSettingsActivity.java
+++ b/src/com/android/messaging/ui/appsettings/ApplicationSettingsActivity.java
@@ -21,10 +21,10 @@
 import android.content.Intent;
 import android.net.Uri;
 import android.os.Bundle;
-import android.preference.Preference;
-import android.preference.PreferenceFragment;
-import android.preference.PreferenceScreen;
-import android.preference.SwitchPreference;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceFragmentCompat;
+import androidx.preference.PreferenceScreen;
+import androidx.preference.SwitchPreference;
 import android.provider.Settings;
 import androidx.core.app.NavUtils;
 import android.text.TextUtils;
@@ -51,10 +51,8 @@
         if (topLevel) {
             getSupportActionBar().setTitle(getString(R.string.settings_activity_title));
         }
-
-        FragmentTransaction ft = getFragmentManager().beginTransaction();
-        ft.replace(android.R.id.content, new ApplicationSettingsFragment());
-        ft.commit();
+        getWindow().getDecorView().setBackgroundColor(getColor(R.color.contextual_action_bar_background_color));
+        getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new ApplicationSettingsFragment()).commit();
     }
 
     @Override
@@ -80,7 +78,7 @@
         return super.onOptionsItemSelected(item);
     }
 
-    public static class ApplicationSettingsFragment extends PreferenceFragment {
+    public static class ApplicationSettingsFragment extends PreferenceFragmentCompat {
 
         private String mNotificationsPreferenceKey;
         private Preference mNotificationsPreference;
@@ -94,8 +92,7 @@
         }
 
         @Override
-        public void onCreate(final Bundle savedInstanceState) {
-            super.onCreate(savedInstanceState);
+        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
 
             getPreferenceManager().setSharedPreferencesName(BuglePrefs.SHARED_PREFERENCES_NAME);
             addPreferencesFromResource(R.xml.preferences_application);
@@ -131,8 +128,7 @@
         }
 
         @Override
-        public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
-                Preference preference) {
+        public boolean onPreferenceTreeClick(Preference preference) {
             if (preference.getKey() == mNotificationsPreferenceKey) {
                 Intent intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
                 intent.putExtra(Settings.EXTRA_APP_PACKAGE, getContext().getPackageName());
@@ -144,7 +140,7 @@
                 startActivity(intent);
                 return true;
             }
-            return super.onPreferenceTreeClick(preferenceScreen, preference);
+            return super.onPreferenceTreeClick(preference);
         }
 
         private void updateSmsEnabledPreferences() {
diff --git a/src/com/android/messaging/ui/appsettings/PerSubscriptionSettingsActivity.java b/src/com/android/messaging/ui/appsettings/PerSubscriptionSettingsActivity.java
index c2fd473..03b0f6e 100644
--- a/src/com/android/messaging/ui/appsettings/PerSubscriptionSettingsActivity.java
+++ b/src/com/android/messaging/ui/appsettings/PerSubscriptionSettingsActivity.java
@@ -23,16 +23,21 @@
 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
 import android.content.pm.PackageManager;
 import android.os.Bundle;
-import android.preference.Preference;
-import android.preference.Preference.OnPreferenceClickListener;
-import android.preference.PreferenceCategory;
-import android.preference.PreferenceFragment;
-import android.preference.PreferenceScreen;
+import android.text.InputType;
 import android.text.TextUtils;
+import android.util.Log;
 import android.view.MenuItem;
+import android.widget.EditText;
 
+import androidx.annotation.NonNull;
 import androidx.appcompat.mms.MmsManager;
 import androidx.core.app.NavUtils;
+import androidx.preference.EditTextPreference;
+import androidx.preference.Preference;
+import androidx.preference.Preference.OnPreferenceClickListener;
+import androidx.preference.PreferenceCategory;
+import androidx.preference.PreferenceFragmentCompat;
+import androidx.preference.PreferenceScreen;
 
 import com.android.messaging.Factory;
 import com.android.messaging.R;
@@ -61,11 +66,8 @@
         } else {
             // This will fall back to the default title, i.e. "Messaging settings," so No-op.
         }
-
-        final FragmentTransaction ft = getFragmentManager().beginTransaction();
-        final PerSubscriptionSettingsFragment fragment = new PerSubscriptionSettingsFragment();
-        ft.replace(android.R.id.content, fragment);
-        ft.commit();
+        getWindow().getDecorView().setBackgroundColor(getColor(R.color.contextual_action_bar_background_color));
+        getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new PerSubscriptionSettingsFragment()).commit();
     }
 
     @Override
@@ -78,7 +80,7 @@
         return super.onOptionsItemSelected(item);
     }
 
-    public static class PerSubscriptionSettingsFragment extends PreferenceFragment
+    public static class PerSubscriptionSettingsFragment extends PreferenceFragmentCompat
             implements OnSharedPreferenceChangeListener {
         private PhoneNumberPreference mPhoneNumberPreference;
         private Preference mGroupMmsPreference;
@@ -91,8 +93,7 @@
         }
 
         @Override
-        public void onCreate(final Bundle savedInstanceState) {
-            super.onCreate(savedInstanceState);
+        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
 
             // Get sub id from launch intent
             final Intent intent = getActivity().getIntent();
@@ -113,6 +114,7 @@
 
             mPhoneNumberPreference.setDefaultPhoneNumber(
                     PhoneUtils.get(mSubId).getCanonicalForSelf(false/*allowOverride*/), mSubId);
+            mPhoneNumberPreference.updateSummary();
 
             mGroupMmsPrefKey = getString(R.string.group_mms_pref_key);
             mGroupMmsPreference = findPreference(mGroupMmsPrefKey);
diff --git a/src/com/android/messaging/ui/appsettings/PhoneNumberPreference.java b/src/com/android/messaging/ui/appsettings/PhoneNumberPreference.java
index 11d6160..82af8fa 100644
--- a/src/com/android/messaging/ui/appsettings/PhoneNumberPreference.java
+++ b/src/com/android/messaging/ui/appsettings/PhoneNumberPreference.java
@@ -17,14 +17,20 @@
 package com.android.messaging.ui.appsettings;
 
 import android.content.Context;
-import android.preference.EditTextPreference;
-import androidx.core.text.BidiFormatter;
-import androidx.core.text.TextDirectionHeuristicsCompat;
 import android.text.InputType;
 import android.text.TextUtils;
 import android.util.AttributeSet;
+import android.util.Log;
 import android.view.ContextThemeWrapper;
 import android.view.View;
+import android.widget.EditText;
+
+import androidx.annotation.NonNull;
+import androidx.preference.EditTextPreference;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceViewHolder;
+import androidx.core.text.BidiFormatter;
+import androidx.core.text.TextDirectionHeuristicsCompat;
 
 import com.android.messaging.R;
 import com.android.messaging.util.PhoneUtils;
@@ -39,12 +45,27 @@
  */
 public class PhoneNumberPreference extends EditTextPreference {
 
-    private String mDefaultPhoneNumber;
+    private String mDefaultPhoneNumber = "";
     private int mSubId;
 
-    public PhoneNumberPreference(final Context context, final AttributeSet attrs) {
+    public PhoneNumberPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
+        super(context, attrs, defStyleAttr, defStyleRes);
+        init();
+    }
+
+    public PhoneNumberPreference(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+        init();
+    }
+
+    public PhoneNumberPreference(Context context, AttributeSet attrs) {
         super(context, attrs);
-        mDefaultPhoneNumber = "";
+        init();
+    }
+
+    public PhoneNumberPreference(Context context) {
+        super(context);
+        init();
     }
 
     public void setDefaultPhoneNumber(final String phoneNumber, final int subscriptionId) {
@@ -52,13 +73,19 @@
         mSubId = subscriptionId;
     }
 
-    @Override
-    protected void onBindView(final View view) {
-        // Show the preference value if it's set, or the default number if not.
-        // If we don't have a default, fall back to a static string (e.g. Unknown).
+    private void init() {
+        setOnBindEditTextListener(new EditTextPreference.OnBindEditTextListener() {
+            @Override
+            public void onBindEditText(@NonNull EditText editText) {
+                editText.setText(getSummary());
+                editText.setInputType(InputType.TYPE_CLASS_PHONE);
+            }
+        });
+    }
+    public void updateSummary() {
         String value = getText();
         if (TextUtils.isEmpty(value)) {
-          value = mDefaultPhoneNumber;
+            value = mDefaultPhoneNumber;
         }
         final String displayValue = (!TextUtils.isEmpty(value))
                 ? PhoneUtils.get(mSubId).formatForDisplay(value)
@@ -66,52 +93,24 @@
         final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
         final String phoneNumber = bidiFormatter.unicodeWrap
                         (displayValue, TextDirectionHeuristicsCompat.LTR);
-        // Set the value as the summary and let the superclass populate the views
         setSummary(phoneNumber);
-        super.onBindView(view);
     }
 
     @Override
-    protected void onBindDialogView(final View view) {
-        super.onBindDialogView(view);
-
-        final String value = getText();
-
-        // If the preference is empty, populate the EditText with the default number instead.
-        if (TextUtils.isEmpty(value) && !TextUtils.isEmpty(mDefaultPhoneNumber)) {
-            final BidiFormatter bidiFormatter = BidiFormatter.getInstance();
-            final String phoneNumber = bidiFormatter.unicodeWrap
-                (PhoneUtils.get(mSubId).getCanonicalBySystemLocale(mDefaultPhoneNumber),
-                            TextDirectionHeuristicsCompat.LTR);
-            getEditText().setText(phoneNumber);
-        }
-        getEditText().setInputType(InputType.TYPE_CLASS_PHONE);
-    }
-
-    @Override
-    protected void onDialogClosed(final boolean positiveResult) {
-        if (positiveResult && mDefaultPhoneNumber != null) {
-            final String value = getEditText().getText().toString();
+    public void setText(final String text) {
+        String textValue = text;
+        if (mDefaultPhoneNumber != null) {
             final PhoneUtils phoneUtils = PhoneUtils.get(mSubId);
-            final String phoneNumber = phoneUtils.getCanonicalBySystemLocale(value);
+            final String phoneNumber = phoneUtils.getCanonicalBySystemLocale(textValue);
             final String defaultPhoneNumber = phoneUtils.getCanonicalBySystemLocale(
                     mDefaultPhoneNumber);
 
             // If the new value is the default, clear the preference.
             if (phoneNumber.equals(defaultPhoneNumber)) {
-                setText("");
-                return;
+                textValue = "";
             }
         }
-        super.onDialogClosed(positiveResult);
-    }
-
-    @Override
-    public void setText(final String text) {
-        super.setText(text);
-
-        // EditTextPreference doesn't show the value on the preference view, but we do.
-        // We thus need to force a rebind of the view when a new value is set.
-        notifyChanged();
+        super.setText(textValue);
+        updateSummary();
     }
 }
diff --git a/src/com/android/messaging/ui/appsettings/SettingsActivity.java b/src/com/android/messaging/ui/appsettings/SettingsActivity.java
index dc16d0a..2744210 100644
--- a/src/com/android/messaging/ui/appsettings/SettingsActivity.java
+++ b/src/com/android/messaging/ui/appsettings/SettingsActivity.java
@@ -57,6 +57,7 @@
     protected void onCreate(final Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
+        getWindow().getDecorView().setBackgroundColor(getColor(R.color.contextual_action_bar_background_color));
 
         // Directly open the detailed settings page as the top-level settings activity if this is
         // not a multi-SIM device.