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

Commit d05dda1d authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes from topic 'allow_none_scorer' into oc-dev

* changes:
  Revert permission check on setActiveScorer.
  Allow the active scorer to be set to NULL.
parents c7642c32 a9f933e5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ interface INetworkScoreService
     * Set the active scorer and clear existing scores.
     * @param packageName the package name of the new scorer to use.
     * @return true if the operation succeeded, or false if the new package is not a valid scorer.
     * @throws SecurityException if the caller is not the system.
     * @throws SecurityException if the caller is not the system or a network scorer.
     */
    boolean setActiveScorer(in String packageName);

+8 −34
Original line number Diff line number Diff line
@@ -16,27 +16,20 @@

package android.net;

import static android.net.NetworkRecommendationProvider.EXTRA_RECOMMENDATION_RESULT;

import android.Manifest.permission;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SystemApi;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.RemoteCallback;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.ServiceManager.ServiceNotFoundException;
import com.android.internal.util.Preconditions;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * Class that manages communication between network subsystems and a network scorer.
@@ -49,9 +42,9 @@ import java.util.concurrent.CompletableFuture;
 *
 * <p>A network scorer is any application which:
 * <ul>
 * <li>Declares the {@link android.Manifest.permission#SCORE_NETWORKS} permission.
 * <li>Declares the {@link permission#SCORE_NETWORKS} permission.
 * <li>Include a Service for the {@link #ACTION_RECOMMEND_NETWORKS} action
 *     protected by the {@link android.Manifest.permission#BIND_NETWORK_RECOMMENDATION_SERVICE}
 *     protected by the {@link permission#BIND_NETWORK_RECOMMENDATION_SERVICE}
 *     permission.
 * </ul>
 *
@@ -319,7 +312,7 @@ public class NetworkScoreManager {
     *
     * @return true if the operation succeeded, or false if the new package is not a valid scorer.
     * @throws SecurityException if the caller is not a system process or does not hold the
     *         {@link android.Manifest.permission#REQUEST_NETWORK_SCORES} permission
     *         {@link permission#SCORE_NETWORKS} permission
     * @hide
     */
    @SystemApi
@@ -351,7 +344,7 @@ public class NetworkScoreManager {
     *
     * @return true if the broadcast was sent, or false if there is no active scorer.
     * @throws SecurityException if the caller does not hold the
     *         {@link android.Manifest.permission#REQUEST_NETWORK_SCORES} permission.
     *         {@link permission#REQUEST_NETWORK_SCORES} permission.
     * @hide
     */
    public boolean requestScores(NetworkKey[] networks) throws SecurityException {
@@ -368,7 +361,7 @@ public class NetworkScoreManager {
     * @param networkType the type of network this cache can handle. See {@link NetworkKey#type}.
     * @param scoreCache implementation of {@link INetworkScoreCache} to store the scores.
     * @throws SecurityException if the caller does not hold the
     *         {@link android.Manifest.permission#REQUEST_NETWORK_SCORES} permission.
     *         {@link permission#REQUEST_NETWORK_SCORES} permission.
     * @throws IllegalArgumentException if a score cache is already registered for this type.
     * @deprecated equivalent to registering for cache updates with CACHE_FILTER_NONE.
     * @hide
@@ -385,7 +378,7 @@ public class NetworkScoreManager {
     * @param scoreCache implementation of {@link INetworkScoreCache} to store the scores
     * @param filterType the {@link CacheUpdateFilter} to apply
     * @throws SecurityException if the caller does not hold the
     *         {@link android.Manifest.permission#REQUEST_NETWORK_SCORES} permission.
     *         {@link permission#REQUEST_NETWORK_SCORES} permission.
     * @throws IllegalArgumentException if a score cache is already registered for this type.
     * @hide
     */
@@ -404,7 +397,7 @@ public class NetworkScoreManager {
     * @param networkType the type of network this cache can handle. See {@link NetworkKey#type}.
     * @param scoreCache implementation of {@link INetworkScoreCache} to store the scores.
     * @throws SecurityException if the caller does not hold the
     *         {@link android.Manifest.permission#REQUEST_NETWORK_SCORES} permission.
     *         {@link permission#REQUEST_NETWORK_SCORES} permission.
     * @throws IllegalArgumentException if a score cache is already registered for this type.
     * @hide
     */
@@ -416,25 +409,6 @@ public class NetworkScoreManager {
        }
    }

    /**
     * Request a recommendation for which network to connect to.
     *
     * <p>It is not safe to call this method from the main thread.
     *
     * @param request a {@link RecommendationRequest} instance containing additional
     *                request details
     * @return a {@link RecommendationResult} instance containing the recommended network
     *         to connect to
     * @throws SecurityException if the caller does not hold the
     *         {@link android.Manifest.permission#REQUEST_NETWORK_SCORES} permission.
     * @hide
     * @deprecated to be removed.
     */
    public RecommendationResult requestRecommendation(RecommendationRequest request)
            throws SecurityException {
        return null;
    }

    /**
     * Determine whether the application with the given UID is the enabled scorer.
     *
+7 −2
Original line number Diff line number Diff line
@@ -631,6 +631,11 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
                 PackageManager.PERMISSION_GRANTED;
    }

    private boolean callerCanScoreNetworks() {
        return mContext.checkCallingOrSelfPermission(permission.SCORE_NETWORKS) ==
                PackageManager.PERMISSION_GRANTED;
    }

    @Override
    public boolean clearScores() {
        // Only the active scorer or the system should be allowed to flush all scores.
@@ -651,9 +656,9 @@ public class NetworkScoreService extends INetworkScoreService.Stub {
    @Override
    public boolean setActiveScorer(String packageName) {
        // Only the system can set the active scorer
        if (!isCallerSystemProcess(getCallingUid()) && !callerCanRequestScores()) {
        if (!isCallerSystemProcess(getCallingUid()) && !callerCanScoreNetworks()) {
            throw new SecurityException(
                    "Caller is neither the system process nor a score requester.");
                    "Caller is neither the system process or a network scorer.");
        }

        return mNetworkScorerAppManager.setActiveScorer(packageName);
+13 −10
Original line number Diff line number Diff line
@@ -208,11 +208,11 @@ public class NetworkScorerAppManager {
     *
     * <p>The caller must have permission to write to {@link Settings.Global}.
     *
     * @param packageName the packageName of the new scorer to use. If null, the scoring app will
     *                    revert back to the configured default. Otherwise, the scorer will only
     *                    be set if it is a valid scorer application.
     * @return true if the scorer was changed, or false if the package is not a valid scorer or
     *         a valid network recommendation provider exists.
     * @param packageName the packageName of the new scorer to use. If null, scoring will be forced
     *                    off, otherwise the scorer will only be set if it is a valid scorer
     *                    application.
     * @return true if the package was a valid scorer (including <code>null</code>) and now
     *         represents the active scorer, false otherwise.
     */
    @VisibleForTesting
    public boolean setActiveScorer(String packageName) {
@@ -223,16 +223,19 @@ public class NetworkScorerAppManager {
            return true;
        }

        if (packageName == null) {
            // revert to the default setting.
            packageName = getDefaultPackageSetting();
        if (TextUtils.isEmpty(packageName)) {
            Log.i(TAG, "Network scorer forced off, was: " + oldPackageName);
            setNetworkRecommendationsPackage(null);
            setNetworkRecommendationsEnabledSetting(
                    NetworkScoreManager.RECOMMENDATIONS_ENABLED_FORCED_OFF);
            return true;
        }

        Log.i(TAG, "Changing network scorer from " + oldPackageName + " to " + packageName);

        // We only make the change if the new package is valid.
        if (getScorer(packageName) != null) {
            Log.i(TAG, "Changing network scorer from " + oldPackageName + " to " + packageName);
            setNetworkRecommendationsPackage(packageName);
            setNetworkRecommendationsEnabledSetting(NetworkScoreManager.RECOMMENDATIONS_ENABLED_ON);
            return true;
        } else {
            Log.w(TAG, "Requested network scorer is not valid: " + packageName);
+2 −2
Original line number Diff line number Diff line
@@ -368,8 +368,8 @@ public class NetworkScoreServiceTest {
    }

    @Test
    public void testSetActiveScorer_noRequestNetworkScoresPermission() {
        when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES))
    public void testSetActiveScorer_noScoreNetworksPermission() {
        when(mContext.checkCallingOrSelfPermission(permission.SCORE_NETWORKS))
                .thenReturn(PackageManager.PERMISSION_DENIED);

        try {
Loading