Example usage for android.view.accessibility AccessibilityEvent getText

List of usage examples for android.view.accessibility AccessibilityEvent getText

Introduction

In this page you can find the example usage for android.view.accessibility AccessibilityEvent getText.

Prototype

public List<CharSequence> getText() 

Source Link

Document

Gets the text of the event.

Usage

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

/**
 * Says {@code text} when accessibility is turned on.
 *//*from  ww w .ja v a 2 s  . c  o  m*/
public void sendAccessibilityText(String text) {
    if (mAccessibilityManager.isEnabled()) {
        AccessibilityEvent event = AccessibilityEvent.obtain();
        event.setClassName(getClass().getName());
        event.setPackageName(getPackageName());
        event.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT);
        event.getText().add(text);
        mAccessibilityManager.sendAccessibilityEvent(event);
    }
}

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

private void tune() {
    if (DEBUG)//from   ww  w.ja  v a2s. c  o  m
        Log.d(TAG, "tune()");
    mTuneDurationTimer.start();

    lazyInitializeIfNeeded(LAZY_INITIALIZATION_DELAY);

    // Prerequisites to be able to tune.
    if (mInputIdUnderSetup != null) {
        mTunePending = true;
        return;
    }
    mTunePending = false;
    final Channel channel = mChannelTuner.getCurrentChannel();
    if (!mChannelTuner.isCurrentChannelPassthrough()) {
        if (mTvInputManagerHelper.getTunerTvInputSize() == 0) {
            Toast.makeText(this, R.string.msg_no_input, Toast.LENGTH_SHORT).show();
            // TODO: Direct the user to a Play Store landing page for TvInputService apps.
            finish();
            return;
        }
        SetupUtils setupUtils = SetupUtils.getInstance(this);
        if (setupUtils.isFirstTune()) {
            if (!mChannelTuner.areAllChannelsLoaded()) {
                // tune() will be called, once all channels are loaded.
                stopTv("tune()", false);
                return;
            }
            if (mChannelDataManager.getChannelCount() > 0) {
                mOverlayManager.showIntroDialog();
            } else if (!Features.ONBOARDING_EXPERIENCE.isEnabled(this)) {
                mOverlayManager.showSetupFragment();
                return;
            }
        }
        if (!TvCommonUtils.isRunningInTest() && mShowNewSourcesFragment
                && setupUtils.hasUnrecognizedInput(mTvInputManagerHelper)) {
            // Show new channel sources fragment.
            runAfterAttachedToWindow(new Runnable() {
                @Override
                public void run() {
                    mOverlayManager.runAfterOverlaysAreClosed(new Runnable() {
                        @Override
                        public void run() {
                            mOverlayManager.showNewSourcesFragment();
                        }
                    });
                }
            });
        }
        mShowNewSourcesFragment = false;
        if (mChannelTuner.getBrowsableChannelCount() == 0 && mChannelDataManager.getChannelCount() > 0
                && !mOverlayManager.getSideFragmentManager().isActive()) {
            if (!mChannelTuner.areAllChannelsLoaded()) {
                return;
            }
            if (mTvInputManagerHelper.getTunerTvInputSize() == 1) {
                mOverlayManager.getSideFragmentManager().show(new CustomizeChannelListFragment());
            } else {
                showSettingsFragment();
            }
            return;
        }
        // TODO: need to refactor the following code to put in startTv.
        if (channel == null) {
            // There is no channel to tune to.
            stopTv("tune()", false);
            if (!mChannelDataManager.isDbLoadFinished()) {
                // Wait until channel data is loaded in order to know the number of channels.
                // tune() will be retried, once the channel data is loaded.
                return;
            }
            if (mOverlayManager.getSideFragmentManager().isActive()) {
                return;
            }
            mOverlayManager.showSetupFragment();
            return;
        }
        setupUtils.onTuned();
        if (mTuneParams != null) {
            Long initChannelId = mTuneParams.getLong(KEY_INIT_CHANNEL_ID);
            if (initChannelId == channel.getId()) {
                mTuneParams.remove(KEY_INIT_CHANNEL_ID);
            } else {
                mTuneParams = null;
            }
        }
    }

    mIsCurrentChannelUnblockedByUser = false;
    if (!isUnderShrunkenTvView()) {
        mLastAllowedRatingForCurrentChannel = null;
    }
    mHandler.removeMessages(MSG_UPDATE_CHANNEL_BANNER_BY_INFO_UPDATE);
    if (mAccessibilityManager.isEnabled()) {
        // For every tune, we need to inform the tuned channel or input to a user,
        // if Talkback is turned on.
        AccessibilityEvent event = AccessibilityEvent.obtain();
        event.setClassName(getClass().getName());
        event.setPackageName(getPackageName());
        event.setEventType(AccessibilityEvent.TYPE_ANNOUNCEMENT);
        if (TvContract.isChannelUriForPassthroughInput(channel.getUri())) {
            TvInputInfo input = mTvInputManagerHelper.getTvInputInfo(channel.getInputId());
            event.getText().add(Utils.loadLabel(this, input));
        } else if (TextUtils.isEmpty(channel.getDisplayName())) {
            event.getText().add(channel.getDisplayNumber());
        } else {
            event.getText().add(channel.getDisplayNumber() + " " + channel.getDisplayName());
        }
        mAccessibilityManager.sendAccessibilityEvent(event);
    }

    boolean success = mTvView.tuneTo(channel, mTuneParams, mOnTuneListener);
    mOnTuneListener.onTune(channel, isUnderShrunkenTvView());

    mTuneParams = null;
    if (!success) {
        Toast.makeText(this, R.string.msg_tune_failed, Toast.LENGTH_SHORT).show();
        return;
    }

    // Explicitly make the TV view main to make the selected input an HDMI-CEC active source.
    mTvView.setMain();
    scheduleRestoreMainTvView();
    if (!isUnderShrunkenTvView()) {
        if (!channel.isPassthrough()) {
            addToRecentChannels(channel.getId());
        }
        Utils.setLastWatchedChannel(this, channel);
        TvApplication.getSingletons(this).getMainActivityWrapper().notifyCurrentChannelChange(this, channel);
    }
    checkChannelLockNeeded(mTvView);
    updateChannelBannerAndShowIfNeeded(UPDATE_CHANNEL_BANNER_REASON_TUNE);
    if (mActivityResumed) {
        // requestVisibleBehind should be called after onResume() is called. But, when
        // launcher is over the TV app and the screen is turned off and on, tune() can
        // be called during the pause state by mBroadcastReceiver (Intent.ACTION_SCREEN_ON).
        requestVisibleBehind(true);
    }
    updateMediaSession();
}

