abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/LayoutsPreference.java
blob: ba286308848522f1920a80f7b6b0492035b16e59 (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
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
package juloo.keyboard2;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class LayoutsPreference extends ListGroupPreference<String>
{
  static final String KEY = "layouts";
  static final List<String> DEFAULT = Collections.singletonList("system");
  static final ListGroupPreference.Serializer<String> SERIALIZER =
    new ListGroupPreference.StringSerializer();

  /** Layout names as stored in the preferences. */
  List<String> _layout_names;
  /** Text displayed for each layout in the dialog list. */
  String[] _layout_display_names;

  public LayoutsPreference(Context ctx, AttributeSet attrs)
  {
    super(ctx, attrs);
    setKey(KEY);
    Resources res = ctx.getResources();
    _layout_names = Arrays.asList(res.getStringArray(R.array.pref_layout_values));
    _layout_display_names = res.getStringArray(R.array.pref_layout_entries);
  }

  public static List<String> load_from_preferences(SharedPreferences prefs)
  {
    return load_from_preferences(KEY, prefs, DEFAULT, SERIALIZER);
  }

  @Override
  protected void onSetInitialValue(boolean restoreValue, Object defaultValue)
  {
    super.onSetInitialValue(restoreValue, defaultValue);
    if (_values.size() == 0)
      set_values(new ArrayList<String>(DEFAULT), false);
  }

  @Override
  String label_of_value(String value, int i)
  {
    int value_i = _layout_names.indexOf(value);
    String lname = value_i < 0 ? value : _layout_display_names[value_i];
    return getContext().getString(R.string.pref_layouts_item, i + 1, lname);
  }

  @Override
  AddButton on_attach_add_button(AddButton prev_btn)
  {
    if (prev_btn == null)
      return new LayoutsAddButton(getContext());
    return prev_btn;
  }

  @Override
  boolean should_allow_remove_item()
  {
    return (_values.size() > 1);
  }

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

  @Override
  void select(final SelectionCallback callback)
  {
    ArrayAdapter layouts = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, _layout_display_names);
    new AlertDialog.Builder(getContext())
      .setView(R.layout.dialog_edit_text)
      .setAdapter(layouts, new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface _dialog, int which)
        {
          callback.select(_layout_names.get(which));
        }
      })
      .show();
  }

  class LayoutsAddButton extends AddButton
  {
    public LayoutsAddButton(Context ctx)
    {
      super(ctx);
      setLayoutResource(R.layout.pref_layouts_add_btn);
    }
  }
}