/actionslib/src/com/google/android/marvin/commands/preferences/CommandsPreferenceActivity.java

http://eyes-free.googlecode.com/ · Java · 205 lines · 145 code · 31 blank · 29 comment · 20 complexity · 0e435aabe25be910a02acd2d7ac3354e MD5 · raw file

  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. package com.google.android.marvin.commands.preferences;
  3. import com.google.android.marvin.actionslib.R;
  4. import com.google.android.marvin.commands.Command;
  5. import com.google.android.marvin.commands.CommandsManager;
  6. import android.os.Bundle;
  7. import android.preference.EditTextPreference;
  8. import android.preference.ListPreference;
  9. import android.preference.Preference;
  10. import android.preference.PreferenceActivity;
  11. import android.preference.PreferenceScreen;
  12. import android.preference.Preference.OnPreferenceChangeListener;
  13. import android.text.Editable;
  14. import android.text.method.BaseKeyListener;
  15. import android.text.method.MetaKeyKeyListener;
  16. import android.util.Log;
  17. import android.view.KeyCharacterMap;
  18. import android.view.KeyEvent;
  19. import android.view.View;
  20. import java.util.List;
  21. /**
  22. * The preferences page for configuring keyboard shortcuts for the commands.
  23. *
  24. * @author clsimon@google.com (Cheryl Simon)
  25. *
  26. */
  27. public class CommandsPreferenceActivity extends PreferenceActivity {
  28. private KeyCharacterMap keyCharacterMap;
  29. private List<Command> commands;
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.BUILT_IN_KEYBOARD);
  34. PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(this);
  35. screen.setTitle("Keyboard Shortcuts");
  36. setPreferenceScreen(screen);
  37. // get the possible preferences using the CommandsManager
  38. final CommandsManager commandsManager = new CommandsManager();
  39. commands = commandsManager.getAvailableCommands(this);
  40. createPreferenceScreens(commandsManager, screen);
  41. }
  42. private void createPreferenceScreens(
  43. final CommandsManager commandsManager, final PreferenceScreen parent) {
  44. final String menuString = getString(R.string.menu_key);
  45. final String searchString = getString(R.string.search_key);
  46. String[] modifierKeys = { menuString, searchString };
  47. // for each command, add a screen with two sub preferences, one for the modifier and one
  48. // for the key.
  49. for (final Command command : commands) {
  50. String modifierKeyName = getKeyName(command.getModifier());
  51. String letterKeyName = getKeyName(command.getKeyCode());
  52. final PreferenceScreen shortcutScreen =
  53. getPreferenceManager().createPreferenceScreen(this);
  54. shortcutScreen.setTitle(command.getDisplayName());
  55. shortcutScreen.setSummary(getShortcutSummary(command));
  56. // Create a list preference for the modifier that allows Menu or Search to be selected.
  57. final ListPreference list = new ListPreference(this);
  58. list.setEntries(modifierKeys);
  59. list.setEntryValues(modifierKeys);
  60. list.setTitle(R.string.modifier_key);
  61. list.setSummary(modifierKeyName);
  62. list.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  63. @Override
  64. public boolean onPreferenceChange(Preference preference, Object newValue) {
  65. // when the value changes, update the database with the new value.
  66. int newModifierKey = getKeyCode((String)newValue);
  67. command.setModifier(newModifierKey);
  68. commandsManager.updateCommandShortcut(
  69. CommandsPreferenceActivity.this, command);
  70. // update the summaries with the new value
  71. list.setSummary((String)newValue);
  72. // Note: for some reason you can't update the parent summary from here, instead
  73. // recreate the screens to force an update.
  74. parent.removeAll();
  75. createPreferenceScreens(commandsManager, parent);
  76. return true;
  77. }
  78. });
  79. shortcutScreen.addPreference(list);
  80. // add an edit text preference for the letter key.
  81. final EditTextPreference editTextPreference = new EditTextPreference(this);
  82. editTextPreference.setTitle(R.string.letter_key);
  83. editTextPreference.setSummary(letterKeyName);
  84. editTextPreference.getEditText().setKeyListener(new BaseKeyListener() {
  85. @Override
  86. public boolean onKeyDown(View view, Editable content, int keyCode, KeyEvent event) {
  87. if (keyCode == KeyEvent.KEYCODE_ALT_LEFT ||
  88. keyCode == KeyEvent.KEYCODE_ALT_RIGHT ||
  89. keyCode == KeyEvent.KEYCODE_SHIFT_LEFT ||
  90. keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT ||
  91. keyCode == KeyEvent.KEYCODE_SEARCH ||
  92. keyCode == KeyEvent.KEYCODE_MENU ||
  93. keyCode == KeyEvent.KEYCODE_ENTER||
  94. keyCode == KeyEvent.KEYCODE_DEL) {
  95. return true;
  96. }
  97. return false;
  98. }
  99. @Override
  100. public boolean onKeyUp(View view, Editable text, int keyCode, KeyEvent event) {
  101. int unicode = event.getUnicodeChar();
  102. if (unicode != 0 && unicode != 0xEF02) {
  103. String character = new String(new int[] {unicode}, 0 , 1);
  104. text.clear();
  105. text.append(character);
  106. return true;
  107. }
  108. return false;
  109. }
  110. @Override
  111. public int getInputType() {
  112. return 0;
  113. }
  114. });
  115. editTextPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
  116. @Override
  117. public boolean onPreferenceChange(Preference preference, Object newValue) {
  118. // when the value changes, save the new value to the database
  119. int newLetterKey = getKeyCode((String)newValue);
  120. command.setKeyCode(newLetterKey);
  121. commandsManager.updateCommandShortcut(
  122. CommandsPreferenceActivity.this, command);
  123. // update the summaries with the new value
  124. editTextPreference.setSummary((String)newValue);
  125. // Note: for some reason you can't update the parent summary from here, instead
  126. // recreate the screens to force an update.
  127. parent.removeAll();
  128. createPreferenceScreens(commandsManager, parent);
  129. return true;
  130. }
  131. });
  132. shortcutScreen.addPreference(editTextPreference);
  133. parent.addPreference(shortcutScreen);
  134. }
  135. }
  136. /**
  137. * The summary for the shorcut page is a combination of the modifier and letter.
  138. */
  139. protected String getShortcutSummary(Command command) {
  140. String modifierKeyName = getKeyName(command.getModifier());
  141. String letterKeyName = getKeyName(command.getKeyCode());
  142. return modifierKeyName + " + " + letterKeyName;
  143. }
  144. /**
  145. * Get a string value to display for the key.
  146. */
  147. protected String getKeyName(int key) {
  148. switch(key) {
  149. case KeyEvent.KEYCODE_SEARCH:
  150. return getString(R.string.search_key);
  151. case KeyEvent.KEYCODE_MENU:
  152. return getString(R.string.menu_key);
  153. default:
  154. int unicodeChar = new KeyEvent(KeyEvent.ACTION_DOWN, key).getUnicodeChar();
  155. return new String(new int[] { unicodeChar }, 0, 1);
  156. }
  157. }
  158. /**
  159. * Turn the entered string value back into a integer key code.
  160. */
  161. protected int getKeyCode(String name) {
  162. if (name.equals(getString(R.string.menu_key))) {
  163. return KeyEvent.KEYCODE_MENU;
  164. }
  165. if (name.equals(getString(R.string.search_key))) {
  166. return KeyEvent.KEYCODE_SEARCH;
  167. }
  168. KeyEvent[] events = keyCharacterMap.getEvents(name.toCharArray());
  169. if (events.length > 0) {
  170. return events[0].getKeyCode();
  171. }
  172. return 0;
  173. }
  174. }