android.view.KeyCharacterMap Java Examples

The following examples show how to use android.view.KeyCharacterMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SuperTools.java    From AndroidAnimationExercise with Apache License 2.0 7 votes vote down vote up
/**
 * 获取导航栏高度
 *
 * @param context
 * @return
 */
public static int getNavigationBarHeight(Context context) {

    boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
    boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
    if (!hasMenuKey && !hasBackKey) {
        //没有物理按钮(虚拟导航栏)
        print("没有物理按钮(虚拟导航栏)");
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
        //获取NavigationBar的高度
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    } else {
        print("有物理导航栏,小米非全面屏");
        //有物理导航栏,小米非全面屏
        return 0;
    }
}
 
Example #2
Source File: ShortcutManager.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the shortcut intent for a given keycode+modifier. Make sure you
 * strip whatever modifier is used for invoking shortcuts (for example,
 * if 'Sym+A' should invoke a shortcut on 'A', you should strip the
 * 'Sym' bit from the modifiers before calling this method.
 * <p>
 * This will first try an exact match (with modifiers), and then try a
 * match without modifiers (primary character on a key).
 * 
 * @param kcm The key character map of the device on which the key was pressed.
 * @param keyCode The key code.
 * @param metaState The meta state, omitting any modifiers that were used
 * to invoke the shortcut.
 * @return The intent that matches the shortcut, or null if not found.
 */
public Intent getIntent(KeyCharacterMap kcm, int keyCode, int metaState) {
    ShortcutInfo shortcut = null;

    // If the Shift key is pressed, then search for the shift shortcuts.
    boolean isShiftOn = (metaState & KeyEvent.META_SHIFT_ON) == KeyEvent.META_SHIFT_ON;
    SparseArray<ShortcutInfo> shortcutMap = isShiftOn ? mShiftShortcuts : mShortcuts;

    // First try the exact keycode (with modifiers).
    int shortcutChar = kcm.get(keyCode, metaState);
    if (shortcutChar != 0) {
        shortcut = shortcutMap.get(shortcutChar);
    }

    // Next try the primary character on that key.
    if (shortcut == null) {
        shortcutChar = Character.toLowerCase(kcm.getDisplayLabel(keyCode));
        if (shortcutChar != 0) {
            shortcut = shortcutMap.get(shortcutChar);
        }
    }

    return (shortcut != null) ? shortcut.intent : null;
}
 
Example #3
Source File: InteractionController.java    From za-Farmer with MIT License 6 votes vote down vote up
public boolean sendKey(int keyCode, int metaState) {
    if (DEBUG) {
        Log.d(LOG_TAG, "sendKey (" + keyCode + ", " + metaState + ")");
    }

    final long eventTime = SystemClock.uptimeMillis();
    KeyEvent downEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN,
            keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
            InputDevice.SOURCE_KEYBOARD);
    if (injectEventSync(downEvent)) {
        KeyEvent upEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP,
                keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
                InputDevice.SOURCE_KEYBOARD);
        if(injectEventSync(upEvent)) {
            return true;
        }
    }
    return false;
}
 
Example #4
Source File: MyInteractionController.java    From PUMA with Apache License 2.0 6 votes vote down vote up
/**
 * Send keys and blocks until the first specified accessibility event.
 *
 * Most key presses will cause some UI change to occur. If the device is busy, this will
 * block until the device begins to process the key press at which point the call returns
 * and normal wait for idle processing may begin. If no events are detected for the
 * timeout period specified, the call will return anyway with false.
 *
 * @param keyCode
 * @param metaState
 * @param eventType
 * @param timeout
 * @return true if events is received, otherwise false.
 */
public boolean sendKeyAndWaitForEvent(final int keyCode, final int metaState, final int eventType, long timeout) {
	Runnable command = new Runnable() {
		@Override
		public void run() {
			final long eventTime = SystemClock.uptimeMillis();
			KeyEvent downEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD);
			if (injectEventSync(downEvent)) {
				KeyEvent upEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP, keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD);
				injectEventSync(upEvent);
			}
		}
	};

	return runAndWaitForEvents(command, new WaitForAnyEventPredicate(eventType), timeout) != null;
}
 
