Example usage for android.view KeyEvent getAction

List of usage examples for android.view KeyEvent getAction

Introduction

In this page you can find the example usage for android.view KeyEvent getAction.

Prototype

public final int getAction() 

Source Link

Document

Retrieve the action of this key event.

Usage

From source file:dev.memento.MementoBrowser.java

/** Called when the activity is first created. */
@Override//from   www  .j a  v  a  2  s. c o m
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main);

    mUserAgent = getApplicationContext().getText(R.string.user_agent).toString();

    // Set the date and time format
    SimpleDateTime.mDateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
    SimpleDateTime.mTimeFormat = android.text.format.DateFormat.getTimeFormat(getApplicationContext());

    mDateChosenButton = (Button) findViewById(R.id.dateChosen);
    mDateDisplayedView = (TextView) findViewById(R.id.dateDisplayed);

    // Launch the DatePicker dialog box
    mDateChosenButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DIALOG_DATE);
        }
    });

    // Set the current date
    mToday = new SimpleDateTime();

    // Handle change in orientation gracefully
    if (savedInstanceState == null) {
        mCurrentUrl = getApplicationContext().getText(R.string.homepage).toString();
        mOriginalUrl = mCurrentUrl;

        setChosenDate(mToday);
        setDisplayedDate(mToday);

        mMementos = new MementoList();
    } else {
        mCurrentUrl = savedInstanceState.getString("mCurrentUrl");
        mDateChosen = (SimpleDateTime) savedInstanceState.getSerializable("mDateChosen");
        mDateDisplayed = (SimpleDateTime) savedInstanceState.getSerializable("mDateDisplayed");

        setChosenDate(mDateChosen);
        setDisplayedDate(mDateDisplayed);
    }

    mTimegateUris = getResources().getStringArray(R.array.listTimegates);

    // Add some favicons of web archives used by proxy server
    mFavicons = new HashMap<String, Bitmap>();
    mFavicons.put("ia", BitmapFactory.decodeResource(getResources(), R.drawable.ia_favicon));
    mFavicons.put("webcite", BitmapFactory.decodeResource(getResources(), R.drawable.webcite_favicon));
    mFavicons.put("national-archives",
            BitmapFactory.decodeResource(getResources(), R.drawable.national_archives_favicon));

    mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
    mProgressBar.setVisibility(View.GONE);

    mLocation = (TextView) findViewById(R.id.locationEditText);
    mLocation.setSelectAllOnFocus(true);

    mLocation.setOnFocusChangeListener(new OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // Replace title with URL when focus is lost
            if (hasFocus)
                mLocation.setText(mCurrentUrl);
            else if (mPageTitle.length() > 0)
                mLocation.setText(mPageTitle);
        }
    });

    mLocation.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            //Log.d(LOG_TAG, "keyCode = " + keyCode + "   event = " + event.getAction());

            // Go to URL if user presses Go button
            if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {

                mOriginalUrl = fixUrl(mLocation.getText().toString());

                // Access live version if date is today or in the future
                if (mToday.compareTo(mDateChosen) <= 0) {
                    Log.d(LOG_TAG, "Browsing to " + mOriginalUrl);
                    mWebview.loadUrl(mOriginalUrl);

                    // Clear since we are visiting a different page in the present
                    mMementos.clear();
                } else
                    makeMementoRequests();

                // Hide the virtual keyboard
                ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                        .hideSoftInputFromWindow(mLocation.getWindowToken(), 0);
                return true;
            }

            return false;
        }

    });

    // TEST        
    /*
    Context context = getBaseContext();
    Drawable image = getImage(context, "http://web.archive.org/favicon.ico");
    if (image == null) {
       System.out.println("image is null !!");
    }
    else {
       //image.setBounds(5, 5, 5, 5);
     //ImageView imgView = new ImageView(context);
     //ImageView imgView = (ImageView)findViewById(R.id.imagetest);
     //imgView.setImageDrawable(image);
       mLocation.setCompoundDrawablesWithIntrinsicBounds(image, null, null, null);
    }
    */

    mNextButton = (Button) findViewById(R.id.next);
    mNextButton.setEnabled(false);
    mNextButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Advance to next Memento

            // This could happen if the index has not been set yet
            if (mMementos.getCurrentIndex() < 0) {
                int index = mMementos.getIndex(mDateDisplayed);
                if (index < 0) {
                    Log.d(LOG_TAG, "Could not find next Memento after date " + mDateDisplayed);
                    return;
                } else
                    mMementos.setCurrentIndex(index);
            }

            // Locate the next Memento in the list
            Memento nextMemento = mMementos.getNext();

            if (nextMemento == null) {
                Log.d(LOG_TAG, "Still could not find next Memento!");
                Log.d(LOG_TAG, "Current index is " + mMementos.getCurrentIndex());
            } else {
                SimpleDateTime date = nextMemento.getDateTime();
                setChosenDate(nextMemento.getDateTime());
                showToast("Time travelling to next Memento on " + mDateChosen.dateFormatted());

                mDateDisplayed = date;

                String redirectUrl = nextMemento.getUrl();
                Log.d(LOG_TAG, "Sending browser to " + redirectUrl);
                mWebview.loadUrl(redirectUrl);

                // Just in case it wasn't already enabled
                mPreviousButton.setEnabled(true);

                // If this is the last memento, disable button
                if (mMementos.isLast(date))
                    mNextButton.setEnabled(false);
            }
        }
    });
    mPreviousButton = (Button) findViewById(R.id.previous);
    mPreviousButton.setEnabled(false);
    mPreviousButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Advance to previous Memento

            // This could happen if the index has not been set yet
            if (mMementos.getCurrentIndex() < 0) {
                int index = mMementos.getIndex(mDateDisplayed);
                if (index < 0) {
                    Log.d(LOG_TAG, "Could not find previous Memento before date " + mDateDisplayed);
                    return;
                } else
                    mMementos.setCurrentIndex(index);
            }

            // Locate the prev Memento in the list
            Memento prevMemento = mMementos.getPrevious();

            if (prevMemento == null) {
                Log.d(LOG_TAG, "Still could not find previous Memento!");
                Log.d(LOG_TAG, "Current index is " + mMementos.getCurrentIndex());
            } else {
                SimpleDateTime date = prevMemento.getDateTime();
                setChosenDate(date);
                showToast("Time travelling to previous Memento on " + mDateChosen.dateFormatted());

                mDateDisplayed = date;

                String redirectUrl = prevMemento.getUrl();
                Log.d(LOG_TAG, "Sending browser to " + redirectUrl);
                mWebview.loadUrl(redirectUrl);

                // Just in case it wasn't already enabled
                mNextButton.setEnabled(true);

                // If this is the first memento, disable button
                if (mMementos.isFirst(date))
                    mPreviousButton.setEnabled(false);
            }
        }
    });

    mWebview = (WebView) findViewById(R.id.webview);
    mWebview.setWebViewClient(new MementoWebViewClient());
    mWebview.setWebChromeClient(new MementoWebChromClient());
    mWebview.getSettings().setJavaScriptEnabled(true);
    mWebview.loadUrl(mCurrentUrl);

    mWebview.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            // Set focus here so focus is removed from the location text field
            // which will change the URL into the page's title.
            // There really should be a better way to do this, but it's a general
            // problem that other developers have ran into as well:
            // http://groups.google.com/group/android-developers/browse_thread/thread/9d1681a01f05e782?pli=1

            if (mLocation.hasFocus()) {
                mWebview.requestFocus();
                return true;
            }

            // Hide the virtual keyboard
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(mLocation.getWindowToken(), 0);

            return false;
        }
    });

    //testMementos();
}

