@Override public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) { final String keyAction = getKeyAction(); if (keyAction == null) return false; final KeyboardShortcutSpec spec = KeyboardShortcutsHandler.getKeyboardShortcutSpec(getContextTag(), keyCode, event); if (spec == null || !spec.isValid()) { return super.onKeyUp(keyCode, event); } mKeySpec = spec; mKeysLabel.setText(spec.toKeyString()); final String oldAction = mKeyboardShortcutHandler.findAction(spec); final KeyboardShortcutSpec copyOfSpec = spec.copy(); copyOfSpec.setContextTag(null); final String oldGeneralAction = mKeyboardShortcutHandler.findAction(copyOfSpec); if (!TextUtils.isEmpty(oldAction) && !keyAction.equals(oldAction)) { // Conflicts with keys in same context tag mConflictLabel.setVisibility(View.VISIBLE); final String label = KeyboardShortcutsHandler.getActionLabel(this, oldAction); mConflictLabel.setText(getString(R.string.conflicts_with_name, label)); mButtonPositive.setText((R.string.overwrite)); } else if (!TextUtils.isEmpty(oldGeneralAction) && !keyAction.equals(oldGeneralAction)) { // Conflicts with keys in root context mConflictLabel.setVisibility(View.VISIBLE); final String label = KeyboardShortcutsHandler.getActionLabel(this, oldGeneralAction); mConflictLabel.setText(getString(R.string.conflicts_with_name, label)); mButtonPositive.setText((R.string.overwrite)); } else { mConflictLabel.setVisibility(View.GONE); mButtonPositive.setText((android.R.string.ok)); } return true; }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mKeyboardShortcutHandler = TwidereApplication.getInstance(this).getKeyboardShortcutsHandler(); setContentView(R.layout.activity_keyboard_shortcut_input); setTitle(KeyboardShortcutsHandler.getActionLabel(this, getKeyAction())); mButtonPositive.setOnClickListener(this); mButtonNegative.setOnClickListener(this); mButtonNeutral.setOnClickListener(this); }
@Override public void onClick(View v) { switch (v.getId()) { case R.id.button_positive: { if (mKeySpec == null) return; mKeyboardShortcutHandler.register(mKeySpec, getKeyAction()); finish(); break; } case R.id.button_neutral: { mKeyboardShortcutHandler.unregister(getKeyAction()); finish(); break; } case R.id.button_negative: { finish(); break; } } }
@Override public boolean handleKeyboardShortcutSingle( @NonNull KeyboardShortcutsHandler handler, int keyCode, @NonNull KeyEvent event, int metaState) { final String action = handler.getKeyAction(CONTEXT_TAG_NAVIGATION, keyCode, event, metaState); if (ACTION_NAVIGATION_BACK.equals(action) && mSearchQuery.length() == 0) { if (!mTextChanged) { onBackPressed(); } else { mTextChanged = false; } return true; } return super.handleKeyboardShortcutSingle(handler, keyCode, event, metaState); }
public KeyboardShortcutPreferenceCompat( final Context context, final KeyboardShortcutsHandler handler, @Nullable final String contextTag, @NonNull final String action) { super(context); mKeyboardShortcutHandler = handler; mContextTag = contextTag; mAction = action; setPersistent(false); setTitle(KeyboardShortcutsHandler.getActionLabel(context, action)); mPreferencesChangeListener = new OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { updateSummary(); } }; updateSummary(); }
@Override protected void onPrepareForRemoval() { mKeyboardShortcutHandler.unregisterOnSharedPreferenceChangeListener( mPreferencesChangeListener); super.onPrepareForRemoval(); }
@Override public void onAttached() { super.onAttached(); mKeyboardShortcutHandler.registerOnSharedPreferenceChangeListener(mPreferencesChangeListener); }
private void updateSummary() { final KeyboardShortcutSpec spec = mKeyboardShortcutHandler.findKey(mAction); setSummary(spec != null ? spec.toKeyString() : null); }