From f94401b06dcf8f00d4c1ac868669d15c13e07fe4 Mon Sep 17 00:00:00 2001 From: Doris Ling Date: Thu, 30 Mar 2017 14:40:25 -0700 Subject: [PATCH] Remove hotspot receiver from manifest. The hotspot receiver if used to cancel any tethering alarm set when wifi ap state changes. Removing it from the manifest, and only register the receiver when we schedule the tethering alarm. Change-Id: I97c3be6e2a374949208bbdd4ac5ccc2fabf0291a Fix: 35968322 Test: make RunSettingsRoboTests --- AndroidManifest.xml | 7 -- .../android/settings/HotspotOffReceiver.java | 25 +++++- src/com/android/settings/TetherService.java | 16 +++- .../android/settings/TetherServiceTest.java | 87 +++++++++++++++++++ 4 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 tests/robotests/src/com/android/settings/TetherServiceTest.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index da88f76f739..782ca2bda1b 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -440,13 +440,6 @@ - - - - - - mCurrentTethers; private ArrayMap> mPendingCallbacks; + private HotspotOffReceiver mHotspotReceiver; @Override public IBinder onBind(Intent intent) { @@ -94,6 +94,7 @@ public class TetherService extends Service { if (mUsageManagerWrapper == null) { mUsageManagerWrapper = new UsageStatsManagerWrapper(this); } + mHotspotReceiver = new HotspotOffReceiver(this); } @Override @@ -181,6 +182,11 @@ public class TetherService extends Service { } } + @VisibleForTesting + void setHotspotOffReceiver(HotspotOffReceiver receiver) { + mHotspotReceiver = receiver; + } + private ArrayList stringToTethers(String tethersStr) { ArrayList ret = new ArrayList(); if (TextUtils.isEmpty(tethersStr)) return ret; @@ -276,7 +282,8 @@ public class TetherService extends Service { } } - private void scheduleAlarm() { + @VisibleForTesting + void scheduleAlarm() { Intent intent = new Intent(this, TetherService.class); intent.putExtra(ConnectivityManager.EXTRA_RUN_PROVISION, true); @@ -289,6 +296,7 @@ public class TetherService extends Service { if (DEBUG) Log.d(TAG, "Scheduling alarm at interval " + periodMs); alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, periodMs, pendingIntent); + mHotspotReceiver.register(); } /** @@ -302,7 +310,8 @@ public class TetherService extends Service { context.startService(intent); } - private void cancelAlarmIfNecessary() { + @VisibleForTesting + void cancelAlarmIfNecessary() { if (mCurrentTethers.size() != 0) { if (DEBUG) Log.d(TAG, "Tethering still active, not cancelling alarm"); return; @@ -312,6 +321,7 @@ public class TetherService extends Service { AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.cancel(pendingIntent); if (DEBUG) Log.d(TAG, "Tethering no longer active, canceling recheck"); + mHotspotReceiver.unregister(); } private void fireCallbacksForType(int type, int result) { diff --git a/tests/robotests/src/com/android/settings/TetherServiceTest.java b/tests/robotests/src/com/android/settings/TetherServiceTest.java new file mode 100644 index 00000000000..2d5a2fb3f5f --- /dev/null +++ b/tests/robotests/src/com/android/settings/TetherServiceTest.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2017 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.settings; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +import android.content.Context; +import android.content.Intent; +import android.net.wifi.WifiManager; +import java.util.ArrayList; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.annotation.Config; +import org.robolectric.shadows.ShadowApplication; +import org.robolectric.util.ReflectionHelpers; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class TetherServiceTest { + + @Mock + private Context mContext; + + private ShadowApplication mShadowApplication; + private Context mAppContext; + private TetherService mService; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mShadowApplication = ShadowApplication.getInstance(); + mAppContext = mShadowApplication.getApplicationContext(); + mService = new TetherService(); + ReflectionHelpers.setField(mService, "mBase", mAppContext); + mService.setHotspotOffReceiver(new HotspotOffReceiver(mContext)); + } + + @Test + public void scheduleAlarm_shouldRegisterReceiver() { + mService.setHotspotOffReceiver(new HotspotOffReceiver(mAppContext)); + + mService.scheduleAlarm(); + + assertThat(mShadowApplication.hasReceiverForIntent( + new Intent(WifiManager.WIFI_AP_STATE_CHANGED_ACTION))).isTrue(); + } + + @Test + public void cancelAlarmIfNecessary_hasActiveTethers_shouldNotUnregisterReceiver() { + mService.scheduleAlarm(); + final ArrayList tethers = new ArrayList<>(); + tethers.add(1); + ReflectionHelpers.setField(mService, "mCurrentTethers", tethers); + + mService.cancelAlarmIfNecessary(); + verify(mContext, never()).unregisterReceiver(any(HotspotOffReceiver.class)); + } + + @Test + public void cancelAlarmIfNecessary_noActiveTethers_shouldUnregisterReceiver() { + final ArrayList tethers = new ArrayList<>(); + ReflectionHelpers.setField(mService, "mCurrentTethers", tethers); + mService.scheduleAlarm(); + + mService.cancelAlarmIfNecessary(); + verify(mContext).unregisterReceiver(any(HotspotOffReceiver.class)); + } +}