Use/expose the receiver's label as the scorer name.

Allows scorer apps to set a custom name for the scoring functionality,
even if the app has other non-scoring responsibilities, in which case
the application's label would be too generic.

Bug: 15775300
Change-Id: I61a7d1966742d463508e6801b7845ced8392db84
diff --git a/core/java/android/net/NetworkScoreManager.java b/core/java/android/net/NetworkScoreManager.java
index 352512e..1a9a6378 100644
--- a/core/java/android/net/NetworkScoreManager.java
+++ b/core/java/android/net/NetworkScoreManager.java
@@ -20,6 +20,7 @@
 import android.annotation.SdkConstant.SdkConstantType;
 import android.content.Context;
 import android.content.Intent;
+import android.net.NetworkScorerAppManager.NetworkScorerAppData;
 import android.os.IBinder;
 import android.os.RemoteException;
 import android.os.ServiceManager;
@@ -38,7 +39,9 @@
  * <li>Declares the {@link android.Manifest.permission#SCORE_NETWORKS} permission.
  * <li>Includes a receiver for {@link #ACTION_SCORE_NETWORKS} guarded by the
  *     {@link android.Manifest.permission#BROADCAST_SCORE_NETWORKS} permission which scores networks
- *     and (eventually) calls {@link #updateScores} with the results.
+ *     and (eventually) calls {@link #updateScores} with the results. If this receiver specifies an
+ *     android:label attribute, this label will be used when referring to the application throughout
+ *     system settings; otherwise, the application label will be used.
  * </ul>
  *
  * <p>The system keeps track of an active scorer application; at any time, only this application
@@ -105,7 +108,11 @@
      *         scorer.
      */
     public String getActiveScorerPackage() {
-        return NetworkScorerAppManager.getActiveScorer(mContext);
+        NetworkScorerAppData app = NetworkScorerAppManager.getActiveScorer(mContext);
+        if (app == null) {
+            return null;
+        }
+        return app.mPackageName;
     }
 
     /**
diff --git a/core/java/android/net/NetworkScorerAppManager.java b/core/java/android/net/NetworkScorerAppManager.java
index 3660e7a..7c61710 100644
--- a/core/java/android/net/NetworkScorerAppManager.java
+++ b/core/java/android/net/NetworkScorerAppManager.java
@@ -24,7 +24,6 @@
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.provider.Settings;
-import android.provider.Settings.Global;
 import android.text.TextUtils;
 import android.util.Log;
 
@@ -35,6 +34,8 @@
 /**
  * Internal class for managing the primary network scorer application.
  *
+ * TODO: Rename this to something more generic.
+ *
  * @hide
  */
 public final class NetworkScorerAppManager {
@@ -46,8 +47,21 @@
     /** This class cannot be instantiated. */
     private NetworkScorerAppManager() {}
 
+    public static class NetworkScorerAppData {
+        /** Package name of this scorer app. */
+        public final String mPackageName;
+
+        /** Name of this scorer app for display. */
+        public final CharSequence mScorerName;
+
+        public NetworkScorerAppData(String packageName, CharSequence scorerName) {
+            mScorerName = scorerName;
+            mPackageName = packageName;
+        }
+    }
+
     /**
-     * Returns the list of available scorer app package names.
+     * Returns the list of available scorer apps.
      *
      * <p>A network scorer is any application which:
      * <ul>
@@ -58,8 +72,8 @@
      *
      * @return the list of scorers, or the empty list if there are no valid scorers.
      */
-    public static Collection<String> getAllValidScorers(Context context) {
-        List<String> scorers = new ArrayList<>();
+    public static Collection<NetworkScorerAppData> getAllValidScorers(Context context) {
+        List<NetworkScorerAppData> scorers = new ArrayList<>();
 
         PackageManager pm = context.getPackageManager();
         List<ResolveInfo> receivers = pm.queryBroadcastReceivers(SCORE_INTENT, 0 /* flags */);
@@ -81,33 +95,32 @@
                 // approved it as a network scorer.
                 continue;
             }
-            scorers.add(receiverInfo.packageName);
+            // NOTE: loadLabel will attempt to load the receiver's label and fall back to the app
+            // label if none is present.
+            scorers.add(new NetworkScorerAppData(
+                    receiverInfo.packageName, receiverInfo.loadLabel(pm)));
         }
 
         return scorers;
     }
 
     /**
-     * Get the application package name to use for scoring networks.
+     * Get the application to use for scoring networks.
      *
-     * @return the scorer package or null if scoring is disabled (including if no scorer was ever
+     * @return the scorer app info or null if scoring is disabled (including if no scorer was ever
      *     selected) or if the previously-set scorer is no longer a valid scorer app (e.g. because
      *     it was disabled or uninstalled).
      */
-    public static String getActiveScorer(Context context) {
+    public static NetworkScorerAppData getActiveScorer(Context context) {
         String scorerPackage = Settings.Global.getString(context.getContentResolver(),
-                Global.NETWORK_SCORER_APP);
-        if (isPackageValidScorer(context, scorerPackage)) {
-            return scorerPackage;
-        } else {
-            return null;
-        }
+                Settings.Global.NETWORK_SCORER_APP);
+        return getScorer(context, scorerPackage);
     }
 
     /**
      * Set the specified package as the default scorer application.
      *
-     * <p>The caller must have permission to write to {@link Settings.Global}.
+     * <p>The caller must have permission to write to {@link android.provider.Settings.Global}.
      *
      * @param context the context of the calling application
      * @param packageName the packageName of the new scorer to use. If null, scoring will be
@@ -125,12 +138,12 @@
         Log.i(TAG, "Changing network scorer from " + oldPackageName + " to " + packageName);
 
         if (packageName == null) {
-            Settings.Global.putString(context.getContentResolver(), Global.NETWORK_SCORER_APP,
-                    null);
+            Settings.Global.putString(context.getContentResolver(),
+                    Settings.Global.NETWORK_SCORER_APP, null);
             return true;
         } else {
             // We only make the change if the new package is valid.
-            if (isPackageValidScorer(context, packageName)) {
+            if (getScorer(context, packageName) != null) {
                 Settings.Global.putString(context.getContentResolver(),
                         Settings.Global.NETWORK_SCORER_APP, packageName);
                 return true;
@@ -143,22 +156,30 @@
 
     /** Determine whether the application with the given UID is the enabled scorer. */
     public static boolean isCallerActiveScorer(Context context, int callingUid) {
-        String defaultApp = getActiveScorer(context);
+        NetworkScorerAppData defaultApp = getActiveScorer(context);
         if (defaultApp == null) {
             return false;
         }
         AppOpsManager appOpsMgr = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
         try {
-            appOpsMgr.checkPackage(callingUid, defaultApp);
+            appOpsMgr.checkPackage(callingUid, defaultApp.mPackageName);
             return true;
         } catch (SecurityException e) {
             return false;
         }
     }
 
-    /** Returns true if the given package is a valid scorer. */
-    public static boolean isPackageValidScorer(Context context, String packageName) {
-        Collection<String> applications = getAllValidScorers(context);
-        return packageName != null && applications.contains(packageName);
+    /** Returns the {@link NetworkScorerAppData} for the given app, or null if it's not a scorer. */
+    public static NetworkScorerAppData getScorer(Context context, String packageName) {
+        if (TextUtils.isEmpty(packageName)) {
+            return null;
+        }
+        Collection<NetworkScorerAppData> applications = getAllValidScorers(context);
+        for (NetworkScorerAppData app : applications) {
+            if (packageName.equals(app.mPackageName)) {
+                return app;
+            }
+        }
+        return null;
     }
 }
diff --git a/core/tests/coretests/src/android/net/NetworkScorerAppManagerTest.java b/core/tests/coretests/src/android/net/NetworkScorerAppManagerTest.java
index cac6b93..ff2c8f0 100644
--- a/core/tests/coretests/src/android/net/NetworkScorerAppManagerTest.java
+++ b/core/tests/coretests/src/android/net/NetworkScorerAppManagerTest.java
@@ -20,8 +20,10 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ActivityInfo;
+import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
+import android.net.NetworkScorerAppManager.NetworkScorerAppData;
 import android.test.InstrumentationTestCase;
 
 import com.google.android.collect.Lists;
@@ -63,11 +65,11 @@
 
         setScorers(package1, package2, package3);
 
-        Iterator<String> result =
+        Iterator<NetworkScorerAppData> result =
                 NetworkScorerAppManager.getAllValidScorers(mMockContext).iterator();
 
         assertTrue(result.hasNext());
-        assertEquals("package1", result.next());
+        assertEquals("package1", result.next().mPackageName);
 
         assertFalse(result.hasNext());
     }
@@ -93,6 +95,7 @@
         ResolveInfo resolveInfo = new ResolveInfo();
         resolveInfo.activityInfo = new ActivityInfo();
         resolveInfo.activityInfo.packageName = packageName;
+        resolveInfo.activityInfo.applicationInfo = new ApplicationInfo();
         if (hasReceiverPermission) {
             resolveInfo.activityInfo.permission = permission.BROADCAST_SCORE_NETWORKS;
         }
diff --git a/services/core/java/com/android/server/NetworkScoreService.java b/services/core/java/com/android/server/NetworkScoreService.java
index 512ebc6..1b71518 100644
--- a/services/core/java/com/android/server/NetworkScoreService.java
+++ b/services/core/java/com/android/server/NetworkScoreService.java
@@ -23,6 +23,7 @@
 import android.net.INetworkScoreCache;
 import android.net.INetworkScoreService;
 import android.net.NetworkScorerAppManager;
+import android.net.NetworkScorerAppManager.NetworkScorerAppData;
 import android.net.ScoredNetwork;
 import android.os.RemoteException;
 import android.text.TextUtils;
@@ -165,12 +166,12 @@
     @Override
     protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
         mContext.enforceCallingOrSelfPermission(permission.DUMP, TAG);
-        String currentScorer = NetworkScorerAppManager.getActiveScorer(mContext);
+        NetworkScorerAppData currentScorer = NetworkScorerAppManager.getActiveScorer(mContext);
         if (currentScorer == null) {
             writer.println("Scoring is disabled.");
             return;
         }
-        writer.println("Current scorer: " + currentScorer);
+        writer.println("Current scorer: " + currentScorer.mPackageName);
         writer.flush();
 
         for (INetworkScoreCache scoreCache : getScoreCaches()) {