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 int mQsbWidgetId = -1; 41 private Launcher mLauncher; 42 43 public LauncherAppWidgetHost(Launcher launcher, int hostId) { 44 super(launcher, hostId); 45 mLauncher = launcher; 46 } 47 48 public void setQsbWidgetId(int widgetId) { 49 mQsbWidgetId = widgetId; 50 } 51 52 @Override 53 protected AppWidgetHostView onCreateView(Context context, int appWidgetId, 54 AppWidgetProviderInfo appWidget) { 55 if (appWidgetId == mQsbWidgetId) { 56 return new LauncherAppWidgetHostView(context) { 57 58 @Override 59 protected View getErrorView() { 60 // For the QSB, show an empty view instead of an error view. 61 return new View(getContext()); 62 } 63 }; 64 } 65 return new LauncherAppWidgetHostView(context); 66 } 67 68 @Override 69 public void startListening() { 70 try { 71 super.startListening(); 72 } catch (Exception e) { 73 if (e.getCause() instanceof TransactionTooLargeException || 74 e.getCause() instanceof DeadObjectException) { 75 // We're willing to let this slide. The exception is being caused by the list of 76 // RemoteViews which is being passed back. The startListening relationship will 77 // have been established by this point, and we will end up populating the 78 // widgets upon bind anyway. See issue 14255011 for more context. 79 } else { 80 throw new RuntimeException(e); 81 } 82 } 83 } 84 85 @Override 86 public void stopListening() { 87 super.stopListening(); 88 clearViews(); 89 } 90 91 public void addProviderChangeListener(Runnable callback) { 92 mProviderChangeListeners.add(callback); 93 } 94 95 public void removeProviderChangeListener(Runnable callback) { 96 mProviderChangeListeners.remove(callback); 97 } 98 99 protected void onProvidersChanged() { 100 if (!mProviderChangeListeners.isEmpty()) { 101 for (Runnable callback : new ArrayList<>(mProviderChangeListeners)) { 102 callback.run(); 103 } 104 } 105 106 if (Utilities.ATLEAST_MARSHMALLOW) { 107 mLauncher.notifyWidgetProvidersChanged(); 108 } 109 } 110 111 public AppWidgetHostView createView(Context context, int appWidgetId, 112 LauncherAppWidgetProviderInfo appWidget) { 113 if (appWidget.isCustomWidget) { 114 LauncherAppWidgetHostView lahv = new LauncherAppWidgetHostView(context); 115 LayoutInflater inflater = (LayoutInflater) 116 context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 117 inflater.inflate(appWidget.initialLayout, lahv); 118 lahv.setAppWidget(0, appWidget); 119 lahv.updateLastInflationOrientation(); 120 return lahv; 121 } else { 122 return super.createView(context, appWidgetId, appWidget); 123 } 124 } 125 126 /** 127 * Called when the AppWidget provider for a AppWidget has been upgraded to a new apk. 128 */ 129 @Override 130 protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidget) { 131 LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo( 132 mLauncher, appWidget); 133 super.onProviderChanged(appWidgetId, info); 134 // The super method updates the dimensions of the providerInfo. Update the 135 // launcher spans accordingly. 136 info.initSpans(); 137 } 138 } 139