From source file:com.fvd.nimbus.PaintActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
    switch (keyCode) {
    case KeyEvent.KEYCODE_VOLUME_UP:
        if (action == KeyEvent.ACTION_DOWN) {
            drawView.zoom(1.25f);/*ww w .  j a v  a  2s.  c om*/
        }
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (action == KeyEvent.ACTION_DOWN) {
            drawView.zoom(0.75f);
        }
        return true;
    case KeyEvent.KEYCODE_BACK:
        if (action == KeyEvent.ACTION_DOWN) {
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
                return true;
            } else if (findViewById(R.id.text_field).getVisibility() != View.GONE) {
                findViewById(R.id.text_field).setVisibility(View.GONE);
                drawView.hideCrop();
                drawView.startEdit();
                setSelectedFoot(0);
                return true;
            } else if (findViewById(R.id.color_menu).getVisibility() != View.GONE) {
                findViewById(R.id.color_menu).setVisibility(View.GONE);
                drawView.hideCrop();
                drawView.startEdit();
                setSelectedFoot(0);
                return true;
            } else if (findViewById(R.id.draw_tools).getVisibility() != View.GONE) {
                findViewById(R.id.draw_tools).setVisibility(View.GONE);
                drawView.hideCrop();
                drawView.startEdit();
                setSelectedFoot(0);
                return false;
            } else if (!drawView.hideCrop()) {
                if (!saved && storePath.length() == 0)
                    showDialog(0);
                else {
                    drawView.recycle();
                    finish();
                    return true;
                }
            } else {
                ((ImageButton) findViewById(R.id.bToolCrop)).setSelected(false);
                drawView.startEdit();
                setSelectedFoot(0);
                return true;
            }

        }
    default:
        return super.dispatchKeyEvent(event);
    }
}

From source file:com.instiwork.RegistrationActivity.java

