1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.launcher3.widget;
17
18import android.appwidget.AppWidgetManager;
19import android.appwidget.AppWidgetProviderInfo;
20import android.content.Context;
21import android.content.Intent;
22import android.os.Parcel;
23import android.os.Parcelable;
24
25import com.android.launcher3.ItemInfo;
26import com.android.launcher3.Launcher;
27import com.android.launcher3.LauncherAppWidgetInfo;
28import com.android.launcher3.LauncherAppWidgetProviderInfo;
29import com.android.launcher3.compat.AppWidgetManagerCompat;
30import com.android.launcher3.util.PendingRequestArgs;
31
32/**
33 * Utility class to handle app widget add flow.
34 */
35public class WidgetAddFlowHandler implements Parcelable {
36
37    private final AppWidgetProviderInfo mProviderInfo;
38
39    public WidgetAddFlowHandler(AppWidgetProviderInfo providerInfo) {
40        mProviderInfo = providerInfo;
41    }
42
43    protected WidgetAddFlowHandler(Parcel parcel) {
44        mProviderInfo = AppWidgetProviderInfo.CREATOR.createFromParcel(parcel);
45    }
46
47    @Override
48    public int describeContents() {
49        return 0;
50    }
51
52    @Override
53    public void writeToParcel(Parcel parcel, int i) {
54        mProviderInfo.writeToParcel(parcel, i);
55    }
56
57    public void startBindFlow(Launcher launcher, int appWidgetId, ItemInfo info, int requestCode) {
58        launcher.setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, this, info));
59
60        Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
61        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
62        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, mProviderInfo.provider);
63        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE,
64                mProviderInfo.getProfile());
65        // TODO: we need to make sure that this accounts for the options bundle.
66        // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
67        launcher.startActivityForResult(intent, requestCode);
68    }
69
70    /**
71     * @see #startConfigActivity(Launcher, int, ItemInfo, int)
72     */
73    public boolean startConfigActivity(Launcher launcher, LauncherAppWidgetInfo info,
74            int requestCode) {
75        return startConfigActivity(launcher, info.appWidgetId, info, requestCode);
76    }
77
78    /**
79     * Starts the widget configuration flow if needed.
80     * @return true if the configuration flow was started, false otherwise.
81     */
82    public boolean startConfigActivity(Launcher launcher, int appWidgetId, ItemInfo info,
83            int requestCode) {
84        if (!needsConfigure()) {
85            return false;
86        }
87        launcher.setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, this, info));
88
89        AppWidgetManagerCompat.getInstance(launcher).startConfigActivity(
90                mProviderInfo, appWidgetId, launcher, launcher.getAppWidgetHost(), requestCode);
91        return true;
92    }
93
94    public boolean needsConfigure() {
95        return mProviderInfo.configure != null;
96    }
97
98    public LauncherAppWidgetProviderInfo getProviderInfo(Context context) {
99        return LauncherAppWidgetProviderInfo.fromProviderInfo(context, mProviderInfo);
100    }
101
102    public static final Parcelable.Creator<WidgetAddFlowHandler> CREATOR =
103            new Parcelable.Creator<WidgetAddFlowHandler>() {
104                public WidgetAddFlowHandler createFromParcel(Parcel source) {
105                    return new WidgetAddFlowHandler(source);
106                }
107
108                public WidgetAddFlowHandler[] newArray(int size) {
109                    return new WidgetAddFlowHandler[size];
110                }
111            };
112}
113