1ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal/*
2ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal * Copyright (C) 2014 The Android Open Source Project
3ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal *
4ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal * Licensed under the Apache License, Version 2.0 (the "License");
5ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal * you may not use this file except in compliance with the License.
6ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal * You may obtain a copy of the License at
7ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal *
8ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal *      http://www.apache.org/licenses/LICENSE-2.0
9ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal *
10ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal * Unless required by applicable law or agreed to in writing, software
11ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal * distributed under the License is distributed on an "AS IS" BASIS,
12ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal * See the License for the specific language governing permissions and
14ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal * limitations under the License.
15ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal */
16ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal
17ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyalpackage com.android.launcher3.compat;
18ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal
19ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyalimport android.appwidget.AppWidgetProviderInfo;
202e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyalimport android.content.ComponentName;
21ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyalimport android.content.Context;
22ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyalimport android.os.Bundle;
23ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyalimport android.os.UserHandle;
24ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyalimport android.os.UserManager;
2586222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickhamimport android.support.annotation.Nullable;
26ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal
275940042d39b576553c2499bcf3d0641281e6ad52Adam Cohenimport com.android.launcher3.LauncherAppWidgetProviderInfo;
2864a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyalimport com.android.launcher3.config.FeatureFlags;
292e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyalimport com.android.launcher3.util.ComponentKey;
3086222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickhamimport com.android.launcher3.util.PackageUserKey;
31ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal
32ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyalimport java.util.ArrayList;
3364a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyalimport java.util.Collections;
342e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyalimport java.util.HashMap;
3586222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickhamimport java.util.Iterator;
36ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyalimport java.util.List;
37ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal
38ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyalclass AppWidgetManagerCompatVL extends AppWidgetManagerCompat {
39ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal
40ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal    private final UserManager mUserManager;
41ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal
42ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal    AppWidgetManagerCompatVL(Context context) {
43ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal        super(context);
44ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal        mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
45ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal    }
46ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal
47ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal    @Override
4886222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham    public List<AppWidgetProviderInfo> getAllProviders(@Nullable PackageUserKey packageUser) {
4964a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyal        if (FeatureFlags.GO_DISABLE_WIDGETS) {
5064a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyal            return Collections.emptyList();
5164a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyal        }
5286222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham        if (packageUser == null) {
5386222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham            ArrayList<AppWidgetProviderInfo> providers = new ArrayList<AppWidgetProviderInfo>();
5486222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham            for (UserHandle user : mUserManager.getUserProfiles()) {
5586222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham                providers.addAll(mAppWidgetManager.getInstalledProvidersForProfile(user));
5686222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham            }
5786222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham            return providers;
5886222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham        }
5986222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham        // Only get providers for the given package/user.
6086222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham        List<AppWidgetProviderInfo> providers = new ArrayList<>(mAppWidgetManager
6186222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham                .getInstalledProvidersForProfile(packageUser.mUser));
6286222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham        Iterator<AppWidgetProviderInfo> iterator = providers.iterator();
6386222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham        while (iterator.hasNext()) {
6486222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham            if (!iterator.next().provider.getPackageName().equals(packageUser.mPackageName)) {
6586222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham                iterator.remove();
6686222d23e28a09a7a26e7a0d4b4830ded2efb384Tony Wickham            }
67ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal        }
68ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal        return providers;
69ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal    }
70ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal
71ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal    @Override
72ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal    public boolean bindAppWidgetIdIfAllowed(int appWidgetId, AppWidgetProviderInfo info,
73ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal            Bundle options) {
7464a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyal        if (FeatureFlags.GO_DISABLE_WIDGETS) {
7564a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyal            return false;
7664a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyal        }
77ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal        return mAppWidgetManager.bindAppWidgetIdIfAllowed(
78ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal                appWidgetId, info.getProfile(), info.provider, options);
79ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal    }
80ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal
81ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal    @Override
827c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal    public LauncherAppWidgetProviderInfo findProvider(ComponentName provider, UserHandle user) {
8364a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyal        if (FeatureFlags.GO_DISABLE_WIDGETS) {
8464a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyal            return null;
8564a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyal        }
8664a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyal        for (AppWidgetProviderInfo info :
8764a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyal                getAllProviders(new PackageUserKey(provider.getPackageName(), user))) {
882e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal            if (info.provider.equals(provider)) {
892e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal                return LauncherAppWidgetProviderInfo.fromProviderInfo(mContext, info);
902e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal            }
912e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal        }
922e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal        return null;
932e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal    }
942e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal
952e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal    @Override
962e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal    public HashMap<ComponentKey, AppWidgetProviderInfo> getAllProvidersMap() {
972e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal        HashMap<ComponentKey, AppWidgetProviderInfo> result = new HashMap<>();
9864a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyal        if (FeatureFlags.GO_DISABLE_WIDGETS) {
9964a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyal            return result;
10064a75aa305bdd1ca8f22d2b48dedc5cada8fabc5Sunny Goyal        }
1012e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal        for (UserHandle user : mUserManager.getUserProfiles()) {
1022e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal            for (AppWidgetProviderInfo info :
1032e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal                    mAppWidgetManager.getInstalledProvidersForProfile(user)) {
1047c74e4ae641e76f73d74348e293c244a157f6585Sunny Goyal                result.put(new ComponentKey(info.provider, user), info);
1052e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal            }
1062e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal        }
1072e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal        return result;
1082e1efb480a9b77a97cb623d4f5faf6802a417422Sunny Goyal    }
109ffe83f13319cdb833a25596d47cf1af098cc4c60Sunny Goyal}
110