index.uts 1.7 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
import Context from 'android.content.Context';
import AccessibilityManager from 'android.view.accessibility.AccessibilityManager';
import TextUtils from 'android.text.TextUtils';
import Settings from 'android.provider.Settings';
import ArrayList from 'java.util.ArrayList';
import AccessibilityServiceInfo from 'android.accessibilityservice.AccessibilityServiceInfo';

import { GetAccessibilityInfo } from '../interface.uts'


export const getAccessibilityInfo: GetAccessibilityInfo = (): UTSJSONObject => {
	let activity = UTSAndroid.getUniActivity();
	let service = activity!!.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
	let serviceList = service.getInstalledAccessibilityServiceList();
	let services = new Array<UTSJSONObject>()
	for (let i:Int = 0; i < serviceList.size as Int; i++) {
		if (isAccessibilitySettingsOn(activity!, serviceList.get(i).id)) {
			const tmp = serviceList.get(i) as AccessibilityServiceInfo;
			let info = {
				id: tmp.getId(),
				packageNames: tmp.packageNames
			}
			services.add(info);
		}
	}


	let result = {
		enabled: service.isEnabled(),
		services: services
	};

	return result;
}

function isAccessibilitySettingsOn(context: Context, service: string): boolean {
	let split: Char = ":".get(0);
	let mStringColonSplitter = new TextUtils.SimpleStringSplitter(split);
	let settingValue = Settings.Secure.getString(
		context.getApplicationContext().getContentResolver(),
		Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
	);
	if (settingValue != null) {
		mStringColonSplitter.setString(settingValue);
		while (mStringColonSplitter.hasNext()) {
			var accessibilityService = mStringColonSplitter.next() as string;
			if (accessibilityService == service) {
				return true;
			}
		}
	}
	return false;
}