diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java index be99641d38b9f..1a0eb24e1fb5a 100644 --- a/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java +++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsActivity.java @@ -33,6 +33,8 @@ import android.view.View; import android.view.ViewStub; import com.android.internal.logging.MetricsLogger; import com.android.systemui.R; +import com.android.systemui.recents.events.EventBus; +import com.android.systemui.recents.events.activity.AppWidgetProviderChangedEvent; import com.android.systemui.recents.misc.Console; import com.android.systemui.recents.misc.ReferenceCountedTrigger; import com.android.systemui.recents.misc.SystemServicesProxy; @@ -45,14 +47,12 @@ import com.android.systemui.recents.views.RecentsView; import com.android.systemui.recents.views.SystemBarScrimViews; import com.android.systemui.recents.views.ViewAnimation; -import java.lang.ref.WeakReference; import java.util.ArrayList; /** * The main Recents activity that is started from AlternateRecentsComponent. */ -public class RecentsActivity extends Activity implements RecentsView.RecentsViewCallbacks, - RecentsAppWidgetHost.RecentsAppWidgetHostCallbacks { +public class RecentsActivity extends Activity implements RecentsView.RecentsViewCallbacks { public final static int EVENT_BUS_PRIORITY = Recents.EVENT_BUS_PRIORITY + 1; @@ -322,6 +322,10 @@ public class RecentsActivity extends Activity implements RecentsView.RecentsView @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + // Register this activity with the event bus + EventBus.getDefault().register(this, EVENT_BUS_PRIORITY); + // For the non-primary user, ensure that the SystemServicesProxy and configuration is // initialized RecentsTaskLoader.initialize(this); @@ -442,6 +446,7 @@ public class RecentsActivity extends Activity implements RecentsView.RecentsView // Stop listening for widget package changes if there was one bound mAppWidgetHost.stopListening(); + EventBus.getDefault().unregister(this); } public void onEnterAnimationTriggered() { @@ -451,16 +456,12 @@ public class RecentsActivity extends Activity implements RecentsView.RecentsView mRecentsView.startEnterRecentsAnimation(ctx); if (mSearchWidgetInfo != null) { - final WeakReference cbRef = - new WeakReference( - RecentsActivity.this); ctx.postAnimationTrigger.addLastDecrementRunnable(new Runnable() { @Override public void run() { // Start listening for widget package changes if there is one bound - RecentsAppWidgetHost.RecentsAppWidgetHostCallbacks cb = cbRef.get(); - if (cb != null) { - mAppWidgetHost.startListening(cb); + if (mAppWidgetHost != null) { + mAppWidgetHost.startListening(); } } }); @@ -577,10 +578,13 @@ public class RecentsActivity extends Activity implements RecentsView.RecentsView mAfterPauseRunnable = r; } - /**** RecentsAppWidgetHost.RecentsAppWidgetHostCallbacks Implementation ****/ + /**** EventBus events ****/ - @Override - public void refreshSearchWidgetView() { + public final void onBusEvent(AppWidgetProviderChangedEvent event) { + refreshSearchWidgetView(); + } + + private void refreshSearchWidgetView() { if (mSearchWidgetInfo != null) { SystemServicesProxy ssp = RecentsTaskLoader.getInstance().getSystemServicesProxy(); int searchWidgetId = ssp.getSearchAppWidgetId(this); diff --git a/packages/SystemUI/src/com/android/systemui/recents/RecentsAppWidgetHost.java b/packages/SystemUI/src/com/android/systemui/recents/RecentsAppWidgetHost.java index 0102332b17cb6..fc96c11e41248 100644 --- a/packages/SystemUI/src/com/android/systemui/recents/RecentsAppWidgetHost.java +++ b/packages/SystemUI/src/com/android/systemui/recents/RecentsAppWidgetHost.java @@ -20,24 +20,19 @@ import android.appwidget.AppWidgetHost; import android.appwidget.AppWidgetHostView; import android.appwidget.AppWidgetProviderInfo; import android.content.Context; +import com.android.systemui.recents.events.EventBus; +import com.android.systemui.recents.events.activity.AppWidgetProviderChangedEvent; /** Our special app widget host for the Search widget */ public class RecentsAppWidgetHost extends AppWidgetHost { - /* Callbacks to notify when an app package changes */ - interface RecentsAppWidgetHostCallbacks { - void refreshSearchWidgetView(); - } - - RecentsAppWidgetHostCallbacks mCb; boolean mIsListening; public RecentsAppWidgetHost(Context context, int hostId) { super(context, hostId); } - public void startListening(RecentsAppWidgetHostCallbacks cb) { - mCb = cb; + public void startListening() { if (!mIsListening) { mIsListening = true; super.startListening(); @@ -47,11 +42,9 @@ public class RecentsAppWidgetHost extends AppWidgetHost { @Override public void stopListening() { if (mIsListening) { + mIsListening = false; super.stopListening(); } - // Ensure that we release any references to the callbacks - mCb = null; - mIsListening = false; } @Override @@ -66,8 +59,8 @@ public class RecentsAppWidgetHost extends AppWidgetHost { @Override protected void onProviderChanged(int appWidgetId, AppWidgetProviderInfo appWidgetInfo) { super.onProviderChanged(appWidgetId, appWidgetInfo); - if (mIsListening && mCb != null) { - mCb.refreshSearchWidgetView(); + if (mIsListening) { + EventBus.getDefault().send(new AppWidgetProviderChangedEvent()); } } } diff --git a/packages/SystemUI/src/com/android/systemui/recents/events/activity/AppWidgetProviderChangedEvent.java b/packages/SystemUI/src/com/android/systemui/recents/events/activity/AppWidgetProviderChangedEvent.java new file mode 100644 index 0000000000000..52cfe18a9e26b --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/recents/events/activity/AppWidgetProviderChangedEvent.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.recents.events.activity; + +import com.android.systemui.recents.RecentsAppWidgetHost; +import com.android.systemui.recents.events.EventBus; + +/** + * This is sent by the {@link RecentsAppWidgetHost} whenever the search provider widget changes, and + * subscribers can update accordingly. + */ +public class AppWidgetProviderChangedEvent extends EventBus.Event { + // Simple event +}