Example #5
Source File: ViewUtil.java    From MaterialChipsInput with Apache License 2.0 6 votes vote down vote up
public static int getNavBarHeight(Context context) {
    int result = 0;
    boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
    boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

    if(!hasMenuKey && !hasBackKey) {
        //The device has a navigation bar
        Resources resources = context.getResources();

        int orientation = context.getResources().getConfiguration().orientation;
        int resourceId;
        if (isTablet(context)){
            resourceId = resources.getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height" : "navigation_bar_height_landscape", "dimen", "android");
        }  else {
            resourceId = resources.getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT ? "navigation_bar_height" : "navigation_bar_width", "dimen", "android");
        }

        if (resourceId > 0) {
            return context.getResources().getDimensionPixelSize(resourceId);
        }
    }
    return result;
}
 
Example #6
Source File: InteractionController.java    From za-Farmer with MIT License 6 votes vote down vote up
/**
 * Send keys and blocks until the first specified accessibility event.
 *
 * Most key presses will cause some UI change to occur. If the device is busy, this will
 * block until the device begins to process the key press at which point the call returns
 * and normal wait for idle processing may begin. If no events are detected for the
 * timeout period specified, the call will return anyway with false.
 *
 * @param keyCode
 * @param metaState
 * @param eventType
 * @param timeout
 * @return true if events is received, otherwise false.
 */
public boolean sendKeyAndWaitForEvent(final int keyCode, final int metaState,
        final int eventType, long timeout) {
    Runnable command = new Runnable() {
        @Override
        public void run() {
            final long eventTime = SystemClock.uptimeMillis();
            KeyEvent downEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN,
                    keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
                    InputDevice.SOURCE_KEYBOARD);
            if (injectEventSync(downEvent)) {
                KeyEvent upEvent = new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_UP,
                        keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
                        InputDevice.SOURCE_KEYBOARD);
                injectEventSync(upEvent);
            }
        }
    };

    return runAndWaitForEvents(command, new WaitForAnyEventPredicate(eventType), timeout)
            != null;
}
 
Example #7
Source File: Instrumentation.java    From AndroidComponentPlugin with Apache License 2.0 6 votes vote down vote up
/**
 * Sends the key events corresponding to the text to the app being
 * instrumented.
 * 
 * @param text The text to be sent. 
 */
public void sendStringSync(String text) {
    if (text == null) {
        return;
    }
    KeyCharacterMap keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);

    KeyEvent[] events = keyCharacterMap.getEvents(text.toCharArray());

    if (events != null) {
        for (int i = 0; i < events.length; i++) {
            // We have to change the time of an event before injecting it because
            // all KeyEvents returned by KeyCharacterMap.getEvents() have the same
            // time stamp and the system rejects too old events. Hence, it is
            // possible for an event to become stale before it is injected if it
            // takes too long to inject the preceding ones.
            sendKeySync(KeyEvent.changeTimeRepeat(events[i], SystemClock.uptimeMillis(), 0));
        }
    }
}
 
Example #8
Source File: Utils.java    From Snake with Apache License 2.0 6 votes vote down vote up
/**
 * Get whether navigation bar is visible.
 *
 * @param context Context
 * @return true: visible, false: invisible
 */
public static boolean navigationBarVisible(@NonNull Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        Display display = windowManager.getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        Point realSize = new Point();
        display.getRealSize(realSize);

        return realSize.y != size.y;
    }else {
        boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

        if(hasMenuKey || hasBackKey) {
            return false;
        } else {
            return true;
        }
    }
}
 
Example #9
Source File: Instrumentation.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Sends the key events corresponding to the text to the app being
 * instrumented.
 * 
 * @param text The text to be sent. 
 */
public void sendStringSync(String text) {
    if (text == null) {
        return;
    }
    KeyCharacterMap keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);

    KeyEvent[] events = keyCharacterMap.getEvents(text.toCharArray());

    if (events != null) {
        for (int i = 0; i < events.length; i++) {
            // We have to change the time of an event before injecting it because
            // all KeyEvents returned by KeyCharacterMap.getEvents() have the same
            // time stamp and the system rejects too old events. Hence, it is
            // possible for an event to become stale before it is injected if it
            // takes too long to inject the preceding ones.
            sendKeySync(KeyEvent.changeTimeRepeat(events[i], SystemClock.uptimeMillis(), 0));
        }
    }
}
 
