1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
package juloo.keyboard2;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
/** Allows to enter custom keys to be added to the keyboard. This shows up at
the top of the "Add keys to the keyboard" option. */
public class CustomExtraKeysPreference extends PreferenceCategory
{
/** This pref stores a list of strings encoded as JSON. */
static String KEY = "custom_extra_keys";
boolean _attached = false;
/** Mutable. This is the list of the key strings, not the key names. */
List<String> _keys;
public CustomExtraKeysPreference(Context context, AttributeSet attrs)
{
super(context, attrs);
setKey(KEY);
setOrderingAsAdded(true);
_keys = new ArrayList<String>();
}
public static List<KeyValue> get(SharedPreferences prefs)
{
List<KeyValue> kvs = new ArrayList<KeyValue>();
String inp = prefs.getString(KEY, null);
if (inp != null)
for (String key_name : load_from_string(inp))
kvs.add(KeyValue.makeStringKey(key_name));
return kvs;
}
@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue)
{
if (restoreValue)
{
String persisted = getPersistedString(null);
if (persisted != null)
set_keys(load_from_string(persisted), false);
}
else if (defaultValue != null)
set_keys(load_from_string((String)defaultValue), false);
}
@Override
protected void onAttachedToActivity()
{
super.onAttachedToActivity();
if (_attached)
return;
_attached = true;
reattach();
}
void reattach()
{
removeAll();
for (String k : _keys)
addPreference(this.new CustomExtraKey(getContext(), k));
addPreference(this.new AddButton(getContext()));
}
void set_keys(List<String> v, boolean persist)
{
_keys = v;
reattach();
if (persist)
persistString(save_to_string(_keys));
}
void add_key(String k)
{
_keys.add(k);
set_keys(_keys, true);
}
void remove_key(String k)
{
_keys.remove(k);
set_keys(_keys, true);
}
static String save_to_string(List<String> keys)
{
return (new JSONArray(keys)).toString();
}
static List<String> load_from_string(String inp)
{
List<String> keys = new ArrayList<String>();
try
{
JSONArray arr = new JSONArray(inp);
for (int i = 0; i < arr.length(); i++)
keys.add(arr.getString(i));
}
catch (JSONException e)
{
Logs.err_load_custom_extra_keys(e);
}
return keys;
}
/** A preference with no key that is only intended to be rendered. */
final class CustomExtraKey extends Preference implements View.OnClickListener
{
String _key;
public CustomExtraKey(Context ctx, String key)
{
super(ctx);
_key = key;
setTitle(key);
setPersistent(false);
setWidgetLayoutResource(R.layout.custom_extra_key_widget);
}
/** Remove-button listener. */
@Override
public void onClick(View _v)
{
CustomExtraKeysPreference.this.remove_key(_key);
}
@Override
protected View onCreateView(ViewGroup parent)
{
View v = super.onCreateView(parent);
v.findViewById(R.id.btn_custom_extra_key_remove).setOnClickListener(this);
return v;
}
}
final class AddButton extends Preference
{
public AddButton(Context ctx)
{
super(ctx);
setPersistent(false);
setLayoutResource(R.layout.custom_extra_key_add);
}
@Override
protected void onClick()
{
new AlertDialog.Builder(getContext())
.setView(R.layout.custom_extra_key_add_dialog)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which)
{
EditText input = (EditText)((AlertDialog)dialog).findViewById(R.id.key_name);
String k = input.getText().toString();
if (!k.equals(""))
CustomExtraKeysPreference.this.add_key(k);
}
})
.setNegativeButton(android.R.string.cancel, null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
}
}
|