From source file:com.android.launcher2.Launcher.java

@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
    final boolean result = super.dispatchPopulateAccessibilityEvent(event);
    final List<CharSequence> text = event.getText();
    text.clear();/*  w ww.  j  av  a 2 s  . c  o m*/
    // Populate event with a fake title based on the current state.
    if (mState == State.APPS_CUSTOMIZE) {
        text.add(getString(R.string.all_apps_button_label));
    } else {
        text.add(getString(R.string.all_apps_home_button_label));
    }
    return result;
}

From source file:com.android.launcher3.Launcher.java

@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
    final boolean result = super.dispatchPopulateAccessibilityEvent(event);
    final List<CharSequence> text = event.getText();
    text.clear();//from  www . j av  a 2  s  .  c o  m
    // Populate event with a fake title based on the current state.
    if (mState == State.APPS) {
        text.add(getString(R.string.all_apps_button_label));
    } else if (mState == State.WIDGETS) {
        text.add(getString(R.string.widget_button_text));
    } else if (mWorkspace != null) {
        text.add(mWorkspace.getCurrentPageDescription());
    } else {
        text.add(getString(R.string.all_apps_home_button_label));
    }
    return result;
}

From source file:com.android.soma.Launcher.java

@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
    final boolean result = super.dispatchPopulateAccessibilityEvent(event);
    final List<CharSequence> text = event.getText();
    text.clear();//w  ww .j ava2  s. com
    // Populate event with a fake title based on the current state.
    if (mState == State.APPS_CUSTOMIZE) {
        text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
    } else {
        text.add(getString(R.string.all_apps_home_button_label));
    }
    return result;
}

From source file:android.app.Activity.java

