abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/LayoutsPreference.java
diff options
context:
space:
mode:
authorJules Aguillon2023-07-29 18:37:06 +0200
committerJules Aguillon2023-07-30 21:44:14 +0200
commitdad5f57a036e3f0ad7278ccee5bd248192cbbad2 (patch)
tree022b170f190edcf204dc453a49e83193821aeb80 /srcs/juloo.keyboard2/LayoutsPreference.java
parent818aa4c7d597116ed595e960d4415b57ed56d2ec (diff)
downloadunexpected-keyboard-dad5f57a036e3f0ad7278ccee5bd248192cbbad2.tar.gz
unexpected-keyboard-dad5f57a036e3f0ad7278ccee5bd248192cbbad2.zip
Allow more than 2 layouts
The two layout selection options are replaced by a ListGroupPreference that allow to enter an arbitrary amount of layouts. The "switch_second" and "switch_second_back" keys are replaced by "switch_forward" and "switch_backward", which allow to cycle through the selected layouts in two directions. Layouts are changed to place these two key on the space bar. The backward key is not shown if there's only two layouts.
Diffstat (limited to 'srcs/juloo.keyboard2/LayoutsPreference.java')
-rw-r--r--srcs/juloo.keyboard2/LayoutsPreference.java89
1 files changed, 89 insertions, 0 deletions
diff --git a/srcs/juloo.keyboard2/LayoutsPreference.java b/srcs/juloo.keyboard2/LayoutsPreference.java
new file mode 100644
index 0000000..5d1d566
--- /dev/null
+++ b/srcs/juloo.keyboard2/LayoutsPreference.java
@@ -0,0 +1,89 @@
+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.Collections;
+import java.util.List;
+
+public class LayoutsPreference extends ListGroupPreference
+{
+ static final String KEY = "layouts";
+ static final List<String> DEFAULT = Collections.singletonList("system");
+
+ /** Layout names as stored in the preferences. */
+ 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 = 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);
+ }
+
+ @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)
+ {
+ return getContext().getString(R.string.pref_layouts_item, i + 1, value);
+ }
+
+ @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);
+ }
+
+ 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.custom_extra_key_add_dialog)
+ .setAdapter(layouts, new DialogInterface.OnClickListener(){
+ public void onClick(DialogInterface dialog, int which)
+ {
+ callback.select(_layout_names[which]);
+ }
+ })
+ .show();
+ }
+
+ class LayoutsAddButton extends AddButton
+ {
+ public LayoutsAddButton(Context ctx)
+ {
+ super(ctx);
+ setLayoutResource(R.layout.pref_layouts_add_btn);
+ }
+ }
+}