Android: Add InputBindingSetting class

Also update MotionAlertDialog to work with the new setting, and remove the
old InputBindingPreference.
This commit is contained in:
Sean Maas 2016-10-30 19:21:36 -04:00
parent e21aa1d990
commit d10b336b0a
7 changed files with 166 additions and 75 deletions

View File

@ -3,13 +3,14 @@ package org.dolphinemu.dolphinemu.dialogs;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
import org.dolphinemu.dolphinemu.NativeLibrary;
import org.dolphinemu.dolphinemu.model.settings.StringSetting;
import org.dolphinemu.dolphinemu.model.settings.view.InputBindingSetting;
import org.dolphinemu.dolphinemu.utils.Log;
import java.util.ArrayList;
@ -22,7 +23,7 @@ import java.util.List;
public final class MotionAlertDialog extends AlertDialog
{
// The selected input preference
private final Preference inputPref;
private final InputBindingSetting setting;
private boolean firstEvent = true;
private final ArrayList<Float> m_values = new ArrayList<>();
@ -30,14 +31,14 @@ public final class MotionAlertDialog extends AlertDialog
/**
* Constructor
*
* @param ctx The current {@link Context}.
* @param inputPref The Preference to show this dialog for.
* @param context The current {@link Context}.
* @param setting The Preference to show this dialog for.
*/
public MotionAlertDialog(Context ctx, Preference inputPref)
public MotionAlertDialog(Context context, InputBindingSetting setting)
{
super(ctx);
super(context);
this.inputPref = inputPref;
this.setting = setting;
}
@Override
@ -150,7 +151,7 @@ public final class MotionAlertDialog extends AlertDialog
if (bindStr != null)
{
NativeLibrary.SetConfig("Dolphin.ini", "Android", inputPref.getKey(), bindStr);
setting.setValue(bindStr);
}
else
{
@ -163,10 +164,8 @@ public final class MotionAlertDialog extends AlertDialog
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
SharedPreferences.Editor editor = preferences.edit();
editor.putString(inputPref.getKey(), uiString);
editor.putString(setting.getKey(), uiString);
editor.apply();
inputPref.setSummary(uiString);
}
else
{

View File

@ -0,0 +1,53 @@
package org.dolphinemu.dolphinemu.model.settings.view;
import org.dolphinemu.dolphinemu.model.settings.BooleanSetting;
import org.dolphinemu.dolphinemu.model.settings.Setting;
import org.dolphinemu.dolphinemu.model.settings.StringSetting;
public final class InputBindingSetting extends SettingsItem
{
public InputBindingSetting(String key, String section, int file, int titleId, Setting setting)
{
super(key, section, file, setting, titleId, 0);
}
public String getValue()
{
if (getSetting() == null)
{
return "";
}
StringSetting setting = (StringSetting) getSetting();
return setting.getValue();
}
/**
* Write a value to the backing string. If that string was previously null,
* initializes a new one and returns it, so it can be added to the Hashmap.
*
* @param bind The input that will be bound
* @return null if overwritten successfully; otherwise, a newly created StringSetting.
*/
public StringSetting setValue(String bind)
{
if (getSetting() == null)
{
StringSetting setting = new StringSetting(getKey(), getSection(), getFile(), bind);
setSetting(setting);
return setting;
}
else
{
StringSetting setting = (StringSetting) getSetting();
setting.setValue(bind);
return null;
}
}
@Override
public int getType()
{
return TYPE_INPUT_BINDING;
}
}

View File

@ -16,6 +16,7 @@ public abstract class SettingsItem
public static final int TYPE_SINGLE_CHOICE = 2;
public static final int TYPE_SLIDER = 3;
public static final int TYPE_SUBMENU = 4;
public static final int TYPE_INPUT_BINDING = 5;
private String mKey;
private String mSection;

View File

@ -2,6 +2,8 @@ package org.dolphinemu.dolphinemu.ui.settings;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
@ -11,17 +13,20 @@ import android.widget.SeekBar;
import android.widget.TextView;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.dialogs.MotionAlertDialog;
import org.dolphinemu.dolphinemu.model.settings.BooleanSetting;
import org.dolphinemu.dolphinemu.model.settings.FloatSetting;
import org.dolphinemu.dolphinemu.model.settings.IntSetting;
import org.dolphinemu.dolphinemu.model.settings.StringSetting;
import org.dolphinemu.dolphinemu.model.settings.view.CheckBoxSetting;
import org.dolphinemu.dolphinemu.model.settings.view.InputBindingSetting;
import org.dolphinemu.dolphinemu.model.settings.view.SettingsItem;
import org.dolphinemu.dolphinemu.model.settings.view.SingleChoiceSetting;
import org.dolphinemu.dolphinemu.model.settings.view.SliderSetting;
import org.dolphinemu.dolphinemu.model.settings.view.SubmenuSetting;
import org.dolphinemu.dolphinemu.ui.settings.viewholder.CheckBoxSettingViewHolder;
import org.dolphinemu.dolphinemu.ui.settings.viewholder.HeaderViewHolder;
import org.dolphinemu.dolphinemu.ui.settings.viewholder.InputBindingSettingViewHolder;
import org.dolphinemu.dolphinemu.ui.settings.viewholder.SettingViewHolder;
import org.dolphinemu.dolphinemu.ui.settings.viewholder.SingleChoiceViewHolder;
import org.dolphinemu.dolphinemu.ui.settings.viewholder.SliderViewHolder;
@ -78,6 +83,10 @@ public final class SettingsAdapter extends RecyclerView.Adapter<SettingViewHolde
view = inflater.inflate(R.layout.list_item_setting, parent, false);
return new SubmenuViewHolder(view, this);
case SettingsItem.TYPE_INPUT_BINDING:
view = inflater.inflate(R.layout.list_item_setting, parent, false);
return new InputBindingSettingViewHolder(view, this, mContext);
default:
Log.error("[SettingsAdapter] Invalid view type: " + viewType);
return null;
@ -186,6 +195,45 @@ public final class SettingsAdapter extends RecyclerView.Adapter<SettingViewHolde
mView.loadSubMenu(item.getMenuKey());
}
public void onInputBindingClick(final InputBindingSetting item, final int position)
{
final MotionAlertDialog dialog = new MotionAlertDialog(mContext, item);
dialog.setTitle(R.string.input_binding);
dialog.setMessage(String.format(mContext.getString(R.string.input_binding_descrip), mContext.getString(item.getNameId())));
dialog.setButton(AlertDialog.BUTTON_NEGATIVE, mContext.getString(R.string.cancel), this);
dialog.setButton(AlertDialog.BUTTON_NEUTRAL, mContext.getString(R.string.clear), new AlertDialog.OnClickListener()
{
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
item.setValue("");
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(item.getKey(), "");
editor.apply();
}
});
dialog.setOnDismissListener(new AlertDialog.OnDismissListener()
{
@Override
public void onDismiss(DialogInterface dialog)
{
StringSetting setting = new StringSetting(item.getKey(), item.getSection(), item.getFile(), item.getValue());
notifyItemChanged(position);
if (setting != null)
{
mView.putSetting(setting);
}
mView.onSettingChanged();
}
});
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
@Override
public void onClick(DialogInterface dialog, int which)
{

View File

@ -0,0 +1,53 @@
package org.dolphinemu.dolphinemu.ui.settings.viewholder;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.TextView;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.model.settings.view.InputBindingSetting;
import org.dolphinemu.dolphinemu.model.settings.view.SettingsItem;
import org.dolphinemu.dolphinemu.ui.settings.SettingsAdapter;
public final class InputBindingSettingViewHolder extends SettingViewHolder
{
private InputBindingSetting mItem;
private TextView mTextSettingName;
private TextView mTextSettingDescription;
private Context mContext;
public InputBindingSettingViewHolder(View itemView, SettingsAdapter adapter, Context context)
{
super(itemView, adapter);
mContext = context;
}
@Override
protected void findViews(View root)
{
mTextSettingName = (TextView) root.findViewById(R.id.text_setting_name);
mTextSettingDescription = (TextView) root.findViewById(R.id.text_setting_description);
}
@Override
public void bind(SettingsItem item)
{
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
mItem = (InputBindingSetting) item;
mTextSettingName.setText(item.getNameId());
mTextSettingDescription.setText(sharedPreferences.getString(mItem.getKey(), ""));
}
@Override
public void onClick(View clicked)
{
getAdapter().onInputBindingClick(mItem, getAdapterPosition());
}
}

View File

@ -1,64 +0,0 @@
package org.dolphinemu.dolphinemu.utils;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.util.AttributeSet;
import org.dolphinemu.dolphinemu.R;
import org.dolphinemu.dolphinemu.dialogs.MotionAlertDialog;
/**
* {@link Preference} subclass that represents a preference
* used for assigning a key bind.
*/
public final class InputBindingPreference extends EditTextPreference
{
/**
* Constructor that is called when inflating an InputBindingPreference from XML.
*
* @param context The current {@link Context}.
* @param attrs The attributes of the XML tag that is inflating the preference.
*/
public InputBindingPreference(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
protected void onClick()
{
// Begin the creation of the input alert.
final MotionAlertDialog dialog = new MotionAlertDialog(getContext(), this);
// Set the cancel button.
dialog.setButton(AlertDialog.BUTTON_NEGATIVE, getContext().getString(R.string.cancel), new AlertDialog.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
// Do nothing. Just makes the cancel button show up.
}
});
// Set the title and description message.
dialog.setTitle(R.string.input_binding);
dialog.setMessage(String.format(getContext().getString(R.string.input_binding_descrip), getTitle()));
// Don't allow the dialog to close when a user taps
// outside of it. They must press cancel or provide an input.
dialog.setCanceledOnTouchOutside(false);
// Everything is set, show the dialog.
dialog.show();
}
@Override
public CharSequence getSummary()
{
String summary = super.getSummary().toString();
return String.format(summary, getText());
}
}

View File

@ -232,6 +232,7 @@
<string name="no">No</string>
<string name="ok">OK</string>
<string name="cancel">Cancel</string>
<string name="clear">Clear</string>
<string name="disabled">Disabled</string>
<string name="other">Other</string>