public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
    event.setClassName(getClass().getName());
    event.setPackageName(getPackageName());

    LayoutParams params = getWindow().getAttributes();
    boolean isFullScreen = (params.width == LayoutParams.MATCH_PARENT)
            && (params.height == LayoutParams.MATCH_PARENT);
    event.setFullScreen(isFullScreen);/*from  w  w w.ja va  2 s.  co  m*/

    CharSequence title = getTitle();
    if (!TextUtils.isEmpty(title)) {
        event.getText().add(title);
    }

    return true;
}

From source file:com.dattasmoon.pebble.plugin.NotificationService.java

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    // handle the prefs changing, because of how accessibility services
    // work, sharedprefsonchange listeners don't work
    if (watchFile.lastModified() > lastChange) {
        loadPrefs();//w  w  w. j  a v  a  2  s. co m
    }
    if (Constants.IS_LOGGABLE) {
        Log.i(Constants.LOG_TAG, "Service: Mode is: " + String.valueOf(mode.ordinal()));
    }
    // if we are off, don't do anything.
    if (mode == Mode.OFF) {
        if (Constants.IS_LOGGABLE) {
            Log.i(Constants.LOG_TAG, "Service: Mode is off, not sending any notifications");
        }
        return;
    }

    //handle quiet hours
    if (quiet_hours) {

        Calendar c = Calendar.getInstance();
        Date now = new Date(0, 0, 0, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
        if (Constants.IS_LOGGABLE) {
            Log.i(Constants.LOG_TAG, "Checking quiet hours. Now: " + now.toString() + " vs "
                    + quiet_hours_before.toString() + " and " + quiet_hours_after.toString());
        }

        if (quiet_hours_before.after(quiet_hours_after)) {
            if (now.after(quiet_hours_after) && now.before(quiet_hours_before)) {
                if (Constants.IS_LOGGABLE) {
                    Log.i(Constants.LOG_TAG, "Time is during quiet time. Returning.");
                }
                return;
            }

        } else if (now.before(quiet_hours_before) || now.after(quiet_hours_after)) {
            if (Constants.IS_LOGGABLE) {
                Log.i(Constants.LOG_TAG, "Time is before or after the quiet hours time. Returning.");
            }
            return;
        }

    }

    // handle if they only want notifications
    if (notifications_only) {
        if (event != null) {
            Parcelable parcelable = event.getParcelableData();
            if (!(parcelable instanceof Notification)) {

                if (Constants.IS_LOGGABLE) {
                    Log.i(Constants.LOG_TAG,
                            "Event is not a notification and notifications only is enabled. Returning.");
                }
                return;
            }
        }
    }
    if (no_ongoing_notifs) {
        Parcelable parcelable = event.getParcelableData();
        if (parcelable instanceof Notification) {
            Notification notif = (Notification) parcelable;
            if ((notif.flags & Notification.FLAG_ONGOING_EVENT) == Notification.FLAG_ONGOING_EVENT) {
                if (Constants.IS_LOGGABLE) {
                    Log.i(Constants.LOG_TAG,
                            "Event is a notification, notification flag contains ongoing, and no ongoing notification is true. Returning.");
                }
                return;
            }
        } else {
            if (Constants.IS_LOGGABLE) {
                Log.i(Constants.LOG_TAG, "Event is not a notification.");
            }
        }
    }

    // Handle the do not disturb screen on settings
    PowerManager powMan = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
    if (Constants.IS_LOGGABLE) {
        Log.d(Constants.LOG_TAG, "NotificationService.onAccessibilityEvent: notifScreenOn=" + notifScreenOn
                + "  screen=" + powMan.isScreenOn());
    }
    if (!notifScreenOn && powMan.isScreenOn()) {
        return;
    }

    if (event == null) {
        if (Constants.IS_LOGGABLE) {
            Log.i(Constants.LOG_TAG, "Event is null. Returning.");
        }
        return;
    }
    if (Constants.IS_LOGGABLE) {
        Log.i(Constants.LOG_TAG, "Event: " + event.toString());
    }

    // main logic
    PackageManager pm = getPackageManager();

    String eventPackageName;
    if (event.getPackageName() != null) {
        eventPackageName = event.getPackageName().toString();
    } else {
        eventPackageName = "";
    }
    if (Constants.IS_LOGGABLE) {
        Log.i(Constants.LOG_TAG, "Service package list is: ");
        for (String strPackage : packages) {
            Log.i(Constants.LOG_TAG, strPackage);
        }
        Log.i(Constants.LOG_TAG, "End Service package list");
    }

    switch (mode) {
    case EXCLUDE:
        // exclude functionality
        if (Constants.IS_LOGGABLE) {
            Log.i(Constants.LOG_TAG, "Mode is set to exclude");
        }

        for (String packageName : packages) {
            if (packageName.equalsIgnoreCase(eventPackageName)) {
                if (Constants.IS_LOGGABLE) {
                    Log.i(Constants.LOG_TAG, packageName + " == " + eventPackageName
                            + " which is on the exclude list. Returning.");
                }
                return;
            }
        }
        break;
    case INCLUDE:
        // include only functionality
        if (Constants.IS_LOGGABLE) {
            Log.i(Constants.LOG_TAG, "Mode is set to include only");
        }
        boolean found = false;
        for (String packageName : packages) {
            if (packageName.equalsIgnoreCase(eventPackageName)) {
                found = true;
                break;
            }
        }
        if (!found) {
            Log.i(Constants.LOG_TAG, eventPackageName + " was not found in the include list. Returning.");
            return;
        }
        break;
    }

    // get the title
    String title = "";
    try {
        boolean renamed = false;
        for (int i = 0; i < pkg_renames.length(); i++) {
            if (pkg_renames.getJSONObject(i).getString("pkg").equalsIgnoreCase(eventPackageName)) {
                renamed = true;
                title = pkg_renames.getJSONObject(i).getString("to");
            }
        }
        if (!renamed) {
            title = pm.getApplicationLabel(pm.getApplicationInfo(eventPackageName, 0)).toString();
        }
    } catch (NameNotFoundException e) {
        title = eventPackageName;
    } catch (JSONException e) {
        title = eventPackageName;
    }

    // get the notification text
    String notificationText = event.getText().toString();
    // strip the first and last characters which are [ and ]
    notificationText = notificationText.substring(1, notificationText.length() - 1);

    if (notification_extras) {
        if (Constants.IS_LOGGABLE) {
            Log.i(Constants.LOG_TAG, "Fetching extras from notification");
        }
        Parcelable parcelable = event.getParcelableData();
        if (parcelable instanceof Notification) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                notificationText += "\n" + getExtraBigData((Notification) parcelable, notificationText.trim());
            } else {
                notificationText += "\n" + getExtraData((Notification) parcelable, notificationText.trim());
            }

        }
    }

    // Check ignore lists
    for (int i = 0; i < ignores.length(); i++) {
        try {
            JSONObject ignore = ignores.getJSONObject(i);
            String app = ignore.getString("app");
            boolean exclude = ignore.optBoolean("exclude", true);
            boolean case_insensitive = ignore.optBoolean("insensitive", true);
            if ((!app.equals("-1")) && (!eventPackageName.equalsIgnoreCase(app))) {
                //this rule doesn't apply to all apps and this isn't the app we're looking for.
                continue;
            }
            String regex = "";
            if (case_insensitive) {
                regex += "(?i)";
            }
            if (!ignore.getBoolean("raw")) {
                regex += Pattern.quote(ignore.getString("match"));
            } else {
                regex += ignore.getString("match");
            }
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(notificationText);
            if (m.find()) {
                if (exclude) {
                    if (Constants.IS_LOGGABLE) {
                        Log.i(Constants.LOG_TAG, "Notification text of '" + notificationText + "' matches: '"
                                + regex + "' and exclude is on. Returning");
                    }
                    return;
                }
            } else {
                if (!exclude) {
                    if (Constants.IS_LOGGABLE) {
                        Log.i(Constants.LOG_TAG, "Notification text of '" + notificationText
                                + "' does not match: '" + regex + "' and include is on. Returning");
                    }
                    return;
                }

            }
        } catch (JSONException e) {
            continue;
        }
    }

    // Send the alert to Pebble

    sendToPebble(title, notificationText);

    if (Constants.IS_LOGGABLE) {
        Log.i(Constants.LOG_TAG, event.toString());
        Log.i(Constants.LOG_TAG, event.getPackageName().toString());
    }
}