Example usage for android.view KeyEvent ACTION_DOWN

List of usage examples for android.view KeyEvent ACTION_DOWN

Introduction

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

Prototype

int ACTION_DOWN

To view the source code for android.view KeyEvent ACTION_DOWN.

Click Source Link

Document

#getAction value: the key has been pressed down.

Usage

From source file:com.andrew.apollo.MediaButtonIntentReceiver.java

/**
 * {@inheritDoc}/* w w w .  j ava 2  s .  c  o m*/
 */
@Override
public void onReceive(final Context context, final Intent intent) {
    if (DEBUG)
        Log.v(TAG, "Received intent: " + intent);
    final String intentAction = intent.getAction();
    if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intentAction)) {
        startService(context, MusicPlaybackService.CMDPAUSE);
    } else if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return;
        }

        final int keycode = event.getKeyCode();
        final int action = event.getAction();
        final long eventtime = event.getEventTime();

        String command = null;
        switch (keycode) {
        case KeyEvent.KEYCODE_MEDIA_STOP:
            command = MusicPlaybackService.CMDSTOP;
            break;
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
            command = MusicPlaybackService.CMDTOGGLEPAUSE;
            break;
        case KeyEvent.KEYCODE_MEDIA_NEXT:
            command = MusicPlaybackService.CMDNEXT;
            break;
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
            command = MusicPlaybackService.CMDPREVIOUS;
            break;
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
            command = MusicPlaybackService.CMDPAUSE;
            break;
        case KeyEvent.KEYCODE_MEDIA_PLAY:
            command = MusicPlaybackService.CMDPLAY;
            break;
        }
        if (command != null) {
            if (action == KeyEvent.ACTION_DOWN) {
                if (mDown) {
                    if (MusicPlaybackService.CMDTOGGLEPAUSE.equals(command)
                            || MusicPlaybackService.CMDPLAY.equals(command)) {
                        if (mLastClickTime != 0 && eventtime - mLastClickTime > LONG_PRESS_DELAY) {
                            acquireWakeLockAndSendMessage(context,
                                    mHandler.obtainMessage(MSG_LONGPRESS_TIMEOUT, context), 0);
                        }
                    }
                } else if (event.getRepeatCount() == 0) {
                    // Only consider the first event in a sequence, not the repeat events,
                    // so that we don't trigger in cases where the first event went to
                    // a different app (e.g. when the user ends a phone call by
                    // long pressing the headset button)

                    // The service may or may not be running, but we need to send it
                    // a command.
                    if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                        if (eventtime - mLastClickTime >= DOUBLE_CLICK) {
                            mClickCounter = 0;
                        }

                        mClickCounter++;
                        if (DEBUG)
                            Log.v(TAG, "Got headset click, count = " + mClickCounter);
                        mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

                        Message msg = mHandler.obtainMessage(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0,
                                context);

                        long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
                        if (mClickCounter >= 3) {
                            mClickCounter = 0;
                        }
                        mLastClickTime = eventtime;
                        acquireWakeLockAndSendMessage(context, msg, delay);
                    } else {
                        startService(context, command);
                    }
                    mLaunched = false;
                    mDown = true;
                }
            } else {
                mHandler.removeMessages(MSG_LONGPRESS_TIMEOUT);
                mDown = false;
            }
            if (isOrderedBroadcast()) {
                abortBroadcast();
            }
            releaseWakeLockIfHandlerIdle();
        }
    }
}