public void dialogTermsUse() {
    View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_terms_use, null);
    mBottomSheetDialogTermsUse = new Dialog(RegistrationActivity.this, R.style.MaterialDialogSheet);
    mBottomSheetDialogTermsUse.setContentView(view);
    mBottomSheetDialogTermsUse.setCancelable(false);
    mBottomSheetDialogTermsUse.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mBottomSheetDialogTermsUse.getWindow()
                .addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        mBottomSheetDialogTermsUse.getWindow().setStatusBarColor(Color.parseColor("#536942"));
    }/*from   ww w. j a va2 s  . c  o  m*/
    mBottomSheetDialogTermsUse.getWindow().setGravity(Gravity.BOTTOM);
    mBottomSheetDialogTermsUse.show();

    view.findViewById(R.id.back_me_dlog_payment).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            mBottomSheetDialogTermsUse.dismiss();
        }
    });

    pBarTermsUse = (ProgressBar) view.findViewById(R.id.pbar_terms_use);
    //        txtTermsUse = (RobotoLight) view.findViewById(R.id.txt_terms_use);

    webViewTermsUse = (WebView) view.findViewById(R.id.webv);
    webViewTermsUse.getSettings().setJavaScriptEnabled(true);
    webViewTermsUse.setVisibility(View.GONE);

    getTermsUse(InstiworkConstants.URL_DOMAIN + "Privacy_control");

    mBottomSheetDialogTermsUse.setOnKeyListener(new DialogInterface.OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
                if ((keyCode == KeyEvent.KEYCODE_BACK)) {
                    mBottomSheetDialogTermsUse.dismiss();
                    return true;
                }
            return false;
        }
    });
}

From source file:io.github.vomitcuddle.SearchViewAllowEmpty.SearchView.java

/**
 * React to the user typing while in the suggestions list. First, check for
 * action keys. If not handled, try refocusing regular characters into the
 * EditText.//from   w  w  w. j a  v a 2s  .  co  m
 */
private boolean onSuggestionsKey(View v, int keyCode, KeyEvent event) {
    // guard against possible race conditions (late arrival after dismiss)
    if (mSearchable == null) {
        return false;
    }
    if (mSuggestionsAdapter == null) {
        return false;
    }
    if (event.getAction() == KeyEvent.ACTION_DOWN && KeyEventCompat.hasNoModifiers(event)) {
        // First, check for enter or search (both of which we'll treat as a
        // "click")
        if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_SEARCH
                || keyCode == KeyEvent.KEYCODE_TAB) {
            int position = mQueryTextView.getListSelection();
            return onItemClicked(position, KeyEvent.KEYCODE_UNKNOWN, null);
        }

        // Next, check for left/right moves, which we use to "return" the
        // user to the edit view
        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            // give "focus" to text editor, with cursor at the beginning if
            // left key, at end if right key
            int selPoint = (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) ? 0 : mQueryTextView.length();
            mQueryTextView.setSelection(selPoint);
            mQueryTextView.setListSelection(0);
            mQueryTextView.clearListSelection();
            HIDDEN_METHOD_INVOKER.ensureImeVisible(mQueryTextView, true);

            return true;
        }

        // Next, check for an "up and out" move
        if (keyCode == KeyEvent.KEYCODE_DPAD_UP && 0 == mQueryTextView.getListSelection()) {
            // TODO: restoreUserQuery();
            // let ACTV complete the move
            return false;
        }
    }
    return false;
}

From source file:com.vuze.android.remote.fragment.TorrentListFragment.java

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (event.getAction() != KeyEvent.ACTION_UP) {
        return false;
    }// w  w w  .java  2  s  .  co  m
    switch (keyCode) {
    // NOTE:
    // KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_MENU);
    case KeyEvent.KEYCODE_MENU:
    case KeyEvent.KEYCODE_BUTTON_X:
    case KeyEvent.KEYCODE_INFO: {
        if (tb == null) {
            return showTorrentContextMenu();
        }
        break;
    }

    }
    return false;
}

From source file:android.support.v7.widget.SearchView.java

/**
 * React to the user typing while in the suggestions list. First, check for
 * action keys. If not handled, try refocusing regular characters into the
 * EditText./*from w w w. j a v  a  2  s. com*/
 */
