1
+ /*
2
+ * Copyright (C) 2015 Domoticz - Mark Heinis
3
+ *
4
+ * Licensed to the Apache Software Foundation (ASF) under one
5
+ * or more contributor license agreements. See the NOTICE file
6
+ * distributed with this work for additional information
7
+ * regarding copyright ownership. The ASF licenses this file
8
+ * to you under the Apache License, Version 2.0 (the
9
+ * "License"); you may not use this file except in compliance
10
+ * with the License. You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing,
15
+ * software distributed under the License is distributed on an
16
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17
+ * KIND, either express or implied. See the License for the
18
+ * specific language governing permissions and limitations
19
+ * under the License.
20
+ */
21
+
22
+ package nl .hnogames .domoticz .preference ;
23
+
24
+ import android .content .Context ;
25
+ import android .content .res .TypedArray ;
26
+ import android .util .AttributeSet ;
27
+ import android .util .Log ;
28
+
29
+ import java .util .ArrayList ;
30
+ import java .util .HashSet ;
31
+ import java .util .List ;
32
+ import java .util .Set ;
33
+
34
+ import androidx .preference .MultiSelectListPreference ;
35
+ import nl .hnogames .domoticz .R ;
36
+ import nl .hnogames .domoticz .helpers .StaticHelper ;
37
+ import nl .hnogames .domoticzapi .Containers .DevicesInfo ;
38
+ import nl .hnogames .domoticzapi .Domoticz ;
39
+ import nl .hnogames .domoticzapi .Interfaces .DevicesReceiver ;
40
+
41
+ public class AutoMultiSelectListPreference extends MultiSelectListPreference {
42
+ private static final String TAG = AutoMultiSelectListPreference .class .getName ();
43
+ private final boolean selectAllValuesByDefault ;
44
+ private CharSequence [] mEntryValues ;
45
+
46
+ public AutoMultiSelectListPreference (Context context , AttributeSet attrs ) {
47
+ super (context , attrs );
48
+ TypedArray typedArray = context .obtainStyledAttributes (attrs , R .styleable .CustomListPreference );
49
+ selectAllValuesByDefault = typedArray .getBoolean (R .styleable .CustomListPreference_selectAllValuesByDefault , false );
50
+ typedArray .recycle ();
51
+ initSwitches ();
52
+ }
53
+
54
+ private void initSwitches () {
55
+ StaticHelper .getDomoticz (getContext ()).getDevices (new DevicesReceiver () {
56
+ @ Override
57
+ public void onReceiveDevices (ArrayList <DevicesInfo > mDevicesInfo ) {
58
+ ArrayList <DevicesInfo > supportedSwitches = new ArrayList <>();
59
+ for (DevicesInfo mExtendedStatusInfo : mDevicesInfo ) {
60
+ String name = mExtendedStatusInfo .getName ();
61
+ if (!name .startsWith (Domoticz .HIDDEN_CHARACTER )) {
62
+ supportedSwitches .add (mExtendedStatusInfo );
63
+ }
64
+ }
65
+ if (supportedSwitches .size () > 0 )
66
+ processSwitches (supportedSwitches );
67
+ }
68
+
69
+ @ Override
70
+ public void onReceiveDevice (DevicesInfo mDevicesInfo ) {
71
+ }
72
+
73
+ @ Override
74
+ public void onError (Exception error ) {
75
+ if (error != null && error .getMessage () != null && error .getMessage ().length () > 0 )
76
+ Log .e (TAG , error .getMessage ());
77
+ }
78
+ }, 0 , "all" );
79
+ }
80
+
81
+ private void processSwitches (ArrayList <DevicesInfo > switches ) {
82
+ CharSequence [] mEntries = getEntries ();
83
+ mEntryValues = getEntryValues ();
84
+
85
+ if (switches != null ) {
86
+ List <String > entries = new ArrayList <>();
87
+ List <String > entryValues = new ArrayList <>();
88
+
89
+ for (DevicesInfo s : switches ) {
90
+ entryValues .add (s .getIdx () + "" );
91
+ entries .add (s .getIdx () + " - " + s .getName ());
92
+ }
93
+
94
+ if (entries != null && entryValues != null && !entries .isEmpty () && !entryValues .isEmpty ()) {
95
+ CharSequence [] dynamicEntries = entries .toArray (new CharSequence [entries .size ()]);
96
+ CharSequence [] dynamicEntryValues = entryValues .toArray (new CharSequence [entryValues .size ()]);
97
+
98
+ //if either of the android attributes for specifying the entries and their values have been left empty, then ignore both and use only the dynamic providers
99
+ if (mEntries == null || mEntryValues == null ) {
100
+ mEntries = dynamicEntries ;
101
+ mEntryValues = dynamicEntryValues ;
102
+ } else {
103
+ CharSequence [] fullEntriesList = new CharSequence [mEntries .length + dynamicEntries .length ];
104
+ CharSequence [] fullEntryValuesList = new CharSequence [mEntryValues .length + dynamicEntryValues .length ];
105
+
106
+ int i , j ;
107
+ for (i = 0 ; i <= mEntries .length - 1 ; i ++) {
108
+ fullEntriesList [i ] = mEntries [i ];
109
+ fullEntryValuesList [i ] = mEntryValues [i ];
110
+ }
111
+
112
+ try {
113
+ for (i = mEntries .length , j = 0 ; j <= dynamicEntries .length - 1 ; i ++, j ++) {
114
+ fullEntriesList [i ] = dynamicEntries [j ];
115
+ fullEntryValuesList [i ] = dynamicEntryValues [j ];
116
+ }
117
+ } catch (ArrayIndexOutOfBoundsException ex ) {
118
+ }
119
+
120
+ //replace the entries and entryValues arrays with the new lists
121
+ mEntries = fullEntriesList ;
122
+ mEntryValues = fullEntryValuesList ;
123
+
124
+ setEntries (mEntries );
125
+ setEntryValues (mEntryValues );
126
+ }
127
+ }
128
+ }
129
+ }
130
+
131
+ @ Override
132
+ protected void onSetInitialValue (boolean restoreValue , Object defaultValue ) {
133
+ if (!restoreValue && selectAllValuesByDefault && mEntryValues != null ) {
134
+ final Set <String > result = new HashSet <>();
135
+
136
+ for (CharSequence mEntryValue : mEntryValues ) {
137
+ result .add (mEntryValue .toString ());
138
+ }
139
+ setValues (result );
140
+ return ;
141
+ }
142
+ super .onSetInitialValue (restoreValue , defaultValue );
143
+ }
144
+ }
0 commit comments