Example usage for android.view KeyEvent KeyEvent

List of usage examples for android.view KeyEvent KeyEvent

Introduction

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

Prototype

private KeyEvent(KeyEvent origEvent, int action) 

Source Link

Document

Copy an existing key event, modifying its action.

Usage

From source file:github.daneren2005.dsub.util.Util.java

private static void setupViews(RemoteViews rv, Context context, MusicDirectory.Entry song, boolean playing) {

    // Use the same text for the ticker and the expanded notification
    String title = song.getTitle();
    String arist = song.getArtist();
    String album = song.getAlbum();

    // Set the album art.
    try {/*w w w  .  ja  va  2s .c om*/
        int size = context.getResources().getDrawable(R.drawable.unknown_album).getIntrinsicHeight();
        Bitmap bitmap = FileUtil.getAlbumArtBitmap(context, song, size);
        if (bitmap == null) {
            // set default album art
            rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
        } else {
            rv.setImageViewBitmap(R.id.notification_image, bitmap);
        }
    } catch (Exception x) {
        Log.w(TAG, "Failed to get notification cover art", x);
        rv.setImageViewResource(R.id.notification_image, R.drawable.unknown_album);
    }

    // set the text for the notifications
    rv.setTextViewText(R.id.notification_title, title);
    rv.setTextViewText(R.id.notification_artist, arist);
    rv.setTextViewText(R.id.notification_album, album);

    Pair<Integer, Integer> colors = getNotificationTextColors(context);
    if (colors.getFirst() != null) {
        rv.setTextColor(R.id.notification_title, colors.getFirst());
    }
    if (colors.getSecond() != null) {
        rv.setTextColor(R.id.notification_artist, colors.getSecond());
    }

    if (!playing) {
        rv.setImageViewResource(R.id.control_pause, R.drawable.notification_play);
        rv.setImageViewResource(R.id.control_previous, R.drawable.notification_stop);
    }

    // Create actions for media buttons
    PendingIntent pendingIntent;
    if (playing) {
        Intent prevIntent = new Intent("KEYCODE_MEDIA_PREVIOUS");
        prevIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
        prevIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
        pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
        rv.setOnClickPendingIntent(R.id.control_previous, pendingIntent);
    } else {
        Intent prevIntent = new Intent("KEYCODE_MEDIA_STOP");
        prevIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
        prevIntent.putExtra(Intent.EXTRA_KEY_EVENT,
                new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_STOP));
        pendingIntent = PendingIntent.getService(context, 0, prevIntent, 0);
        rv.setOnClickPendingIntent(R.id.control_previous, pendingIntent);
    }

    Intent pauseIntent = new Intent("KEYCODE_MEDIA_PLAY_PAUSE");
    pauseIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    pauseIntent.putExtra(Intent.EXTRA_KEY_EVENT,
            new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
    pendingIntent = PendingIntent.getService(context, 0, pauseIntent, 0);
    rv.setOnClickPendingIntent(R.id.control_pause, pendingIntent);

    Intent nextIntent = new Intent("KEYCODE_MEDIA_NEXT");
    nextIntent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    nextIntent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
    pendingIntent = PendingIntent.getService(context, 0, nextIntent, 0);
    rv.setOnClickPendingIntent(R.id.control_next, pendingIntent);
}

From source file:jackpal.androidterm.Term.java

private boolean doSendActionBarKey(EmulatorView view, int key) {
    if (key == 999) {
        // do nothing
    } else if (key == 1002) {

        doToggleSoftKeyboard();/*from  www.  ja  v  a2  s . c  o m*/
    } else if (key == 1249) {
        doPaste();
    } else if (key == 1250) {
        doCreateNewWindow();
    } else if (key == 1251) {
        if (mVimApp && mSettings.getInitialCommand().matches("(.|\n)*(^|\n)-vim\\.app(.|\n)*")
                && mTermSessions.size() == 1) {
            sendKeyStrings(":confirm qa\r", true);
        } else {
            confirmCloseWindow();
        }
    } else if (key == 1252) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showInputMethodPicker();
    } else if (key == 1253) {
        sendKeyStrings(":confirm qa\r", true);
    } else if (key == 1254) {
        view.sendFnKeyCode();
    } else if (key == KeycodeConstants.KEYCODE_ALT_LEFT) {
        view.sendAltKeyCode();
    } else if (key == KeycodeConstants.KEYCODE_CTRL_LEFT) {
        view.sendControlKeyCode();
    } else if (key == 1247) {
        sendKeyStrings(":", false);
    } else if (key == 1255) {
        setFunctionBar(2);
    } else if (key == 1260) {
        view.doImeShortcutsAction();
    } else if (key >= 1351 && key <= 1353) {
        view.doImeShortcutsAction(key - 1300);
    } else if (key > 0) {
        KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, key);
        dispatchKeyEvent(event);
        event = new KeyEvent(KeyEvent.ACTION_UP, key);
        dispatchKeyEvent(event);
    }
    return true;
}

