Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a3b9ca5a authored by Jeff Davidson's avatar Jeff Davidson Committed by Android (Google) Code Review
Browse files

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

parents 3d3317db c7415536
Loading
Loading
Loading
Loading
+9 −2
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.annotation.SdkConstant;
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 @@ import android.os.ServiceManager;
 * <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 @@ public class NetworkScoreManager {
     *         scorer.
     */
    public String getActiveScorerPackage() {
        return NetworkScorerAppManager.getActiveScorer(mContext);
        NetworkScorerAppData app = NetworkScorerAppManager.getActiveScorer(mContext);
        if (app == null) {
            return null;
        }
        return app.mPackageName;
    }

    /**
+45 −24
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ import android.content.pm.ActivityInfo;
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 @@ import java.util.List;
/**
 * 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 @@ public final class NetworkScorerAppManager {
    /** 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 @@ public final class NetworkScorerAppManager {
     *
     * @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 @@ public final class NetworkScorerAppManager {
                // 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 @@ public final class NetworkScorerAppManager {
        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 @@ public final class NetworkScorerAppManager {

    /** 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;
    }
}
+5 −2
Original line number Diff line number Diff line
@@ -20,8 +20,10 @@ import android.Manifest.permission;
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 @@ public class NetworkScorerAppManagerTest extends InstrumentationTestCase {

        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 @@ public class NetworkScorerAppManagerTest extends InstrumentationTestCase {
        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;
        }
+3 −2
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.content.pm.PackageManager;
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 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
    @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()) {