下面列出了怎么用android.support.annotation.RestrictTo的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* The size of a pixel when scaled from the intrinsic dimension to the viewport dimension. This
* is used to calculate the path animation accuracy.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP)
public float getPixelSize() {
if (mVectorState == null && mVectorState.mVPathRenderer == null ||
mVectorState.mVPathRenderer.mBaseWidth == 0 ||
mVectorState.mVPathRenderer.mBaseHeight == 0 ||
mVectorState.mVPathRenderer.mViewportHeight == 0 ||
mVectorState.mVPathRenderer.mViewportWidth == 0) {
return 1; // fall back to 1:1 pixel mapping.
}
float intrinsicWidth = mVectorState.mVPathRenderer.mBaseWidth;
float intrinsicHeight = mVectorState.mVPathRenderer.mBaseHeight;
float viewportWidth = mVectorState.mVPathRenderer.mViewportWidth;
float viewportHeight = mVectorState.mVPathRenderer.mViewportHeight;
float scaleX = viewportWidth / intrinsicWidth;
float scaleY = viewportHeight / intrinsicHeight;
return Math.min(scaleX, scaleY);
}
/**
* Check if the application is debuggable.
*
* <p>This method check if the application is signed by a debug key.
* https://stackoverflow.com/questions/7085644/how-to-check-if-apk-is-signed-or-debug-build
*
* @param context application context
* @return true if app is debuggable, false otherwise
*/
@RestrictTo(LIBRARY)
@VisibleForTesting(otherwise = PACKAGE_PRIVATE)
static boolean isAppDebuggable(@NonNull final Context context) {
try {
PackageManager packageManager = context.getPackageManager();
PackageInfo packageInfo =
packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
Signature[] signatures = packageInfo.signatures;
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
for (Signature signature : signatures) {
ByteArrayInputStream stream = new ByteArrayInputStream(signature.toByteArray());
X509Certificate cert = (X509Certificate) certificateFactory.generateCertificate(stream);
String principal = cert.getSubjectX500Principal().toString().toUpperCase();
return principal.contains("C=US")
&& principal.contains("O=ANDROID")
&& principal.contains("CN=ANDROID DEBUG");
}
} catch (Exception e) {
// Things went south, anyway the app is not debuggable.
}
return false;
}
/**
* 兼容原生android系统直接断开系统蓝牙导致的异常
* 直接断开系统蓝牙不回调onConnectionStateChange接口问题
*/
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public void disconnectBluetooth(){
if (!connectedDevices.isEmpty()){
for (T device: connectedDevices) {
if (null != connectCallback){
device.setConnectionState(BleDevice.DISCONNECT);
BleLog.e(TAG, "System Bluetooth is disconnected>>>> "+device.getBleName());
connectCallback.onConnectionChanged(device);
}
}
bleRequest.close();
connectedDevices.clear();
devices.clear();
}
}
/** @hide */
@RestrictTo(LIBRARY_GROUP)
@VisibleForTesting
static void launchIntent(Context context, Intent intent, List<ResolveInfo> handlers) {
if (handlers == null || handlers.size() == 0) {
openFallbackBrowserActionsMenu(context, intent);
return;
} else if (handlers.size() == 1) {
intent.setPackage(handlers.get(0).activityInfo.packageName);
} else {
Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(TEST_URL));
PackageManager pm = context.getPackageManager();
ResolveInfo defaultHandler =
pm.resolveActivity(viewIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (defaultHandler != null) {
String defaultPackageName = defaultHandler.activityInfo.packageName;
for (int i = 0; i < handlers.size(); i++) {
if (defaultPackageName.equals(handlers.get(i).activityInfo.packageName)) {
intent.setPackage(defaultPackageName);
break;
}
}
}
}
ContextCompat.startActivity(context, intent, null);
}
/**
* @hide This method should be called by ItemTouchHelper only.
*/
@RestrictTo(LIBRARY_GROUP)
@Override
public void prepareForDrop(View view, View target, int x, int y) {
assertNotInLayoutOrScroll("Cannot drop a view during a scroll or layout calculation");
ensureLayoutState();
resolveShouldLayoutReverse();
final int myPos = getPosition(view);
final int targetPos = getPosition(target);
final int dropDirection = myPos < targetPos ? LayoutState.ITEM_DIRECTION_TAIL
: LayoutState.ITEM_DIRECTION_HEAD;
if (mShouldReverseLayout) {
if (dropDirection == LayoutState.ITEM_DIRECTION_TAIL) {
scrollToPositionWithOffset(targetPos,
mOrientationHelper.getEndAfterPadding()
- (mOrientationHelper.getDecoratedStart(target)
+ mOrientationHelper.getDecoratedMeasurement(view)));
} else {
scrollToPositionWithOffset(targetPos,
mOrientationHelper.getEndAfterPadding()
- mOrientationHelper.getDecoratedEnd(target));
}
} else {
if (dropDirection == LayoutState.ITEM_DIRECTION_HEAD) {
scrollToPositionWithOffset(targetPos, mOrientationHelper.getDecoratedStart(target));
} else {
scrollToPositionWithOffset(targetPos,
mOrientationHelper.getDecoratedEnd(target)
- mOrientationHelper.getDecoratedMeasurement(view));
}
}
}
/**
* @hide This method should be called by ItemTouchHelper only.
*/
@RestrictTo(LIBRARY_GROUP)
@Override
public void prepareForDrop(View view, View target, int x, int y) {
assertNotInLayoutOrScroll("Cannot drop a view during a scroll or layout calculation");
ensureLayoutState();
resolveShouldLayoutReverse();
final int myPos = getPosition(view);
final int targetPos = getPosition(target);
final int dropDirection = myPos < targetPos ? LayoutState.ITEM_DIRECTION_TAIL
: LayoutState.ITEM_DIRECTION_HEAD;
if (mShouldReverseLayout) {
if (dropDirection == LayoutState.ITEM_DIRECTION_TAIL) {
scrollToPositionWithOffset(targetPos,
mOrientationHelper.getEndAfterPadding()
- (mOrientationHelper.getDecoratedStart(target)
+ mOrientationHelper.getDecoratedMeasurement(view)));
} else {
scrollToPositionWithOffset(targetPos,
mOrientationHelper.getEndAfterPadding()
- mOrientationHelper.getDecoratedEnd(target));
}
} else {
if (dropDirection == LayoutState.ITEM_DIRECTION_HEAD) {
scrollToPositionWithOffset(targetPos, mOrientationHelper.getDecoratedStart(target));
} else {
scrollToPositionWithOffset(targetPos,
mOrientationHelper.getDecoratedEnd(target)
- mOrientationHelper.getDecoratedMeasurement(view));
}
}
}
@SuppressLint("MissingPermission")
@NonNull
@RestrictTo(RestrictTo.Scope.LIBRARY)
public static PushController getConnected(@NonNull Context context, @Nullable final AbstractConnectionStatusListener disconnectListener) {
PushController controller = new ControllerImpl(context);
if (disconnectListener != null)
new Handler(Looper.getMainLooper())
.postDelayed(disconnectListener::onReady, 300);
return controller;
}
/** @hide */
@RestrictTo(LIBRARY_GROUP)
public BrowserActionItem(
@NonNull String title, @NonNull PendingIntent action, @NonNull Uri iconUri) {
mTitle = title;
mAction = action;
mIconUri = iconUri;
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
public ConfigureAPStep newConfigureApStep(CommandClient commandClient, SetupStepApReconnector reconnector,
ScanApCommand.Scan networkToConnectTo, String networkSecretPlaintext,
PublicKey publicKey) {
return new ConfigureAPStep(
StepConfig.newBuilder()
.setMaxAttempts(MAX_RETRIES_CONFIGURE_AP)
.setResultCode(SuccessActivity.RESULT_FAILURE_CONFIGURE)
.setStepId(R.id.configure_device_wifi_credentials)
.build(),
commandClient, reconnector, networkToConnectTo, networkSecretPlaintext, publicKey);
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
public ConnectDeviceToNetworkStep newConnectDeviceToNetworkStep(CommandClient commandClient, SetupStepApReconnector reconnector) {
return new ConnectDeviceToNetworkStep(
StepConfig.newBuilder()
.setMaxAttempts(MAX_RETRIES_CONNECT_AP)
.setResultCode(SuccessActivity.RESULT_FAILURE_CONFIGURE)
.setStepId(R.id.connect_to_wifi_network)
.build(),
commandClient, reconnector);
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
public WaitForDisconnectionFromDeviceStep newWaitForDisconnectionFromDeviceStep(SSID deviceSoftApSsid,
WifiFacade wifiFacade) {
return new WaitForDisconnectionFromDeviceStep(
StepConfig.newBuilder()
.setMaxAttempts(MAX_RETRIES_DISCONNECT_FROM_DEVICE)
.setResultCode(SuccessActivity.RESULT_FAILURE_NO_DISCONNECT)
.setStepId(R.id.reconnect_to_wifi_network)
.build(),
deviceSoftApSsid, wifiFacade);
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
public EnsureSoftApNotVisible newEnsureSoftApNotVisible(SSID deviceSoftApSsid, WifiFacade wifiFacade) {
return new EnsureSoftApNotVisible(
StepConfig.newBuilder()
.setMaxAttempts(MAX_RETRIES_DISCONNECT_FROM_DEVICE)
.setResultCode(SuccessActivity.RESULT_FAILURE_CONFIGURE)
.setStepId(R.id.wait_for_device_cloud_connection)
.build(),
deviceSoftApSsid, wifiFacade);
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
public WaitForCloudConnectivityStep newWaitForCloudConnectivityStep(Context context) {
return new WaitForCloudConnectivityStep(
StepConfig.newBuilder()
.setMaxAttempts(MAX_RETRIES_DISCONNECT_FROM_DEVICE)
.setResultCode(SuccessActivity.RESULT_FAILURE_NO_DISCONNECT)
.setStepId(R.id.check_for_internet_connectivity)
.build(), context.getApplicationContext());
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
public CheckIfDeviceClaimedStep newCheckIfDeviceClaimedStep(ParticleCloud sparkCloud, String deviceId) {
return new CheckIfDeviceClaimedStep(
StepConfig.newBuilder()
.setMaxAttempts(MAX_RETRIES_CLAIM)
.setResultCode(SuccessActivity.RESULT_FAILURE_CLAIMING)
.setStepId(R.id.verify_product_ownership)
.build(),
sparkCloud, deviceId);
}
/**
* This method is used to ensure all of the methods in this class can remain private and no grantees can be made
* to the developer what methods are available or not.
*
* @param sdlManager this must be a working manager instance
* @return the internal interface that hooks into this manager
*/
@RestrictTo(RestrictTo.Scope.LIBRARY)
public ISdl getInternalInterface(SdlManager sdlManager) {
if (sdlManager != null) {
return internalInterface;
}
return null;
}
/** @hide */
@Override
@RestrictTo(LIBRARY)
public void collectAdjacentPrefetchPositions(int dx, int dy, RecyclerView.State state,
LayoutPrefetchRegistry layoutPrefetchRegistry) {
/* This method uses the simplifying assumption that the next N items (where N = span count)
* will be assigned, one-to-one, to spans, where ordering is based on which span extends
* least beyond the viewport.
*
* While this simplified model will be incorrect in some cases, it's difficult to know
* item heights, or whether individual items will be full span prior to construction.
*
* While this greedy estimation approach may underestimate the distance to prefetch items,
* it's very unlikely to overestimate them, so distances can be conservatively used to know
* the soonest (in terms of scroll distance) a prefetched view may come on screen.
*/
int delta = (mOrientation == HORIZONTAL) ? dx : dy;
if (getChildCount() == 0 || delta == 0) {
// can't support this scroll, so don't bother prefetching
return;
}
prepareLayoutStateForDelta(delta, state);
// build sorted list of distances to end of each span (though we don't care which is which)
if (mPrefetchDistances == null || mPrefetchDistances.length < mSpanCount) {
mPrefetchDistances = new int[mSpanCount];
}
int itemPrefetchCount = 0;
for (int i = 0; i < mSpanCount; i++) {
// compute number of pixels past the edge of the viewport that the current span extends
int distance = mLayoutState.mItemDirection == LAYOUT_START
? mLayoutState.mStartLine - mSpans[i].getStartLine(mLayoutState.mStartLine)
: mSpans[i].getEndLine(mLayoutState.mEndLine) - mLayoutState.mEndLine;
if (distance >= 0) {
// span extends to the edge, so prefetch next item
mPrefetchDistances[itemPrefetchCount] = distance;
itemPrefetchCount++;
}
}
Arrays.sort(mPrefetchDistances, 0, itemPrefetchCount);
// then assign them in order to the next N views (where N = span count)
for (int i = 0; i < itemPrefetchCount && mLayoutState.hasMore(state); i++) {
layoutPrefetchRegistry.addPosition(mLayoutState.mCurrentPosition,
mPrefetchDistances[i]);
mLayoutState.mCurrentPosition += mLayoutState.mItemDirection;
}
}
/** @hide */
@Override
@RestrictTo(LIBRARY)
public void collectAdjacentPrefetchPositions(int dx, int dy, RecyclerView.State state,
LayoutPrefetchRegistry layoutPrefetchRegistry) {
/* This method uses the simplifying assumption that the next N items (where N = span count)
* will be assigned, one-to-one, to spans, where ordering is based on which span extends
* least beyond the viewport.
*
* While this simplified model will be incorrect in some cases, it's difficult to know
* item heights, or whether individual items will be full span prior to construction.
*
* While this greedy estimation approach may underestimate the distance to prefetch items,
* it's very unlikely to overestimate them, so distances can be conservatively used to know
* the soonest (in terms of scroll distance) a prefetched view may come on screen.
*/
int delta = (mOrientation == HORIZONTAL) ? dx : dy;
if (getChildCount() == 0 || delta == 0) {
// can't support this scroll, so don't bother prefetching
return;
}
prepareLayoutStateForDelta(delta, state);
// build sorted list of distances to end of each span (though we don't care which is which)
if (mPrefetchDistances == null || mPrefetchDistances.length < mSpanCount) {
mPrefetchDistances = new int[mSpanCount];
}
int itemPrefetchCount = 0;
for (int i = 0; i < mSpanCount; i++) {
// compute number of pixels past the edge of the viewport that the current span extends
int distance = mLayoutState.mItemDirection == LAYOUT_START
? mLayoutState.mStartLine - mSpans[i].getStartLine(mLayoutState.mStartLine)
: mSpans[i].getEndLine(mLayoutState.mEndLine) - mLayoutState.mEndLine;
if (distance >= 0) {
// span extends to the edge, so prefetch next item
mPrefetchDistances[itemPrefetchCount] = distance;
itemPrefetchCount++;
}
}
Arrays.sort(mPrefetchDistances, 0, itemPrefetchCount);
// then assign them in order to the next N views (where N = span count)
for (int i = 0; i < itemPrefetchCount && mLayoutState.hasMore(state); i++) {
layoutPrefetchRegistry.addPosition(mLayoutState.mCurrentPosition,
mPrefetchDistances[i]);
mLayoutState.mCurrentPosition += mLayoutState.mItemDirection;
}
}
@RestrictTo({RestrictTo.Scope.LIBRARY, RestrictTo.Scope.LIBRARY_GROUP})
static boolean isApiPSupported() {
return (IS_PREVIEW_SDK_SUPPORTED && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1
&& Build.VERSION.PREVIEW_SDK_INT >= 1)
|| Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
public static boolean isViewBeingInspected(@Nullable View view) {
return view != null && view.getTag(ResourceInspector.TAG_RES_NAME) instanceof String;
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
@Nullable
public static String getViewLayoutResName(@Nullable View view) {
return view != null ? (String) view.getTag(ResourceInspector.TAG_RES_NAME) : null;
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
public static boolean isViewBeingInspected(@Nullable View view) {
return false;
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
@Nullable
public static String getViewLayoutResName(@Nullable View view) {
return null;
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
public OperationBuilder(Context context) {
this.context = context.getApplicationContext();
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
public static CommandResult createCharacteristicRead(BluetoothGattCharacteristic characteristic, int status) {
long when = System.currentTimeMillis();
return new CommandResult(characteristic.getUuid(), characteristic.getValue(), status, when);
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
public static CommandResult createErrorResult(UUID characteristicUUID, int error) {
long when = System.currentTimeMillis();
return new CommandResult(characteristicUUID, null, error, when);
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
public static CommandResult createEmptySuccess(UUID characteristicUUID) {
long when = System.currentTimeMillis();
return new CommandResult(characteristicUUID, null, BluetoothGatt.GATT_SUCCESS, when);
}
@RestrictTo(RestrictTo.Scope.LIBRARY)
public static CommandResult createCharacteristicChanged(BluetoothGattCharacteristic characteristic) {
long when = System.currentTimeMillis();
return new CommandResult(characteristic.getUuid(), characteristic.getValue(), BluetoothGatt.GATT_SUCCESS, when);
}
@RestrictTo(RestrictTo.Scope.TESTS)
LinkedList<Command> getCommands() {
return commands;
}
@RestrictTo(RestrictTo.Scope.TESTS)
OperationObserver getOperationObserver() {
return operationObserver;
}
@RestrictTo(RestrictTo.Scope.TESTS)
int getRetryCount() {
return retryCount;
}