Java Code Examples for android.view.accessibility.AccessibilityEvent#getText()

The following examples show how to use android.view.accessibility.AccessibilityEvent#getText() . 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: TextEventInterpreter.java    From talkback with Apache License 2.0 6 votes vote down vote up
private static boolean passesSanityCheck(AccessibilityEvent event) {
  final List<CharSequence> afterTexts = event.getText();
  final CharSequence afterText =
      (afterTexts == null || afterTexts.isEmpty()) ? null : afterTexts.get(0);

  final CharSequence beforeText = event.getBeforeText();

  // Special case for deleting all the text in an EditText with a
  // hint, since the event text will contain the hint rather than an
  // empty string.
  int beforeTextLength = (beforeText == null) ? 0 : beforeText.length();
  if ((event.getAddedCount() == 0) && (event.getRemovedCount() == beforeTextLength)) {
    return true;
  }

  if (afterText == null || beforeText == null) {
    return false;
  }

  final int diff = (event.getAddedCount() - event.getRemovedCount());

  return ((beforeText.length() + diff) == afterText.length());
}
 
Example 2
Source File: TranslucentDrawerLayout.java    From 920-text-editor-v2 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
    // Special case to handle window state change events. As far as
    // accessibility services are concerned, state changes from
    // DrawerLayout invalidate the entire contents of the screen (like
    // an Activity or Dialog) and they should announce the title of the
    // new content.
    if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
        final List<CharSequence> eventText = event.getText();
        final View visibleDrawer = findVisibleDrawer();
        if (visibleDrawer != null) {
            final int edgeGravity = getDrawerViewAbsoluteGravity(visibleDrawer);
            final CharSequence title = getDrawerTitle(edgeGravity);
            if (title != null) {
                eventText.add(title);
            }
        }

        return true;
    }

    return super.dispatchPopulateAccessibilityEvent(host, event);
}
 
Example 3
Source File: AccessibilityEventUtils.java    From brailleback with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the text of an <code>event</code> by concatenating the text members
 * (regardless of their priority) using space as a delimiter.
 *
 * @param event The event.
 * @return The event text.
 */
private static CharSequence getEventAggregateText(AccessibilityEvent event) {
    final StringBuilder aggregator = new StringBuilder();
    final List<CharSequence> eventText = event.getText();
    final Iterator<CharSequence> it = eventText.iterator();

    while (it.hasNext()) {
        final CharSequence text = it.next();

        if (it.hasNext()) {
            StringUtils.appendWithSpaces(aggregator, text);
        } else {
            aggregator.append(text);
        }
    }

    return aggregator;
}
 
Example 4
Source File: DrawerLayout.java    From adt-leanback-support with Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
    // Special case to handle window state change events. As far as
    // accessibility services are concerned, state changes from
    // DrawerLayout invalidate the entire contents of the screen (like
    // an Activity or Dialog) and they should announce the title of the
    // new content.
    if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
        final List<CharSequence> eventText = event.getText();
        final View visibleDrawer = findVisibleDrawer();
        if (visibleDrawer != null) {
            final int edgeGravity = getDrawerViewAbsoluteGravity(visibleDrawer);
            final CharSequence title = getDrawerTitle(edgeGravity);
            if (title != null) {
                eventText.add(title);
            }
        }

        return true;
    }

    return super.dispatchPopulateAccessibilityEvent(host, event);
}
 
Example 5
Source File: Launcher.java    From LaunchEnr with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
    final boolean result = super.dispatchPopulateAccessibilityEvent(event);
    final List<CharSequence> text = event.getText();
    text.clear();
    // 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;
}
 
Example 6
Source File: DebugDrawerLayout.java    From u2020 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatchPopulateAccessibilityEvent(View host, AccessibilityEvent event) {
  // Special case to handle window state change events. As far as
  // accessibility services are concerned, state changes from
  // DrawerLayout invalidate the entire contents of the screen (like
  // an Activity or Dialog) and they should announce the title of the
  // new content.
  if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
    final List<CharSequence> eventText = event.getText();
    final View visibleDrawer = findVisibleDrawer();
    if (visibleDrawer != null) {
      final int edgeGravity = getDrawerViewAbsoluteGravity(visibleDrawer);
      final CharSequence title = getDrawerTitle(edgeGravity);
      if (title != null) {
        eventText.add(title);
      }
    }

    return true;
  }

  return super.dispatchPopulateAccessibilityEvent(host, event);
}
 
