辅助功能移除桌面快捷方式

系统分析

Posted by JovenHe on September 13, 2021

在之前的创建快捷方式中,没有做到移除,这显然不符合一个完整功能的实现。

但在查api的时候还真没找到固定快捷方式的移除,只好从辅助功能的方式来实现移除了

当要删除一个桌面快捷方式的时候,首先查看是否有这个快捷方式

    /**
     * 是否有该快捷方式
     */
    fun hasShortcut(context: Context, id: String): Boolean{
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            val shortcutManager:ShortcutManager = context.getSystemService(ShortcutManager::class.java)
            for (pinnedShortcut in shortcutManager.pinnedShortcuts) {
                val hasId = pinnedShortcut.intent?.extras?.keySet()?.contains(ShortcutActivity.Id) ?: false
                if (hasId) {
                    if (pinnedShortcut.intent?.extras?.get(ShortcutActivity.Id) as String == id) {
                        pinnedShortcut.isEnabled
                        return true
                    }
                }
            }
        }
        return false
    }

如果有的话再禁用

    /**
     * 禁用快捷方式
     */
    fun deleteShortcut(context: Context, id: String) {
        Prefs.removeDevShortcuts(context, id)
        if (hasShortcut(context, id)) {
            setShortcutEnabled(context, id, false)
        }
    }
   /**
     * 禁用快捷方式
     */
    private fun setShortcutEnabled(context: Context, id: String, enable: Boolean) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
            val shortcutManager: ShortcutManager = context.getSystemService(ShortcutManager::class.java)
            //禁用
            if (enable) {
                shortcutManager.enableShortcuts(mutableListOf(id))
            } else {
                shortcutManager.disableShortcuts(mutableListOf(id))
            }
        }
    }

禁用后,桌面快捷方式就不能点击了,不同品牌手机有不同效果,有的图标变灰,有的没变化。接下来就是通过辅助功能移除该快捷方式

if (mLauncher.isEmpty()) {
    mLauncher= DevUtil.getLauncherPackageName(this);
    XLog.d(TAG, "onAccessibilityEvent: mLauncher="+mLauncher);
}
if (mLauncher.equals(packageName) ) {
    List<String> delShortcuts = Prefs.getDelShortcuts(mContext);
    if (!delShortcuts.isEmpty()) {
        XLog.d(TAG, "onAccessibilityEvent: delShortcuts="+ Arrays.toString(delShortcuts.toArray()));
        long time = TimeUtils.getNowMills() - mClickTime;
        if (mClickTime > 0 && time > 1000L) {
            SysManagerHelper.getInstance(this).performDelShortcutAction(this, mRootNode);
            SysManagerHelper.getInstance(this).performRemoveShortcutAction(this, mRootNode);
        }
        mClickTime = TimeUtils.getNowMills();
    } else {
        SysManagerHelper.getInstance(this).performCancelRemoveShortcutAction(this, mRootNode);

    }
}
public void performDelShortcutAction(AccessibilityService service, AccessibilityNodeInfo mRootNode) {
    //快捷方式长按
    XLog.d(TAG, "performDelShortcutAction: ");
    List<String> delShortcuts = Prefs.getDelShortcuts(mContext);
    for (String delShortcut : delShortcuts) {
        XLog.d(TAG, "performDelShortcutAction: " + delShortcut);
        List<AccessibilityNodeInfo> accessibilityNodeInfosByText = mRootNode.findAccessibilityNodeInfosByText(delShortcut);
        if (!accessibilityNodeInfosByText.isEmpty()) {
            XLog.d(TAG, "performDelShortcutAction: accessibilityNodeInfosByText" + accessibilityNodeInfosByText.size());
            for (AccessibilityNodeInfo accessibilityNodeInfo : accessibilityNodeInfosByText) {
                if (accessibilityNodeInfo.getText() != null && delShortcut.equals(accessibilityNodeInfo.getText().toString())) {
                    dispatchGestureLongClick(service, getCanClickAble(accessibilityNodeInfo));
                    XLog.d(TAG, "performDelShortcutAction: click");
                    Prefs.setLongClickShortcut(mContext, delShortcut);
                }
            }
        }
    }
}

public void performRemoveShortcutAction(AccessibilityService service, AccessibilityNodeInfo mRootNode) {
    //快捷方式自动移除
    XLog.d(TAG, "performRemoveShortcutAction: ");
    String longClickShortcut = Prefs.getLongClickShortcut(mContext);
    XLog.d(TAG, "performRemoveShortcutAction: longClickShortcut=" + longClickShortcut);
    if (!longClickShortcut.isEmpty()) {
        List<AccessibilityNodeInfo> removeInfos = mRootNode.findAccessibilityNodeInfosByText("移除");
        XLog.d(TAG, "performRemoveShortcutAction: removeInfos=" + removeInfos.size());
        if (!removeInfos.isEmpty()) {
            getCanClickAble(removeInfos.get(0)).performAction(AccessibilityNodeInfo.ACTION_CLICK);
            Prefs.removeDelShortcuts(mContext, Arrays.asList(longClickShortcut));
            Prefs.setLongClickShortcut(mContext, "");
        }
    }
}

public void performCancelRemoveShortcutAction(AccessibilityService service, AccessibilityNodeInfo mRootNode) {
    //快捷方式用户移除监听
    XLog.d(TAG, "performCancelRemoveShortcutAction: ");
    List<AccessibilityNodeInfo> removeInfos = mRootNode.findAccessibilityNodeInfosByText("移除");
    if (!removeInfos.isEmpty()) {
        hasRemoveShortcut = true;
    } else if (hasRemoveShortcut && !Prefs.getDevShortcuts(mContext).isEmpty()) {
        //检查
        AppUtil.INSTANCE.checkShortcut(mContext);
    }
}

先获取Launcher包名,处于launcher时,获取是否有要移除的桌面快捷方式,如果有的话,长按-移除