Merge "Add a network scorer label metadata field to NetworkScoreAppData"

This commit is contained in:
TreeHugger Robot
2017-03-01 21:54:33 +00:00
committed by Android (Google) Code Review
5 changed files with 81 additions and 7 deletions

View File

@@ -102,6 +102,14 @@ public class NetworkScoreManager {
@SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
public static final String ACTION_CUSTOM_ENABLE = "android.net.scoring.CUSTOM_ENABLE";
/**
* Meta-data specified on a {@link NetworkRecommendationProvider} that provides a user-visible
* label of the recommendation service.
* @hide
*/
public static final String RECOMMENDATION_SERVICE_LABEL_META_DATA =
"android.net.scoring.recommendation_service_label";
/**
* Meta-data specified on a {@link NetworkRecommendationProvider} that specified the package
* name of the application that connects and secures open wifi networks automatically. The

View File

@@ -16,6 +16,8 @@ public final class NetworkScorerAppData implements Parcelable {
/** UID of the scorer app. */
public final int packageUid;
private final ComponentName mRecommendationService;
/** User visible label in Settings for the recommendation service. */
private final String mRecommendationServiceLabel;
/**
* The {@link ComponentName} of the Activity to start before enabling the "connect to open
* wifi networks automatically" feature.
@@ -23,15 +25,17 @@ public final class NetworkScorerAppData implements Parcelable {
private final ComponentName mEnableUseOpenWifiActivity;
public NetworkScorerAppData(int packageUid, ComponentName recommendationServiceComp,
ComponentName enableUseOpenWifiActivity) {
String recommendationServiceLabel, ComponentName enableUseOpenWifiActivity) {
this.packageUid = packageUid;
this.mRecommendationService = recommendationServiceComp;
this.mRecommendationServiceLabel = recommendationServiceLabel;
this.mEnableUseOpenWifiActivity = enableUseOpenWifiActivity;
}
protected NetworkScorerAppData(Parcel in) {
packageUid = in.readInt();
mRecommendationService = ComponentName.readFromParcel(in);
mRecommendationServiceLabel = in.readString();
mEnableUseOpenWifiActivity = ComponentName.readFromParcel(in);
}
@@ -39,6 +43,7 @@ public final class NetworkScorerAppData implements Parcelable {
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(packageUid);
ComponentName.writeToParcel(mRecommendationService, dest);
dest.writeString(mRecommendationServiceLabel);
ComponentName.writeToParcel(mEnableUseOpenWifiActivity, dest);
}
@@ -73,11 +78,17 @@ public final class NetworkScorerAppData implements Parcelable {
return mEnableUseOpenWifiActivity;
}
@Nullable
public String getRecommendationServiceLabel() {
return mRecommendationServiceLabel;
}
@Override
public String toString() {
return "NetworkScorerAppData{" +
"packageUid=" + packageUid +
", mRecommendationService=" + mRecommendationService +
", mRecommendationServiceLabel=" + mRecommendationServiceLabel +
", mEnableUseOpenWifiActivity=" + mEnableUseOpenWifiActivity +
'}';
}
@@ -89,11 +100,13 @@ public final class NetworkScorerAppData implements Parcelable {
NetworkScorerAppData that = (NetworkScorerAppData) o;
return packageUid == that.packageUid &&
Objects.equals(mRecommendationService, that.mRecommendationService) &&
Objects.equals(mRecommendationServiceLabel, that.mRecommendationServiceLabel) &&
Objects.equals(mEnableUseOpenWifiActivity, that.mEnableUseOpenWifiActivity);
}
@Override
public int hashCode() {
return Objects.hash(packageUid, mRecommendationService, mEnableUseOpenWifiActivity);
return Objects.hash(packageUid, mRecommendationService, mRecommendationServiceLabel,
mEnableUseOpenWifiActivity);
}
}

View File

@@ -85,11 +85,12 @@ public class NetworkScorerAppManager {
}
final ComponentName serviceComponentName =
new ComponentName(serviceInfo.packageName, serviceInfo.name);
final String serviceLabel = getRecommendationServiceLabel(serviceInfo, pm);
final ComponentName useOpenWifiNetworksActivity =
findUseOpenWifiNetworksActivity(serviceInfo);
appDataList.add(
new NetworkScorerAppData(serviceInfo.applicationInfo.uid,
serviceComponentName, useOpenWifiNetworksActivity));
serviceComponentName, serviceLabel, useOpenWifiNetworksActivity));
} else {
if (VERBOSE) Log.v(TAG, serviceInfo.packageName
+ " is NOT a valid scorer/recommender.");
@@ -99,6 +100,19 @@ public class NetworkScorerAppManager {
return appDataList;
}
@Nullable
private String getRecommendationServiceLabel(ServiceInfo serviceInfo, PackageManager pm) {
if (serviceInfo.metaData != null) {
final String label = serviceInfo.metaData
.getString(NetworkScoreManager.RECOMMENDATION_SERVICE_LABEL_META_DATA);
if (!TextUtils.isEmpty(label)) {
return label;
}
}
CharSequence label = serviceInfo.loadLabel(pm);
return label == null ? null : label.toString();
}
@Nullable
private ComponentName findUseOpenWifiNetworksActivity(ServiceInfo serviceInfo) {
if (serviceInfo.metaData == null) {

View File

@@ -119,6 +119,7 @@ public class NetworkScoreServiceTest {
private static final String INVALID_BSSID = "invalid_bssid";
private static final ComponentName RECOMMENDATION_SERVICE_COMP =
new ComponentName("newPackageName", "newScoringServiceClass");
private static final String RECOMMENDATION_SERVICE_LABEL = "Test Recommendation Service";
private static final ComponentName USE_WIFI_ENABLE_ACTIVITY_COMP =
new ComponentName("useWifiPackageName", "enableUseWifiActivityClass");
private static final ScoredNetwork SCORED_NETWORK =
@@ -128,7 +129,8 @@ public class NetworkScoreServiceTest {
new ScoredNetwork(new NetworkKey(new WifiKey(quote(SSID_2), "00:00:00:00:00:00")),
null /* rssiCurve*/);
private static final NetworkScorerAppData NEW_SCORER = new NetworkScorerAppData(
1, RECOMMENDATION_SERVICE_COMP, USE_WIFI_ENABLE_ACTIVITY_COMP);
1, RECOMMENDATION_SERVICE_COMP, RECOMMENDATION_SERVICE_LABEL,
USE_WIFI_ENABLE_ACTIVITY_COMP);
@Mock private NetworkScorerAppManager mNetworkScorerAppManager;
@Mock private Context mContext;
@@ -924,7 +926,8 @@ public class NetworkScoreServiceTest {
when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES))
.thenReturn(PackageManager.PERMISSION_GRANTED);
NetworkScorerAppData expectedAppData = new NetworkScorerAppData(Binder.getCallingUid(),
RECOMMENDATION_SERVICE_COMP, USE_WIFI_ENABLE_ACTIVITY_COMP);
RECOMMENDATION_SERVICE_COMP, RECOMMENDATION_SERVICE_LABEL,
USE_WIFI_ENABLE_ACTIVITY_COMP);
bindToScorer(expectedAppData);
assertEquals(expectedAppData, mNetworkScoreService.getActiveScorer());
}
@@ -965,7 +968,8 @@ public class NetworkScoreServiceTest {
private void bindToScorer(boolean callerIsScorer) {
final int callingUid = callerIsScorer ? Binder.getCallingUid() : Binder.getCallingUid() + 1;
NetworkScorerAppData appData = new NetworkScorerAppData(callingUid,
RECOMMENDATION_SERVICE_COMP, USE_WIFI_ENABLE_ACTIVITY_COMP);
RECOMMENDATION_SERVICE_COMP, RECOMMENDATION_SERVICE_LABEL,
USE_WIFI_ENABLE_ACTIVITY_COMP);
bindToScorer(appData);
}

