在Launcher3的小部件中隐藏Widgets或Shortcuts

495 阅读1分钟

可参考文章:

https://blog.csdn.net/lyjIT/article/details/54377813

https://www.loongwind.com/archives/365.html

https://www.jb51.net/article/80232.htm

在WidgetsModel类的updateAndClone方法中,在两个for循环中添加条件判断

public WidgetsModel updateAndClone(Context context) {
    Preconditions.assertWorkerThread();
    try {
        final ArrayList<WidgetItem> widgetsAndShortcuts = new ArrayList<>();
        // Widgets
        AppWidgetManagerCompat widgetManager = AppWidgetManagerCompat.getInstance(context);
        for (AppWidgetProviderInfo widgetInfo : widgetManager.getAllProviders()) {
            //lijun add start
            if(widgetInfo.minWidth < 0 || widgetInfo.minHeight < 0) {
                Log.e("LAUNCHER_DEBUG", "WidgetsModel updateAndClone minWidth or minHeight < 0, widgetInfo label=" + widgetInfo.label + ", minWidth="
                        + widgetInfo.minWidth + ", minHeight=" + widgetInfo.minHeight + ", provider = " + widgetInfo.provider);
                continue;
            }
            Log.e("gem", "updateAndClone:---------- Widgets widgetInfo label=" + widgetInfo.label + ", minWidth="
                    + widgetInfo.minWidth + ", minHeight=" + widgetInfo.minHeight + ", provider = " + widgetInfo.provider);
            //lijun add end
            widgetsAndShortcuts.add(new WidgetItem(
                    LauncherAppWidgetProviderInfo.fromProviderInfo(context, widgetInfo),
                    widgetManager));
        }

        // Shortcuts
        PackageManager pm = context.getPackageManager();
        for (ResolveInfo info :
                pm.queryIntentActivities(new Intent(Intent.ACTION_CREATE_SHORTCUT), 0)) {
            Log.e("gem", "updateAndClone:---------- Shortcuts ResolveInfo packageName:"+info.activityInfo.packageName+" providerInfo:"+info.providerInfo+" toString"+info.toString());

            //dx add start : disable useless app shortcuts
            if(!TextUtils.isEmpty(info.activityInfo.packageName)&&(info.activityInfo.packageName.equals("com.netease.mail")//网易邮箱
                                        ||info.activityInfo.packageName.equals("com.alibaba.android.rimet")//阿里钉钉
            )) continue;
            //dx add end

            widgetsAndShortcuts.add(new WidgetItem(info, pm));
        }
        setWidgetsAndShortcuts(widgetsAndShortcuts);
    } catch (Exception e) {
        if (!ProviderConfig.IS_DOGFOOD_BUILD &&
                (e.getCause() instanceof TransactionTooLargeException ||
                        e.getCause() instanceof DeadObjectException)) {
            // the returned value may be incomplete and will not be refreshed until the next
            // time Launcher starts.
            // TODO: after figuring out a repro step, introduce a dirty bit to check when
            // onResume is called to refresh the widget provider list.
        } else {
            throw e;
        }
    }
    return clone();
}