1 package org.dolphinemu.dolphinemu.features.settings.model.view;
2 
3 import android.content.SharedPreferences;
4 import android.preference.PreferenceManager;
5 import android.view.InputDevice;
6 import android.view.KeyEvent;
7 
8 import org.dolphinemu.dolphinemu.DolphinApplication;
9 import org.dolphinemu.dolphinemu.features.settings.model.AbstractSetting;
10 import org.dolphinemu.dolphinemu.features.settings.model.Settings;
11 
12 public class InputBindingSetting extends SettingsItem
13 {
14   private String mFile;
15   private String mSection;
16   private String mKey;
17 
18   private String mGameId;
19 
InputBindingSetting(String file, String section, String key, int titleId, String gameId)20   public InputBindingSetting(String file, String section, String key, int titleId, String gameId)
21   {
22     super(titleId, 0);
23     mFile = file;
24     mSection = section;
25     mKey = key;
26     mGameId = gameId;
27   }
28 
getKey()29   public String getKey()
30   {
31     return mKey;
32   }
33 
getValue(Settings settings)34   public String getValue(Settings settings)
35   {
36     return settings.getSection(mFile, mSection).getString(mKey, "");
37   }
38 
39   /**
40    * Saves the provided key input setting both to the INI file (so native code can use it) and as
41    * an Android preference (so it persists correctly and is human-readable.)
42    *
43    * @param keyEvent KeyEvent of this key press.
44    */
onKeyInput(Settings settings, KeyEvent keyEvent)45   public void onKeyInput(Settings settings, KeyEvent keyEvent)
46   {
47     InputDevice device = keyEvent.getDevice();
48     String bindStr = "Device '" + device.getDescriptor() + "'-Button " + keyEvent.getKeyCode();
49     String uiString = device.getName() + ": Button " + keyEvent.getKeyCode();
50     setValue(settings, bindStr, uiString);
51   }
52 
53   /**
54    * Saves the provided motion input setting both to the INI file (so native code can use it) and as
55    * an Android preference (so it persists correctly and is human-readable.)
56    *
57    * @param device      InputDevice from which the input event originated.
58    * @param motionRange MotionRange of the movement
59    * @param axisDir     Either '-' or '+'
60    */
onMotionInput(Settings settings, InputDevice device, InputDevice.MotionRange motionRange, char axisDir)61   public void onMotionInput(Settings settings, InputDevice device,
62           InputDevice.MotionRange motionRange, char axisDir)
63   {
64     String bindStr =
65             "Device '" + device.getDescriptor() + "'-Axis " + motionRange.getAxis() + axisDir;
66     String uiString = device.getName() + ": Axis " + motionRange.getAxis() + axisDir;
67     setValue(settings, bindStr, uiString);
68   }
69 
setValue(Settings settings, String bind, String ui)70   public void setValue(Settings settings, String bind, String ui)
71   {
72     SharedPreferences
73             preferences =
74             PreferenceManager.getDefaultSharedPreferences(DolphinApplication.getAppContext());
75     SharedPreferences.Editor editor = preferences.edit();
76     editor.putString(mKey + mGameId, ui);
77     editor.apply();
78 
79     settings.getSection(mFile, mSection).setString(mKey, bind);
80   }
81 
clearValue(Settings settings)82   public void clearValue(Settings settings)
83   {
84     setValue(settings, "", "");
85   }
86 
87   @Override
getType()88   public int getType()
89   {
90     return TYPE_INPUT_BINDING;
91   }
92 
getGameId()93   public String getGameId()
94   {
95     return mGameId;
96   }
97 
98   @Override
getSetting()99   public AbstractSetting getSetting()
100   {
101     return null;
102   }
103 }
104