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