summaryrefslogtreecommitdiff
path: root/src/com/android
diff options
context:
space:
mode:
authorJoe LaPenna <jlapenna@google.com>2017-01-12 01:01:57 +0000
committerJoe LaPenna <jlapenna@google.com>2017-01-11 21:23:18 -0800
commitf3811fdd2f08881535e916aa869ced48c400062d (patch)
tree0b15ed3bc0048082532adff9c1cf5704b7203c5e /src/com/android
parent996de25b83c42648e31f87f305f866d4d5c03ac8 (diff)
downloadNetworkRecommendation-f3811fdd2f08881535e916aa869ced48c400062d.tar.gz
Compile NetworkRecommendation with the system jar.
Test: build/flashed && adb shell am instrument -w com.android.networkrecommendation.tests/android.support.test.runner.AndroidJUnitRunner Bug: 34234095 Bug: 34235341 Change-Id: Ib5377cb240b509edd8a92e069070b46752389922
Diffstat (limited to 'src/com/android')
-rw-r--r--src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java28
-rw-r--r--src/com/android/networkrecommendation/ImageUtils.java62
-rw-r--r--src/com/android/networkrecommendation/ScanResultUtil.java23
-rw-r--r--src/com/android/networkrecommendation/WifiConfigurationUtil.java21
-rw-r--r--src/com/android/networkrecommendation/WifiNotificationController.java11
-rw-r--r--src/com/android/networkrecommendation/WifiNotificationHelper.java31
-rw-r--r--src/com/android/networkrecommendation/WifiWakeupController.java11
-rw-r--r--src/com/android/networkrecommendation/WifiWakeupNetworkSelector.java7
8 files changed, 136 insertions, 58 deletions
diff --git a/src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java b/src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java
index 5698e7d..1b58ace 100644
--- a/src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java
+++ b/src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java
@@ -28,18 +28,18 @@ import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.os.Bundle;
import android.os.Handler;
+import android.support.annotation.VisibleForTesting;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
-import com.android.internal.annotations.GuardedBy;
-import com.android.internal.annotations.VisibleForTesting;
-
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
+import javax.annotation.concurrent.GuardedBy;
+
/**
* In memory, debuggable network recommendation provider.
*
@@ -161,7 +161,8 @@ public class DefaultNetworkRecommendationProvider
}
final NetworkKey networkKey = new NetworkKey(
- new WifiKey(quoteSsid(scanResult), scanResult.BSSID));
+ new WifiKey(ScanResultUtil.createQuotedSSID(scanResult.SSID),
+ scanResult.BSSID));
if (VERBOSE) Log.v(TAG, "Evaluating network: " + networkKey);
// We will only score networks we know about.
@@ -198,7 +199,7 @@ public class DefaultNetworkRecommendationProvider
} else {
// Build a configuration based on the scan.
WifiConfiguration recommendedConfig = new WifiConfiguration();
- recommendedConfig.SSID = quoteSsid(recommendedScanResult);
+ recommendedConfig.SSID = ScanResultUtil.createQuotedSSID(recommendedScanResult.SSID);
recommendedConfig.BSSID = recommendedScanResult.BSSID;
recommendedConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
recommendationResult = RecommendationResult
@@ -351,25 +352,10 @@ public class DefaultNetworkRecommendationProvider
.append("ID=").append(config.networkId)
.append(",SSID=").append(config.SSID)
.append(",useExternalScores=").append(config.useExternalScores)
- .append(",meteredHint=").append(config.meteredHint)
- .append(",meteredOverride=").append(config.meteredOverride);
+ .append(",meteredHint=").append(config.meteredHint);
return sb.toString();
}
- /**
- * Add quotes to ScanResult ssids. WifiConfigurations, WifiKeys and ScoredNetworks have the
- * SSID quoted but scan results don't.
- */
- private static String quoteSsid(ScanResult scanResult) {
- if (scanResult.wifiSsid != null) {
- return "\"" + scanResult.wifiSsid + "\"";
- } else if (scanResult.SSID != null) {
- return "\"" + scanResult.SSID + "\"";
- } else {
- throw new IllegalArgumentException("ScanResult is missing an SSID.");
- }
- }
-
/** Stores scores about networks. Initial implementation is in-memory-only. */
@VisibleForTesting
static class ScoreStorage {
diff --git a/src/com/android/networkrecommendation/ImageUtils.java b/src/com/android/networkrecommendation/ImageUtils.java
new file mode 100644
index 0000000..4b3d042
--- /dev/null
+++ b/src/com/android/networkrecommendation/ImageUtils.java
@@ -0,0 +1,62 @@
+/*
+ * 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.networkrecommendation;
+
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.Drawable;
+
+/** Helper for image manipulation */
+public class ImageUtils {
+
+ /**
+ * Convert a drawable to a bitmap, scaled to fit within maxWidth and maxHeight.
+ */
+ public static Bitmap buildScaledBitmap(Drawable drawable, int maxWidth,
+ int maxHeight) {
+ if (drawable == null) {
+ return null;
+ }
+ int originalWidth = drawable.getIntrinsicWidth();
+ int originalHeight = drawable.getIntrinsicHeight();
+
+ if ((originalWidth <= maxWidth) && (originalHeight <= maxHeight)
+ && (drawable instanceof BitmapDrawable)) {
+ return ((BitmapDrawable) drawable).getBitmap();
+ }
+ if (originalHeight <= 0 || originalWidth <= 0) {
+ return null;
+ }
+
+ // create a new bitmap, scaling down to fit the max dimensions of
+ // a large notification icon if necessary
+ float ratio = Math.min((float) maxWidth / (float) originalWidth,
+ (float) maxHeight / (float) originalHeight);
+ ratio = Math.min(1.0f, ratio);
+ int scaledWidth = (int) (ratio * originalWidth);
+ int scaledHeight = (int) (ratio * originalHeight);
+ Bitmap result = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
+
+ // and paint our app bitmap on it
+ Canvas canvas = new Canvas(result);
+ drawable.setBounds(0, 0, scaledWidth, scaledHeight);
+ drawable.draw(canvas);
+
+ return result;
+ }
+}
diff --git a/src/com/android/networkrecommendation/ScanResultUtil.java b/src/com/android/networkrecommendation/ScanResultUtil.java
index c62f707..08b71d0 100644
--- a/src/com/android/networkrecommendation/ScanResultUtil.java
+++ b/src/com/android/networkrecommendation/ScanResultUtil.java
@@ -18,10 +18,9 @@ package com.android.networkrecommendation;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
+import android.support.annotation.VisibleForTesting;
import android.text.TextUtils;
-import com.android.internal.annotations.VisibleForTesting;
-
/**
* Scan result utility for any {@link ScanResult} related operations.
* TODO(b/34125341): Delete this class once exposed as a SystemApi
@@ -71,6 +70,26 @@ public class ScanResultUtil {
return "\"" + ssid + "\"";
}
+ /** @return {@code true} if the result is for a 2.4GHz network. */
+ public static boolean is24GHz(ScanResult result) {
+ return is24GHz(result.frequency);
+ }
+
+ /** @return {@code true} if the frequency is for a 2.4GHz network. */
+ public static boolean is24GHz(int freq) {
+ return freq > 2400 && freq < 2500;
+ }
+
+ /** @return {@code true} if the result is for a 5GHz network. */
+ public static boolean is5GHz(ScanResult result) {
+ return is5GHz(result.frequency);
+ }
+
+ /** @return {@code true} if the frequency is for a 5GHz network. */
+ public static boolean is5GHz(int freq) {
+ return freq > 4900 && freq < 5900;
+ }
+
/**
* Checks if the provided |scanResult| match with the provided |config|. Essentially checks
* if the network config and scan result have the same SSID and encryption type.
diff --git a/src/com/android/networkrecommendation/WifiConfigurationUtil.java b/src/com/android/networkrecommendation/WifiConfigurationUtil.java
index e65ecba..d653a4f 100644
--- a/src/com/android/networkrecommendation/WifiConfigurationUtil.java
+++ b/src/com/android/networkrecommendation/WifiConfigurationUtil.java
@@ -65,4 +65,25 @@ public class WifiConfigurationUtil {
return !(isConfigForWepNetwork(config) || isConfigForPskNetwork(config)
|| isConfigForEapNetwork(config));
}
+
+ /** @return a ssid that can be shown to the user. */
+ public static String getPrintableSsid(WifiConfiguration config) {
+ if (config.SSID == null) return "";
+ final int length = config.SSID.length();
+ if (length > 2 && (config.SSID.charAt(0) == '"') && config.SSID.charAt(length - 1) == '"') {
+ return config.SSID.substring(1, length - 1);
+ }
+ return config.SSID;
+ }
+
+ /** Removes " from the ssid in a wifi configuration (to match against a ScanResult). */
+ public static String removeDoubleQuotes(WifiConfiguration config) {
+ if (config.SSID == null) return null;
+ final int length = config.SSID.length();
+ if ((length > 1) && (config.SSID.charAt(0) == '"')
+ && (config.SSID.charAt(length - 1) == '"')) {
+ return config.SSID.substring(1, length - 1);
+ }
+ return config.SSID;
+ }
}
diff --git a/src/com/android/networkrecommendation/WifiNotificationController.java b/src/com/android/networkrecommendation/WifiNotificationController.java
index c8cf1f8..11bb8ac 100644
--- a/src/com/android/networkrecommendation/WifiNotificationController.java
+++ b/src/com/android/networkrecommendation/WifiNotificationController.java
@@ -16,7 +16,6 @@
package com.android.networkrecommendation;
-import android.annotation.Nullable;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
@@ -34,8 +33,8 @@ import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Handler;
-import android.os.UserHandle;
import android.provider.Settings;
+import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import java.io.FileDescriptor;
@@ -53,8 +52,7 @@ public class WifiNotificationController {
* The icon to show in the 'available networks' notification. This will also
* be the ID of the Notification given to the NotificationManager.
*/
- private static final int ICON_NETWORKS_AVAILABLE =
- com.android.internal.R.drawable.stat_notify_wifi_in_range;
+ private static final int ICON_NETWORKS_AVAILABLE = R.drawable.stat_notify_wifi_in_range;
/**
* When a notification is shown, we wait this amount before possibly showing it again.
*/
@@ -393,8 +391,7 @@ public class WifiNotificationController {
}
private void postNotification(Notification notification) {
- mNotificationManager.notifyAsUser(null /* tag */, ICON_NETWORKS_AVAILABLE,
- notification, UserHandle.ALL);
+ mNotificationManager.notify(null /* tag */, ICON_NETWORKS_AVAILABLE, notification);
}
/**
@@ -410,7 +407,7 @@ public class WifiNotificationController {
}
private void removeNotification() {
- mNotificationManager.cancelAsUser(null /* tag */, ICON_NETWORKS_AVAILABLE, UserHandle.ALL);
+ mNotificationManager.cancel(null /* tag */, ICON_NETWORKS_AVAILABLE);
mNotificationShown = false;
}
diff --git a/src/com/android/networkrecommendation/WifiNotificationHelper.java b/src/com/android/networkrecommendation/WifiNotificationHelper.java
index b8fc0f0..4408804 100644
--- a/src/com/android/networkrecommendation/WifiNotificationHelper.java
+++ b/src/com/android/networkrecommendation/WifiNotificationHelper.java
@@ -18,7 +18,6 @@ package com.android.networkrecommendation;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
-import android.annotation.NonNull;
import android.app.Notification;
import android.app.Notification.Action;
import android.app.PendingIntent;
@@ -34,15 +33,13 @@ import android.net.ScoredNetwork;
import android.net.WifiKey;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
-import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
+import android.support.annotation.NonNull;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
-import com.android.internal.util.ImageUtils;
-
import java.util.List;
/**
@@ -148,41 +145,39 @@ public class WifiNotificationHelper {
FLAG_UPDATE_CURRENT);
return new Notification.Builder(mContext)
.setDeleteIntent(deleteIntent)
- .setSmallIcon(com.android.internal.R.drawable.stat_notify_wifi_in_range)
+ .setSmallIcon(R.drawable.stat_notify_wifi_in_range)
.setLargeIcon(badge)
.setAutoCancel(true)
- .setColor(mContext.getColor(
- com.android.internal.R.color.system_notification_accent_color))
.setTicker(title)
.setContentTitle(title)
- .setContentText(config.getPrintableSsid())
+ .setContentText(WifiConfigurationUtil.getPrintableSsid(config))
.addExtras(getSystemLabelExtras());
}
private Bundle getSystemLabelExtras() {
Bundle extras = new Bundle();
extras.putString(Notification.EXTRA_SUBSTITUTE_APP_NAME,
- mContext.getString(com.android.internal.R.string.android_system_label));
+ mContext.getString(R.string.android_system_label));
return extras;
}
//TODO(34177812): Share this logic between systemUi and Settings.
static final int[] WIFI_PIE_FOR_BADGING = {
- com.android.internal.R.drawable.ic_signal_wifi_badged_0_bars,
- com.android.internal.R.drawable.ic_signal_wifi_badged_1_bar,
- com.android.internal.R.drawable.ic_signal_wifi_badged_2_bars,
- com.android.internal.R.drawable.ic_signal_wifi_badged_3_bars,
- com.android.internal.R.drawable.ic_signal_wifi_badged_4_bars
+ R.drawable.ic_signal_wifi_badged_0_bars,
+ R.drawable.ic_signal_wifi_badged_1_bar,
+ R.drawable.ic_signal_wifi_badged_2_bars,
+ R.drawable.ic_signal_wifi_badged_3_bars,
+ R.drawable.ic_signal_wifi_badged_4_bars
};
private int getWifiBadgeResourceForEnum(int badgeEnum) {
switch (badgeEnum) {
case ScoredNetwork.BADGING_SD:
- return com.android.internal.R.drawable.ic_signal_wifi_badged_sd;
+ return R.drawable.ic_signal_wifi_badged_sd;
case ScoredNetwork.BADGING_HD:
- return com.android.internal.R.drawable.ic_signal_wifi_badged_hd;
+ return R.drawable.ic_signal_wifi_badged_hd;
case ScoredNetwork.BADGING_4K:
- return com.android.internal.R.drawable.ic_signal_wifi_badged_4k;
+ return R.drawable.ic_signal_wifi_badged_4k;
default:
throw new IllegalArgumentException("No badge resource for enum :" + badgeEnum);
}
@@ -222,7 +217,7 @@ public class WifiNotificationHelper {
private ScanResult findMatchingScanResult(List<ScanResult> scanResults,
WifiConfiguration wifiConfiguration) {
- String ssid = WifiInfo.removeDoubleQuotes(wifiConfiguration.SSID);
+ String ssid = WifiConfigurationUtil.removeDoubleQuotes(wifiConfiguration);
String bssid = wifiConfiguration.BSSID;
for (ScanResult scanResult : scanResults) {
if (ssid.equals(scanResult.SSID) && bssid.equals(scanResult.BSSID)) {
diff --git a/src/com/android/networkrecommendation/WifiWakeupController.java b/src/com/android/networkrecommendation/WifiWakeupController.java
index 7354106..d1ac74a 100644
--- a/src/com/android/networkrecommendation/WifiWakeupController.java
+++ b/src/com/android/networkrecommendation/WifiWakeupController.java
@@ -24,18 +24,16 @@ import android.content.IntentFilter;
import android.database.ContentObserver;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
-import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
+import android.support.annotation.VisibleForTesting;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;
-import com.android.internal.annotations.VisibleForTesting;
-
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.List;
@@ -161,15 +159,14 @@ public class WifiWakeupController {
for (int i = 0; i < wifiConfigurations.size(); i++) {
WifiConfiguration wifiConfiguration = wifiConfigurations.get(i);
if (wifiConfiguration.status != WifiConfiguration.Status.ENABLED
- || wifiConfiguration.ephemeral
|| wifiConfiguration.useExternalScores) {
- continue; // Ignore disabled, ephemeral and externally scored networks.
+ continue; // Ignore disabled and externally scored networks.
}
if (wifiConfiguration.hasNoInternetAccess()
- || wifiConfiguration.noInternetAccessExpected) {
+ || wifiConfiguration.isNoInternetAccessExpected()) {
continue; // Ignore networks that will likely not have internet access.
}
- String ssid = WifiInfo.removeDoubleQuotes(wifiConfiguration.SSID);
+ String ssid = WifiConfigurationUtil.removeDoubleQuotes(wifiConfiguration);
if (TextUtils.isEmpty(ssid)) {
continue;
}
diff --git a/src/com/android/networkrecommendation/WifiWakeupNetworkSelector.java b/src/com/android/networkrecommendation/WifiWakeupNetworkSelector.java
index 730ef4c..94fce56 100644
--- a/src/com/android/networkrecommendation/WifiWakeupNetworkSelector.java
+++ b/src/com/android/networkrecommendation/WifiWakeupNetworkSelector.java
@@ -69,8 +69,9 @@ public class WifiWakeupNetworkSelector {
if (wifiConfiguration == null) {
continue;
}
- if (scanResult.is5GHz() && scanResult.level < mThresholdQualifiedRssi5
- || scanResult.is24GHz() && scanResult.level < mThresholdQualifiedRssi24) {
+ if (ScanResultUtil.is5GHz(scanResult) && scanResult.level < mThresholdQualifiedRssi5
+ || ScanResultUtil.is24GHz(scanResult)
+ && scanResult.level < mThresholdQualifiedRssi24) {
continue;
}
if (!ScanResultUtil.doesScanResultMatchWithNetwork(scanResult, wifiConfiguration)) {
@@ -95,7 +96,7 @@ public class WifiWakeupNetworkSelector {
score += (rssi + mRssiScoreOffset) * mRssiScoreSlope;
// 5GHz band bonus.
- if (scanResult.is5GHz()) {
+ if (ScanResultUtil.is5GHz(scanResult)) {
score += mBand5GHzAward;
}