Example #10
Source File: TextKeyListener.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private KeyListener getKeyListener(KeyEvent event) {
    KeyCharacterMap kmap = event.getKeyCharacterMap();
    int kind = kmap.getKeyboardType();

    if (kind == KeyCharacterMap.ALPHA) {
        return QwertyKeyListener.getInstance(mAutoText, mAutoCap);
    } else if (kind == KeyCharacterMap.NUMERIC) {
        return MultiTapKeyListener.getInstance(mAutoText, mAutoCap);
    } else if (kind == KeyCharacterMap.FULL
            || kind == KeyCharacterMap.SPECIAL_FUNCTION) {
        // We consider special function keyboards full keyboards as a workaround for
        // devices that do not have built-in keyboards.  Applications may try to inject
        // key events using the built-in keyboard device id which may be configured as
        // a special function keyboard using a default key map.  Ideally, as of Honeycomb,
        // these applications should be modified to use KeyCharacterMap.VIRTUAL_KEYBOARD.
        return QwertyKeyListener.getInstanceForFullKeyboard();
    }

    return NullKeyListener.getInstance();
}
 
Example #11
Source File: PjSipService.java    From CSipSimple with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Send a dtmf signal to a call
 * 
 * @param callId the call to send the signal
 * @param keyCode the keyCode to send (android style)
 * @return
 */
public int sendDtmf(int callId, int keyCode) throws SameThreadException {
    if (!created) {
        return -1;
    }
    String keyPressed = "";
    // Since some device (xoom...) are apparently buggy with key character
    // map loading...
    // we have to do crappy thing here
    if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
        keyPressed = Integer.toString(keyCode - KeyEvent.KEYCODE_0);
    } else if (keyCode == KeyEvent.KEYCODE_POUND) {
        keyPressed = "#";
    } else if (keyCode == KeyEvent.KEYCODE_STAR) {
        keyPressed = "*";
    } else {
        // Fallback... should never be there if using visible dialpad, but
        // possible using keyboard
        KeyCharacterMap km = KeyCharacterMap.load(KeyCharacterMap.NUMERIC);
        keyPressed = Integer.toString(km.getNumber(keyCode));
    }
    return sendDtmf(callId, keyPressed);
}
 
Example #12
Source File: MetaKeyKeyListener.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private static long release(long state, int what, long mask,
        long pressed, long released, long used, KeyEvent event) {
    switch (event.getKeyCharacterMap().getModifierBehavior()) {
        case KeyCharacterMap.MODIFIER_BEHAVIOR_CHORDED_OR_TOGGLED:
            if ((state & used) != 0) {
                state &= ~mask;
            } else if ((state & pressed) != 0) {
                state |= what | released;
            }
            break;

        default:
            state &= ~mask;
            break;
    }
    return state;
}
 
Example #13
Source File: LongPressKeyCode.java    From appium-uiautomator2-server with Apache License 2.0 6 votes vote down vote up
@Override
protected AppiumResponse safeHandle(IHttpRequest request) {
    final KeyCodeModel model = toModel(request, KeyCodeModel.class);
    final int keyCode = model.keycode;
    int metaState = model.metastate == null ? 0 : model.metastate;
    int flags = model.flags == null ? 0 : model.flags;

    final long downTime = SystemClock.uptimeMillis();
    final InteractionController interactionController = UiAutomatorBridge.getInstance().getInteractionController();
    boolean isSuccessful = interactionController.injectEventSync(new KeyEvent(downTime, downTime,
                    KeyEvent.ACTION_DOWN, keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD,
                    0, flags));
    // https://android.googlesource.com/platform/frameworks/base.git/+/9d83b4783c33f1fafc43f367503e129e5a5047fa%5E%21/#F0
    isSuccessful &= interactionController.injectEventSync(new KeyEvent(downTime, SystemClock.uptimeMillis(),
                    KeyEvent.ACTION_DOWN, keyCode, 1, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD,
                    0, flags | KeyEvent.FLAG_LONG_PRESS));
    isSuccessful &= interactionController.injectEventSync(new KeyEvent(downTime, SystemClock.uptimeMillis(),
                    KeyEvent.ACTION_UP, keyCode, 0, metaState, KeyCharacterMap.VIRTUAL_KEYBOARD,
                    0, flags));
    if (!isSuccessful) {
        throw new InvalidElementStateException("Cannot inject long press event for key code " + keyCode);
    }
    return new AppiumResponse(getSessionId(request));
}
 
