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