/* * * * * This file is part of QuickLyric * * Copyright © 2017 QuickLyric SPRL * * * * QuickLyric is free software: you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation, either version 3 of the License, or * * (at your option) any later version. * * * * QuickLyric is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * * along with QuickLyric. If not, see <http://www.gnu.org/licenses/>. * */ package com.geecko.QuickLyric.view; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.preference.ListPreference; import android.preference.PreferenceManager; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatDialog; import android.util.AttributeSet; import java.lang.reflect.Method; public class AppCompatListPreference extends ListPreference { private AppCompatDialog mDialog; public AppCompatListPreference(Context context) { super(context); } public AppCompatListPreference(Context context, AttributeSet attrs) { super(context, attrs); } @Override public AppCompatDialog getDialog() { return mDialog; } @Override protected void showDialog(Bundle state) { if (getEntries() == null || getEntryValues() == null) { throw new IllegalStateException( "ListPreference requires an entries array and an entryValues array."); } int preselect = findIndexOfValue(getValue()); AlertDialog.Builder builder = new AlertDialog.Builder(getContext()) .setTitle(getDialogTitle()) .setIcon(getDialogIcon()) .setSingleChoiceItems(getEntries(), preselect, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (which >= 0 && getEntryValues() != null) { String value = getEntryValues()[which].toString(); if (callChangeListener(value) && isPersistent()) setValue(value); } dialog.dismiss(); } }); PreferenceManager pm = getPreferenceManager(); try { Method method = pm.getClass().getDeclaredMethod( "registerOnActivityDestroyListener", PreferenceManager.OnActivityDestroyListener.class); method.setAccessible(true); method.invoke(pm, this); } catch (Exception e) { e.printStackTrace(); } mDialog = builder.create(); if (state != null) mDialog.onRestoreInstanceState(state); mDialog.show(); } @Override public void onActivityDestroy() { super.onActivityDestroy(); if (mDialog != null && mDialog.isShowing() && mDialog.getWindow() != null && mDialog.getWindow().getWindowManager() != null) mDialog.dismiss(); } }