OwnCloudListPreference.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.owncloud.android.ui.dialog;
  2. import android.annotation.TargetApi;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.os.Build;
  6. import android.os.Bundle;
  7. import android.preference.ListPreference;
  8. import android.preference.PreferenceManager;
  9. import android.support.v7.app.AppCompatDialog;
  10. import android.util.AttributeSet;
  11. import com.owncloud.android.R;
  12. import com.owncloud.android.lib.common.utils.Log_OC;
  13. import java.lang.reflect.Method;
  14. public class OwnCloudListPreference extends ListPreference {
  15. private static final String TAG = OwnCloudListPreference.class.getSimpleName();
  16. private Context mContext;
  17. private AppCompatDialog mDialog;
  18. public OwnCloudListPreference(Context context) {
  19. super(context);
  20. this.mContext = context;
  21. }
  22. public OwnCloudListPreference(Context context, AttributeSet attrs) {
  23. super(context, attrs);
  24. this.mContext = context;
  25. }
  26. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  27. public OwnCloudListPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  28. super(context, attrs, defStyleAttr, defStyleRes);
  29. }
  30. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  31. public OwnCloudListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
  32. super(context, attrs, defStyleAttr);
  33. }
  34. @Override
  35. protected void showDialog(Bundle state) {
  36. if (getEntries() == null || getEntryValues() == null) {
  37. throw new IllegalStateException(
  38. "ListPreference requires an entries array and an entryValues array.");
  39. }
  40. int preselect = findIndexOfValue(getValue());
  41. // same thing happens for the Standard ListPreference though
  42. android.support.v7.app.AlertDialog.Builder builder =
  43. new android.support.v7.app.AlertDialog.Builder(mContext, R.style.ownCloud_AlertDialog)
  44. .setTitle(getDialogTitle())
  45. .setIcon(getDialogIcon())
  46. .setSingleChoiceItems(getEntries(), preselect, this);
  47. PreferenceManager pm = getPreferenceManager();
  48. try {
  49. Method method = pm.getClass().getDeclaredMethod(
  50. "registerOnActivityDestroyListener",
  51. PreferenceManager.OnActivityDestroyListener.class);
  52. method.setAccessible(true);
  53. method.invoke(pm, this);
  54. } catch (Exception e) {
  55. // no way to handle this but logging it
  56. Log_OC.e(TAG, "error invoking registerOnActivityDestroyListener", e);
  57. }
  58. mDialog = builder.create();
  59. if (state != null) {
  60. mDialog.onRestoreInstanceState(state);
  61. }
  62. mDialog.show();
  63. }
  64. @Override
  65. public void onClick(DialogInterface dialog, int which) {
  66. if (which >= 0 && getEntryValues() != null) {
  67. String value = getEntryValues()[which].toString();
  68. if (callChangeListener(value)) {
  69. setValue(value);
  70. // Workaround for pre kitkat since they don't support change listener within setValue
  71. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
  72. setSummary(getEntries()[which]);
  73. }
  74. }
  75. dialog.dismiss();
  76. }
  77. }
  78. @Override
  79. public AppCompatDialog getDialog() {
  80. return mDialog;
  81. }
  82. @Override
  83. public void onActivityDestroy() {
  84. super.onActivityDestroy();
  85. if (mDialog != null && mDialog.isShowing()) {
  86. mDialog.dismiss();
  87. }
  88. }
  89. }