From source file:hku.fyp14017.blencode.ui.dialogs.OverwriteRenameDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    View dialogView = LayoutInflater.from(context)
            .inflate(hku.fyp14017.blencode.R.layout.dialog_overwrite_project, null);

    replaceButton = (RadioButton) dialogView
            .findViewById(hku.fyp14017.blencode.R.id.dialog_overwrite_project_radio_replace);
    replaceButton.setOnClickListener(this);
    renameButton = (RadioButton) dialogView
            .findViewById(hku.fyp14017.blencode.R.id.dialog_overwrite_project_radio_rename);
    renameButton.setOnClickListener(this);
    projectText = (EditText) dialogView.findViewById(hku.fyp14017.blencode.R.id.dialog_overwrite_project_edit);
    projectText.setText(programName);//from   w  w w  .  j a  v  a  2s  .  c  om
    projectTextView = (TextView) dialogView
            .findViewById(hku.fyp14017.blencode.R.id.dialog_overwrite_project_edit_text);
    projectTextLine = dialogView.findViewById(hku.fyp14017.blencode.R.id.dialog_overwrite_project_edit_line);

    Dialog dialog = new AlertDialog.Builder(getActivity()).setView(dialogView)
            .setTitle(hku.fyp14017.blencode.R.string.overwrite_text)
            .setPositiveButton(hku.fyp14017.blencode.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            }).setNegativeButton(hku.fyp14017.blencode.R.string.cancel_button,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(context,
                                    hku.fyp14017.blencode.R.string.notification_download_project_cancel,
                                    Toast.LENGTH_SHORT).show();
                        }
                    })
            .create();

    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    dialog.setOnShowListener(new OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            positiveButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    handleOkButton();
                }
            });
        }
    });

    dialog.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
                boolean okButtonResult = handleOkButton();
                if (okButtonResult) {
                    dismiss();
                }
                return okButtonResult;
            } else if (keyCode == KeyEvent.KEYCODE_BACK) {
                Toast.makeText(context, hku.fyp14017.blencode.R.string.notification_download_project_cancel,
                        Toast.LENGTH_SHORT).show();
                return true;
            }

            return false;
        }
    });

    return dialog;
}

From source file:com.anxpp.blog.fragment.AboutFragment.java

@SuppressLint("SetJavaScriptEnabled")
private void initView() {
    webView = (WebView) getView().findViewById(R.id.webView);
    //      webView.setVisibility(View.GONE);
    webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            Activity activity = getActivity();
            /**/*  www.j  av a  2 s . co  m*/
             * ?Fragment???
             * I am not sure why are you getting this error, 
             * i think it should be something like NullPointerException. 
             * Try next: 
             *     Evert time you calling getActivity() on Fragment instance you should be sure,
             *     that fragment is actually have this Activity. 
             *     Because when your webview is loading you are calling this function:
             * */
            if (activity == null)
                return;
            activity.setTitle("Loading..." + progress + "%");
            activity.setProgress(progress * 100);
            if (progress == 100) {
                activity.setTitle(R.string.app_name);
                webView.setVisibility(View.VISIBLE);
            }
        }
    });
    webView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Toast("shouldOverrideUrlLoading");
            view.loadUrl(url);
            return true;
        }
    });
    //??
    WebSettings webSettings = webView.getSettings();
    //?
    //webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    //?js
    webSettings.setJavaScriptEnabled(true);
    //? ?
    webSettings.setUseWideViewPort(false); //??
    webSettings.setLoadWithOverviewMode(true); // ??
    webView.loadUrl("http://anxpp.com");
    //
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //trueWebViewfalse??
            view.loadUrl(url);
            return true; //false?
        }
    });
    webView.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) { // ?
                    webView.goBack(); //?
                    return true; //?
                }
            }
            return false;
        }
    });
}

From source file:de.qspool.clementineremote.ui.Player.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        // Control the volume of clementine if enabled in the options
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (mSharedPref.getBoolean(App.SP_KEY_USE_VOLUMEKEYS, true)) {
                Message msgDown = Message.obtain();
                msgDown.obj = new RequestVolume(App.mClementine.getVolume() - 10);
                App.mClementineConnection.mHandler.sendMessage(msgDown);
                return true;
            }/* w  w  w  .j  ava  2 s .  co  m*/
            break;
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (mSharedPref.getBoolean(App.SP_KEY_USE_VOLUMEKEYS, true)) {
                Message msgUp = Message.obtain();
                msgUp.obj = new RequestVolume(App.mClementine.getVolume() + 10);
                App.mClementineConnection.mHandler.sendMessage(msgUp);
                return true;
            }
            break;
        default:
            break;
        }
    }
    return super.onKeyDown(keyCode, event);
}