Example 7
Source File: MainService.java    From Anti-recall with GNU Affero General Public License v3.0 6 votes vote down vote up
private void onNotification(AccessibilityEvent event) {
        List<CharSequence> texts = event.getText();
        if (texts.isEmpty()) {
            // 微信的登录通知的 text 是 null
            // 现在转为Notification Listener来判定了
            // App.autoLoginFlagEnable();
            return;
        }
        Log.i(TAG, "onNotification: " + packageName + " | " + texts);
        switch (packageName) {
            case pkgQQ:
//                new QQClient(this).onContentChanged(root);
                new QQClient(this).onNotificationChanged(event);
                break;
            case pkgTim:
//                new TimClient(this).onContentChanged(root);
                new TimClient(this).onNotificationChanged(event);
                break;
        }
    }
 
Example 8
Source File: WeChatMsg.java    From pc-android-controller-android with Apache License 2.0 6 votes vote down vote up
public static void sendNotify(AccessibilityEvent event) {
    List<CharSequence> texts = event.getText();
    if (!texts.isEmpty()) {
        String message = texts.get(0).toString();

        //过滤微信内部通知消息
        if (isInside(message)) {
            return;
        }

        //模拟打开通知栏消息
        if (event.getParcelableData() != null && event.getParcelableData() instanceof Notification) {
            Log.i("demo", "标题栏canReply=true");
            try {
                Notification notification = (Notification) event.getParcelableData();
                PendingIntent pendingIntent = notification.contentIntent;
                pendingIntent.send();
            } catch (PendingIntent.CanceledException e) {
                e.printStackTrace();
            }
        }
    }
}
 
Example 9
Source File: dex_smali.java    From stynico with MIT License 5 votes vote down vote up
@Override
   public void onAccessibilityEvent(AccessibilityEvent event)
   {
SharedPreferences sharedPreferences = getSharedPreferences("nico.styTool_preferences", MODE_PRIVATE); 
boolean isFirstRun = sharedPreferences.getBoolean("ok_c", true); 
//Editor editor = sharedPreferences.edit(); 
if (isFirstRun) 
{ 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setContentTitle("妮媌");
    builder.setContentText("QQ抢红包正在运行");
    builder.setOngoing(true);
    Notification notification = builder.build();
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(NOTIFICATION_ID, notification);
}
else 
{ 

}

if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED)
{
    List<CharSequence> texts = event.getText();
    if (!texts.isEmpty())
    {
	for (CharSequence text : texts)
	{
	    String content = text.toString();
	    if (content.contains(QQ_KEYWORD_NOTIFICATION))
	    {
		openNotify(event);
		return;
	    }
	}
    }
}
openHongBao(event);
   }
 
Example 10
Source File: Launcher.java    From TurboLauncher with Apache License 2.0 5 votes vote down vote up
@Override
public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
	final boolean result = super.dispatchPopulateAccessibilityEvent(event);
	final List<CharSequence> text = event.getText();
	text.clear();
	// 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;
}
 
Example 11
Source File: NotificationListener.java    From appium-uiautomator2-server with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void onAccessibilityEvent(AccessibilityEvent event) {
    if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
        Logger.debug("Catch toast message: " + event);
        List<CharSequence> text = event.getText();
        if (text != null && !text.isEmpty()) {
            setToastMessage(text);
        }
    }

    if (originalListener != null) {
        originalListener.onAccessibilityEvent(event);
    }
}
 
Example 12
Source File: MentionsEditText.java    From Spyglass with Apache License 2.0 5 votes vote down vote up
/**
 * Populate an {@link AccessibilityEvent} with information about this text view. Note that this implementation uses
 * a copy of the text that is explicitly not an instance of {@link MentionsEditable}. This is due to the fact that
 * AccessibilityEvent will use the default system classloader when unparcelling the data within the event. This
 * results in a ClassNotFoundException. For more details, see: https://github.com/linkedin/Spyglass/issues/10
 *
 * @param event the populated AccessibilityEvent
 */
