2023-07-18 00:31:32 +02:00
|
|
|
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;
|
2023-09-15 18:00:27 +02:00
|
|
|
import java.util.HashMap;
|
2023-07-18 00:31:32 +02:00
|
|
|
import java.util.List;
|
2023-09-15 18:00:27 +02:00
|
|
|
import java.util.Map;
|
2023-07-18 00:31:32 +02:00
|
|
|
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. */
|
2023-08-08 17:58:27 +02:00
|
|
|
public class CustomExtraKeysPreference extends ListGroupPreference<String>
|
2023-07-18 00:31:32 +02:00
|
|
|
{
|
|
|
|
/** This pref stores a list of strings encoded as JSON. */
|
2023-07-29 18:31:26 +02:00
|
|
|
static final String KEY = "custom_extra_keys";
|
2023-08-08 17:58:27 +02:00
|
|
|
static final ListGroupPreference.Serializer<String> SERIALIZER =
|
|
|
|
new ListGroupPreference.StringSerializer();
|
2023-07-18 00:31:32 +02:00
|
|
|
|
|
|
|
public CustomExtraKeysPreference(Context context, AttributeSet attrs)
|
|
|
|
{
|
|
|
|
super(context, attrs);
|
|
|
|
setKey(KEY);
|
|
|
|
}
|
|
|
|
|
2023-09-15 18:00:27 +02:00
|
|
|
public static Map<KeyValue, KeyboardData.PreferredPos> get(SharedPreferences prefs)
|
2023-07-18 00:31:32 +02:00
|
|
|
{
|
2023-09-15 18:00:27 +02:00
|
|
|
Map<KeyValue, KeyboardData.PreferredPos> kvs =
|
|
|
|
new HashMap<KeyValue, KeyboardData.PreferredPos>();
|
2023-08-08 17:58:27 +02:00
|
|
|
List<String> key_names = load_from_preferences(KEY, prefs, null, SERIALIZER);
|
2023-07-29 18:31:26 +02:00
|
|
|
if (key_names != null)
|
2023-07-18 00:31:32 +02:00
|
|
|
{
|
2023-07-29 18:31:26 +02:00
|
|
|
for (String key_name : key_names)
|
2023-09-15 18:00:27 +02:00
|
|
|
kvs.put(KeyValue.makeStringKey(key_name), KeyboardData.PreferredPos.DEFAULT);
|
2023-07-18 00:31:32 +02:00
|
|
|
}
|
2023-07-29 18:31:26 +02:00
|
|
|
return kvs;
|
2023-07-18 00:31:32 +02:00
|
|
|
}
|
|
|
|
|
2023-08-08 17:58:27 +02:00
|
|
|
String label_of_value(String value, int i) { return value; }
|
|
|
|
|
2023-07-18 00:31:32 +02:00
|
|
|
@Override
|
2023-08-08 17:58:27 +02:00
|
|
|
void select(final SelectionCallback<String> callback)
|
2023-07-29 18:31:26 +02:00
|
|
|
{
|
|
|
|
new AlertDialog.Builder(getContext())
|
2023-08-10 12:55:27 +02:00
|
|
|
.setView(R.layout.dialog_edit_text)
|
2023-07-29 18:31:26 +02:00
|
|
|
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
|
|
|
|
public void onClick(DialogInterface dialog, int which)
|
|
|
|
{
|
2023-08-10 12:55:27 +02:00
|
|
|
EditText input = (EditText)((AlertDialog)dialog).findViewById(R.id.text);
|
2023-07-29 18:31:26 +02:00
|
|
|
final String k = input.getText().toString();
|
|
|
|
if (!k.equals(""))
|
|
|
|
callback.select(k);
|
|
|
|
}
|
|
|
|
})
|
2023-08-10 12:55:27 +02:00
|
|
|
.setNegativeButton(android.R.string.cancel, null)
|
2023-07-29 18:31:26 +02:00
|
|
|
.show();
|
2023-07-18 00:31:32 +02:00
|
|
|
}
|
2023-08-08 17:58:27 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
Serializer<String> get_serializer() { return SERIALIZER; }
|
2023-07-18 00:31:32 +02:00
|
|
|
}
|