Settings: convert to PreferenceFragmentCompat

Change-Id: Ic5509377f06ff147180d3f58a58469afff3ee40f
Signed-off-by: Alexander Martinz <amartinz@shiftphones.com>
Signed-off-by: Joey <joey@lineageos.org>
diff --git a/Android.mk b/Android.mk
index df5c61b..089882b 100644
--- a/Android.mk
+++ b/Android.mk
@@ -16,6 +16,7 @@
     androidx.core_core \
     androidx.legacy_legacy-support-v4 \
     androidx.palette_palette \
+    androidx.preference_preference \
     androidx.recyclerview_recyclerview
 
 LOCAL_USE_AAPT2 := true
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index f6e5693..7efe3a9 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -126,7 +126,8 @@
         <!-- Used to set options -->
         <activity
             android:name="org.lineageos.eleven.ui.activities.SettingsActivity"
-            android:label="@string/menu_settings"/>
+            android:label="@string/menu_settings"
+            android:theme="@style/Eleven.Theme.ActionBar.Overlay"/>
         <!-- Audio Preview -->
         <activity
             android:name=".ui.activities.preview.AudioPreviewActivity"
diff --git a/build.gradle b/build.gradle
index 37b5791..b8ae38f 100644
--- a/build.gradle
+++ b/build.gradle
@@ -132,6 +132,9 @@
     // see https://developer.android.com/jetpack/androidx/releases/palette
     implementation "androidx.palette:palette:1.0.0"
 
+    // see https://developer.android.com/jetpack/androidx/releases/preference
+    implementation "androidx.preference:preference:1.0.0"
+
     // see https://developer.android.com/jetpack/androidx/releases/recyclerview
     implementation "androidx.recyclerview:recyclerview:1.0.0"
 
diff --git a/res/layout/activity_settings.xml b/res/layout/activity_settings.xml
new file mode 100644
index 0000000..0f7fcc5
--- /dev/null
+++ b/res/layout/activity_settings.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright (C) 2019 The LineageOS Project
+  Copyright (C) 2019 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.
+-->
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical">
+
+    <androidx.appcompat.widget.Toolbar
+        android:id="@+id/toolbar"
+        android:layout_width="match_parent"
+        android:layout_height="?android:attr/actionBarSize"
+        android:background="@color/header_action_bar_color"
+        android:elevation="@dimen/action_bar_elevation"
+        android:theme="@style/ToolBarTheme"
+        app:popupTheme="@style/ToolBarPopupTheme" />
+
+    <fragment
+        android:name="org.lineageos.eleven.ui.activities.SettingsActivity$SettingsFragment"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+        android:tag="org.lineageos.eleven.ui.activities.SettingsActivity$SettingsFragment" />
+
+</LinearLayout>
diff --git a/src/org/lineageos/eleven/ui/activities/SettingsActivity.java b/src/org/lineageos/eleven/ui/activities/SettingsActivity.java
index 08f9b42..3858563 100644
--- a/src/org/lineageos/eleven/ui/activities/SettingsActivity.java
+++ b/src/org/lineageos/eleven/ui/activities/SettingsActivity.java
@@ -2,6 +2,7 @@
  * Copyright (C) 2012 Andrew Neal
  * Copyright (C) 2014 The CyanogenMod Project
  * Copyright (C) 2019 The LineageOS Project
+ * Copyright (C) 2019 SHIFT GmbH
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -19,46 +20,33 @@
 package org.lineageos.eleven.ui.activities;
 
 import android.app.AlertDialog;
-import android.content.DialogInterface;
 import android.content.SharedPreferences;
+import android.graphics.drawable.ColorDrawable;
+import android.graphics.drawable.Drawable;
 import android.os.Bundle;
-import android.preference.Preference;
-import android.preference.Preference.OnPreferenceClickListener;
-import android.preference.PreferenceActivity;
 import android.view.MenuItem;
 
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.appcompat.widget.Toolbar;
+import androidx.core.content.ContextCompat;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceFragmentCompat;
+
 import org.lineageos.eleven.R;
 import org.lineageos.eleven.cache.ImageFetcher;
 import org.lineageos.eleven.utils.MusicUtils;
 import org.lineageos.eleven.utils.PreferenceUtils;
 
-/**
- * Settings.
- *
- * @author Andrew Neal (andrewdneal@gmail.com)
- */
-@SuppressWarnings("deprecation")
-public class SettingsActivity extends PreferenceActivity
-        implements SharedPreferences.OnSharedPreferenceChangeListener {
+public class SettingsActivity extends AppCompatActivity {
 
     @Override
     protected void onCreate(final Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_settings);
 
-        // Add the preferences
-        addPreferencesFromResource(R.xml.settings);
-
-        final Preference deleteCache = findPreference("delete_cache");
-        deleteCache.setOnPreferenceClickListener(preference -> {
-            new AlertDialog.Builder(SettingsActivity.this).setMessage(R.string.delete_warning)
-                    .setPositiveButton(android.R.string.ok, (dialog, which) ->
-                            ImageFetcher.getInstance(SettingsActivity.this).clearCaches())
-                    .setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss())
-                    .create().show();
-            return true;
-        });
-
-        PreferenceUtils.getInstance(this).setOnSharedPreferenceChangeListener(this);
+        final Toolbar toolbar = findViewById(R.id.toolbar);
+        setSupportActionBar(toolbar);
+        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
     }
 
     @Override