@Override
public void onPopulateAccessibilityEvent(@NonNull AccessibilityEvent event) {
    super.onPopulateAccessibilityEvent(event);
    List<CharSequence> textList = event.getText();
    CharSequence mentionLessText = getTextWithoutMentions();
    for (int i = 0; i < textList.size(); i++) {
        CharSequence text = textList.get(i);
        if (text instanceof MentionsEditable) {
            textList.set(i, mentionLessText);
        }
    }
}
 
Example 13
Source File: MD5_jni.java    From stynico with MIT License 5 votes vote down vote up
@Override
public void onAccessibilityEvent(AccessibilityEvent event)
{
	final int eventType = event.getEventType(); // ClassName:
	// com.tencent.mm.ui.LauncherUI

	// 通知栏事件
	if (eventType == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED)
	{
		List<CharSequence> texts = event.getText();
		if (!texts.isEmpty())
		{
			for (CharSequence t : texts)
			{
				String text = String.valueOf(t);
				if (text.contains(WX_HONGBAO_TEXT_KEY) || text.contains(QQ_HONGBAO_TEXT_KEY))
				{
					openNotify(event);
					break;
				}
			}
		}
	} else if (eventType == AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE)
	{
		// 从微信主界面进入聊天界面
		openHongBao(event);
	} else if (eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED)
	{
		// 处理微信聊天界面
		openHongBao(event);
	}
}
 
Example 14
Source File: TextEntryProvider.java    From PrivacyStreams with Apache License 2.0 5 votes vote down vote up
private void onViewTextChanged(AccessibilityEvent event) {
    List<CharSequence> text = event.getText();
    if (text == null
            || text.size()==0
            || text.get(0).length() == 0
            || event.isPassword()) {
        this.mEvent = null;

    } else {
        onNewText(text.get(0), event);
    }
}
 
Example 15
Source File: WechatService.java    From WechatHook-Dusan with Apache License 2.0 5 votes vote down vote up
private void handleNotificationEvent(AccessibilityEvent event) {
    Parcelable data = event.getParcelableData();
    if (data == null || !(data instanceof Notification)) {
        return;
    }
    List<CharSequence> texts = event.getText();
    if (!texts.isEmpty()) {
        String text = String.valueOf(texts.get(0));//包括微信名  :
        handleNotification(text, (Notification) data);
    }
}
 
Example 16
Source File: PushMonitorAccessibilityService.java    From NewsPushMonitor with Apache License 2.0 5 votes vote down vote up
@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
    int type = accessibilityEvent.getEventType();
    if (type != AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
        return;
    }

    String pkg = accessibilityEvent.getPackageName().toString();
    long time = accessibilityEvent.getEventTime();
    Notification notification = (Notification) accessibilityEvent.getParcelableData();
    List<CharSequence> texts = accessibilityEvent.getText();

    StringBuilder sb = new StringBuilder();
    for (CharSequence text : texts) {
        sb.append(text).append("###");
    }

    LogWriter.i(TAG, "onAccessibilityEvent "
            + "package=" + pkg
            + ",time=" + time
            + ",text=" + sb.toString());

    LogWriter.i(TAG, "Parse notification for " + pkg + " begin!");
    BaseNotificationParser notificationParser = NotificationParserFactory.getNotificationParser(pkg);
    BaseNotificationParser.NewsInfo newsInfo = notificationParser.parse(notification);
    LogWriter.i(TAG, "when:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(newsInfo.when));
    LogWriter.i(TAG, "ContentTitle:" + newsInfo.contentTitle);
    LogWriter.i(TAG, "ContentText:" + newsInfo.contentText);
    LogWriter.i(TAG, "Url:" + newsInfo.url);
    LogWriter.i(TAG, "Parse notification for " + pkg + " end!");
    LogWriter.i(TAG, "##################################################################");

    commitNewsInfoToServer(newsInfo);
    showNewsInfo(newsInfo);
}
 
