2023-07-29 18:37:06 +02:00
|
|
|
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;
|
2023-08-06 19:54:38 +02:00
|
|
|
import java.util.Arrays;
|
2023-07-29 18:37:06 +02:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
|
|
|
|
2023-08-08 17:58:27 +02:00
|
|
|
public class LayoutsPreference extends ListGroupPreference<String>
|
2023-07-29 18:37:06 +02:00
|
|
|
{
|
|
|
|
static final String KEY = "layouts";
|
|
|
|
static final List<String> DEFAULT = Collections.singletonList("system");
|
2023-08-08 17:58:27 +02:00
|
|
|
static final ListGroupPreference.Serializer<String> SERIALIZER =
|
|
|
|
new ListGroupPreference.StringSerializer();
|
2023-07-29 18:37:06 +02:00
|
|
|
|
|
|
|
/** Layout names as stored in the preferences. */
|
2023-08-06 19:54:38 +02:00
|
|
|
List<String> _layout_names;
|
2023-07-29 18:37:06 +02:00
|
|
|
/** 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();
|
2023-08-06 19:54:38 +02:00
|
|
|
_layout_names = Arrays.asList(res.getStringArray(R.array.pref_layout_values));
|
2023-07-29 18:37:06 +02:00
|
|
|
_layout_display_names = res.getStringArray(R.array.pref_layout_entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<String> load_from_preferences(SharedPreferences prefs)
|
|
|
|
{
|
2023-08-08 17:58:27 +02:00
|
|
|
return load_from_preferences(KEY, prefs, DEFAULT, SERIALIZER);
|
2023-07-29 18:37:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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)
|
|
|
|
{
|
2023-08-06 19:54:38 +02:00
|
|
|
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);
|
2023-07-29 18:37:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
2023-08-08 17:58:27 +02:00
|
|
|
@Override
|
|
|
|
Serializer<String> get_serializer() { return SERIALIZER; }
|
|
|
|
|
2023-07-29 18:37:06 +02:00
|
|
|
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)
|
|
|
|
{
|
2023-08-06 19:54:38 +02:00
|
|
|
callback.select(_layout_names.get(which));
|
2023-07-29 18:37:06 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
class LayoutsAddButton extends AddButton
|
|
|
|
{
|
|
|
|
public LayoutsAddButton(Context ctx)
|
|
|
|
{
|
|
|
|
super(ctx);
|
|
|
|
setLayoutResource(R.layout.pref_layouts_add_btn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|