From source file:com.undatech.opaque.RemoteCanvasActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    int itemID = menuItem.getItemId();
    switch (itemID) {
    case R.id.menuExtraKeys:
        if (connection.getExtraKeysToggleType() == Constants.EXTRA_KEYS_ON) {
            connection.setExtraKeysToggleType(Constants.EXTRA_KEYS_OFF);
            menuItem.setTitle(R.string.extra_keys_enable);
            setExtraKeysVisibility(View.GONE, false);
        } else {/* www . j a  v a2  s .  com*/
            connection.setExtraKeysToggleType(Constants.EXTRA_KEYS_ON);
            menuItem.setTitle(R.string.extra_keys_disable);
            setExtraKeysVisibility(View.VISIBLE, false);
            extraKeysHidden = false;
        }
        setKeyStowDrawableAndVisibility();
        connection.saveToSharedPreferences(getApplicationContext());
        return true;
    case R.id.menuDisconnect:
        canvas.disconnectAndCleanUp();
        finish();
        return true;
    case R.id.menuSendCAD:
        canvas.getKeyboard().keyEvent(112, new KeyEvent(KeyEvent.ACTION_DOWN, 112),
                RemoteKeyboard.CTRL_ON_MASK | RemoteKeyboard.ALT_ON_MASK);
        canvas.getKeyboard().keyEvent(112, new KeyEvent(KeyEvent.ACTION_UP, 112));
        return true;
    case R.id.menuHelpInputMethod:
        showDialog(R.id.menuHelpInputMethod);
        return true;
    default:
        InputHandler newInputHandler = inputHandlerIdMap.get(menuItem.getItemId());
        if (newInputHandler != null) {
            inputHandler = newInputHandler;
            connection.setInputMethod(newInputHandler.getId());
            menuItem.setChecked(true);
            displayInputModeInfo(true);
            connection.saveToSharedPreferences(getApplicationContext());
            return true;
        }
    }
    return super.onOptionsItemSelected(menuItem);
}

From source file:com.strathclyde.highlightingkeyboard.SoftKeyboardService.java

/**
 * Helper to send a key down / key up pair to the current editor.
 *///from   www . j  av a  2 s  .co  m
private void keyDownUp(int keyEventCode) {
    ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyEventCode));
    ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyEventCode));
}

From source file:com.thejoshwa.ultrasonic.androidapp.util.Util.java

public static void linkButtons(Context context, RemoteViews views, boolean playerActive) {

    Intent intent = new Intent(context, playerActive ? DownloadActivity.class : MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
    views.setOnClickPendingIntent(R.id.appwidget_coverart, pendingIntent);
    views.setOnClickPendingIntent(R.id.appwidget_top, pendingIntent);

    // Emulate media button clicks.
    intent = new Intent("1");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Intent.EXTRA_KEY_EVENT,
            new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
    pendingIntent = PendingIntent.getService(context, 0, intent, 0);
    views.setOnClickPendingIntent(R.id.control_play, pendingIntent);

    intent = new Intent("2");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
    pendingIntent = PendingIntent.getService(context, 0, intent, 0);
    views.setOnClickPendingIntent(R.id.control_next, pendingIntent);

    intent = new Intent("3");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Intent.EXTRA_KEY_EVENT,
            new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
    pendingIntent = PendingIntent.getService(context, 0, intent, 0);
    views.setOnClickPendingIntent(R.id.control_previous, pendingIntent);

    intent = new Intent("4");
    intent.setComponent(new ComponentName(context, DownloadServiceImpl.class));
    intent.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_STOP));
    pendingIntent = PendingIntent.getService(context, 0, intent, 0);
    views.setOnClickPendingIntent(R.id.control_stop, pendingIntent);
}

