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  */
16 package com.android.launcher3.widget;
17 
18 import android.appwidget.AppWidgetManager;
19 import android.appwidget.AppWidgetProviderInfo;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 import com.android.launcher3.ItemInfo;
26 import com.android.launcher3.Launcher;
27 import com.android.launcher3.LauncherAppWidgetInfo;
28 import com.android.launcher3.LauncherAppWidgetProviderInfo;
29 import com.android.launcher3.compat.AppWidgetManagerCompat;
30 import com.android.launcher3.util.PendingRequestArgs;
31 
32 /**
33  * Utility class to handle app widget add flow.
34  */
35 public class WidgetAddFlowHandler implements Parcelable {
36 
37     private final AppWidgetProviderInfo mProviderInfo;
38 
WidgetAddFlowHandler(AppWidgetProviderInfo providerInfo)39     public WidgetAddFlowHandler(AppWidgetProviderInfo providerInfo) {
40         mProviderInfo = providerInfo;
41     }
42 
WidgetAddFlowHandler(Parcel parcel)43     protected WidgetAddFlowHandler(Parcel parcel) {
44         mProviderInfo = AppWidgetProviderInfo.CREATOR.createFromParcel(parcel);
45     }
46 
47     @Override
describeContents()48     public int describeContents() {
49         return 0;
50     }
51 
52     @Override
writeToParcel(Parcel parcel, int i)53     public void writeToParcel(Parcel parcel, int i) {
54         mProviderInfo.writeToParcel(parcel, i);
55     }
56 
startBindFlow(Launcher launcher, int appWidgetId, ItemInfo info, int requestCode)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      */
startConfigActivity(Launcher launcher, LauncherAppWidgetInfo info, int requestCode)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      */
startConfigActivity(Launcher launcher, int appWidgetId, ItemInfo info, int requestCode)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 
needsConfigure()94     public boolean needsConfigure() {
95         return mProviderInfo.configure != null;
96     }
97 
getProviderInfo(Context context)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