@@ -74,31 +62,63 @@
         return super.onOptionsItemSelected(item);
     }
 
-    @Override
-    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
-        switch (key) {
-            case PreferenceUtils.SHOW_VISUALIZER: {
-                final boolean showVisualizer = sharedPreferences.getBoolean(key, false);
-                if (showVisualizer && !PreferenceUtils.canRecordAudio(this)) {
-                    PreferenceUtils.requestRecordAudio(this);
+    public static class SettingsFragment extends PreferenceFragmentCompat implements
+            SharedPreferences.OnSharedPreferenceChangeListener {
+        @Override
+        public void onCreate(final Bundle savedInstanceState) {
+            super.onCreate(savedInstanceState);
+
+            final Preference deleteCache = findPreference("delete_cache");
+            deleteCache.setOnPreferenceClickListener(preference -> {
+                new AlertDialog.Builder(getContext())
+                        .setMessage(R.string.delete_warning)
+                        .setPositiveButton(android.R.string.ok, (dialog, which) ->
+                                ImageFetcher.getInstance(getContext()).clearCaches())
+                        .setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss())
+                        .show();
+                return true;
+            });
+
+            PreferenceUtils.getInstance(getContext()).setOnSharedPreferenceChangeListener(this);
+        }
+
+        @Override
+        public void onDestroy() {
+            PreferenceUtils.getInstance(getContext()).removeOnSharedPreferenceChangeListener(this);
+            super.onDestroy();
+        }
+
+        @Override
+        public void onCreatePreferences(Bundle bundle, String rootKey) {
+            setPreferencesFromResource(R.xml.settings, rootKey);
+        }
+
+        @Override
+        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
+            switch (key) {
+                case PreferenceUtils.SHOW_VISUALIZER: {
+                    final boolean showVisualizer = sharedPreferences.getBoolean(key, false);
+                    if (showVisualizer && !PreferenceUtils.canRecordAudio(getActivity())) {
+                        PreferenceUtils.requestRecordAudio(getActivity());
+                    }
+                    break;
                 }
-                break;
-            }
-            case PreferenceUtils.USE_BLUR: {
-                final boolean useBlur = sharedPreferences.getBoolean(key, false);
-                ImageFetcher.getInstance(SettingsActivity.this).setUseBlur(useBlur);
-                ImageFetcher.getInstance(SettingsActivity.this).clearCaches();
-                break;
-            }
-            case PreferenceUtils.SHAKE_TO_PLAY: {
-                final boolean enableShakeToPlay = sharedPreferences.getBoolean(key, false);
-                MusicUtils.setShakeToPlayEnabled(enableShakeToPlay);
-                break;
-            }
-            case PreferenceUtils.SHOW_ALBUM_ART_ON_LOCKSCREEN: {
-                final boolean showAlbumArtOnLockscreen = sharedPreferences.getBoolean(key, true);
-                MusicUtils.setShowAlbumArtOnLockscreen(showAlbumArtOnLockscreen);
-                break;
+                case PreferenceUtils.USE_BLUR: {
+                    final boolean useBlur = sharedPreferences.getBoolean(key, false);
+                    ImageFetcher.getInstance(getActivity()).setUseBlur(useBlur);
+                    ImageFetcher.getInstance(getActivity()).clearCaches();
+                    break;
+                }
+                case PreferenceUtils.SHAKE_TO_PLAY: {
+                    final boolean enableShakeToPlay = sharedPreferences.getBoolean(key, false);
+                    MusicUtils.setShakeToPlayEnabled(enableShakeToPlay);
+                    break;
+                }
+                case PreferenceUtils.SHOW_ALBUM_ART_ON_LOCKSCREEN: {
+                    final boolean showAlbumArtOnLockscreen = sharedPreferences.getBoolean(key, true);
+                    MusicUtils.setShowAlbumArtOnLockscreen(showAlbumArtOnLockscreen);
+                    break;
+                }
             }
         }
     }
diff --git a/src/org/lineageos/eleven/utils/PreferenceUtils.java b/src/org/lineageos/eleven/utils/PreferenceUtils.java
index 6385dc5..74d5939 100644
--- a/src/org/lineageos/eleven/utils/PreferenceUtils.java
+++ b/src/org/lineageos/eleven/utils/PreferenceUtils.java
@@ -144,6 +144,15 @@
     }
 
     /**
+     * Set the listener for preference change
+     * @param listener
+     */
+    public void removeOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener){
+        mPreferences.unregisterOnSharedPreferenceChangeListener(listener);
+    }
+
+
+    /**
      * Returns the last page the user was on when the app was exited.
      *
      * @return The page to start on when the app is opened.