From ce106d20d4bb71ab13f4f351a41ab0481ae49609 Mon Sep 17 00:00:00 2001 From: Quang Luong Date: Wed, 6 May 2020 21:05:56 -0700 Subject: [WifiTrackerLib] Use open wifi package name for app label if applicable App label of an open wifi network should use the open wifi package name instead of the active scorer name. Bug: 153352576 Test: manual visual verification that connected open wifi network uses the open wifi package name instead of the active scorer name Change-Id: I1e5dd068a5e9086143b928e3f16fec86b23b89b3 --- .../android/wifitrackerlib/PasspointWifiEntry.java | 3 +- .../android/wifitrackerlib/StandardWifiEntry.java | 6 +- .../src/com/android/wifitrackerlib/Utils.java | 65 +++++++++------------- .../src/com/android/wifitrackerlib/UtilsTest.java | 25 --------- 4 files changed, 28 insertions(+), 71 deletions(-) diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java index 350580250..8b66fd5e8 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/PasspointWifiEntry.java @@ -21,7 +21,6 @@ import static android.net.wifi.WifiInfo.sanitizeSsid; import static androidx.core.util.Preconditions.checkNotNull; import static com.android.wifitrackerlib.Utils.getAppLabel; -import static com.android.wifitrackerlib.Utils.getAppLabelForWifiConfiguration; import static com.android.wifitrackerlib.Utils.getAutoConnectDescription; import static com.android.wifitrackerlib.Utils.getAverageSpeedFromScanResults; import static com.android.wifitrackerlib.Utils.getBestScanResultByLevel; @@ -167,7 +166,7 @@ public class PasspointWifiEntry extends WifiEntry { getSubIdForConfig(mContext, mWifiConfig)); sj.add(mContext.getString(R.string.available_via_app, carrierName != null ? carrierName - : getAppLabelForWifiConfiguration(mContext, mWifiConfig))); + : getAppLabel(mContext, mWifiConfig.creatorName))); } else { sj.add(mContext.getString(R.string.wifi_remembered)); } diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java index 06b27656a..5518a39bb 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/StandardWifiEntry.java @@ -25,8 +25,6 @@ import static android.net.wifi.WifiInfo.sanitizeSsid; import static androidx.core.util.Preconditions.checkNotNull; import static com.android.wifitrackerlib.Utils.getAppLabel; -import static com.android.wifitrackerlib.Utils.getAppLabelForSavedNetwork; -import static com.android.wifitrackerlib.Utils.getAppLabelForWifiConfiguration; import static com.android.wifitrackerlib.Utils.getAutoConnectDescription; import static com.android.wifitrackerlib.Utils.getAverageSpeedFromScanResults; import static com.android.wifitrackerlib.Utils.getBestScanResultByLevel; @@ -196,7 +194,7 @@ public class StandardWifiEntry extends WifiEntry { StringJoiner sj = new StringJoiner(mContext.getString(R.string.summary_separator)); if (!concise && mForSavedNetworksPage && isSaved()) { - final CharSequence appLabel = getAppLabelForSavedNetwork(mContext, this); + final CharSequence appLabel = getAppLabel(mContext, mWifiConfig.creatorName); if (!TextUtils.isEmpty(appLabel)) { sj.add(mContext.getString(R.string.saved_network, appLabel)); } @@ -214,7 +212,7 @@ public class StandardWifiEntry extends WifiEntry { getSubIdForConfig(mContext, mWifiConfig)); sj.add(mContext.getString(R.string.available_via_app, carrierName != null ? carrierName - : getAppLabelForWifiConfiguration(mContext, mWifiConfig))); + : getAppLabel(mContext, mWifiConfig.creatorName))); } else if (isSaved()) { sj.add(mContext.getString(R.string.wifi_remembered)); } diff --git a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java index ced1d81bd..1125e086c 100644 --- a/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java +++ b/libs/WifiTrackerLib/src/com/android/wifitrackerlib/Utils.java @@ -37,15 +37,14 @@ import static com.android.wifitrackerlib.WifiEntry.Speed; import static java.util.Comparator.comparingInt; import static java.util.stream.Collectors.groupingBy; -import android.app.AppGlobals; import android.content.Context; import android.content.pm.ApplicationInfo; -import android.content.pm.IPackageManager; import android.content.pm.PackageManager; import android.net.NetworkCapabilities; import android.net.NetworkInfo; import android.net.NetworkInfo.DetailedState; import android.net.NetworkKey; +import android.net.NetworkScoreManager; import android.net.ScoredNetwork; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; @@ -53,8 +52,8 @@ import android.net.wifi.WifiConfiguration.NetworkSelectionStatus; import android.net.wifi.WifiInfo; import android.net.wifi.WifiNetworkScoreCache; import android.os.PersistableBundle; -import android.os.RemoteException; import android.os.UserHandle; +import android.provider.Settings; import android.telephony.CarrierConfigManager; import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionManager; @@ -83,6 +82,15 @@ import java.util.StringJoiner; * Utility methods for WifiTrackerLib. */ class Utils { + private static NetworkScoreManager sNetworkScoreManager; + + private static String getActiveScorerPackage(@NonNull Context context) { + if (sNetworkScoreManager == null) { + sNetworkScoreManager = context.getSystemService(NetworkScoreManager.class); + } + return sNetworkScoreManager.getActiveScorerPackage(); + } + // Returns the ScanResult with the best RSSI from a list of ScanResults. @Nullable static ScanResult getBestScanResultByLevel(@NonNull List scanResults) { @@ -360,51 +368,28 @@ class Utils { } } + /** + * Get the app label for a suggestion/specifier package name. + */ static CharSequence getAppLabel(Context context, String packageName) { try { + String openWifiPackageName = Settings.Global.getString(context.getContentResolver(), + Settings.Global.USE_OPEN_WIFI_PACKAGE); + if (!TextUtils.isEmpty(openWifiPackageName) && TextUtils.equals(packageName, + getActiveScorerPackage(context))) { + packageName = openWifiPackageName; + } + ApplicationInfo appInfo = context.getPackageManager().getApplicationInfoAsUser( packageName, 0 /* flags */, UserHandle.getUserId(UserHandle.USER_CURRENT)); return appInfo.loadLabel(context.getPackageManager()); } catch (PackageManager.NameNotFoundException e) { - // Do nothing. - } - return ""; - } - - static CharSequence getAppLabelForSavedNetwork(@NonNull Context context, - @NonNull WifiEntry wifiEntry) { - return getAppLabelForWifiConfiguration(context, wifiEntry.getWifiConfiguration()); - } - - static CharSequence getAppLabelForWifiConfiguration(@NonNull Context context, - @NonNull WifiConfiguration config) { - if (context == null || config == null) { - return ""; - } - - final PackageManager pm = context.getPackageManager(); - final String systemName = pm.getNameForUid(android.os.Process.SYSTEM_UID); - final int userId = UserHandle.getUserId(config.creatorUid); - ApplicationInfo appInfo = null; - if (config.creatorName != null && config.creatorName.equals(systemName)) { - appInfo = context.getApplicationInfo(); - } else { - try { - final IPackageManager ipm = AppGlobals.getPackageManager(); - appInfo = ipm.getApplicationInfo(config.creatorName, 0 /* flags */, userId); - } catch (RemoteException rex) { - // Do nothing. - } - } - if (appInfo != null - && !appInfo.packageName.equals(context.getString(R.string.settings_package)) - && !appInfo.packageName.equals( - context.getString(R.string.certinstaller_package))) { - return appInfo.loadLabel(pm); - } else { - return ""; + // The packageName should come from a suggestion/specifier which is guaranteed to + // have an associated app label. If there is a concurrency issue between the current + // connection and the suggestion being removed, we should fall back to the packageName. + return packageName; } } diff --git a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/UtilsTest.java b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/UtilsTest.java index de4b6903c..5532a2304 100644 --- a/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/UtilsTest.java +++ b/libs/WifiTrackerLib/tests/src/com/android/wifitrackerlib/UtilsTest.java @@ -19,7 +19,6 @@ package com.android.wifitrackerlib; import static com.android.wifitrackerlib.StandardWifiEntry.ssidAndSecurityToStandardWifiEntryKey; import static com.android.wifitrackerlib.StandardWifiEntry.wifiConfigToStandardWifiEntryKey; import static com.android.wifitrackerlib.TestUtils.buildScanResult; -import static com.android.wifitrackerlib.Utils.getAppLabelForSavedNetwork; import static com.android.wifitrackerlib.Utils.getAutoConnectDescription; import static com.android.wifitrackerlib.Utils.getBestScanResultByLevel; import static com.android.wifitrackerlib.Utils.getCarrierNameForSubId; @@ -43,8 +42,6 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import android.content.Context; -import android.content.pm.ApplicationInfo; -import android.content.pm.PackageManager; import android.content.res.Resources; import android.net.NetworkInfo; import android.net.NetworkScoreManager; @@ -192,28 +189,6 @@ public class UtilsTest { ssidAndSecurityToStandardWifiEntryKey(oweTransitionScan.SSID, SECURITY_NONE)); } - @Test - public void testGetAppLabelForSavedNetwork_returnAppLabel() { - final PackageManager mockPackageManager = mock(PackageManager.class); - when(mMockContext.getPackageManager()).thenReturn(mockPackageManager); - when(mockPackageManager.getNameForUid(android.os.Process.SYSTEM_UID)) - .thenReturn(SYSTEM_UID_APP_NAME); - final ApplicationInfo mockApplicationInfo = mock(ApplicationInfo.class); - when(mMockContext.getApplicationInfo()).thenReturn(mockApplicationInfo); - mockApplicationInfo.packageName = SYSTEM_UID_APP_NAME; - when(mockApplicationInfo.loadLabel(mockPackageManager)).thenReturn(APP_LABEL); - final WifiConfiguration config = new WifiConfiguration(); - config.SSID = "\"ssid\""; - config.creatorName = SYSTEM_UID_APP_NAME; - final StandardWifiEntry entry = getStandardWifiEntry(config); - when(mMockResources.getString(R.string.settings_package)) - .thenReturn(SETTINGS_APP_NAME); - - final CharSequence appLabel = getAppLabelForSavedNetwork(mMockContext, entry); - - assertThat(appLabel.toString()).isEqualTo(APP_LABEL); - } - @Test public void testGetAutoConnectDescription_autoJoinEnabled_returnEmptyString() { final WifiConfiguration config = new WifiConfiguration(); -- cgit v1.2.3