|
16 | 16 |
|
17 | 17 | package android.net; |
18 | 18 |
|
| 19 | +import android.Manifest; |
19 | 20 | import android.Manifest.permission; |
20 | 21 | import android.annotation.Nullable; |
21 | 22 | import android.app.AppOpsManager; |
|
24 | 25 | import android.content.pm.ActivityInfo; |
25 | 26 | import android.content.pm.PackageManager; |
26 | 27 | import android.content.pm.ResolveInfo; |
| 28 | +import android.os.UserHandle; |
27 | 29 | import android.provider.Settings; |
28 | 30 | import android.text.TextUtils; |
29 | 31 | import android.util.Log; |
@@ -86,7 +88,9 @@ public static Collection<NetworkScorerAppData> getAllValidScorers(Context contex |
86 | 88 | List<NetworkScorerAppData> scorers = new ArrayList<>(); |
87 | 89 |
|
88 | 90 | PackageManager pm = context.getPackageManager(); |
89 | | - List<ResolveInfo> receivers = pm.queryBroadcastReceivers(SCORE_INTENT, 0 /* flags */); |
| 91 | + // Only apps installed under the primary user of the device can be scorers. |
| 92 | + List<ResolveInfo> receivers = |
| 93 | + pm.queryBroadcastReceivers(SCORE_INTENT, 0 /* flags */, UserHandle.USER_OWNER); |
90 | 94 | for (ResolveInfo receiver : receivers) { |
91 | 95 | // This field is a misnomer, see android.content.pm.ResolveInfo#activityInfo |
92 | 96 | final ActivityInfo receiverInfo = receiver.activityInfo; |
@@ -186,10 +190,14 @@ public static boolean isCallerActiveScorer(Context context, int callingUid) { |
186 | 190 | AppOpsManager appOpsMgr = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); |
187 | 191 | try { |
188 | 192 | appOpsMgr.checkPackage(callingUid, defaultApp.mPackageName); |
189 | | - return true; |
190 | 193 | } catch (SecurityException e) { |
191 | 194 | return false; |
192 | 195 | } |
| 196 | + |
| 197 | + // To be extra safe, ensure the caller holds the SCORE_NETWORKS permission. It always |
| 198 | + // should, since it couldn't become the active scorer otherwise, but this can't hurt. |
| 199 | + return context.checkCallingPermission(Manifest.permission.SCORE_NETWORKS) == |
| 200 | + PackageManager.PERMISSION_GRANTED; |
193 | 201 | } |
194 | 202 |
|
195 | 203 | /** Returns the {@link NetworkScorerAppData} for the given app, or null if it's not a scorer. */ |
|
0 commit comments