From source file:com.github.kanata3249.ffxieq.android.AugmentEditActivity.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            EditText et;//from   w w w .java  2  s .c o m
            Equipment eq;

            eq = getDAO().instantiateEquipment(mBaseID, mAugID);
            et = (EditText) findViewById(R.id.AugmentDescription);
            if (eq != null && et != null) {
                String augment;

                augment = et.getText().toString();
                if (!eq.getAugment().equals(augment)) {
                    showDialog(R.string.QueryDiscardChanges);
                    return true;
                }
            }
        }
    }

    return super.dispatchKeyEvent(event);
}

From source file:com.miz.mizuu.fragments.SearchWebMoviesFragment.java

@Override
public void onViewCreated(View v, Bundle savedInstanceState) {
    super.onViewCreated(v, savedInstanceState);

    mToolbar = (Toolbar) v.findViewById(R.id.toolbar);
    ((MizActivity) getActivity()).setSupportActionBar(mToolbar);

    mProgressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
    v.findViewById(R.id.spinner1).setVisibility(View.GONE);

    mListView = (ListView) v.findViewById(R.id.listView1);
    mListView.setOnItemClickListener(new OnItemClickListener() {
        @Override/*from ww  w . j  a  va 2 s.  co m*/
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            showMovie(arg2);
        }
    });
    mListView.setEmptyView(v.findViewById(R.id.no_results));
    v.findViewById(R.id.no_results).setVisibility(View.GONE);

    // Both the ProgressBar and ListView have been set, so let's hide the ProgressBar
    hideProgressBar();

    mSearchField = (EditText) v.findViewById(R.id.editText1);
    mSearchField.setSelection(mSearchField.length());
    mSearchField.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() > 0)
                searchForMovies();
            else {
                mSearchTask.cancel(true);
                mResults.clear();
                mAdapter.notifyDataSetChanged();
            }
        }
    });
    mSearchField.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_DOWN
                    || actionId == EditorInfo.IME_ACTION_SEARCH)
                searchForMovies();
            return true;
        }
    });
}

From source file:net.inbox.Pager.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Show first use help
    boolean show_help = false;

    // Init SharedPreferences
    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    if (!prefs.contains("initialized")) {
        PreferenceManager.setDefaultValues(this, R.xml.settings, false);
        prefs.edit().putBoolean("initialized", true).apply();

        // Initial values that don't have a preference screen
        prefs.edit().putBoolean("imap_or_pop", true).apply();
        prefs.edit().putBoolean("using_smtp", false).apply();
        prefs.edit().putBoolean("enable_pw", false).apply();
        show_help = true;/*from  w ww. j  a  va  2 s . c  o m*/
    }

    if (show_help || !prefs.getBoolean("enable_pw", false)) {
        init_db("cleartext");

        // Initial entry view
        View v = View.inflate(this, R.layout.pager, null);
        v.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
        setContentView(v);

        rv_main = (RelativeLayout) findViewById(R.id.app_main);
        rv_main.setVisibility(View.VISIBLE);
        rv_main.setAlpha(0.01f);
        rv_main.animate().alpha(1f).setListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                activity_load();
            }
        });
    } else {
        // Initial entry view
        View v = View.inflate(this, R.layout.pager, null);
        v.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
        setContentView(v);

        // Entry text edit
        llay_pw = (LinearLayout) findViewById(R.id.llay_pw);
        llay_pw.setVisibility(View.VISIBLE);
        rv_main = (RelativeLayout) findViewById(R.id.app_main);
        et_pw = (EditText) findViewById(R.id.pw);
        et_pw.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int key, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN && key == KeyEvent.KEYCODE_ENTER) {
                    init_db(et_pw.getText().toString());
                    et_pw.setText("");
                    if (unlocked) {
                        activity_load();
                        fade_in_ui();
                    } else {
                        if (++over >= 3)
                            finish();
                    }
                    return true;
                }
                return false;
            }
        });
    }

    // Helper dialog
    if (show_help) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setCancelable(true);
        builder.setTitle(getString(R.string.helper_title));
        builder.setMessage(getString(R.string.helper_msg));
        builder.setPositiveButton(getString(android.R.string.ok), null);
        builder.setNegativeButton(getString(R.string.btn_pw), new AlertDialog.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                startActivity(new Intent(getApplicationContext(), Settings.class));
                overridePendingTransition(R.anim.right_in, R.anim.right_out);
            }
        });
        builder.show();
    }
}

