• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static android.appwidget.AppWidgetProviderInfo.WIDGET_FEATURE_CONFIGURATION_OPTIONAL;
19 import static android.appwidget.AppWidgetProviderInfo.WIDGET_FEATURE_RECONFIGURABLE;
20 
21 import android.appwidget.AppWidgetProviderInfo;
22 import android.content.Context;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import com.android.launcher3.Launcher;
27 import com.android.launcher3.model.data.ItemInfo;
28 import com.android.launcher3.model.data.LauncherAppWidgetInfo;
29 import com.android.launcher3.util.PendingRequestArgs;
30 
31 /**
32  * Utility class to handle app widget add flow.
33  */
34 public class WidgetAddFlowHandler implements Parcelable {
35 
36     private final AppWidgetProviderInfo mProviderInfo;
37 
WidgetAddFlowHandler(AppWidgetProviderInfo providerInfo)38     public WidgetAddFlowHandler(AppWidgetProviderInfo providerInfo) {
39         mProviderInfo = providerInfo;
40     }
41 
WidgetAddFlowHandler(Parcel parcel)42     protected WidgetAddFlowHandler(Parcel parcel) {
43         mProviderInfo = AppWidgetProviderInfo.CREATOR.createFromParcel(parcel);
44     }
45 
46     @Override
describeContents()47     public int describeContents() {
48         return 0;
49     }
50 
51     @Override
writeToParcel(Parcel parcel, int i)52     public void writeToParcel(Parcel parcel, int i) {
53         mProviderInfo.writeToParcel(parcel, i);
54     }
55 
startBindFlow(Launcher launcher, int appWidgetId, ItemInfo info, int requestCode)56     public void startBindFlow(Launcher launcher, int appWidgetId, ItemInfo info, int requestCode) {
57         launcher.setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, this, info));
58         launcher.getAppWidgetHost()
59                 .startBindFlow(launcher, appWidgetId, mProviderInfo, requestCode);
60     }
61 
62     /**
63      * @see #startConfigActivity(Launcher, int, ItemInfo, int)
64      */
startConfigActivity(Launcher launcher, LauncherAppWidgetInfo info, int requestCode)65     public boolean startConfigActivity(Launcher launcher, LauncherAppWidgetInfo info,
66             int requestCode) {
67         return startConfigActivity(launcher, info.appWidgetId, info, requestCode);
68     }
69 
70     /**
71      * Starts the widget configuration flow if needed.
72      * @return true if the configuration flow was started, false otherwise.
73      */
startConfigActivity(Launcher launcher, int appWidgetId, ItemInfo info, int requestCode)74     public boolean startConfigActivity(Launcher launcher, int appWidgetId, ItemInfo info,
75             int requestCode) {
76         if (!needsConfigure()) {
77             return false;
78         }
79         launcher.setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, this, info));
80         launcher.getAppWidgetHost().startConfigActivity(launcher, appWidgetId, requestCode);
81         return true;
82     }
83 
84     /**
85      * Checks whether the widget needs configuration.
86      *
87      * A widget needs configuration if (1) it has a configuration activity and (2)
88      * it's configuration is not optional.
89      *
90      * @return true if the widget needs configuration, false otherwise.
91      */
needsConfigure()92     public boolean needsConfigure() {
93         int featureFlags = mProviderInfo.widgetFeatures;
94         // A widget's configuration is optional only if it's configuration is marked as optional AND
95         // it can be reconfigured later.
96         boolean configurationOptional = (featureFlags & WIDGET_FEATURE_CONFIGURATION_OPTIONAL) != 0
97                 && (featureFlags & WIDGET_FEATURE_RECONFIGURABLE) != 0;
98 
99         return mProviderInfo.configure != null && !configurationOptional;
100     }
101 
getProviderInfo(Context context)102     public LauncherAppWidgetProviderInfo getProviderInfo(Context context) {
103         return LauncherAppWidgetProviderInfo.fromProviderInfo(context, mProviderInfo);
104     }
105 
106     public static final Parcelable.Creator<WidgetAddFlowHandler> CREATOR =
107             new Parcelable.Creator<WidgetAddFlowHandler>() {
108                 public WidgetAddFlowHandler createFromParcel(Parcel source) {
109                     return new WidgetAddFlowHandler(source);
110                 }
111 
112                 public WidgetAddFlowHandler[] newArray(int size) {
113                     return new WidgetAddFlowHandler[size];
114                 }
115             };
116 }
117