Example 17
Source File: TextEventInterpreter.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Returns {@code null}, empty string or the added text depending on the event.
 *
 * <p>For cases where event.getText() is null or bad size, text interpretation is expected to be
 * set invalid with "addedText is null" in interpretTextChange(). Hence where event.getText() is
 * null or bad size, we return null as returning an empty string here would bypass this condition
 * and the text interpretation would be incorrect.
 *
 * @param event
 * @return the added text.
 */
private static @Nullable CharSequence getAddedText(AccessibilityEvent event) {
  final List<CharSequence> textList = event.getText();
  // noinspection ConstantConditions
  if (textList == null || textList.size() > 1) {
    LogUtils.w(TAG, "getAddedText: Text list was null or bad size");
    return null;
  }

  // If the text was empty, the list will be empty. See the
  // implementation for TextView.onPopulateAccessibilityEvent().
  if (textList.size() == 0) {
    return "";
  }

  final CharSequence text = textList.get(0);
  if (text == null) {
    LogUtils.w(TAG, "getAddedText: First text entry was null");
    return null;
  }

  final int addedBegIndex = event.getFromIndex();
  final int addedEndIndex = addedBegIndex + event.getAddedCount();
  if (areInvalidIndices(text, addedBegIndex, addedEndIndex)) {
    LogUtils.w(
        TAG,
        "getAddedText: Invalid indices (%d,%d) for \"%s\"",
        addedBegIndex,
        addedEndIndex,
        text);
    return "";
  }

  return getSubsequenceWithSpans(text, addedBegIndex, addedEndIndex);
}
 
Example 18
Source File: peService.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * 描述:处理QQ状态栏
 * 作者:卜俊文
 * 邮箱:[email protected]
 * 日期:2017/11/1 下午1:49
 */
