From e652160c9484247ac5eb7214666299d85b8338c8 Mon Sep 17 00:00:00 2001 From: Sudheer Shanka Date: Wed, 4 May 2022 18:17:38 -0700 Subject: [PATCH] Avoid unregistering the same receiver more than once. Bug: 231496005 Test: atest com.android.cts.net.HostsideRestrictBackgroundNetworkTests Ignore-AOSP-First: This will cherry-picked to AOSP later. Change-Id: Iba17daa70f14bdee1d1a7d32cb9dc64623ba1d24 --- tests/cts/hostside/app2/Android.bp | 1 + .../cts/net/hostside/app2/MyActivity.java | 30 ++++++++++++------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/tests/cts/hostside/app2/Android.bp b/tests/cts/hostside/app2/Android.bp index 01c8cd25d7..edfaf9fdd3 100644 --- a/tests/cts/hostside/app2/Android.bp +++ b/tests/cts/hostside/app2/Android.bp @@ -23,6 +23,7 @@ android_test_helper_app { defaults: ["cts_support_defaults"], sdk_version: "test_current", static_libs: [ + "androidx.annotation_annotation", "CtsHostsideNetworkTestsAidl", "NetworkStackApiStableShims", ], diff --git a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyActivity.java b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyActivity.java index a337fe283a..bffa7b0165 100644 --- a/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyActivity.java +++ b/tests/cts/hostside/app2/src/com/android/cts/net/hostside/app2/MyActivity.java @@ -28,11 +28,14 @@ import android.os.Bundle; import android.os.RemoteCallback; import android.util.Log; +import androidx.annotation.GuardedBy; + /** * Activity used to bring process to foreground. */ public class MyActivity extends Activity { + @GuardedBy("this") private BroadcastReceiver finishCommandReceiver = null; @Override @@ -43,8 +46,11 @@ public class MyActivity extends Activity { @Override public void finish() { - if (finishCommandReceiver != null) { - unregisterReceiver(finishCommandReceiver); + synchronized (this) { + if (finishCommandReceiver != null) { + unregisterReceiver(finishCommandReceiver); + finishCommandReceiver = null; + } } super.finish(); } @@ -67,15 +73,17 @@ public class MyActivity extends Activity { super.onResume(); Log.d(TAG, "MyActivity.onResume(): " + getIntent()); Common.notifyNetworkStateObserver(this, getIntent(), TYPE_COMPONENT_ACTIVTY); - finishCommandReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - Log.d(TAG, "Finishing MyActivity"); - MyActivity.this.finish(); - } - }; - registerReceiver(finishCommandReceiver, new IntentFilter(ACTION_FINISH_ACTIVITY), - Context.RECEIVER_EXPORTED); + synchronized (this) { + finishCommandReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + Log.d(TAG, "Finishing MyActivity"); + MyActivity.this.finish(); + } + }; + registerReceiver(finishCommandReceiver, new IntentFilter(ACTION_FINISH_ACTIVITY), + Context.RECEIVER_EXPORTED); + } final RemoteCallback callback = getIntent().getParcelableExtra( Intent.EXTRA_REMOTE_CALLBACK); if (callback != null) {