Home | History | Annotate | Download | only in launcher3
      1 /*
      2  * Copyright (C) 2009 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 
     17 package com.android.launcher3;
     18 
     19 import android.appwidget.AppWidgetHost;
     20 import android.appwidget.AppWidgetHostView;
     21 import android.appwidget.AppWidgetProviderInfo;
     22 import android.content.Context;
     23 import android.os.DeadObjectException;
     24 import android.os.TransactionTooLargeException;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 
     28 import java.util.ArrayList;
     29 
     30 
     31 /**
     32  * Specific {@link AppWidgetHost} that creates our {@link LauncherAppWidgetHostView}
     33  * which correctly captures all long-press events. This ensures that users can
     34  * always pick up and move widgets.
     35  */
     36 public class LauncherAppWidgetHost extends AppWidgetHost {
     37 
     38     private final ArrayList<Runnable> mProviderChangeListeners = new ArrayList<Runnable>();
     39 
     40     private Launcher mLauncher;
     41 
     42     public LauncherAppWidgetHost(Launcher launcher, int hostId) {
     43         super(launcher, hostId);
     44         mLauncher = launcher;
     45     }
     46 
     47     @Override
     48     protected AppWidgetHostView onCreateView(Context context, int appWidgetId,
     49             AppWidgetProviderInfo appWidget) {
     50         return new LauncherAppWidgetHostView(context);
     51     }
     52 
     53     @Override
     54     public void startListening() {
     55         try {
     56             super.startListening();
     57         } catch (Exception e) {
     58             if (e.getCause() instanceof TransactionTooLargeException ||
     59                     e.getCause() instanceof DeadObjectException) {
     60                 // We're willing to let this slide. The exception is being caused by the list of
     61                 // RemoteViews which is being passed back. The startListening relationship will
     62                 // have been established by this point, and we will end up populating the
     63                 // widgets upon bind anyway. See issue 14255011 for more context.
     64             } else {
     65                 throw new RuntimeException(e);
     66             }
     67         }
     68     }
     69 
     70     public void addProviderChangeListener(Runnable callback) {
     71         mProviderChangeListeners.add(callback);
     72     }
     73 
     74     public void removeProviderChangeListener(Runnable callback) {
     75         mProviderChangeListeners.remove(callback);
     76     }
     77 
     78     protected void onProvidersChanged() {
     79         if (!mProviderChangeListeners.isEmpty()) {
     80             for (Runnable callback : new ArrayList<>(mProviderChangeListeners)) {
     81                 callback.run();
     82             }
     83         }
     84 
     85         if (Utilities.ATLEAST_MARSHMALLOW) {
     86             mLauncher.notifyWidgetProvidersChanged();
     87         }
     88     }
     89 
     90     public AppWidgetHostView createView(Context context, int appWidgetId,
     91             LauncherAppWidgetProviderInfo appWidget) {
     92         if (appWidget.isCustomWidget) {
     93             LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context);
     94             LayoutInflater inflater = (LayoutInflater)
     95                     context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     96             inflater.inflate(appWidget.initialLayout, lahv);
     97             lahv.setAppWidget(0, appWidget);
     98             lahv.updateLastInflationOrientation();
     99             return lahv;
    100         } else {
    101             return super.createView(context, appWidgetId, appWidget);
    102         }
    103     }
    104 
    105     /**
    106      * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
    107      */
    108     @Override
    109     protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) {
    110         LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
    111                 mLauncher, appWidget);
    112         super.onProviderChanged(appWidgetId, info);
    113         // The super method updates the dimensions of the providerInfo. Update the
    114         // launcher spans accordingly.
    115         info.initSpans();
    116     }
    117 }
    118