public void progressQQStatusBar(AccessibilityEvent event) {
    List<CharSequence> text = event.getText();
    //开始检索界面上是否有QQ红包的文本,并且他是通知栏的信息
    if (text != null && text.size() > 0) {
        for (CharSequence charSequence : text) {
            if (charSequence.toString().contains(QQConstant.QQ_ENVELOPE_KEYWORD)) {
                //说明存在红包弹窗,马上进去
                if (event.getParcelableData() != null && event.getParcelableData() instanceof Notification) {
                    Notification notification = (Notification) event.getParcelableData();
                    if (notification == null) {
                        return;
                    }
                    PendingIntent pendingIntent = notification.contentIntent;
                    if (pendingIntent == null) {
                        return;
                    }
                    try {
                        //要跳转之前,先进行解锁屏幕,然后再跳转,有可能你现在屏幕是锁屏状态,先进行解锁,然后打开页面,有密码的可能就不行了
                        wakeUpAndUnlock(HApp.context);
                        //跳转
                        pendingIntent.send();
                    } catch (PendingIntent.CanceledException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
 
Example 19
Source File: AccessibilityUtils.java    From DevUtils with Apache License 2.0 4 votes vote down vote up
/**
 * 打印 AccessibilityEvent 信息日志
 * @param event {@link AccessibilityEvent}
 * @param tag   日志 TAG
 */
public static void printAccessibilityEvent(final AccessibilityEvent event, final String tag) {
    if (event == null || !LogPrintUtils.isPrintLog()) return;

    StringBuilder builder = new StringBuilder();
    builder.append("=========================");
    builder.append(NEW_LINE_STR);

    int eventType = event.getEventType(); // 事件类型
    builder.append("packageName: " + event.getPackageName() + ""); // 响应事件的应用包名
    builder.append(NEW_LINE_STR);

    builder.append("source: " + event.getSource() + ""); // 事件源信息
    builder.append(NEW_LINE_STR);

    builder.append("source class: " + event.getClassName() + ""); // 事件源的类名, 如 android.widget.TextView
    builder.append(NEW_LINE_STR);

    builder.append("event type(int): " + eventType + "");
    builder.append(NEW_LINE_STR);

    switch (eventType) {
        case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:// 通知栏事件
            builder.append("event type: TYPE_NOTIFICATION_STATE_CHANGED");
            break;
        case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: // 窗体状态改变
            builder.append("event type: TYPE_WINDOW_STATE_CHANGED");
            break;
        case AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED: // View 获取到焦点
            builder.append("event type: TYPE_VIEW_ACCESSIBILITY_FOCUSED");
            break;
        case AccessibilityEvent.TYPE_GESTURE_DETECTION_START:
            builder.append("event type: TYPE_VIEW_ACCESSIBILITY_FOCUSED");
            break;
        case AccessibilityEvent.TYPE_GESTURE_DETECTION_END:
            builder.append("event type: TYPE_GESTURE_DETECTION_END");
            break;
        case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
            builder.append("event type: TYPE_WINDOW_CONTENT_CHANGED");
            break;
        case AccessibilityEvent.TYPE_VIEW_CLICKED:
            builder.append("event type: TYPE_VIEW_CLICKED");
            break;
        case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
            builder.append("event type: TYPE_VIEW_TEXT_CHANGED");
            break;
        case AccessibilityEvent.TYPE_VIEW_SCROLLED:
            builder.append("event type: TYPE_VIEW_SCROLLED");
            break;
        case AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED:
            builder.append("event type: TYPE_VIEW_TEXT_SELECTION_CHANGED");
            break;
    }
    builder.append(NEW_LINE_STR);

    for (CharSequence txt : event.getText()) {
        // 输出当前事件包含的文本信息
        builder.append("text: " + txt);
        builder.append(NEW_LINE_STR);
    }
    builder.append("=========================");

    // 打印日志
    LogPrintUtils.dTag(tag, builder.toString());
}
 
Example 20
Source File: TimeCatMonitorService.java    From timecat with Apache License 2.0 4 votes vote down vote up
private synchronized void getText(AccessibilityEvent event) {
        LogUtil.d(TAG, "getText:" + event);
        if (!monitorClick || event == null) {
            return;
        }
        if (showFloatView && !isRun) {
            return;
        }
        int type = getClickType(event);
        CharSequence className = event.getClassName();
        if (mWindowClassName == null) {
            return;
        }
        if (mWindowClassName.toString().startsWith("com.time.timecat")) {
            //自己的应用不监控
            return;
        }
        if (mCurrentPackage.equals(event.getPackageName())) {
            if (type != mCurrentType) {
                //点击方式不匹配,直接返回
                return;
            }
        } else {
            //包名不匹配,直接返回
            return;
        }
        if (className == null || className.equals("android.widget.EditText")) {
            //输入框不监控
            return;
        }
        if (onlyText) {
            //onlyText方式下,只获取TextView的内容
            if (!className.equals("android.widget.TextView")) {
                if (!hasShowTipToast) {
                    ToastUtil.i(R.string.toast_tip_content);
                    hasShowTipToast = true;
                }
                return;
            }
        }
        AccessibilityNodeInfo info = event.getSource();
        if (info == null) {
            return;
        }
        CharSequence txt = info.getText();
        if (TextUtils.isEmpty(txt) && !onlyText) {
            //非onlyText方式下获取文字更多,但是可能并不是想要的文字
            //比如系统短信页面需要这样才能获取到内容。
            List<CharSequence> txts = event.getText();
            if (txts != null) {
                StringBuilder sb = new StringBuilder();
                for (CharSequence t : txts) {
                    sb.append(t);
                }
                txt = sb.toString();
            }
        }
        if (!TextUtils.isEmpty(txt)) {
            if (txt.length() <= 2) {
                //对于太短的词进行屏蔽,因为这些词往往是“发送”等功能按钮,其实应该根据不同的activity进行区分
                if (!hasShowTooShortToast) {
                    ToastUtil.w(R.string.too_short_to_split);
                    hasShowTooShortToast = true;
                }
                return;
            }
            Intent intent = new Intent(this, TimeCatActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra(TimeCatActivity.TO_SPLIT_STR, txt.toString());
//            startActivity(intent);
            //放到ArcTipViewController中触发试试
            ArcTipViewController.getInstance().showTipViewForStartActivity(intent);
        }
    }