summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmin Shaikh <ashaikh@google.com>2017-01-03 12:42:00 -0800
committerAmin Shaikh <ashaikh@google.com>2017-01-06 12:27:48 -0800
commitb2eb07c2e28e39a897e38ab71ff297a5a7ad8cf0 (patch)
tree37765d94f85e5806e19452a8ad46069384e19971
parent6f81d759d3241c987cc61231281293bfcd019a38 (diff)
downloadNetworkRecommendation-b2eb07c2e28e39a897e38ab71ff297a5a7ad8cf0.tar.gz
Move WifiWakeupController to NetworkRecommendation
Added ability to enable/disable WifiNotificationController based on whether the framework is bound to the DefaultNetworkRecommendationProvider. Bug: 34054262 Test: runtest --path tests/src/com/android/networkrecommendation/WifiWakeupControllerTest.java Change-Id: Ia3c645bf001dc5bc6ee129bac6d5a24b80e09897
-rw-r--r--src/com/android/networkrecommendation/DefaultNetworkRecommendationService.java11
-rw-r--r--src/com/android/networkrecommendation/WifiWakeupController.java108
-rw-r--r--tests/src/com/android/networkrecommendation/WifiWakeupControllerTest.java92
3 files changed, 211 insertions, 0 deletions
diff --git a/src/com/android/networkrecommendation/DefaultNetworkRecommendationService.java b/src/com/android/networkrecommendation/DefaultNetworkRecommendationService.java
index ba2bd64..301e949 100644
--- a/src/com/android/networkrecommendation/DefaultNetworkRecommendationService.java
+++ b/src/com/android/networkrecommendation/DefaultNetworkRecommendationService.java
@@ -72,6 +72,7 @@ public class DefaultNetworkRecommendationService extends Service {
private Handler mHandler;
private DefaultNetworkRecommendationProvider mProvider;
private WifiNotificationController mWifiNotificationController;
+ private WifiWakeupController mWifiWakeupController;
@Override
public void onCreate() {
@@ -83,16 +84,26 @@ public class DefaultNetworkRecommendationService extends Service {
new DefaultNetworkRecommendationProvider.ScoreStorage());
mWifiNotificationController = new WifiNotificationController(
this, mHandler.getLooper(), null);
+ mWifiWakeupController = new WifiWakeupController(
+ this, getContentResolver(), mHandlerThread.getLooper());
}
@Override
public IBinder onBind(Intent intent) {
+ mWifiWakeupController.start();
return mProvider.getBinder();
}
@Override
+ public boolean onUnbind(Intent intent) {
+ mWifiWakeupController.stop();
+ return super.onUnbind(intent);
+ }
+
+ @Override
protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
mProvider.dump(fd, writer, args);
mWifiNotificationController.dump(fd, writer, args);
+ mWifiWakeupController.dump(fd, writer, args);
}
}
diff --git a/src/com/android/networkrecommendation/WifiWakeupController.java b/src/com/android/networkrecommendation/WifiWakeupController.java
new file mode 100644
index 0000000..a2e3e36
--- /dev/null
+++ b/src/com/android/networkrecommendation/WifiWakeupController.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright (C) 2013 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.networkrecommendation;
+
+import android.content.BroadcastReceiver;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.database.ContentObserver;
+import android.net.wifi.WifiManager;
+import android.os.Handler;
+import android.os.Looper;
+import android.provider.Settings;
+
+import com.android.internal.annotations.VisibleForTesting;
+
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * Handles enabling Wi-Fi for the Wi-Fi Wakeup feature.
+ * @hide
+ */
+public class WifiWakeupController {
+ private final Context mContext;
+ private final ContentResolver mContentResolver;
+ private final Handler mHandler;
+ private final AtomicBoolean mStarted;
+ @VisibleForTesting
+ final ContentObserver mContentObserver;
+ @VisibleForTesting
+ boolean mWifiWakeupEnabled;
+
+ WifiWakeupController(Context context, ContentResolver contentResolver, Looper looper) {
+ mContext = context;
+ mContentResolver = contentResolver;
+ mHandler = new Handler(looper);
+ mStarted = new AtomicBoolean(false);
+ mContentObserver = new ContentObserver(mHandler) {
+ @Override
+ public void onChange(boolean selfChange) {
+ mWifiWakeupEnabled = getWifiWakeupEnabledSetting();
+ }
+ };
+ }
+
+ /** Starts {@link WifiWakeupController}. */
+ public void start() {
+ if (!mStarted.compareAndSet(false, true)) {
+ return;
+ }
+ IntentFilter filter = new IntentFilter();
+ filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
+ filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
+ mContext.registerReceiver(mBroadcastReceiver, filter, null, mHandler);
+ mContentResolver.registerContentObserver(Settings.Global.getUriFor(
+ Settings.Global.WIFI_WAKEUP_ENABLED), true, mContentObserver);
+ mWifiWakeupEnabled = getWifiWakeupEnabledSetting();
+ }
+
+ /** Stops {@link WifiWakeupController}. */
+ public void stop() {
+ if (!mStarted.compareAndSet(true, false)) {
+ return;
+ }
+ mContext.unregisterReceiver(mBroadcastReceiver);
+ mContentResolver.unregisterContentObserver(mContentObserver);
+ }
+
+ private boolean getWifiWakeupEnabledSetting() {
+ return Settings.Global.getInt(
+ mContentResolver, Settings.Global.WIFI_WAKEUP_ENABLED, 0) == 1;
+ }
+
+ private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
+ handleWifiStateChanged(intent);
+ } else if (intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
+ handleScanResultsAvailable(intent);
+ }
+ }
+ };
+
+ private void handleWifiStateChanged(Intent intent) {};
+ private void handleScanResultsAvailable(Intent intent) {};
+
+ void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
+ pw.println("mStarted " + mStarted.get());
+ pw.println("mWifiWakeupEnabled " + mWifiWakeupEnabled);
+ }
+}
diff --git a/tests/src/com/android/networkrecommendation/WifiWakeupControllerTest.java b/tests/src/com/android/networkrecommendation/WifiWakeupControllerTest.java
new file mode 100644
index 0000000..5ab02b4
--- /dev/null
+++ b/tests/src/com/android/networkrecommendation/WifiWakeupControllerTest.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright (C) 2016 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.networkrecommendation;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.net.wifi.WifiScanner;
+import android.os.Looper;
+import android.provider.Settings;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+
+/**
+ * Unit tests for {@link com.android.server.wifi.WifiWakeupController}.
+ */
+@RunWith(AndroidJUnit4.class)
+public class WifiWakeupControllerTest {
+ public static final String TAG = "WifiScanningServiceTest";
+
+ @Mock private Context mContext;
+ @Mock private WifiScanner mWifiScanner;
+ private ContentResolver mContentResolver;
+
+ private WifiWakeupController mWifiWakeupController;
+ private int mWifiWakeupEnabledOriginalValue;
+
+ /** Initialize objects before each test run. */
+ @Before
+ public void setUp() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ mContentResolver = InstrumentationRegistry.getTargetContext().getContentResolver();
+
+ mWifiWakeupEnabledOriginalValue =
+ Settings.Global.getInt(mContentResolver, Settings.Global.WIFI_WAKEUP_ENABLED, 0);
+ Settings.Global.putInt(mContentResolver, Settings.Global.WIFI_WAKEUP_ENABLED, 1);
+ mWifiWakeupController = new WifiWakeupController(mContext, mContentResolver,
+ Looper.getMainLooper());
+ mWifiWakeupController.start();
+ }
+
+ @After
+ public void tearDown() {
+ Settings.Global.putInt(mContentResolver, Settings.Global.WIFI_WAKEUP_ENABLED,
+ mWifiWakeupEnabledOriginalValue);
+ }
+
+ /** Test WifiWakeupEnabledSettingObserver enables feature correctly. */
+ @Test
+ public void testEnableWifiWakeup() {
+ assertTrue(mWifiWakeupController.mWifiWakeupEnabled);
+
+ Settings.Global.putInt(mContentResolver, Settings.Global.WIFI_WAKEUP_ENABLED, 0);
+ mWifiWakeupController.mContentObserver.onChange(true);
+ assertFalse(mWifiWakeupController.mWifiWakeupEnabled);
+ }
+
+ /** Test dump() does not crash. */
+ @Test
+ public void testDump() {
+ StringWriter stringWriter = new StringWriter();
+ mWifiWakeupController.dump(
+ new FileDescriptor(), new PrintWriter(stringWriter), new String[0]);
+ }
+}