From source file:com.yangtsaosoftware.pebblemessenger.services.PebbleCenter.java

private void answerCall(boolean isSpeakon) {

    Context context = getApplicationContext();
    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    // ??//from  w  ww.  ja v  a2  s. c  o  m
    if (!(audioManager.isWiredHeadsetOn() || audioManager.isBluetoothA2dpOn())) {
        // 4.1??? 4.1??Permission Denial: not allowed to
        // send broadcast android.intent.action.HEADSET_PLUG from pid=1324,
        // uid=10017
        // ??????android.permission.CALL_PRIVLEGED?????4.1??????????NULL??

        Constants.log("speakerset", String.format("AudioManager before mode:%d speaker mod:%s",
                audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));
        Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);
        meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
        context.sendOrderedBroadcast(meidaButtonIntent, "android.permission.CALL_PRIVILEGED");

        /*TelephonyManager telMag = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        Class<TelephonyManager> c = TelephonyManager.class;
        Method mthAnswerCall;
        try {
        mthAnswerCall = c.getDeclaredMethod("getITelephony", (Class[]) null);
        mthAnswerCall.setAccessible(true);
        ITelephony iTel = (ITelephony) mthAnswerCall.invoke(telMag, (Object[]) null);
        iTel.answerRingingCall();
                
        } catch (Exception e) {
        e.printStackTrace();
        }*/
        if (isSpeakon) {
            try {
                Thread.sleep(500);

            } catch (InterruptedException e) {
                Constants.log("speakerset", "Problem while sleeping");
            }
            Constants.log("speakerset", "AudioManager answer mode:" + audioManager.getMode() + " speaker mod:"
                    + String.valueOf(audioManager.isSpeakerphoneOn()));
            while (audioManager.getMode() != AudioManager.MODE_IN_CALL) {
                try {
                    Thread.sleep(300);

                } catch (InterruptedException e) {
                    Constants.log("speakerset", "Problem while sleeping");
                }
            }
            // audioManager.setMicrophoneMute(true);
            audioManager.setSpeakerphoneOn(true);
            // audioManager.setMode(AudioManager.MODE_IN_CALL);
            Constants.log("speakerset", String.format("AudioManager set mode:%d speaker mod:%s",
                    audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));

        }
    } else {
        Constants.log("speakerset", String.format("AudioManager before mode:%d speaker mod:%s",
                audioManager.getMode(), String.valueOf(audioManager.isSpeakerphoneOn())));

        Intent meidaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK);
        meidaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
        context.sendOrderedBroadcast(meidaButtonIntent, "android.permission.CALL_PRIVILEGED");

    }
}

From source file:com.andrew.apolloMod.service.ApolloService.java

/** Return notification remote views
 * /*w ww.j a  v a2s  .c o  m*/
 * @return [views, bigViews]
 */
