Add get last entitlement value callback API

The callback would be used to notify entitlement value. If the
cache value indicates entitlement succeeded, it just fire
callback with cache value instead of run entitlement check.

Bug: 120887283
Test: atest FrameworksNetTests
Change-Id: I8afe928423bd75c54c61533a50a5c0814922ceb1
This commit is contained in:
markchien
2019-01-16 17:44:13 +08:00
parent 7e81ae73b4
commit 0f45bb9d3d
3 changed files with 81 additions and 0 deletions

View File

@@ -2544,6 +2544,7 @@ public class ConnectivityManager {
} }
/** {@hide} */ /** {@hide} */
@SystemApi
public static final int TETHER_ERROR_NO_ERROR = 0; public static final int TETHER_ERROR_NO_ERROR = 0;
/** {@hide} */ /** {@hide} */
public static final int TETHER_ERROR_UNKNOWN_IFACE = 1; public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
@@ -2566,9 +2567,13 @@ public class ConnectivityManager {
/** {@hide} */ /** {@hide} */
public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10; public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
/** {@hide} */ /** {@hide} */
@SystemApi
public static final int TETHER_ERROR_PROVISION_FAILED = 11; public static final int TETHER_ERROR_PROVISION_FAILED = 11;
/** {@hide} */ /** {@hide} */
public static final int TETHER_ERROR_DHCPSERVER_ERROR = 12; public static final int TETHER_ERROR_DHCPSERVER_ERROR = 12;
/** {@hide} */
@SystemApi
public static final int TETHER_ERROR_ENTITLEMENT_UNKONWN = 13;
/** /**
* Get a more detailed error code after a Tethering or Untethering * Get a more detailed error code after a Tethering or Untethering
@@ -2590,6 +2595,65 @@ public class ConnectivityManager {
} }
} }
/**
* Callback for use with {@link #getLatestTetheringEntitlementValue} to find out whether
* entitlement succeeded.
* @hide
*/
@SystemApi
public abstract static class TetheringEntitlementValueListener {
/**
* Called to notify entitlement result.
*
* @param resultCode a int value of entitlement result. It may be one of
* {@link #TETHER_ERROR_NO_ERROR},
* {@link #TETHER_ERROR_PROVISION_FAILED}, or
* {@link #TETHER_ERROR_ENTITLEMENT_UNKONWN}.
*/
public void onEntitlementResult(int resultCode) {}
}
/**
* Get the last value of the entitlement check on this downstream. If the cached value is
* {@link #TETHER_ERROR_NO_ERROR} or showEntitlementUi argument is false, it just return the
* cached value. Otherwise, a UI-based entitlement check would be performed. It is not
* guaranteed that the UI-based entitlement check will complete in any specific time period
* and may in fact never complete. Any successful entitlement check the platform performs for
* any reason will update the cached value.
*
* @param type the downstream type of tethering. Must be one of
* {@link #TETHERING_WIFI},
* {@link #TETHERING_USB}, or
* {@link #TETHERING_BLUETOOTH}.
* @param showEntitlementUi a boolean indicating whether to run UI-based entitlement check.
* @param listener an {@link TetheringEntitlementValueListener} which will be called to notify
* the caller of the result of entitlement check. The listener may be called zero or
* one time.
* @param handler {@link Handler} to specify the thread upon which the listener will be invoked.
* {@hide}
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
public void getLatestTetheringEntitlementValue(int type, boolean showEntitlementUi,
@NonNull final TetheringEntitlementValueListener listener, @Nullable Handler handler) {
Preconditions.checkNotNull(listener, "TetheringEntitlementValueListener cannot be null.");
ResultReceiver wrappedListener = new ResultReceiver(handler) {
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
listener.onEntitlementResult(resultCode);
}
};
try {
String pkgName = mContext.getOpPackageName();
Log.i(TAG, "getLatestTetheringEntitlementValue:" + pkgName);
mService.getLatestTetheringEntitlementValue(type, wrappedListener,
showEntitlementUi, pkgName);
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
/** /**
* Report network connectivity status. This is currently used only * Report network connectivity status. This is currently used only
* to alter status bar UI. * to alter status bar UI.

View File

@@ -194,4 +194,7 @@ interface IConnectivityManager
int getConnectionOwnerUid(in ConnectionInfo connectionInfo); int getConnectionOwnerUid(in ConnectionInfo connectionInfo);
boolean isCallerCurrentAlwaysOnVpnApp(); boolean isCallerCurrentAlwaysOnVpnApp();
boolean isCallerCurrentAlwaysOnVpnLockdownApp(); boolean isCallerCurrentAlwaysOnVpnLockdownApp();
void getLatestTetheringEntitlementValue(int type, in ResultReceiver receiver,
boolean showEntitlementUi, String callerPkg);
} }

View File

@@ -3624,6 +3624,20 @@ public class ConnectivityService extends IConnectivityManager.Stub
mTethering.stopTethering(type); mTethering.stopTethering(type);
} }
/**
* Get the latest value of the tethering entitlement check.
*
* Note: Allow privileged apps who have TETHER_PRIVILEGED permission to access. If it turns
* out some such apps are observed to abuse this API, change to per-UID limits on this API
* if it's really needed.
*/
@Override
public void getLatestTetheringEntitlementValue(int type, ResultReceiver receiver,
boolean showEntitlementUi, String callerPkg) {
ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg);
mTethering.getLatestTetheringEntitlementValue(type, receiver, showEntitlementUi);
}
// Called when we lose the default network and have no replacement yet. // Called when we lose the default network and have no replacement yet.
// This will automatically be cleared after X seconds or a new default network // This will automatically be cleared after X seconds or a new default network
// becomes CONNECTED, whichever happens first. The timer is started by the // becomes CONNECTED, whichever happens first. The timer is started by the