private boolean onSuggestionsKey(View v, int keyCode, KeyEvent event) {
    // guard against possible race conditions (late arrival after dismiss)
    if (mSearchable == null) {
        return false;
    }
    if (mSuggestionsAdapter == null) {
        return false;
    }
    if (event.getAction() == KeyEvent.ACTION_DOWN && KeyEventCompat.hasNoModifiers(event)) {
        // First, check for enter or search (both of which we'll treat as a
        // "click")
        if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_SEARCH
                || keyCode == KeyEvent.KEYCODE_TAB) {
            int position = mSearchSrcTextView.getListSelection();
            return onItemClicked(position, KeyEvent.KEYCODE_UNKNOWN, null);
        }

        // Next, check for left/right moves, which we use to "return" the
        // user to the edit view
        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            // give "focus" to text editor, with cursor at the beginning if
            // left key, at end if right key
            // TODO: Reverse left/right for right-to-left languages, e.g.
            // Arabic
            int selPoint = (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) ? 0 : mSearchSrcTextView.length();
            mSearchSrcTextView.setSelection(selPoint);
            mSearchSrcTextView.setListSelection(0);
            mSearchSrcTextView.clearListSelection();
            HIDDEN_METHOD_INVOKER.ensureImeVisible(mSearchSrcTextView, true);

            return true;
        }

        // Next, check for an "up and out" move
        if (keyCode == KeyEvent.KEYCODE_DPAD_UP && 0 == mSearchSrcTextView.getListSelection()) {
            // TODO: restoreUserQuery();
            // let ACTV complete the move
            return false;
        }
    }
    return false;
}

From source file:com.chenglong.muscle.ui.LazyViewPager.java

/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy./*from   w  w  w  . j  av  a 2  s  .c  o m*/
 *
 * @param event The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
    boolean handled = false;
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            handled = arrowScroll(FOCUS_LEFT);
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            handled = arrowScroll(FOCUS_RIGHT);
            break;
        case KeyEvent.KEYCODE_TAB:
            if (Build.VERSION.SDK_INT >= 11) {
                // The focus finder had a bug handling FOCUS_FORWARD and
                // FOCUS_BACKWARD
                // before Android 3.0. Ignore the include_tab key on those
                // devices.
                if (KeyEventCompat.hasNoModifiers(event)) {
                    handled = arrowScroll(FOCUS_FORWARD);
                } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
                    handled = arrowScroll(FOCUS_BACKWARD);
                }
            }
            break;
        }
    }
    return handled;
}

From source file:cm.aptoide.com.actionbarsherlock.widget.SearchView.java

/**
 * React to the user typing while in the suggestions list. First, check for
 * action keys. If not handled, try refocusing regular characters into the
 * EditText.//from   w ww . j  a  va2 s . c om
 */
private boolean onSuggestionsKey(View v, int keyCode, KeyEvent event) {
    // guard against possible race conditions (late arrival after dismiss)
    if (mSearchable == null) {
        return false;
    }
    if (mSuggestionsAdapter == null) {
        return false;
    }
    if (event.getAction() == KeyEvent.ACTION_DOWN && KeyEventCompat.hasNoModifiers(event)) {
        // First, check for enter or search (both of which we'll treat as a
        // "click")
        if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_SEARCH
                || keyCode == KeyEvent.KEYCODE_TAB) {
            int position = mQueryTextView.getListSelection();
            return onItemClicked(position, KeyEvent.KEYCODE_UNKNOWN, null);
        }

        // Next, check for left/right moves, which we use to "return" the
        // user to the edit view
        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            // give "focus" to text editor, with cursor at the beginning if
            // left key, at end if right key
            // TODO: Reverse left/right for right-to-left languages, e.g.
            // Arabic
            int selPoint = (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) ? 0 : mQueryTextView.length();
            mQueryTextView.setSelection(selPoint);
            mQueryTextView.setListSelection(0);
            mQueryTextView.clearListSelection();
            ensureImeVisible(mQueryTextView, true);

            return true;
        }

        // Next, check for an "up and out" move
        if (keyCode == KeyEvent.KEYCODE_DPAD_UP && 0 == mQueryTextView.getListSelection()) {
            // TODO: restoreUserQuery();
            // let ACTV complete the move
            return false;
        }

        // Next, check for an "action key"
        // TODO SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode);
        // TODO if ((actionKey != null)
        // TODO         && ((actionKey.getSuggestActionMsg() != null) || (actionKey
        // TODO                 .getSuggestActionMsgColumn() != null))) {
        // TODO     // launch suggestion using action key column
        // TODO     int position = mQueryTextView.getListSelection();
        // TODO     if (position != ListView.INVALID_POSITION) {
        // TODO         Cursor c = mSuggestionsAdapter.getCursor();
        // TODO         if (c.moveToPosition(position)) {
        // TODO             final String actionMsg = getActionKeyMessage(c, actionKey);
        // TODO             if (actionMsg != null && (actionMsg.length() > 0)) {
        // TODO                 return onItemClicked(position, keyCode, actionMsg);
        // TODO             }
        // TODO         }
        // TODO     }
        // TODO }
    }
    return false;
}

From source file:com.oakesville.mythling.MediaActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN && getAppSettings().isTv()) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
            setFocusOnActionBar();//from ww w .  ja  v a2s  .  c  o m
            return true;
        }
    }
    return super.dispatchKeyEvent(event);
}