abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/prefs/CustomExtraKeysPreference.java
blob: cf47d46979482b55896e2aba610f0b2fc46311e9 (plain)
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
package juloo.keyboard2.prefs;

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.HashMap;
import java.util.List;
import java.util.Map;
import juloo.keyboard2.*;
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 ListGroupPreference<String>
{
  /** This pref stores a list of strings encoded as JSON. */
  static final String KEY = "custom_extra_keys";
  static final ListGroupPreference.Serializer<String> SERIALIZER =
    new ListGroupPreference.StringSerializer();

  public CustomExtraKeysPreference(Context context, AttributeSet attrs)
  {
    super(context, attrs);
    setKey(KEY);
  }

  public static Map<KeyValue, KeyboardData.PreferredPos> get(SharedPreferences prefs)
  {
    Map<KeyValue, KeyboardData.PreferredPos> kvs =
      new HashMap<KeyValue, KeyboardData.PreferredPos>();
    List<String> key_names = load_from_preferences(KEY, prefs, null, SERIALIZER);
    if (key_names != null)
    {
      for (String key_name : key_names)
        kvs.put(KeyValue.makeStringKey(key_name), KeyboardData.PreferredPos.DEFAULT);
    }
    return kvs;
  }

  String label_of_value(String value, int i) { return value; }

  @Override
  void select(final SelectionCallback<String> callback)
  {
    new AlertDialog.Builder(getContext())
      .setView(View.inflate(getContext(), R.layout.dialog_edit_text, null))
      .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int which)
        {
          EditText input = (EditText)((AlertDialog)dialog).findViewById(R.id.text);
          final String k = input.getText().toString();
          if (!k.equals(""))
            callback.select(k);
        }
      })
      .setNegativeButton(android.R.string.cancel, null)
      .show();
  }

  @Override
  Serializer<String> get_serializer() { return SERIALIZER; }
}