diff --git a/AccessibilityService/AccessibilityService/README.md b/AccessibilityService/AccessibilityService/README.md new file mode 100644 index 0000000000000000000000000000000000000000..45fcee4fce4389fb73f1f4582ad8d19ad7392264 --- /dev/null +++ b/AccessibilityService/AccessibilityService/README.md @@ -0,0 +1,22 @@ +# Accessibility Service + +### Introduction + +This sample shows how to use the accessibility service. This service inherits from **AccessibleAbility** to listen for window changes, click events, and physical keys of the system to perform some operations. + +1. Configure the types of events to listen for in the **accessibility\_ability\_config.xml** file. + +2. **MyAccessibilityService** inherits from **AccessibleAbility**. It listens for screen changing events and provides prompts; listens for click events, plays voice prompts, and executes global events; listens for the volume key events to scroll the text up and down. + +### Usage + +1. Go to **Settings** \> **Accessibility features** \> **Accessibility** \> **Installed services**, enable **Accessibility**, and return to the app operation. + +2. Go to the app screen and touch **Click, Back to home**. The service listens to the touch of the button, broadcasts the button content, and executes the global HOME event to return to the home screen. + +3. Touch **KeyPressEventPage**. On the screen displayed, the text can be scrolled up and down by pressing the volume key. + +### Constraints + +This sample can only be run on large-system devices. + diff --git a/AccessibilityService/AccessibilityService/README_zh.md b/AccessibilityService/AccessibilityService/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..88881c48da90fe8a6822e6490d4f23a49773c9aa --- /dev/null +++ b/AccessibilityService/AccessibilityService/README_zh.md @@ -0,0 +1,22 @@ +# 无障碍服务 + +### 简介 + +本示例为用户演示无障碍服务功能的使用,继承AccessibleAbility实现无障碍服务中监听窗口变化和点击事件以及系统物理按键来做一些操作。 + +1、accessibility\_ability\_config.xml中配制要监听事件的类型。 + +2、MyAccessibilityService继承AccessibleAbility,实现监听窗口变化进行提示,监听按钮点击进行语音播报并执行全局事件,监听系统音量键实现文本内容的上下滚动。 + +### 使用说明 + +1、根据提示,前往设置-辅助功能-无障碍-已安装的服务 中打开无障碍服务的开关后返回该应用操作。 + +2、进入此应用界面,点击“点击,返回桌面”,无障碍服务会监听按钮的点击,进行语音播报按钮内容并执行全局HOME事件,返回桌面。 + +3、点击“系统按键事件”按钮进入新界面,该界面通过监听系统音量键实现文本内容的上下滚动。 + +### 约束与限制 + +本示例支持在大型设备上运行。 + diff --git a/AccessibilityService/AccessibilityService/build.gradle b/AccessibilityService/AccessibilityService/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..b25795fc1d8bc01db48078a95070172cb77e5c2f --- /dev/null +++ b/AccessibilityService/AccessibilityService/build.gradle @@ -0,0 +1,47 @@ +// Top-level build file where you can add configuration options common to all sub-projects/modules. +apply plugin: 'com.huawei.ohos.app' + +//For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 6 + defaultConfig { + compatibleSdkVersion 4 + } +} + +buildscript { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + maven { + url 'http://repo.ark.tools.huawei.com/artifactory/maven-public' + } + maven { + url 'http://mirrors.tools.huawei.com/maven/' + } + } + dependencies { + classpath 'com.huawei.ohos:hap:2.4.5.0' + } +} + +allprojects { + repositories { + maven { + url 'https://repo.huaweicloud.com/repository/maven/' + } + maven { + url 'https://developer.huawei.com/repo/' + } + maven { + url 'http://repo.ark.tools.huawei.com/artifactory/maven-public' + } + maven { + url 'http://mirrors.tools.huawei.com/maven/' + } + } +} diff --git a/AccessibilityService/AccessibilityService/entry/build.gradle b/AccessibilityService/AccessibilityService/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..b1b882781763d89e6ecfc352a0b382a7efb80354 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/build.gradle @@ -0,0 +1,20 @@ +apply plugin: 'com.huawei.ohos.hap' +//For instructions on signature configuration, see https://developer.harmonyos.com/cn/docs/documentation/doc-guides/ide_debug_device-0000001053822404#section1112183053510 +ohos { + compileSdkVersion 6 + defaultConfig { + compatibleSdkVersion 4 + } + buildTypes { + release { + proguardOpt { + proguardEnabled false + rulesFiles 'proguard-rules.pro' + } + } + } +} + +dependencies { + implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) +} diff --git a/AccessibilityService/AccessibilityService/entry/src/main/config.json b/AccessibilityService/AccessibilityService/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..a79e90f4603d00b17117b597f7cac782221fff85 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/config.json @@ -0,0 +1,68 @@ +{ + "app": { + "bundleName": "ohos.samples.accessibilityservice", + "vendor": "samples", + "version": { + "code": 1000000, + "name": "1.0.0" + } + }, + "deviceConfig": {}, + "module": { + "package": "ohos.samples.accessibilityservice", + "name": ".MyApplication", + "mainAbility": ".MainAbility", + "deviceType": [ + "phone" + ], + "distro": { + "deliveryWithInstall": true, + "moduleName": "entry", + "moduleType": "entry", + "installationFree": false + }, + "abilities": [ + { + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + "orientation": "unspecified", + "name": ".MainAbility", + "icon": "$media:icon", + "description": "$string:mainability_description", + "label": "$string:entry_MainAbility", + "type": "page", + "launchType": "standard" + }, + { + "name": ".service.MyAccessibilityService", + "visible": true, + "type": "service", + "permissions": [ + "ohos.permission.CONNECT_ACCESSIBILITY_ABILITY" + ], + "skills": [ + { + "actions": ["ohos.accessibilityservice.AccessibilityService"] + } + ], + "metaData": { + "customizeData": [ + { + "name": "ohos.accessibleability", + "extra": "$profile:accessibility_ability_config" + } + ], + "results": [] + } + } + ] + } +} \ No newline at end of file diff --git a/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/MainAbility.java b/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/MainAbility.java new file mode 100644 index 0000000000000000000000000000000000000000..10b7e314c982618f10d51096b36e4fb47af3b7f2 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/MainAbility.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.accessibilityservice; + +import ohos.samples.accessibilityservice.slice.MainAbilitySlice; +import ohos.aafwk.ability.Ability; +import ohos.aafwk.content.Intent; + +/** + * MainAbility + * + * @since 2021-07-23 + */ +public class MainAbility extends Ability { + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setMainRoute(MainAbilitySlice.class.getName()); + } +} diff --git a/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/MyApplication.java b/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/MyApplication.java new file mode 100644 index 0000000000000000000000000000000000000000..d49bd1424228737dfa31083b8e98a1557b260e05 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/MyApplication.java @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.accessibilityservice; + +import ohos.aafwk.ability.AbilityPackage; + +/** + * MyApplication + * + * @since 2021-07-23 + */ +public class MyApplication extends AbilityPackage { + @Override + public void onInitialize() { + super.onInitialize(); + } +} diff --git a/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/service/MyAccessibilityService.java b/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/service/MyAccessibilityService.java new file mode 100644 index 0000000000000000000000000000000000000000..020c49f4d1dd686c1b5146a28358aa9a225314cf --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/service/MyAccessibilityService.java @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.accessibilityservice.service; + +import ohos.accessibility.AccessibilityEventInfo; +import ohos.accessibility.ability.AccessibilityInfo; +import ohos.accessibility.ability.AccessibleAbility; +import ohos.agp.window.dialog.ToastDialog; +import ohos.global.resource.Element; +import ohos.global.resource.NotExistException; +import ohos.global.resource.WrongTypeException; +import ohos.multimodalinput.event.KeyEvent; +import ohos.samples.accessibilityservice.ResourceTable; +import ohos.samples.accessibilityservice.tts.TtsManager; +import ohos.samples.accessibilityservice.utils.LogUtils; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.Optional; + +/** + * MyAccessibilityService + * + * @since 2021-07-23 + */ +public class MyAccessibilityService extends AccessibleAbility { + private static final String TAG = MyAccessibilityService.class.getSimpleName(); + + private static final String BUNDLE_NAME = "ohos.samples.accessibilityservice"; + + private static boolean isNeedCustomVolumeKey = false; + + private boolean isInSpecialApp = false; + + @Override + public void onAccessibilityEvent(AccessibilityEventInfo accessibilityEventInfo) { + int eventType = accessibilityEventInfo.getAccessibilityEventType(); + int windowType = accessibilityEventInfo.getWindowChangeTypes(); + LogUtils.info(TAG, "onAccessibilityEvent,enventType=" + eventType + "windowType" + windowType); + switch (eventType) { + case AccessibilityEventInfo.TYPE_VIEW_CLICKED_EVENT: + LogUtils.info(TAG, "type view clicked"); + ArrayList contentList = accessibilityEventInfo.getContentList(); + try { + Element element = getContext().getResourceManager() + .getElement(ResourceTable.String_string_button_perform_click); + String string = element.getString(); + for (CharSequence content : contentList) { + LogUtils.info(TAG, "type view clicked,content:" + content.toString() + ",string:" + string); + if (content.toString().equals(string)) { + LogUtils.info(TAG, "type view clicked,content:equals"); + TtsManager.getInstance().speakText(content.toString(), null); + performCommonAction(AccessibleAbility.GLOBAL_ACTION_HOME); + } + } + } catch (IOException | NotExistException | WrongTypeException e) { + LogUtils.error(TAG, "type view clicked,Exception:" + e.getMessage()); + } + break; + default: + break; + } + switch (windowType) { + case AccessibilityEventInfo.WINDOWS_CHANGE_ADDED: + Optional accessibilityInfo = getRootAccessibilityInfo(); + String bundleName = accessibilityInfo.get().getBundleName().toString(); + LogUtils.info(TAG, "WINDOWS_CHANGE_ADDED:" + bundleName); + if (bundleName.equals(BUNDLE_NAME) && isInSpecialApp == false) { + isInSpecialApp = true; + LogUtils.info(TAG, "You are in the " + bundleName); + runUIThread(() -> new ToastDialog(getContext()).setText("You are in the " + BUNDLE_NAME).show()); + } + break; + case AccessibilityEventInfo.WINDOWS_CHANGE_REMOVED: + Optional accessibilityInfo1 = getRootAccessibilityInfo(); + LogUtils.info(TAG, "WINDOWS_CHANGE_REMOVED"); + if (!accessibilityInfo1.get().getBundleName().equals(BUNDLE_NAME)) { + isInSpecialApp = false; + } + break; + default: + break; + } + } + + public static void setNeedCustomVolumeKey(boolean needCustomVolumeKey) { + isNeedCustomVolumeKey = needCustomVolumeKey; + } + + @Override + public void onInterrupt() { + } + + private void runUIThread(Runnable runnable) { + getUITaskDispatcher().asyncDispatch(runnable); + } + + @Override + protected void onAbilityConnected() { + LogUtils.info(TAG, "onAbilityConnected"); + } + + @Override + protected boolean onKeyPressEvent(KeyEvent keyEvent) { + LogUtils.info(TAG, "onKeyPressEvent,isNeedCustomVolumeKey:" + isNeedCustomVolumeKey); + if (isNeedCustomVolumeKey) { + if (keyEvent.getKeyCode() == KeyEvent.KEY_VOLUME_DOWN || keyEvent.getKeyCode() == KeyEvent.KEY_VOLUME_UP) { + if (keyEvent.getKeyCode() == KeyEvent.KEY_VOLUME_DOWN && keyEvent.isKeyDown()) { + runUIThread(() -> { + try { + Class mClass = Class.forName("ohos.samples.accessibilityservice.slice.KeyPressEventSlice"); + mClass.getDeclaredMethod("scrollDown").invoke(mClass.newInstance(), new Object[]{}); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException + | ClassNotFoundException | InstantiationException e) { + LogUtils.error(TAG, "KEY_VOLUME_DOWN error:" + e.getMessage()); + } + }); + } + if (keyEvent.getKeyCode() == KeyEvent.KEY_VOLUME_UP && keyEvent.isKeyDown()) { + runUIThread(() -> { + try { + Class mClass = Class.forName("ohos.samples.accessibilityservice.slice.KeyPressEventSlice"); + mClass.getDeclaredMethod("scrollUp").invoke(mClass.newInstance(), new Object[]{}); + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException + | ClassNotFoundException | InstantiationException e) { + LogUtils.error(TAG, "KEY_VOLUME_UP error:" + e.getMessage()); + } + }); + } + return true; + } + } + return super.onKeyPressEvent(keyEvent); + } +} diff --git a/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/slice/KeyPressEventSlice.java b/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/slice/KeyPressEventSlice.java new file mode 100644 index 0000000000000000000000000000000000000000..cab0cb1fc37592abd4d91f60b1561fdca75148e3 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/slice/KeyPressEventSlice.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.accessibilityservice.slice; + +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.agp.components.Component; +import ohos.agp.components.TabList; +import ohos.samples.accessibilityservice.ResourceTable; +import ohos.samples.accessibilityservice.service.MyAccessibilityService; +import ohos.samples.accessibilityservice.utils.LogUtils; + +/** + * MainAbilitySlice + * + * @since 2021-07-23 + */ +public class KeyPressEventSlice extends AbilitySlice { + private static final String TAG = KeyPressEventSlice.class.getSimpleName(); + + private static TabList tabList; + + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setUIContent(ResourceTable.Layout_ability_key_press_event); + MyAccessibilityService.setNeedCustomVolumeKey(true); + initComponents(); + } + + /** + * scrollUp + */ + public void scrollUp() { + LogUtils.info(TAG, "scrollUp"); + if (tabList != null && tabList.getSelectedTabIndex() > 0) { + tabList.selectTabAt(tabList.getSelectedTabIndex() - 1); + } + } + + /** + * scrollDown + */ + public void scrollDown() { + LogUtils.info(TAG, "scrollDown"); + if (tabList != null && tabList.getSelectedTabIndex() < tabList.getTabCount() - 1) { + tabList.selectTabAt(tabList.getSelectedTabIndex() + 1); + } + } + + private void initComponents() { + Component component = findComponentById(ResourceTable.Id_tabList); + if (component instanceof TabList) { + tabList = (TabList) component; + } + for (int i = 0; i < 10; i++) { + TabList.Tab tab = tabList.new Tab(getContext()); + tab.setText(ResourceTable.String_text_content); + tabList.addTab(tab); + } + tabList.selectTabAt(0); + } + + + @Override + public void onActive() { + super.onActive(); + } + + @Override + public void onForeground(Intent intent) { + super.onForeground(intent); + } + + @Override + protected void onStop() { + super.onStop(); + tabList.removeAllComponents(); + MyAccessibilityService.setNeedCustomVolumeKey(false); + } +} diff --git a/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/slice/MainAbilitySlice.java b/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/slice/MainAbilitySlice.java new file mode 100644 index 0000000000000000000000000000000000000000..ad3f7e51fa4e7bee59dc2ba66c4136b3236355c2 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/slice/MainAbilitySlice.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.accessibilityservice.slice; + +import ohos.agp.components.Component; +import ohos.agp.window.dialog.ToastDialog; +import ohos.aafwk.ability.AbilitySlice; +import ohos.aafwk.content.Intent; +import ohos.samples.accessibilityservice.ResourceTable; +import ohos.samples.accessibilityservice.tts.TtsManager; + +/** + * MainAbilitySlice + * + * @since 2021-07-23 + */ +public class MainAbilitySlice extends AbilitySlice { + @Override + public void onStart(Intent intent) { + super.onStart(intent); + super.setUIContent(ResourceTable.Layout_ability_main); + TtsManager.getInstance().init(this); + initComponents(); + } + + private void initComponents() { + Component performBtn = findComponentById(ResourceTable.Id_perform_button); + performBtn.setClickedListener(this::onClickListener); + Component keyEventButton = findComponentById(ResourceTable.Id_keyPressEvent_button); + keyEventButton.setClickedListener(this::enterKeyPressEventPage); + } + + private void enterKeyPressEventPage(Component component) { + present(new KeyPressEventSlice(), new Intent()); + } + + private void onClickListener(Component component) { + String tips = getString(ResourceTable.String_string_button_perform_click); + new ToastDialog(getContext()).setText(tips).show(); + } + + @Override + public void onActive() { + super.onActive(); + } + + @Override + public void onForeground(Intent intent) { + super.onForeground(intent); + } + + @Override + protected void onStop() { + super.onStop(); + TtsManager.getInstance().destroy(); + } +} diff --git a/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/tts/TtsManager.java b/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/tts/TtsManager.java new file mode 100644 index 0000000000000000000000000000000000000000..167040b69d3163836bc52b2517e6f67f3692f924 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/tts/TtsManager.java @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.accessibilityservice.tts; + +import ohos.ai.tts.TtsClient; +import ohos.ai.tts.TtsListener; +import ohos.ai.tts.TtsParams; +import ohos.ai.tts.constants.TtsEvent; +import ohos.app.Context; +import ohos.data.distributed.common.KvManagerConfig; +import ohos.data.distributed.common.KvManagerFactory; +import ohos.samples.accessibilityservice.utils.LogUtils; +import ohos.utils.PacMap; + +/** + * TtsManager + * + * @since 2021-07-23 + */ +public class TtsManager { + private static final String TAG = TtsManager.class.getSimpleName(); + + private static TtsManager instance; + + private final TtsClient ttsClient; + + private Context mContext; + + private final TtsListener tsListener = new TtsListener() { + @Override + public void onStart(String s) { + LogUtils.info(TAG, "TtsListener,onStart:" + s); + } + + @Override + public void onProgress(String s, byte[] bytes, int i) { + LogUtils.info(TAG, "TtsListener,onProgress:" + s); + } + + @Override + public void onFinish(String s) { + LogUtils.info(TAG, "TtsListener,onFinish:" + s); + } + + @Override + public void onError(String s, String s1) { + LogUtils.info(TAG, "TtsListener,onError:" + s); + } + + @Override + public void onEvent(int event, PacMap pacMap) { + LogUtils.info(TAG, "TtsListener,onEvent:" + event); + if (event == TtsEvent.CREATE_TTS_CLIENT_SUCCESS) { + TtsParams ttsParams = new TtsParams(); + String deviceId = KvManagerFactory.getInstance() + .createKvManager(new KvManagerConfig((mContext))).getLocalDeviceInfo().getId(); + ttsParams.setDeviceId(deviceId); + ttsClient.init(ttsParams); + } + } + + @Override + public void onSpeechStart(String s) { + LogUtils.info(TAG, "TtsListener,onSpeechStart:" + s); + } + + @Override + public void onSpeechProgressChanged(String s, int i) { + LogUtils.info(TAG, "TtsListener,onSpeechProgressChanged:" + s); + } + + @Override + public void onSpeechFinish(String s) { + LogUtils.info(TAG, "TtsListener,onSpeechFinish:" + s); + } + }; + + /** + * getInstance + * + * @return TtsManager + */ + public static TtsManager getInstance() { + if (instance == null) { + synchronized (TtsManager.class) { + instance = new TtsManager(); + } + } + return instance; + } + + TtsManager() { + ttsClient = TtsClient.getInstance(); + } + + /** + * log error + * + * @param context Context + */ + public void init(Context context) { + mContext = context; + ttsClient.create(context, tsListener); + } + + /** + * log error + * + * @param text String + * @param utteranceId String + */ + public void speakText(String text, String utteranceId) { + ttsClient.speakText(text, utteranceId); + } + + /** + * destroy + */ + public void destroy() { + ttsClient.destroy(); + } +} diff --git a/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/utils/LogUtils.java b/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/utils/LogUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..59363ca8fc1afa7993eb039d0ead271450a30564 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/java/ohos/samples/accessibilityservice/utils/LogUtils.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ohos.samples.accessibilityservice.utils; + +import ohos.hiviewdfx.HiLog; +import ohos.hiviewdfx.HiLogLabel; + +import java.util.Objects; + +/** + * LogUtils + * + * @since 2021-07-23 + */ +public class LogUtils { + private static final String APP_NAME = "AccessibilityService"; + private static final HiLogLabel LABAL_LOG = new HiLogLabel(3, 0xD000f00, APP_NAME); + private static final String LOG_FORMAT = "%{public}s:%{public}s"; + + private LogUtils() { + } + + /** + * log info + * + * @param className String + * @param msg String + */ + public static void info(String className, String msg) { + HiLog.info(LABAL_LOG, LOG_FORMAT, getLogPrefix(className), msg); + } + + /** + * log error + * + * @param className String + * @param msg String + */ + public static void error(String className, String msg) { + HiLog.error(LABAL_LOG, LOG_FORMAT, getLogPrefix(className), msg); + } + + private static String getLogPrefix(String tag) { + Thread currentThread = Thread.currentThread(); + String threadName = currentThread.getName(); + StackTraceElement[] stackTraceElements = currentThread.getStackTrace(); + int lineNum = 0; + for (int index = 0; index < stackTraceElements.length; index++) { + if (Objects.equals(stackTraceElements[index].getClassName(), LogUtils.class.getName()) + && stackTraceElements.length > index + 1 + && !Objects.equals(stackTraceElements[index + 1].getClassName(), LogUtils.class.getName())) { + lineNum = stackTraceElements[index + 1].getLineNumber(); + } + } + return "[" + threadName + "]" + "(" + tag + ".java" + lineNum + ")"; + } +} diff --git a/AccessibilityService/AccessibilityService/entry/src/main/resources/base/element/float.json b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/element/float.json new file mode 100644 index 0000000000000000000000000000000000000000..c9a3fdc5cd5f813cfe5cf31d91a91de9a82f84f9 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/element/float.json @@ -0,0 +1,28 @@ +{ + "float": [ + { + "name": "layout_padding", + "value": "20vp" + }, + { + "name": "button_height", + "value": "40vp" + }, + { + "name": "button_text_size", + "value": "18fp" + }, + { + "name": "tips_text_size", + "value": "17fp" + }, + { + "name": "content_text_size", + "value": "18fp" + }, + { + "name": "layout_margin", + "value": "20vp" + } + ] +} \ No newline at end of file diff --git a/AccessibilityService/AccessibilityService/entry/src/main/resources/base/element/pattern.json b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/element/pattern.json new file mode 100644 index 0000000000000000000000000000000000000000..6ab70783157d6f75ce5ac6a00e53639eb04e09b8 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/element/pattern.json @@ -0,0 +1,43 @@ +{ + "pattern": [ + { + "name": "button_base", + "value": [ + { + "name": "width", + "value": "200vp" + }, + { + "name": "height", + "value": "$float:button_height" + }, + { + "name": "text_size", + "value": "$float:button_text_size" + }, + { + "name": "margin", + "value": "20vp" + }, + { + "name": "layout_alignment", + "value": "horizontal_center" + } + ] + }, + { + "name": "button_blue", + "parent": "button_base", + "value": [ + { + "name": "background_element", + "value": "$graphic:button_blue" + }, + { + "name": "text_color", + "value": "#FFFFFF" + } + ] + } + ] +} \ No newline at end of file diff --git a/AccessibilityService/AccessibilityService/entry/src/main/resources/base/element/string.json b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..ef0d88e10eb5233c715c013ed413eb21c04946d1 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/element/string.json @@ -0,0 +1,48 @@ +{ + "string": [ + { + "name": "entry_MainAbility", + "value": "AccessibleAbility" + }, + { + "name": "mainability_description", + "value": "AccessibleAbility" + }, + { + "name": "accessibility_service_description", + "value": "accessibleService" + }, + { + "name": "mainability_HelloWorld", + "value": "Hello World" + }, + { + "name": "string_button_perform_click", + "value": "Click,back to home" + }, + { + "name": "string_button_accessibility_settings_click", + "value": "goto AccessibilitySettings" + }, + { + "name": "Tips1", + "value": "Go Settings-Accessibility features-Accessbility-Installed services,enable AccessibleAbility,then back to this application." + }, + { + "name": "Tips2", + "value": "After turning on the accessibility service switch:\n1. There will be a prompt when you listen to the application interface\n2. Click the button to perform the home button operation\n3. The volume key operation of the system key event interface will execute the text scroll up and down to select" + }, + { + "name": "string_button_key_press_event", + "value": "KeyPressEventPage" + }, + { + "name": "key_event_tips", + "value": "Press the volume up button to scroll up, press the volume down button to scroll down." + }, + { + "name": "text_content", + "value": "This is a text content." + } + ] +} \ No newline at end of file diff --git a/AccessibilityService/AccessibilityService/entry/src/main/resources/base/graphic/background_ability_main.xml b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/graphic/background_ability_main.xml new file mode 100644 index 0000000000000000000000000000000000000000..44fb1387798e43f7b7fdf3f4886d8213bcc2618b --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/graphic/background_ability_main.xml @@ -0,0 +1,20 @@ + + + + + \ No newline at end of file diff --git a/AccessibilityService/AccessibilityService/entry/src/main/resources/base/graphic/button_blue.xml b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/graphic/button_blue.xml new file mode 100644 index 0000000000000000000000000000000000000000..c7ac8a58fa538cb49c2002099764577c5465bfe3 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/graphic/button_blue.xml @@ -0,0 +1,22 @@ + + + + + + \ No newline at end of file diff --git a/AccessibilityService/AccessibilityService/entry/src/main/resources/base/graphic/button_blue_empty.xml b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/graphic/button_blue_empty.xml new file mode 100644 index 0000000000000000000000000000000000000000..e90b693d2efa580d4767d55250f0e5a2c6ab5958 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/graphic/button_blue_empty.xml @@ -0,0 +1,21 @@ + + + + + + \ No newline at end of file diff --git a/AccessibilityService/AccessibilityService/entry/src/main/resources/base/graphic/button_blue_pressed.xml b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/graphic/button_blue_pressed.xml new file mode 100644 index 0000000000000000000000000000000000000000..fe2343b5ec2010eb2435bcee605b37b0c3f29d97 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/graphic/button_blue_pressed.xml @@ -0,0 +1,21 @@ + + + + + + \ No newline at end of file diff --git a/AccessibilityService/AccessibilityService/entry/src/main/resources/base/layout/ability_key_press_event.xml b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/layout/ability_key_press_event.xml new file mode 100644 index 0000000000000000000000000000000000000000..8d124272584dc2899ddacedcf69811e284242960 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/layout/ability_key_press_event.xml @@ -0,0 +1,40 @@ + + + + + + \ No newline at end of file diff --git a/AccessibilityService/AccessibilityService/entry/src/main/resources/base/layout/ability_main.xml b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/layout/ability_main.xml new file mode 100644 index 0000000000000000000000000000000000000000..7fb2b26615a032991de0aae82328a26c8b392ce8 --- /dev/null +++ b/AccessibilityService/AccessibilityService/entry/src/main/resources/base/layout/ability_main.xml @@ -0,0 +1,55 @@ + + + + + + + + +