public RemoteViews[] getNotificationViews() {
    Bitmap b = getAlbumBitmap();

    RemoteViews bigViews = new RemoteViews(getPackageName(), R.layout.status_bar_expanded);
    RemoteViews views = new RemoteViews(getPackageName(), R.layout.status_bar);

    if (b != null) {
        bigViews.setImageViewBitmap(R.id.status_bar_album_art, b);
        views.setViewVisibility(R.id.status_bar_icon, View.GONE);
        views.setViewVisibility(R.id.status_bar_album_art, View.VISIBLE);
        views.setImageViewBitmap(R.id.status_bar_album_art, b);
    } else {
        views.setViewVisibility(R.id.status_bar_icon, View.VISIBLE);
        views.setViewVisibility(R.id.status_bar_album_art, View.GONE);
    }

    ComponentName rec = new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName());
    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.putExtra(CMDNOTIF, 1);
    mediaButtonIntent.setComponent(rec);
    KeyEvent mediaKey = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
    mediaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, mediaKey);
    PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, mediaButtonIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    views.setOnClickPendingIntent(R.id.status_bar_play, mediaPendingIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_play, mediaPendingIntent);

    mediaButtonIntent.putExtra(CMDNOTIF, 2);
    mediaKey = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT);
    mediaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, mediaKey);
    mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 2, mediaButtonIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    views.setOnClickPendingIntent(R.id.status_bar_next, mediaPendingIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_next, mediaPendingIntent);

    mediaButtonIntent.putExtra(CMDNOTIF, 4);
    mediaKey = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
    mediaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, mediaKey);
    mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 4, mediaButtonIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    bigViews.setOnClickPendingIntent(R.id.status_bar_prev, mediaPendingIntent);

    mediaButtonIntent.putExtra(CMDNOTIF, 3);
    mediaKey = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_STOP);
    mediaButtonIntent.putExtra(Intent.EXTRA_KEY_EVENT, mediaKey);
    mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 3, mediaButtonIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    views.setOnClickPendingIntent(R.id.status_bar_collapse, mediaPendingIntent);
    bigViews.setOnClickPendingIntent(R.id.status_bar_collapse, mediaPendingIntent);

    views.setImageViewResource(R.id.status_bar_play, R.drawable.apollo_holo_dark_pause);
    bigViews.setImageViewResource(R.id.status_bar_play, R.drawable.apollo_holo_dark_pause);

    views.setTextViewText(R.id.status_bar_track_name, getTrackName());
    bigViews.setTextViewText(R.id.status_bar_track_name, getTrackName());

    views.setTextViewText(R.id.status_bar_artist_name, getArtistName());
    bigViews.setTextViewText(R.id.status_bar_artist_name, getArtistName());

    bigViews.setTextViewText(R.id.status_bar_album_name, getAlbumName());

    return new RemoteViews[] { views, bigViews };

}

From source file:com.fishstix.dosboxfree.DBGLSurfaceView.java

@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
    return new BaseInputConnection(this, false) {
        @Override/*ww  w  .  j  a v  a  2  s .c o  m*/
        public boolean sendKeyEvent(KeyEvent event) {
            return super.sendKeyEvent(event);
        }

        @Override
        public boolean deleteSurroundingText(int beforeLength, int afterLength) {
            // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace
            if (beforeLength == 1 && afterLength == 0) {
                // backspace
                super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
                return super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
            }

            return super.deleteSurroundingText(beforeLength, afterLength);
        }
    };
}

From source file:com.android.tv.MainActivity.java

