Home | History | Annotate | Download | only in widget
      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.AppWidgetProviderInfo;
     19 import android.content.Context;
     20 import android.os.Parcel;
     21 import android.os.Parcelable;
     22 
     23 import com.android.launcher3.ItemInfo;
     24 import com.android.launcher3.Launcher;
     25 import com.android.launcher3.LauncherAppWidgetInfo;
     26 import com.android.launcher3.LauncherAppWidgetProviderInfo;
     27 import com.android.launcher3.util.PendingRequestArgs;
     28 
     29 /**
     30  * Utility class to handle app widget add flow.
     31  */
     32 public class WidgetAddFlowHandler implements Parcelable {
     33 
     34     private final AppWidgetProviderInfo mProviderInfo;
     35 
     36     public WidgetAddFlowHandler(AppWidgetProviderInfo providerInfo) {
     37         mProviderInfo = providerInfo;
     38     }
     39 
     40     protected WidgetAddFlowHandler(Parcel parcel) {
     41         mProviderInfo = AppWidgetProviderInfo.CREATOR.createFromParcel(parcel);
     42     }
     43 
     44     @Override
     45     public int describeContents() {
     46         return 0;
     47     }
     48 
     49     @Override
     50     public void writeToParcel(Parcel parcel, int i) {
     51         mProviderInfo.writeToParcel(parcel, i);
     52     }
     53 
     54     public void startBindFlow(Launcher launcher, int appWidgetId, ItemInfo info, int requestCode) {
     55         launcher.setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, this, info));
     56         launcher.getAppWidgetHost()
     57                 .startBindFlow(launcher, appWidgetId, mProviderInfo, requestCode);
     58     }
     59 
     60     /**
     61      * @see #startConfigActivity(Launcher, int, ItemInfo, int)
     62      */
     63     public boolean startConfigActivity(Launcher launcher, LauncherAppWidgetInfo info,
     64             int requestCode) {
     65         return startConfigActivity(launcher, info.appWidgetId, info, requestCode);
     66     }
     67 
     68     /**
     69      * Starts the widget configuration flow if needed.
     70      * @return true if the configuration flow was started, false otherwise.
     71      */
     72     public boolean startConfigActivity(Launcher launcher, int appWidgetId, ItemInfo info,
     73             int requestCode) {
     74         if (!needsConfigure()) {
     75             return false;
     76         }
     77         launcher.setWaitingForResult(PendingRequestArgs.forWidgetInfo(appWidgetId, this, info));
     78         launcher.getAppWidgetHost().startConfigActivity(launcher, appWidgetId, requestCode);
     79         return true;
     80     }
     81 
     82     public boolean needsConfigure() {
     83         return mProviderInfo.configure != null;
     84     }
     85 
     86     public LauncherAppWidgetProviderInfo getProviderInfo(Context context) {
     87         return LauncherAppWidgetProviderInfo.fromProviderInfo(context, mProviderInfo);
     88     }
     89 
     90     public static final Parcelable.Creator<WidgetAddFlowHandler> CREATOR =
     91             new Parcelable.Creator<WidgetAddFlowHandler>() {
     92                 public WidgetAddFlowHandler createFromParcel(Parcel source) {
     93                     return new WidgetAddFlowHandler(source);
     94                 }
     95 
     96                 public WidgetAddFlowHandler[] newArray(int size) {
     97                     return new WidgetAddFlowHandler[size];
     98                 }
     99             };
    100 }
    101