View File

@@ -59,6 +59,9 @@ import java.util.List;
@RunWith(AndroidJUnit4.class)
public class NetworkScorerAppManagerTest {
private static String MOCK_SERVICE_LABEL = "Mock Service";
private static String MOCK_OVERRIDEN_SERVICE_LABEL = "Mock Service Label Override";
@Mock private Context mMockContext;
@Mock private PackageManager mMockPm;
@Mock private Resources mResources;
@@ -94,6 +97,22 @@ public class NetworkScorerAppManagerTest {
assertNotNull(activeScorer);
assertEquals(recoComponent, activeScorer.getRecommendationServiceComponent());
assertEquals(924, activeScorer.packageUid);
assertEquals(MOCK_SERVICE_LABEL, activeScorer.getRecommendationServiceLabel());
}
@Test
public void testGetActiveScorer_providerAvailable_serviceLabelOverride() throws Exception {
final ComponentName recoComponent = new ComponentName("package1", "class1");
setNetworkRecoPackageSetting(recoComponent.getPackageName());
mockScoreNetworksGranted(recoComponent.getPackageName());
mockRecommendationServiceAvailable(recoComponent, 924 /* packageUid */,
null /* enableUseOpenWifiPackageActivityPackage*/, true /* serviceLabelOverride */);
final NetworkScorerAppData activeScorer = mNetworkScorerAppManager.getActiveScorer();
assertNotNull(activeScorer);
assertEquals(recoComponent, activeScorer.getRecommendationServiceComponent());
assertEquals(924, activeScorer.packageUid);
assertEquals(MOCK_OVERRIDEN_SERVICE_LABEL, activeScorer.getRecommendationServiceLabel());
}
@Test
@@ -269,11 +288,17 @@ public class NetworkScorerAppManagerTest {
}
private void mockRecommendationServiceAvailable(final ComponentName compName, int packageUid) {
mockRecommendationServiceAvailable(compName, packageUid, null);
mockRecommendationServiceAvailable(compName, packageUid, null, false);
}
private void mockRecommendationServiceAvailable(final ComponentName compName, int packageUid,
String enableUseOpenWifiActivityPackage) {
mockRecommendationServiceAvailable(
compName, packageUid, enableUseOpenWifiActivityPackage, false);
}
private void mockRecommendationServiceAvailable(final ComponentName compName, int packageUid,
String enableUseOpenWifiActivityPackage, boolean serviceLabelOverride) {
final ResolveInfo serviceInfo = new ResolveInfo();
serviceInfo.serviceInfo = new ServiceInfo();
serviceInfo.serviceInfo.name = compName.getClassName();
@@ -286,6 +311,16 @@ public class NetworkScorerAppManagerTest {
NetworkScoreManager.USE_OPEN_WIFI_PACKAGE_META_DATA,
enableUseOpenWifiActivityPackage);
}
if (serviceLabelOverride) {
if (serviceInfo.serviceInfo.metaData == null) {
serviceInfo.serviceInfo.metaData = new Bundle();
}
serviceInfo.serviceInfo.metaData.putString(
NetworkScoreManager.RECOMMENDATION_SERVICE_LABEL_META_DATA,
MOCK_OVERRIDEN_SERVICE_LABEL);
} else {
serviceInfo.serviceInfo.nonLocalizedLabel = MOCK_SERVICE_LABEL;
}
final int flags = PackageManager.GET_META_DATA;
when(mMockPm.resolveService(