Example #14
Source File: FloatingView.java    From dingo with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Check if there is a software navigation bar(including the navigation bar in the screen).
 *
 * @return True if there is a software navigation bar
 */
private boolean hasSoftNavigationBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        final DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        mWindowManager.getDefaultDisplay().getRealMetrics(realDisplayMetrics);
        return realDisplayMetrics.heightPixels > mMetrics.heightPixels || realDisplayMetrics.widthPixels > mMetrics.widthPixels;
    }

    // old device check flow
    // Navigation bar exists (config_showNavigationBar is true, or both the menu key and the back key are not exists)
    final Context context = getContext();
    final Resources resources = context.getResources();
    final boolean hasMenuKey = ViewConfiguration.get(context).hasPermanentMenuKey();
    final boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
    final int showNavigationBarResId = resources.getIdentifier("config_showNavigationBar", "bool", "android");
    final boolean hasNavigationBarConfig = showNavigationBarResId != 0 && resources.getBoolean(showNavigationBarResId);
    return hasNavigationBarConfig || (!hasMenuKey && !hasBackKey);
}
 
Example #15
Source File: EventInjectorTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Test
public void injectKeyEventUpWithNoDown() throws Exception {
  ActivityScenario<SendActivity> scenario = ActivityScenario.launch(SendActivity.class);

  scenario.onActivity(
      sendActivity -> {
        View view = sendActivity.findViewById(R.id.send_data_edit_text);
        assertTrue(view.requestFocus());
        latch.countDown();
      });

  assertTrue("Timed out!", latch.await(10, TimeUnit.SECONDS));
  KeyCharacterMap keyCharacterMap = UiControllerImpl.getKeyCharacterMap();
  KeyEvent[] events = keyCharacterMap.getEvents("a".toCharArray());
  assertTrue(injector.injectKeyEvent(events[1]));
}
 
Example #16
Source File: BarUtils.java    From Android-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Return whether the navigation bar visible.
 *
 * @return {@code true}: yes<br>{@code false}: no
 */
public static boolean isSupportNavBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        WindowManager wm = (WindowManager) UtilsApp.getApp().getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) return false;
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        Point realSize = new Point();
        display.getSize(size);
        display.getRealSize(realSize);
        return realSize.y != size.y || realSize.x != size.x;
    }
    boolean menu = ViewConfiguration.get(UtilsApp.getApp()).hasPermanentMenuKey();
    boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
    return !menu && !back;
}
 
Example #17
Source File: BarUtils.java    From AndroidUtilCode with Apache License 2.0 6 votes vote down vote up
/**
 * Return whether the navigation bar visible.
 *
 * @return {@code true}: yes<br>{@code false}: no
 */
public static boolean isSupportNavBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        WindowManager wm = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) return false;
        Display display = wm.getDefaultDisplay();
        Point size = new Point();
        Point realSize = new Point();
        display.getSize(size);
        display.getRealSize(realSize);
        return realSize.y != size.y || realSize.x != size.x;
    }
    boolean menu = ViewConfiguration.get(Utils.getApp()).hasPermanentMenuKey();
    boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
    return !menu && !back;
}
 
Example #18
Source File: MainActivity.java    From BottomNavigationBar with Apache License 2.0 6 votes vote down vote up
private boolean hasSystemNavigationBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Display d = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

        DisplayMetrics realDisplayMetrics = new DisplayMetrics();
        d.getRealMetrics(realDisplayMetrics);

        int realHeight = realDisplayMetrics.heightPixels;
        int realWidth = realDisplayMetrics.widthPixels;

        DisplayMetrics displayMetrics = new DisplayMetrics();
        d.getMetrics(displayMetrics);

        int displayHeight = displayMetrics.heightPixels;
        int displayWidth = displayMetrics.widthPixels;

        return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
    } else {
        boolean hasMenuKey = ViewConfiguration.get(this).hasPermanentMenuKey();
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        return !hasMenuKey && !hasBackKey;
    }
}
 
Example #19
Source File: ScreenUtils.java    From NewbieGuide with Apache License 2.0 6 votes vote down vote up
/**
 * 虚拟操作拦(home等)是否显示
 */
