diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java | 29 | ||||
-rw-r--r-- | src/com/android/networkrecommendation/notify/WifiNotificationHelper.java | 6 | ||||
-rw-r--r-- | src/com/android/networkrecommendation/util/Blog.java | 176 | ||||
-rw-r--r-- | src/com/android/networkrecommendation/util/ImageUtil.java (renamed from src/com/android/networkrecommendation/util/ImageUtils.java) | 2 | ||||
-rw-r--r-- | src/com/android/networkrecommendation/wakeup/WifiWakeupController.java | 38 | ||||
-rw-r--r-- | src/com/android/networkrecommendation/wakeup/WifiWakeupNotificationHelper.java | 9 |
6 files changed, 206 insertions, 54 deletions
diff --git a/src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java b/src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java index 0a585f4..badc05d 100644 --- a/src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java +++ b/src/com/android/networkrecommendation/DefaultNetworkRecommendationProvider.java @@ -31,8 +31,8 @@ import android.os.Handler; import android.support.annotation.VisibleForTesting; import android.text.TextUtils; import android.util.ArrayMap; -import android.util.Log; +import com.android.networkrecommendation.util.Blog; import com.android.networkrecommendation.util.ScanResultUtil; import java.io.FileDescriptor; @@ -87,8 +87,6 @@ import javax.annotation.concurrent.GuardedBy; public class DefaultNetworkRecommendationProvider extends NetworkRecommendationProvider implements SynchronousNetworkRecommendationProvider { static final String TAG = "DefaultNetRecProvider"; - static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); - static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); private static final String WILDCARD_MAC = "00:00:00:00:00:00"; @@ -161,39 +159,39 @@ public class DefaultNetworkRecommendationProvider if (results != null) { for (int i = 0; i < results.length; i++) { final ScanResult scanResult = results[i]; - if (VERBOSE) Log.v(TAG, "Scan: " + scanResult + " " + i); + Blog.v(TAG, "Scan: " + scanResult + " " + i); // We only want to recommend open networks. This check is taken from // places like WifiNotificationController and will be extracted to ScanResult in // a future CL. if (!"[ESS]".equals(scanResult.capabilities)) { - if (VERBOSE) Log.v(TAG, "Discarding closed network: " + scanResult); + Blog.v(TAG, "Discarding closed network: " + scanResult); continue; } final NetworkKey networkKey = new NetworkKey( new WifiKey(ScanResultUtil.createQuotedSSID(scanResult.SSID), scanResult.BSSID)); - if (VERBOSE) Log.v(TAG, "Evaluating network: " + networkKey); + Blog.v(TAG, "Evaluating network: " + networkKey); // We will only score networks we know about. final ScoredNetwork network = mStorage.get(networkKey); if (network == null) { - if (VERBOSE) Log.v(TAG, "Discarding unscored network: " + scanResult); + Blog.v(TAG, "Discarding unscored network: " + scanResult); continue; } final int score = network.rssiCurve.lookupScore(scanResult.level); - if (VERBOSE) Log.d(TAG, "Scored " + scanResult + ": " + score); + Blog.v(TAG, "Scored " + scanResult + ": " + score); if (score > recommendedScore) { recommendedScanResult = scanResult; recommendedScore = score; - if (VERBOSE) Log.d(TAG, "New recommended network: " + scanResult); + Blog.v(TAG, "New recommended network: " + scanResult); continue; } } } else { - Log.w(TAG, "Received null scan results in request."); + Blog.w(TAG, "Received null scan results in request."); } // If we ended up without a recommendation, recommend the provided configuration @@ -219,7 +217,7 @@ public class DefaultNetworkRecommendationProvider synchronized (mStatsLock) { mLastRecommended = recommendationResult.getWifiConfiguration(); mRecommendationCounter++; - if (DEBUG) Log.d(TAG, "Recommending network: " + configToString(mLastRecommended)); + Blog.d(TAG, "Recommending network: " + configToString(mLastRecommended)); } return recommendationResult; } @@ -256,7 +254,7 @@ public class DefaultNetworkRecommendationProvider return; } - if (DEBUG) Log.d(TAG, "Scored networks: " + scoredNetworks); + Blog.d(TAG, "Scored networks: " + scoredNetworks); safelyUpdateScores(scoredNetworks.toArray(new ScoredNetwork[scoredNetworks.size()])); } @@ -305,7 +303,7 @@ public class DefaultNetworkRecommendationProvider try { mScoreManager.updateScores(networkScores); } catch (SecurityException e) { - Log.w(TAG, "Tried to update scores when not the active scorer."); + Blog.w(TAG, "Tried to update scores when not the active scorer."); } } @@ -315,7 +313,7 @@ public class DefaultNetworkRecommendationProvider try { mScoreManager.clearScores(); } catch (SecurityException e) { - Log.w(TAG, "Tried to update scores when not the active scorer."); + Blog.w(TAG, "Tried to update scores when not the active scorer."); } } @@ -382,7 +380,7 @@ public class DefaultNetworkRecommendationProvider * score as applying to any bssid with the provided ssid. */ public void addScore(ScoredNetwork scoredNetwork) { - if (DEBUG) Log.d(TAG, "addScore: " + scoredNetwork); + Blog.d(TAG, "addScore: " + scoredNetwork); synchronized (mScores) { mScores.put(scoredNetwork.networkKey, scoredNetwork); } @@ -429,5 +427,4 @@ public class DefaultNetworkRecommendationProvider public ScoredNetwork getCachedScoredNetwork(NetworkKey networkKey) { return mStorage.get(networkKey); } - } diff --git a/src/com/android/networkrecommendation/notify/WifiNotificationHelper.java b/src/com/android/networkrecommendation/notify/WifiNotificationHelper.java index f37d0c1..719e6b4 100644 --- a/src/com/android/networkrecommendation/notify/WifiNotificationHelper.java +++ b/src/com/android/networkrecommendation/notify/WifiNotificationHelper.java @@ -42,7 +42,7 @@ import android.text.style.ForegroundColorSpan; import com.android.networkrecommendation.R; import com.android.networkrecommendation.SynchronousNetworkRecommendationProvider; -import com.android.networkrecommendation.util.ImageUtils; +import com.android.networkrecommendation.util.ImageUtil; import com.android.networkrecommendation.util.WifiConfigurationUtil; import java.util.List; @@ -130,7 +130,7 @@ public class WifiNotificationHelper { Resources resources = mContext.getResources(); Drawable iconDrawable = mContext.getDrawable(R.drawable.ic_signal_wifi_no_network); iconDrawable.setTint(mContext.getColor(R.color.color_tint)); - Bitmap icon = ImageUtils.buildScaledBitmap( + Bitmap icon = ImageUtil.buildScaledBitmap( iconDrawable, resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_width), resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_height)); @@ -221,7 +221,7 @@ public class WifiNotificationHelper { mContext.getDrawable(getWifiBadgeResourceForEnum(badgeEnum))}); layerDrawable.setTint(mContext.getColor(R.color.color_tint)); Resources resources = mContext.getResources(); - return ImageUtils.buildScaledBitmap(layerDrawable, + return ImageUtil.buildScaledBitmap(layerDrawable, resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_width), resources.getDimensionPixelSize(android.R.dimen.notification_large_icon_height)); } diff --git a/src/com/android/networkrecommendation/util/Blog.java b/src/com/android/networkrecommendation/util/Blog.java new file mode 100644 index 0000000..47eff57 --- /dev/null +++ b/src/com/android/networkrecommendation/util/Blog.java @@ -0,0 +1,176 @@ +/* + * 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.util; + +import android.text.TextUtils; +import android.util.Base64; +import android.util.Log; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.IllegalFormatException; +import java.util.Locale; + +/** + * Wrapper for {@link Log} which adds the calling class/method to logged items. + * + * This works by traversing up the call stack and finding the first calling class whose name does + * not end with "Log" (allowing clients to add another layer such as AppLog when a constant tag is + * desired across the application). + */ +public final class Blog { + private static final String TAG = "Blog"; + + private Blog() {} + + public static void i(String tag, String format, Object... args) { + Log.i(tag, buildMessage(format, args)); + } + + public static void i(String tag, Throwable tr, String format, Object... args) { + Log.i(tag, buildMessage(format, args), tr); + } + + public static void v(String tag, String format, Object... args) { + // Not guarded with Log.isLoggable - these calls should be stripped out by proguard for + // release builds. + Log.v(tag, buildMessage(format, args)); + } + + public static void v(String tag, Throwable tr, String format, Object... args) { + // Not guarded with Log.isLoggable - these calls should be stripped out by proguard for + // release builds. + Log.v(tag, buildMessage(format, args), tr); + } + + public static void d(String tag, String format, Object... args) { + if (Log.isLoggable(tag, Log.DEBUG)) { + Log.d(tag, buildMessage(format, args)); + } + } + + public static void d(String tag, Throwable tr, String format, Object... args) { + if (Log.isLoggable(tag, Log.DEBUG)) { + Log.d(tag, buildMessage(format, args), tr); + } + } + + public static void w(String tag, Throwable tr, String format, Object... args) { + Log.w(tag, buildMessage(format, args), tr); + } + + public static void w(String tag, String format, Object... args) { + Log.w(tag, buildMessage(format, args)); + } + + public static void e(String tag, String format, Object... args) { + Log.e(tag, buildMessage(format, args)); + } + + public static void e(String tag, Throwable tr, String format, Object... args) { + Log.e(tag, buildMessage(format, args), tr); + } + + public static void wtf(String tag, String format, Object... args) { + // Ensure we always log a stack trace when calling Log.wtf. + Log.wtf(tag, buildMessage(format, args), new WhatATerribleException()); + } + + public static void wtf(String tag, Throwable tr, String format, Object...args) { + Log.wtf(tag, buildMessage(format, args), new WhatATerribleException(tr)); + } + + /** + * Redact personally identifiable information for production users. + * + * If: + * 1) String is null + * 2) String is empty + * 3) enableSensitiveLogging param passed in is true + * Return the String value of the input object. + * Else: + * Return a SHA-1 hash of the String value of the input object. + */ + public static String pii(Object pii, boolean enableSensitiveLogging) { + String val = String.valueOf(pii); + if (pii == null || TextUtils.isEmpty(val) || enableSensitiveLogging) { + return val; + } + return "[" + secureHash(val.getBytes()) + "]"; + } + + /** + * Returns a secure hash (using the SHA1 algorithm) of the provided input. + * + * @return the hash + * @param input the bytes for which the secure hash should be computed. + */ + public static String secureHash(byte[] input) { + MessageDigest messageDigest; + try { + messageDigest = MessageDigest.getInstance("SHA-1"); + } catch (NoSuchAlgorithmException e) { + return null; + } + messageDigest.update(input); + byte[] result = messageDigest.digest(); + return Base64.encodeToString( + result, Base64.URL_SAFE | Base64.NO_PADDING | Base64.NO_WRAP); + } + + /** + * Formats the caller's provided message and prepends useful info like + * calling thread ID and method name. + */ + private static String buildMessage(String format, Object... args) { + String msg; + try { + msg = (args == null || args.length == 0) ? format + : String.format(Locale.US, format, args); + } catch (IllegalFormatException ife) { + String formattedArgs = Arrays.toString(args); + wtf(TAG, ife, "msg: \"%s\" args: %s", format, formattedArgs); + msg = format + " " + formattedArgs; + } + StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace(); + String caller = "<unknown>"; + // Walk up the stack looking for the first caller outside of Blog whose name doesn't end + // with "Log". It will be at least two frames up, so start there. + for (int i = 2; i < trace.length; i++) { + String callingClass = trace[i].getClassName(); + if (!callingClass.equals(Blog.class.getName()) + && !callingClass.endsWith("Log")) { + callingClass = callingClass.substring(callingClass.lastIndexOf('.') + 1); + callingClass = callingClass.substring(callingClass.lastIndexOf('$') + 1); + caller = callingClass + "." + trace[i].getMethodName(); + break; + } + } + return String.format(Locale.US, "[%d] %s: %s", + Thread.currentThread().getId(), caller, msg); + } + + /** Named exception thrown when calling wtf(). */ + public static class WhatATerribleException extends RuntimeException { + public WhatATerribleException() { + super(); + } + public WhatATerribleException(Throwable t) { + super(t); + } + } +} diff --git a/src/com/android/networkrecommendation/util/ImageUtils.java b/src/com/android/networkrecommendation/util/ImageUtil.java index e870563..8bf3de0 100644 --- a/src/com/android/networkrecommendation/util/ImageUtils.java +++ b/src/com/android/networkrecommendation/util/ImageUtil.java @@ -22,7 +22,7 @@ import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; /** Helper for image manipulation */ -public class ImageUtils { +public class ImageUtil { /** * Convert a drawable to a bitmap, scaled to fit within maxWidth and maxHeight. diff --git a/src/com/android/networkrecommendation/wakeup/WifiWakeupController.java b/src/com/android/networkrecommendation/wakeup/WifiWakeupController.java index a62bfbd..f07f0b6 100644 --- a/src/com/android/networkrecommendation/wakeup/WifiWakeupController.java +++ b/src/com/android/networkrecommendation/wakeup/WifiWakeupController.java @@ -32,8 +32,8 @@ import android.support.annotation.VisibleForTesting; import android.text.TextUtils; import android.util.ArrayMap; import android.util.ArraySet; -import android.util.Log; +import com.android.networkrecommendation.util.Blog; import com.android.networkrecommendation.util.WifiConfigurationUtil; import java.io.FileDescriptor; @@ -56,8 +56,6 @@ import java.util.concurrent.atomic.AtomicBoolean; */ public class WifiWakeupController { private static final String TAG = "WifiWakeupController"; - private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); - private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); /** Number of scans to ensure that a previously in range AP is now out of range. */ private static final int NUM_SCANS_TO_CONFIRM_AP_LOSS = 3; @@ -125,9 +123,7 @@ public class WifiWakeupController { if (!mStarted.compareAndSet(false, true)) { return; } - if (DEBUG) { - Log.d(TAG, "Starting WifiWakeupController."); - } + Blog.d(TAG, "Starting WifiWakeupController."); IntentFilter filter = new IntentFilter(); filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); @@ -151,18 +147,14 @@ public class WifiWakeupController { if (!mStarted.compareAndSet(true, false)) { return; } - if (DEBUG) { - Log.d(TAG, "Stopping WifiWakeupController."); - } + Blog.d(TAG, "Stopping WifiWakeupController."); mContext.unregisterReceiver(mBroadcastReceiver); mContentResolver.unregisterContentObserver(mContentObserver); } private void handleWifiApStateChanged() { mWifiApState = mWifiManager.getWifiApState(); - if (VERBOSE) { - Log.v(TAG, "handleWifiApStateChanged: " + mWifiApState); - } + Blog.v(TAG, "handleWifiApStateChanged: " + mWifiApState); } private void handleConfiguredNetworksChanged() { @@ -170,9 +162,7 @@ public class WifiWakeupController { if (wifiConfigurations == null) { return; } - if (VERBOSE) { - Log.v(TAG, "handleConfiguredNetworksChanged: " + wifiConfigurations.size()); - } + Blog.v(TAG, "handleConfiguredNetworksChanged: " + wifiConfigurations.size()); mSavedNetworks.clear(); mSavedSsids.clear(); @@ -201,9 +191,7 @@ public class WifiWakeupController { private void handleWifiStateChanged() { mWifiState = mWifiManager.getWifiState(); - if (VERBOSE) { - Log.v(TAG, "handleWifiStateChanged: " + mWifiState); - } + Blog.v(TAG, "handleWifiStateChanged: " + mWifiState); switch (mWifiState) { case WifiManager.WIFI_STATE_ENABLED: mSavedSsidsOnDisable.clear(); @@ -221,9 +209,7 @@ public class WifiWakeupController { if (scanResults == null) { return; } - if (VERBOSE) { - Log.v(TAG, "handleScanResultsAvailable: " + scanResults.size()); - } + Blog.v(TAG, "handleScanResultsAvailable: " + scanResults.size()); mSavedSsidsInLastScan.clear(); for (int i = 0; i < scanResults.size(); i++) { @@ -253,19 +239,15 @@ public class WifiWakeupController { } if (!mSavedSsidsOnDisable.isEmpty()) { - if (DEBUG) { - Log.d(TAG, "Latest scan result contains ssids from the disabled set: " - + mSavedSsidsOnDisable); - } + Blog.d(TAG, "Latest scan result contains ssids from the disabled set: " + + mSavedSsidsOnDisable); return; } WifiConfiguration selectedNetwork = mWifiWakeupNetworkSelector.selectNetwork(mSavedNetworks, scanResults); if (selectedNetwork != null) { - if (DEBUG) { - Log.d(TAG, "Enabling wifi for ssid: " + selectedNetwork.SSID); - } + Blog.d(TAG, "Enabling wifi for ssid: " + selectedNetwork.SSID); mWifiManager.setWifiEnabled(true /* enabled */); mWifiWakeupNotificationHelper.maybeShowWifiEnabledNotification(selectedNetwork); } diff --git a/src/com/android/networkrecommendation/wakeup/WifiWakeupNotificationHelper.java b/src/com/android/networkrecommendation/wakeup/WifiWakeupNotificationHelper.java index 2659752..968a71c 100644 --- a/src/com/android/networkrecommendation/wakeup/WifiWakeupNotificationHelper.java +++ b/src/com/android/networkrecommendation/wakeup/WifiWakeupNotificationHelper.java @@ -34,9 +34,9 @@ import android.support.annotation.NonNull; import android.support.annotation.VisibleForTesting; import android.text.TextUtils; import android.util.ArraySet; -import android.util.Log; import com.android.networkrecommendation.R; +import com.android.networkrecommendation.util.Blog; import java.util.Set; import java.util.concurrent.TimeUnit; @@ -46,7 +46,6 @@ import java.util.concurrent.TimeUnit; */ public class WifiWakeupNotificationHelper { private static final String TAG = "WifiWakeupNotifHelper"; - private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); /** Unique ID used for the Wi-Fi Enabled notification. */ private static final int NOTIFICATION_ID = R.string.wifi_wakeup_enabled_notification_title; @@ -121,10 +120,8 @@ public class WifiWakeupNotificationHelper { if (ssidSet == null) { ssidSet = new ArraySet<>(); } else if (ssidSet.contains(wifiConfiguration.SSID)) { - if (DEBUG) { - Log.d(TAG, "Already showed Wi-Fi Enabled notification for ssid: " - + wifiConfiguration.SSID); - } + Blog.d(TAG, "Already showed Wi-Fi Enabled notification for ssid: " + + wifiConfiguration.SSID); return; } ssidSet.add(wifiConfiguration.SSID); |