下面列出了android.content.Context#isRestricted ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Fetch fonts given a font request.
*
* @param context A {@link Context} to be used for fetching fonts.
* @param cancellationSignal A signal to cancel the operation in progress, or null if none. If
* the operation is canceled, then {@link
* android.os.OperationCanceledException} will be thrown when the
* query is executed.
* @param request A {@link FontRequest} object that identifies the provider and query for the
* request.
*
* @return {@link FontFamilyResult}
*
* @throws NameNotFoundException If requested package or authority was not found in system.
*/
public static @NonNull FontFamilyResult fetchFonts(
@NonNull Context context, @Nullable CancellationSignal cancellationSignal,
@NonNull FontRequest request) throws NameNotFoundException {
if (context.isRestricted()) {
// TODO: Should we allow if the peer process is system or myself?
return new FontFamilyResult(FontFamilyResult.STATUS_REJECTED, null);
}
ProviderInfo providerInfo = getProvider(context.getPackageManager(), request);
if (providerInfo == null) {
return new FontFamilyResult(FontFamilyResult.STATUS_WRONG_CERTIFICATES, null);
}
try {
FontInfo[] fonts = getFontFromProvider(
context, request, providerInfo.authority, cancellationSignal);
return new FontFamilyResult(FontFamilyResult.STATUS_OK, fonts);
} catch (IllegalArgumentException e) {
return new FontFamilyResult(FontFamilyResult.STATUS_UNEXPECTED_DATA_PROVIDED, null);
}
}
/**
* Build a Typeface from an array of {@link FontInfo}
*
* Results that are marked as not ready will be skipped.
*
* @param context A {@link Context} that will be used to fetch the font contents.
* @param cancellationSignal A signal to cancel the operation in progress, or null if none. If
* the operation is canceled, then {@link
* android.os.OperationCanceledException} will be thrown.
* @param fonts An array of {@link FontInfo} to be used to create a Typeface.
* @return A Typeface object. Returns null if typeface creation fails.
*/
public static Typeface buildTypeface(@NonNull Context context,
@Nullable CancellationSignal cancellationSignal, @NonNull FontInfo[] fonts) {
if (context.isRestricted()) {
// TODO: Should we allow if the peer process is system or myself?
return null;
}
final Map<Uri, ByteBuffer> uriBuffer =
prepareFontData(context, fonts, cancellationSignal);
if (uriBuffer.isEmpty()) {
return null;
}
return new Typeface.Builder(fonts, uriBuffer).build();
}
@NonNull
private void resolveMethod(@Nullable Context context, @NonNull String name) {
while (context != null) {
try {
if (!context.isRestricted()) {
final Method method = context.getClass().getMethod(mMethodName, View.class);
if (method != null) {
mResolvedMethod = method;
mResolvedContext = context;
return;
}
}
} catch (NoSuchMethodException e) {
// Failed to find method, keep searching up the hierarchy.
}
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
} else {
// Can't search up the hierarchy, null out and fail.
context = null;
}
}
final int id = mHostView.getId();
final String idText = id == View.NO_ID ? "" : " with id '"
+ mHostView.getContext().getResources().getResourceEntryName(id) + "'";
throw new IllegalStateException("Could not find method " + mMethodName
+ "(View) in a parent or ancestor Context for android:onClick "
+ "attribute defined on view " + mHostView.getClass() + idText);
}
public static void call(Context activity, String title, String message, int yes, int no, OnClickListener yesListener, OnClickListener noListener, boolean cancelable) {
if (activity != null) {
Dialog dialog = new Builder(activity).setTitle(title).setIcon(R.drawable.dialog_icon).setMessage(message).setCancelable(cancelable).setPositiveButton(yes, yesListener).setNegativeButton(no, noListener).create();
if ((activity instanceof Activity) && !((Activity) activity).isFinishing() && !activity.isRestricted()) {
try {
dialog.show();
} catch (Exception e) {
}
}
}
}
public static void call(Context activity, String title, String message, int yes, int no, OnClickListener yesListener, OnClickListener noListener, boolean cancelable) {
if (activity != null) {
Dialog dialog = new Builder(activity).setTitle(title).setIcon(R.drawable.dialog_icon).setMessage(message).setCancelable(cancelable).setPositiveButton(yes, yesListener).setNegativeButton(no, noListener).create();
if ((activity instanceof Activity) && !((Activity) activity).isFinishing() && !activity.isRestricted()) {
dialog.show();
}
}
}
@NonNull
private void resolveMethod(@Nullable Context context, @NonNull String name) {
while (context != null) {
try {
if (!context.isRestricted()) {
final Method method = context.getClass().getMethod(mMethodName, View.class);
if (method != null) {
mResolvedMethod = method;
mResolvedContext = context;
return;
}
}
} catch (NoSuchMethodException e) {
// Failed to find method, keep searching up the hierarchy.
}
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
} else {
// Can't search up the hierarchy, null out and fail.
context = null;
}
}
final int id = mHostView.getId();
final String idText = id == View.NO_ID ? "" : " with id '"
+ mHostView.getContext().getResources().getResourceEntryName(id) + "'";
throw new IllegalStateException("Could not find method " + mMethodName
+ "(View) in a parent or ancestor Context for android:onClick "
+ "attribute defined on view " + mHostView.getClass() + idText);
}
@NonNull
private void resolveMethod(@Nullable Context context, @NonNull String name) {
while (context != null) {
try {
if (!context.isRestricted()) {
final Method method = context.getClass().getMethod(mMethodName, View.class);
if (method != null) {
mResolvedMethod = method;
mResolvedContext = context;
return;
}
}
} catch (NoSuchMethodException e) {
// Failed to find method, keep searching up the hierarchy.
}
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
} else {
// Can't search up the hierarchy, null out and fail.
context = null;
}
}
final int id = mHostView.getId();
final String idText = id == View.NO_ID ? "" : " with id '"
+ mHostView.getContext().getResources().getResourceEntryName(id) + "'";
throw new IllegalStateException("Could not find method " + mMethodName
+ "(View) in a parent or ancestor Context for android:onClick "
+ "attribute defined on view " + mHostView.getClass() + idText);
}
@NonNull
private void resolveMethod(@Nullable Context context, @NonNull String name) {
while (context != null) {
try {
if (!context.isRestricted()) {
final Method method = context.getClass().getMethod(mMethodName, View.class);
if (method != null) {
mResolvedMethod = method;
mResolvedContext = context;
return;
}
}
} catch (NoSuchMethodException e) {
// Failed to find method, keep searching up the hierarchy.
}
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
} else {
// Can't search up the hierarchy, null out and fail.
context = null;
}
}
final int id = mHostView.getId();
final String idText = id == View.NO_ID ? "" : " with id '"
+ mHostView.getContext().getResources().getResourceEntryName(id) + "'";
throw new IllegalStateException("Could not find method " + mMethodName
+ "(View) in a parent or ancestor Context for android:onClick "
+ "attribute defined on view " + mHostView.getClass() + idText);
}
@NonNull
private void resolveMethod(@Nullable Context context, @NonNull String name) {
while (context != null) {
try {
if (!context.isRestricted()) {
final Method method = context.getClass().getMethod(mMethodName, View.class);
if (method != null) {
mResolvedMethod = method;
mResolvedContext = context;
return;
}
}
} catch (NoSuchMethodException e) {
// Failed to find method, keep searching up the hierarchy.
}
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
} else {
// Can't search up the hierarchy, null out and fail.
context = null;
}
}
final int id = mHostView.getId();
final String idText = id == View.NO_ID ? "" : " with id '"
+ mHostView.getContext().getResources().getResourceEntryName(id) + "'";
throw new IllegalStateException("Could not find method " + mMethodName
+ "(View) in a parent or ancestor Context for android:onClick "
+ "attribute defined on view " + mHostView.getClass() + idText);
}
@NonNull
private void resolveMethod(@Nullable Context context, @NonNull String name) {
while (context != null) {
try {
if (!context.isRestricted()) {
final Method method = context.getClass().getMethod(mMethodName, View.class);
if (method != null) {
mResolvedMethod = method;
mResolvedContext = context;
return;
}
}
} catch (NoSuchMethodException e) {
// Failed to find method, keep searching up the hierarchy.
}
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
} else {
// Can't search up the hierarchy, null out and fail.
context = null;
}
}
final int id = mHostView.getId();
final String idText = id == View.NO_ID ? "" : " with id '"
+ mHostView.getContext().getResources().getResourceEntryName(id) + "'";
throw new IllegalStateException("Could not find method " + mMethodName
+ "(View) in a parent or ancestor Context for android:onClick "
+ "attribute defined on view " + mHostView.getClass() + idText);
}
@NonNull
private void resolveMethod(@Nullable Context context, @NonNull String name) {
while (context != null) {
try {
if (!context.isRestricted()) {
final Method method = context.getClass().getMethod(mMethodName, View.class);
if (method != null) {
mResolvedMethod = method;
mResolvedContext = context;
return;
}
}
} catch (NoSuchMethodException e) {
// Failed to find method, keep searching up the hierarchy.
}
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
} else {
// Can't search up the hierarchy, null out and fail.
context = null;
}
}
final int id = mHostView.getId();
final String idText = id == View.NO_ID ? "" : " with id '"
+ mHostView.getContext().getResources().getResourceEntryName(id) + "'";
throw new IllegalStateException("Could not find method " + mMethodName
+ "(View) in a parent or ancestor Context for android:onClick "
+ "attribute defined on view " + mHostView.getClass() + idText);
}
/**
* Used by TintTypedArray.
*
* @hide
*/
@RestrictTo(LIBRARY_GROUP_PREFIX)
public static Typeface getFont(@NonNull Context context, @FontRes int id, TypedValue value,
int style, int weight, @Nullable androidx.core.content.res.ResourcesCompat.FontCallback fontCallback) throws NotFoundException {
if (context.isRestricted()) {
return null;
}
return loadFont(context, id, value, style, weight, fontCallback, null /* handler */,
true /* isXmlRequest */);
}
@NonNull
private void resolveMethod(@Nullable Context context, @NonNull String name) {
while (context != null) {
try {
if (!context.isRestricted()) {
final Method method = context.getClass().getMethod(mMethodName, View.class);
if (method != null) {
mResolvedMethod = method;
mResolvedContext = context;
return;
}
}
} catch (NoSuchMethodException e) {
// Failed to find method, keep searching up the hierarchy.
}
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
} else {
// Can't search up the hierarchy, null out and fail.
context = null;
}
}
final int id = mHostView.getId();
final String idText = id == View.NO_ID ? "" : " with id '"
+ mHostView.getContext().getResources().getResourceEntryName(id) + "'";
throw new IllegalStateException("Could not find method " + mMethodName
+ "(View) in a parent or ancestor Context for android:onClick "
+ "attribute defined on view " + mHostView.getClass() + idText);
}
private void resolveMethod(@Nullable Context context, @NonNull String name) {
while (context != null) {
try {
if (!context.isRestricted()) {
final Method method = context.getClass().getMethod(name, View.class);
if (method != null) {
mResolvedMethod = method;
mResolvedContext = context;
return;
}
}
} catch (NoSuchMethodException e) {
// Failed to find method, keep searching up the hierarchy.
}
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
} else {
// Can't search up the hierarchy, null out and fail.
context = null;
}
}
final int id = mHostView.getId();
final String idText = id == View.NO_ID ? "" : " with id '"
+ mHostView.getContext().getResources().getResourceEntryName(id) + "'";
throw new IllegalStateException("Could not find method " + name
+ "(View) in a parent or ancestor Context for android:onClick "
+ "attribute defined on view " + mHostView.getClass() + idText);
}
/**
* Uses the specified TextAppearance resource to determine the
* text appearance, and the specified text color resource
* to determine the color. The <code>appearance</code> should be,
* for example, <code>android.R.style.TextAppearance_Small</code>,
* and the <code>colorList</code> should be, for example,
* <code>android.R.styleable.Theme_textColorPrimary</code>.
*/
public TextAppearanceSpan(Context context, int appearance, int colorList) {
ColorStateList textColor;
TypedArray a =
context.obtainStyledAttributes(appearance,
com.android.internal.R.styleable.TextAppearance);
textColor = a.getColorStateList(com.android.internal.R.styleable.
TextAppearance_textColor);
mTextColorLink = a.getColorStateList(com.android.internal.R.styleable.
TextAppearance_textColorLink);
mTextSize = a.getDimensionPixelSize(com.android.internal.R.styleable.
TextAppearance_textSize, -1);
mStyle = a.getInt(com.android.internal.R.styleable.TextAppearance_textStyle, 0);
if (!context.isRestricted() && context.canLoadUnsafeResources()) {
mTypeface = a.getFont(com.android.internal.R.styleable.TextAppearance_fontFamily);
} else {
mTypeface = null;
}
if (mTypeface != null) {
mFamilyName = null;
} else {
String family = a.getString(com.android.internal.R.styleable.TextAppearance_fontFamily);
if (family != null) {
mFamilyName = family;
} else {
int tf = a.getInt(com.android.internal.R.styleable.TextAppearance_typeface, 0);
switch (tf) {
case 1:
mFamilyName = "sans";
break;
case 2:
mFamilyName = "serif";
break;
case 3:
mFamilyName = "monospace";
break;
default:
mFamilyName = null;
break;
}
}
}
a.recycle();
if (colorList >= 0) {
a = context.obtainStyledAttributes(com.android.internal.R.style.Theme,
com.android.internal.R.styleable.Theme);
textColor = a.getColorStateList(colorList);
a.recycle();
}
mTextColor = textColor;
}
/**
* Returns a font Typeface associated with a particular resource ID.
* <p>
* This method will block the calling thread to retrieve the requested font, including if it
* is from a font provider. If you wish to not have this behavior, use
* {@link #getFont(Context, int, FontCallback, Handler)} instead.
* <p>
* Prior to API level 23, font resources with more than one font in a family will only load the
* font closest to a regular weight typeface.
*
* @param context A context to retrieve the Resources from.
* @param id The desired resource identifier of a {@link Typeface},
* as generated by the aapt tool. This integer encodes the
* package, type, and resource entry. The value 0 is an invalid
* identifier.
* @return A font Typeface object.
* @throws NotFoundException Throws NotFoundException if the given ID does not exist.
* @see #getFont(Context, int, FontCallback, Handler)
*/
@Nullable
public static Typeface getFont(@NonNull Context context, @FontRes int id)
throws NotFoundException {
if (context.isRestricted()) {
return null;
}
return loadFont(context, id, new TypedValue(), Typeface.NORMAL, 400, null /* callback */,
null /* handler */, false /* isXmlRequest */);
}
/**
* Returns a font Typeface associated with a particular resource ID asynchronously.
* <p>
* Prior to API level 23, font resources with more than one font in a family will only load the
* font closest to a regular weight typeface.
* </p>
*
* @param context A context to retrieve the Resources from.
* @param id The desired resource identifier of a {@link Typeface}, as generated by the aapt
* tool. This integer encodes the package, type, and resource entry. The value 0 is an
* invalid identifier.
* @param fontCallback A callback to receive async fetching of this font. The callback will be
* triggered on the UI thread.
* @param handler A handler for the thread the callback should be called on. If null, the
* callback will be called on the UI thread.
* @throws NotFoundException Throws NotFoundException if the given ID does not exist.
*/
public static void getFont(@NonNull Context context, @FontRes int id,
@NonNull androidx.core.content.res.ResourcesCompat.FontCallback fontCallback, @Nullable Handler handler)
throws NotFoundException {
Preconditions.checkNotNull(fontCallback);
if (context.isRestricted()) {
fontCallback.callbackFailAsync(
FontRequestCallback.FAIL_REASON_SECURITY_VIOLATION, handler);
return;
}
loadFont(context, id, new TypedValue(), Typeface.NORMAL, 400, fontCallback, handler,
false /* isXmlRequest */);
}