public static boolean isNavigationBarShow(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Display display = activity.getWindowManager().getDefaultDisplay();
        Point size = new Point();
        Point realSize = new Point();
        display.getSize(size);
        display.getRealSize(realSize);
        return realSize.y != size.y;
    } else {
        boolean menu = ViewConfiguration.get(activity).hasPermanentMenuKey();
        boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        if (menu || back) {
            return false;
        } else {
            return true;
        }
    }
}
 
Example #20
Source File: BarUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 判断是否支持 Navigation Bar
 * @return {@code true} yes, {@code false} no
 */
public static boolean isSupportNavBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        WindowManager windowManager = AppUtils.getWindowManager();
        if (windowManager == null) return false;
        Display display = windowManager.getDefaultDisplay();
        Point size = new Point();
        Point realSize = new Point();
        display.getSize(size);
        display.getRealSize(realSize);
        return realSize.y != size.y || realSize.x != size.x;
    }
    boolean menu = ViewConfiguration.get(DevUtils.getContext()).hasPermanentMenuKey();
    boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
    return !menu && !back;
}
 
Example #21
Source File: PieController.java    From GravityBox with Apache License 2.0 6 votes vote down vote up
public void handleMessage(Message m) {
    switch (m.what) {
        case MSG_INJECT_KEY:
            final long eventTime = SystemClock.uptimeMillis();
            final InputManager inputManager = (InputManager)
                    XposedHelpers.callStaticMethod(InputManager.class, "getInstance");

            int flags = KeyEvent.FLAG_FROM_SYSTEM;
            XposedHelpers.callMethod(inputManager, "injectInputEvent",
                    new KeyEvent(eventTime - 50, eventTime - 50, KeyEvent.ACTION_DOWN, m.arg1, 0,
                            0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, flags, InputDevice.SOURCE_UNKNOWN), 0);
            XposedHelpers.callMethod(inputManager, "injectInputEvent",
                    new KeyEvent(eventTime - 50, eventTime - 25, KeyEvent.ACTION_UP, m.arg1, 0,
                            0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, flags, InputDevice.SOURCE_UNKNOWN), 0);

            break;
    }
}
 
Example #22
Source File: VenvyUIUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param context
 * @return 是否存在导航栏
 */
public static boolean isNavigationBarShow(Context context) {
    if (!(context instanceof Activity)) {
        return false;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        Display display = ((Activity) (context)).getWindowManager().getDefaultDisplay();
        Point size = new Point();
        Point realSize = new Point();
        display.getSize(size);
        display.getRealSize(realSize);
        return realSize.x != size.x;
    } else {
        boolean menu = ViewConfiguration.get(context).hasPermanentMenuKey();
        boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        if (menu || back) {
            return false;
        } else {
            return true;
        }
    }
}
 
