1 package com
.owncloud
.android
.ui
.dialog
;
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
;
13 import com
.owncloud
.android
.R
;
14 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
16 import java
.lang
.reflect
.Method
;
18 public class OwnCloudListPreference
extends ListPreference
{
19 private static final String TAG
= OwnCloudListPreference
.class.getSimpleName();
21 private Context mContext
;
22 private AppCompatDialog mDialog
;
24 public OwnCloudListPreference(Context context
) {
26 this.mContext
= context
;
29 public OwnCloudListPreference(Context context
, AttributeSet attrs
) {
30 super(context
, attrs
);
31 this.mContext
= context
;
34 @TargetApi(Build
.VERSION_CODES
.LOLLIPOP
)
35 public OwnCloudListPreference(Context context
, AttributeSet attrs
, int defStyleAttr
, int defStyleRes
) {
36 super(context
, attrs
, defStyleAttr
, defStyleRes
);
39 @TargetApi(Build
.VERSION_CODES
.LOLLIPOP
)
40 public OwnCloudListPreference(Context context
, AttributeSet attrs
, int defStyleAttr
) {
41 super(context
, attrs
, defStyleAttr
);
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.");
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);
60 PreferenceManager pm
= getPreferenceManager();
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
);
72 mDialog
= builder
.create();
74 mDialog
.onRestoreInstanceState(state
);
80 public void onClick(DialogInterface dialog
, int which
) {
81 if (which
>= 0 && getEntryValues() != null
) {
82 String value
= getEntryValues()[which
].toString();
83 if (callChangeListener(value
)) {
91 public AppCompatDialog
getDialog() {
96 public void onActivityDestroy() {
97 super.onActivityDestroy();
98 if (mDialog
!= null
&& mDialog
.isShowing()) {