Fix null pointer exception when surveys are turned off

Under certain conditions the provider could be null on
download completion which could lead to a
NPE if surveys are disabled. The method to remove
the receiver has been made static so an instance
is no longer required.

Test: robotests
Bug: 33707203
Change-Id: Icfb545697f24172db734dd7dad421796edf68186
This commit is contained in:
Salvador Martinez
2016-12-16 17:34:11 -08:00
parent d9d463be8b
commit d7fe1fb958
3 changed files with 25 additions and 9 deletions

View File

@@ -20,6 +20,7 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
/**
* An interface for classes wishing to provide the ability to serve surveys to implement.
@@ -81,5 +82,11 @@ public interface SurveyFeatureProvider {
* after a call to {@link #createAndRegisterReceiver(Activity)}.
* @param activity The activity that was used to register the BroadcastReceiver.
*/
void unregisterReceiver(Activity activity, BroadcastReceiver receiver);
static void unregisterReceiver(Activity activity, BroadcastReceiver receiver) {
if (activity == null) {
throw new IllegalStateException("Cannot unregister receiver if activity is null");
}
LocalBroadcastManager.getInstance(activity).unregisterReceiver(receiver);
}
}

View File

@@ -74,9 +74,8 @@ public class SurveyMixin implements LifecycleObserver, OnResume, OnPause {
public void onPause() {
Activity activity = mFragment.getActivity();
if (mReceiver != null && activity != null) {
SurveyFeatureProvider provider =
FeatureFactory.getFactory(activity).getSurveyFeatureProvider(activity);
provider.unregisterReceiver(activity, mReceiver);
SurveyFeatureProvider.unregisterReceiver(activity, mReceiver);
mReceiver = null;
}
}
}