Example #23
Source File: EntryKeyboardView.java    From bither-android with Apache License 2.0 6 votes vote down vote up
private void sendKeyEventsToTarget(int character) {
    if (viewRootImpl == null && canGetViewRootImpl) {
        getViewRootImpl();
    }
    KeyEvent[] events = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD).getEvents(new
            char[]{(char) character});
    try {
        Method method = viewRootImpl.getClass().getDeclaredMethod("dispatchKeyFromIme",
                KeyEvent.class);
        method.setAccessible(true);
        if (events != null) {
            final int N = events.length;
            for (int i = 0;
                 i < N;
                 i++) {
                KeyEvent event = events[i];
                event = KeyEvent.changeFlags(event, event.getFlags() | KeyEvent
                        .FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE);
                method.invoke(viewRootImpl, event);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        LogUtil.e(TAG, "can not dispatch input event");
    }
}
 
Example #24
Source File: InteractionController.java    From JsDroidCmd with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Send keys and blocks until the first specified accessibility event.
 * 
 * Most key presses will cause some UI change to occur. If the device is
 * busy, this will block until the device begins to process the key press at
 * which point the call returns and normal wait for idle processing may
 * begin. If no events are detected for the timeout period specified, the
 * call will return anyway with false.
 * 
 * @param keyCode
 * @param metaState
 * @param eventType
 * @param timeout
 * @return true if events is received, otherwise false.
 */
public boolean sendKeyAndWaitForEvent(final int keyCode,
		final int metaState, final int eventType, long timeout) {
	Runnable command = new Runnable() {
		@Override
		public void run() {
			final long eventTime = SystemClock.uptimeMillis();
			KeyEvent downEvent = new KeyEvent(eventTime, eventTime,
					KeyEvent.ACTION_DOWN, keyCode, 0, metaState,
					KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
					InputDevice.SOURCE_KEYBOARD);
			if (injectEventSync(downEvent)) {
				KeyEvent upEvent = new KeyEvent(eventTime, eventTime,
						KeyEvent.ACTION_UP, keyCode, 0, metaState,
						KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0,
						InputDevice.SOURCE_KEYBOARD);
				injectEventSync(upEvent);
			}
		}
	};

	return runAndWaitForEvents(command, new WaitForAnyEventPredicate(
			eventType), timeout) != null;
}
 
Example #25
Source File: Utils.java    From chips-input-layout with MIT License 5 votes vote down vote up
static int getNavBarHeight(Context c) {
    int result = 0;
    boolean hasMenuKey = ViewConfiguration.get(c).hasPermanentMenuKey();
    boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

    if (!hasMenuKey && !hasBackKey) {
        // The device has a navigation bar
        final Resources res = c.getResources();
        final Configuration config = res.getConfiguration();

        int orientation = config.orientation;
        int resourceId;

        // Check if the device is a tablet
        if ((config.screenLayout&Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE) {
            resourceId = res.getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT
                    ? "navigation_bar_height" : "navigation_bar_height_landscape",
                    "dimen", "android");
        } else {
            resourceId = res.getIdentifier(orientation == Configuration.ORIENTATION_PORTRAIT
                    ? "navigation_bar_height" : "navigation_bar_width",
                    "dimen", "android");
        }

        if (resourceId > 0) {
            return res.getDimensionPixelSize(resourceId);
        }
    }
    return result;
}
 
Example #26
Source File: TimePickerDialog.java    From DateTimepicker with Apache License 2.0 5 votes vote down vote up
/**
 * Get the keycode value for AM and PM in the current language.
 */
private int getAmOrPmKeyCode(int amOrPm) {
    // Cache the codes.
    if (mAmKeyCode == -1 || mPmKeyCode == -1) {
        // Find the first character in the AM/PM text that is unique.
        KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
        char amChar;
        char pmChar;
        for (int i = 0; i < Math.max(mAmText.length(), mPmText.length()); i++) {
            amChar = mAmText.toLowerCase(Locale.getDefault()).charAt(i);
            pmChar = mPmText.toLowerCase(Locale.getDefault()).charAt(i);
            if (amChar != pmChar) {
                KeyEvent[] events = kcm.getEvents(new char[]{amChar, pmChar});
                // There should be 4 events: a down and up for both AM and PM.
                if (events != null && events.length == 4) {
                    mAmKeyCode = events[0].getKeyCode();
                    mPmKeyCode = events[2].getKeyCode();
                } else {
                    Log.e(TAG, "Unable to find keycodes for AM and PM.");
                }
                break;
            }
        }
    }
    if (amOrPm == AM) {
        return mAmKeyCode;
    } else if (amOrPm == PM) {
        return mPmKeyCode;
    }

    return -1;
}
 
Example #27
Source File: MenuBuilder.java    From CSipSimple with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
MenuItemImpl findItemWithShortcutForKey(int keyCode, KeyEvent event) {
    // Get all items that can be associated directly or indirectly with the keyCode
    ArrayList<MenuItemImpl> items = mTempShortcutItemList;
    items.clear();
    findItemsWithShortcutForKey(items, keyCode, event);

    if (items.isEmpty()) {
        return null;
    }

    final int metaState = event.getMetaState();
    final KeyCharacterMap.KeyData possibleChars = new KeyCharacterMap.KeyData();
    // Get the chars associated with the keyCode (i.e using any chording combo)
    event.getKeyData(possibleChars);

    // If we have only one element, we can safely returns it
    final int size = items.size();
    if (size == 1) {
        return items.get(0);
    }

    final boolean qwerty = isQwertyMode();
    // If we found more than one item associated with the key,
    // we have to return the exact match
    for (int i = 0; i < size; i++) {
        final MenuItemImpl item = items.get(i);
        final char shortcutChar = qwerty ? item.getAlphabeticShortcut() :
                item.getNumericShortcut();
        if ((shortcutChar == possibleChars.meta[0] &&
                (metaState & KeyEvent.META_ALT_ON) == 0)
            || (shortcutChar == possibleChars.meta[2] &&
                (metaState & KeyEvent.META_ALT_ON) != 0)
            || (qwerty && shortcutChar == '\b' &&
                keyCode == KeyEvent.KEYCODE_DEL)) {
            return item;
        }
    }
    return null;
}
 
Example #28
Source File: ThreadedInputConnection.java    From 365browser with Apache License 2.0 5 votes vote down vote up
private void updateComposingTextOnUiThread(
        CharSequence text, int newCursorPosition, boolean isPendingAccent) {
    int accentToSend =
            isPendingAccent ? (mPendingAccent | KeyCharacterMap.COMBINING_ACCENT) : 0;
    cancelCombiningAccentOnUiThread();
    mImeAdapter.sendCompositionToNative(text, newCursorPosition, false, accentToSend);
}
 
Example #29
Source File: DragScaleCircleView.java    From DragScaleCircleView with Apache License 2.0 5 votes vote down vote up
/**
 * Initialization obtain the screen width and height.
 */
protected void init(@NonNull Context context, @Nullable AttributeSet attrs) {
    // custom attr
    final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DragScaleCircleView);
    try {
        mHasGuideLine = typedArray.getBoolean(R.styleable.DragScaleCircleView_hasGuideLine, true);
        mGuideLineSize = typedArray.getFloat(R.styleable.DragScaleCircleView_guideLineSize, getResources().getDimension(R.dimen.guideline_width));
        mGuideLineColor = typedArray.getInt(R.styleable.DragScaleCircleView_guideLineColor, getResources().getColor(R.color.guideline));
        mBorderSize = typedArray.getFloat(R.styleable.DragScaleCircleView_borderSize, getResources().getDimension(R.dimen.border_width));
        mBorderColor = typedArray.getInt(R.styleable.DragScaleCircleView_borderColor, getResources().getColor(R.color.border));
    } finally {
        typedArray.recycle();
    }

    final Resources resources = context.getResources();
    mScreenWidth = resources.getDisplayMetrics().widthPixels;
    boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
    boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
    if (hasBackKey && hasHomeKey) {
        mScreenHeight = getResources().getDisplayMetrics().heightPixels - 40 - 128;
    } else {
        mScreenHeight = getResources().getDisplayMetrics().heightPixels - 40;
    }
    mBoarderPaint = PaintUtil.newBoarderPaint(mBorderSize, mBorderColor);
    mSurroundingAreaOverlayPaint = PaintUtil.newSurroundingAreaOverlayPaint();
    mHandlePaint = PaintUtil.newHandlerPaint(resources);
    mHandleRadius = resources.getDimension(R.dimen.corner_width);
    mGuideLinePaint = PaintUtil.newGuideLinePaint(mGuideLineSize, mGuideLineColor);
}
 