From source file:com.rks.musicx.services.MediaButtonReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    String intentAction = intent.getAction();
    String command = null;/*from ww  w.ja va  2  s  .  c  om*/
    if (intent.getAction() != null) {
        if (intentAction.equals(Intent.ACTION_MEDIA_BUTTON)) {
            KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);

            if (event == null) {
                return;
            }

            int keycode = event.getKeyCode();
            final int action = event.getAction();
            final long eventTime = event.getEventTime();

            switch (keycode) {
            case KeyEvent.KEYCODE_MEDIA_STOP:
                Log.d(TAG, "stop");
                command = Constants.ACTION_STOP;
                break;
            case KeyEvent.KEYCODE_HEADSETHOOK:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                Log.d(TAG, "toggle");
                command = Constants.ACTION_TOGGLE;
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                Log.d(TAG, "next");
                command = Constants.ACTION_NEXT;
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                Log.d(TAG, "prev");
                command = Constants.ACTION_PREVIOUS;
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                Log.d(TAG, "pause");
                command = Constants.ACTION_PAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                Log.d(TAG, "play");
                command = Constants.ACTION_PLAY;
                break;
            }
            // startServices(context, command);
            if (command != null) {
                if (action == KeyEvent.ACTION_DOWN) {
                    if (event.getRepeatCount() == 0) {
                        if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                            if (eventTime - mLastClickTime >= DOUBLE_CLICK) {
                                mClickCounter = 0;
                            }

                            mClickCounter++;
                            Log.e("MediaButton", "Got headset click, count = " + mClickCounter);
                            mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

                            Message msg = mHandler.obtainMessage(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT,
                                    mClickCounter, 0, context);

                            long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
                            if (mClickCounter >= 3) {
                                mClickCounter = 0;
                            }
                            mLastClickTime = eventTime;
                            acquireWakeLockAndSendMessage(context, msg, delay);
                        } else {
                            startServices(context, command);
                        }
                    }
                }
            }
        }
    }
}

From source file:com.anjalimacwan.activity.NoteEditActivity.java

@Override
public boolean dispatchKeyShortcutEvent(KeyEvent event) {
    super.dispatchKeyShortcutEvent(event);
    if (event.getAction() == KeyEvent.ACTION_DOWN && event.isCtrlPressed()) {
        NoteEditFragment fragment = (NoteEditFragment) getSupportFragmentManager()
                .findFragmentByTag("NoteEditFragment");
        fragment.dispatchKeyShortcutEvent(event.getKeyCode());

        return true;
    }/*from w w  w. j  a  va2 s .  co m*/
    return super.dispatchKeyShortcutEvent(event);
}

From source file:eu.geekgasm.kintrol.DeviceControlActivity.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) {
            kontrollerThread.increaseVolume();
        }/*from  w w  w  .jav  a2 s.  c o  m*/
        return true;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (action == KeyEvent.ACTION_DOWN) {
            kontrollerThread.decreaseVolume();
        }
        return true;
    default:
        return super.dispatchKeyEvent(event);
    }
}