private boolean handleIntent(Intent intent) {
    // Reset the closed caption settings when the activity is 1)created or 2) restarted.
    // And do not reset while TvView is playing.
    if (!mTvView.isPlaying()) {
        mCaptionSettings = new CaptionSettings(this);
    }/*from   ww  w .jav a 2  s . c om*/

    // Handle the passed key press, if any. Note that only the key codes that are currently
    // handled in the TV app will be handled via Intent.
    // TODO: Consider defining a separate intent filter as passing data of mime type
    // vnd.android.cursor.item/channel isn't really necessary here.
    int keyCode = intent.getIntExtra(Utils.EXTRA_KEY_KEYCODE, KeyEvent.KEYCODE_UNKNOWN);
    if (keyCode != KeyEvent.KEYCODE_UNKNOWN) {
        if (DEBUG)
            Log.d(TAG, "Got an intent with keycode: " + keyCode);
        KeyEvent event = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
        onKeyUp(keyCode, event);
        return true;
    }
    mShouldTuneToTunerChannel = intent.getBooleanExtra(Utils.EXTRA_KEY_FROM_LAUNCHER, false);
    mInitChannelUri = null;

    String extraAction = intent.getStringExtra(Utils.EXTRA_KEY_ACTION);
    if (!TextUtils.isEmpty(extraAction)) {
        if (DEBUG)
            Log.d(TAG, "Got an extra action: " + extraAction);
        if (Utils.EXTRA_ACTION_SHOW_TV_INPUT.equals(extraAction)) {
            String lastWatchedChannelUri = Utils.getLastWatchedChannelUri(this);
            if (lastWatchedChannelUri != null) {
                mInitChannelUri = Uri.parse(lastWatchedChannelUri);
            }
            mShowSelectInputView = true;
        }
    }

    if (CommonFeatures.DVR.isEnabled(this) && BuildCompat.isAtLeastN()) {
        mRecordingUri = intent.getParcelableExtra(Utils.EXTRA_KEY_RECORDING_URI);
        if (mRecordingUri != null) {
            return true;
        }
    }

    // TODO: remove the checkState once N API is finalized.
    SoftPreconditions
            .checkState(TvInputManager.ACTION_SETUP_INPUTS.equals("android.media.tv.action.SETUP_INPUTS"));
    if (TvInputManager.ACTION_SETUP_INPUTS.equals(intent.getAction())) {
        runAfterAttachedToWindow(new Runnable() {
            @Override
            public void run() {
                mOverlayManager.showSetupFragment();
            }
        });
    } else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
        Uri uri = intent.getData();
        try {
            mSource = uri.getQueryParameter(Utils.PARAM_SOURCE);
        } catch (UnsupportedOperationException e) {
            // ignore this exception.
        }
        // When the URI points to the programs (directory, not an individual item), go to the
        // program guide. The intention here is to respond to
        // "content://android.media.tv/program", not "content://android.media.tv/program/XXX".
        // Later, we might want to add handling of individual programs too.
        if (Utils.isProgramsUri(uri)) {
            // The given data is a programs URI. Open the Program Guide.
            mShowProgramGuide = true;
            return true;
        }
        // In case the channel is given explicitly, use it.
        mInitChannelUri = uri;
        if (DEBUG)
            Log.d(TAG, "ACTION_VIEW with " + mInitChannelUri);
        if (Channels.CONTENT_URI.equals(mInitChannelUri)) {
            // Tune to default channel.
            mInitChannelUri = null;
            mShouldTuneToTunerChannel = true;
            return true;
        }
        if ((!Utils.isChannelUriForOneChannel(mInitChannelUri)
                && !Utils.isChannelUriForInput(mInitChannelUri))) {
            Log.w(TAG, "Malformed channel uri " + mInitChannelUri + " tuning to default instead");
            mInitChannelUri = null;
            return true;
        }
        mTuneParams = intent.getExtras();
        if (mTuneParams == null) {
            mTuneParams = new Bundle();
        }
        if (Utils.isChannelUriForTunerInput(mInitChannelUri)) {
            long channelId = ContentUris.parseId(mInitChannelUri);
            mTuneParams.putLong(KEY_INIT_CHANNEL_ID, channelId);
        } else if (TvContract.isChannelUriForPassthroughInput(mInitChannelUri)) {
            // If mInitChannelUri is for a passthrough TV input.
            String inputId = mInitChannelUri.getPathSegments().get(1);
            TvInputInfo input = mTvInputManagerHelper.getTvInputInfo(inputId);
            if (input == null) {
                mInitChannelUri = null;
                Toast.makeText(this, R.string.msg_no_specific_input, Toast.LENGTH_SHORT).show();
                return false;
            } else if (!input.isPassthroughInput()) {
                mInitChannelUri = null;
                Toast.makeText(this, R.string.msg_not_passthrough_input, Toast.LENGTH_SHORT).show();
                return false;
            }
        } else if (mInitChannelUri != null) {
            // Handle the URI built by TvContract.buildChannelsUriForInput().
            // TODO: Change hard-coded "input" to TvContract.PARAM_INPUT.
            String inputId = mInitChannelUri.getQueryParameter("input");
            long channelId = Utils.getLastWatchedChannelIdForInput(this, inputId);
            if (channelId == Channel.INVALID_ID) {
                String[] projection = { Channels._ID };
                try (Cursor cursor = getContentResolver().query(uri, projection, null, null, null)) {
                    if (cursor != null && cursor.moveToNext()) {
                        channelId = cursor.getLong(0);
                    }
                }
            }
            if (channelId == Channel.INVALID_ID) {
                // Couldn't find any channel probably because the input hasn't been set up.
                // Try to set it up.
                mInitChannelUri = null;
                mInputToSetUp = mTvInputManagerHelper.getTvInputInfo(inputId);
            } else {
                mInitChannelUri = TvContract.buildChannelUri(channelId);
                mTuneParams.putLong(KEY_INIT_CHANNEL_ID, channelId);
            }
        }
    }
    return true;
}

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

private void sendTab() {
    InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;//www  .j  a v  a2  s  . c  om
    boolean tabHack = isTerminalEmulation();

    // Note: tab and ^I don't work in ConnectBot, hackish workaround
    if (tabHack) {
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER));
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_I));
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_I));
    } else {
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_TAB));
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_TAB));
    }
}