Example #30
Source File: SoftKeyboard.java    From AndroidKeyboard with GNU General Public License v3.0 5 votes vote down vote up
/**
 * This translates incoming hard key events in to edit operations on an
 * InputConnection.  It is only needed when using the
 * PROCESS_HARD_KEYS option.
 */
private boolean translateKeyDown(int keyCode, KeyEvent event) {
    mMetaState = MetaKeyKeyListener.handleKeyDown(mMetaState,
            keyCode, event);
    int c = event.getUnicodeChar(MetaKeyKeyListener.getMetaState(mMetaState));
    mMetaState = MetaKeyKeyListener.adjustMetaAfterKeypress(mMetaState);
    InputConnection ic = getCurrentInputConnection();
    if (c == 0 || ic == null) {
        return false;
    }

    if ((c & KeyCharacterMap.COMBINING_ACCENT) != 0) {
        c = c & KeyCharacterMap.COMBINING_ACCENT_MASK;
    }

    if (mComposing.length() > 0) {
        char accent = mComposing.charAt(mComposing.length() - 1);
        int composed = KeyEvent.getDeadChar(accent, c);
        if (composed != 0) {
            c = composed;
            mComposing.setLength(mComposing.length() - 1);
        }
    }

    onKey(c, null);

    return true;
}