问题背景
测试反馈,无障碍快捷方式和setting里的无障碍图标不一致。
无障碍快捷方式悬浮窗
1、悬浮窗在systemui中
frameworks\base\packages\SystemUI\src\com\android\systemui\accessibility\floatingmenu\AccessibilityTargetAdapter.java
图标获取方式:
java
final AccessibilityTarget target = mTargets.get(position);
holder.mIconView.setBackground(target.getIcon());
frameworks\base\packages\SystemUI\src\com\android\systemui\accessibility\floatingmenu\MenuInfoRepository.java
java
void loadMenuTargetFeatures(OnInfoReady<List<AccessibilityTarget>> callback) {
callback.onReady(getTargets(mContext, ACCESSIBILITY_BUTTON));
}
2、数据来源
frameworks\base\core\java\com\android\internal\accessibility\dialog\AccessibilityTargetHelper.java
java
public static List<AccessibilityTarget> getTargets(Context context,@ShortcutType int shortcutType)
AccessibilityTarget Icon数据来自
frameworks\base\core\java\com\android\internal\accessibility\dialog\AccessibilityServiceTarget.java
java
AccessibilityServiceTarget(Context context, @ShortcutType int shortcutType,
@AccessibilityFragmentType int fragmentType,
@NonNull AccessibilityServiceInfo serviceInfo) {
super(context,
shortcutType,
fragmentType,
isShortcutContained(context, shortcutType,
serviceInfo.getComponentName().flattenToString()),
serviceInfo.getComponentName().flattenToString(),
serviceInfo.getResolveInfo().serviceInfo.applicationInfo.uid,
serviceInfo.getResolveInfo().loadLabel(context.getPackageManager()),
serviceInfo.getResolveInfo().loadIcon(context.getPackageManager()),
convertToKey(convertToUserType(shortcutType)));
}
由此可见,systemui里直接显示的支持无障碍Service/activity的原图。
setting中的无障碍菜单
Settings\src\com\android\settings\accessibility\RestrictedPreferenceHelper.java
java
public List<RestrictedPreference> createAccessibilityServicePreferenceList(
List<AccessibilityServiceInfo> installedServices) {
Drawable icon = resolveInfo.loadIcon(mPm);
}
setting里获取方式和systemUI一样。都是通过service的resolveInfo.loadIcon(mPm);
区别:
setting里把Drawable转换成了AdaptiveIcon,再传给了preference。所以显示圆角。
java
private RestrictedPreference createRestrictedPreference(){
preference.setIcon(Utils.getAdaptiveIcon(mContext, icon, Color.WHITE));
}
创建demo显示在无障碍
是通过android.accessibilityservice.AccessibilityService注册然后系统自动加载进去的。
AndroidManifest.xml
java
<service
android:name=".MyAccessibilityService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:enabled="true"
android:exported="false">
<intent-filter>
<action
android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config" />
</service>
res/xml/accessibility_service_config.xml
java
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/setting_accessibility_tip"
android:accessibilityEventTypes="typeWindowStateChanged"
android:packageNames="com.whatsapp"
android:accessibilityFeedbackType="feedbackGeneric"
android:notificationTimeout="100"
android:accessibilityFlags="flagDefault"
android:canRetrieveWindowContent="true"/>