Unregister hotspot receiver in TetherService.

- when the service exits, also unregister the hotspot receiver.
- move TetherService and HotspotOffReceiver to wifi.tether package.

Change-Id: I0044a52bf80a5530a58b5186ab056de55e83532b
Fixes: 69844871
Test: make RunSettingsRoboTests
This commit is contained in:
Doris Ling
2018-01-18 15:04:59 -08:00
parent b0e89cd1c3
commit c692758818
5 changed files with 35 additions and 10 deletions

View File

@@ -476,7 +476,7 @@
</intent-filter> </intent-filter>
</activity> </activity>
<service android:name=".TetherService" <service android:name=".wifi.tether.TetherService"
android:exported="true" android:exported="true"
android:permission="android.permission.TETHER_PRIVILEGED" /> android:permission="android.permission.TETHER_PRIVILEGED" />

View File

@@ -1,5 +1,5 @@
package com.android.settings; package com.android.settings.wifi.tether;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;

View File

@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.android.settings; package com.android.settings.wifi.tether;
import android.app.Activity; import android.app.Activity;
import android.app.AlarmManager; import android.app.AlarmManager;
@@ -167,11 +167,16 @@ public class TetherService extends Service {
SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE); SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE);
prefs.edit().putString(KEY_TETHERS, tethersToString(mCurrentTethers)).commit(); prefs.edit().putString(KEY_TETHERS, tethersToString(mCurrentTethers)).commit();
unregisterReceivers();
if (DEBUG) Log.d(TAG, "Destroying TetherService"); if (DEBUG) Log.d(TAG, "Destroying TetherService");
unregisterReceiver(mReceiver);
super.onDestroy(); super.onDestroy();
} }
private void unregisterReceivers() {
unregisterReceiver(mReceiver);
mHotspotReceiver.unregister();
}
private void removeTypeAtIndex(int index) { private void removeTypeAtIndex(int index) {
mCurrentTethers.remove(index); mCurrentTethers.remove(index);
// If we are currently in the middle of a check, we may need to adjust the // If we are currently in the middle of a check, we may need to adjust the

View File

@@ -13,17 +13,23 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.android.settings; package com.android.settings.wifi.tether;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import java.util.ArrayList; import java.util.ArrayList;
@@ -87,4 +93,22 @@ public class TetherServiceTest {
mService.cancelAlarmIfNecessary(); mService.cancelAlarmIfNecessary();
verify(mContext).unregisterReceiver(any(HotspotOffReceiver.class)); verify(mContext).unregisterReceiver(any(HotspotOffReceiver.class));
} }
@Test
public void onDestroy_shouldUnregisterReceiver() {
final ArrayList<Integer> tethers = new ArrayList<>();
ReflectionHelpers.setField(mService, "mCurrentTethers", tethers);
ReflectionHelpers.setField(mService, "mBase", mContext);
final SharedPreferences prefs = mock(SharedPreferences .class);
final SharedPreferences.Editor editor = mock(SharedPreferences.Editor.class);
when(mContext.getSharedPreferences(anyString(), anyInt())).thenReturn(prefs);
when(prefs.edit()).thenReturn(editor);
when(editor.putString(anyString(), anyString())).thenReturn(editor);
final HotspotOffReceiver hotspotOffReceiver = mock(HotspotOffReceiver.class);
mService.setHotspotOffReceiver(hotspotOffReceiver);
mService.onDestroy();
verify(hotspotOffReceiver).unregister();
}
} }

View File

@@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package com.android.settings; package com.android.settings.wifi.tether;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.Matchers.*; import static org.mockito.Matchers.*;
@@ -49,17 +49,13 @@ import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor; import android.content.SharedPreferences.Editor;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.ResultReceiver; import android.os.ResultReceiver;
import android.os.SystemClock; import android.os.SystemClock;
import android.test.ServiceTestCase; import android.test.ServiceTestCase;
import android.test.mock.MockResources;
import android.util.Log; import android.util.Log;
import com.android.settings.TetherService;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Captor; import org.mockito.Captor;
import org.mockito.Mock; import org.mockito.Mock;