Java Code Examples for android.content.Context#isRestricted()

The following examples show how to use android.content.Context#isRestricted() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FontsContract.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 2
Source File: RobotoCompatInflater.java    From Android-RobotoTextView with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 3
Source File: LayoutInflaterFactory2.java    From ThemeDemo with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 4
Source File: ResourcesCompat.java    From Carbon with Apache License 2.0 5 votes vote down vote up
/**
 * 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 */);
}
 
Example 5
Source File: SkinAppCompatViewInflater.java    From ReadMark with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 6
Source File: SkinnableViewInflater.java    From SkinSprite with MIT License 5 votes vote down vote up
@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);
}
 
Example 7
Source File: SkinAppCompatViewInflater.java    From ReadMark with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 8
Source File: SkinCompatViewInflater.java    From Android-skin-support with MIT License 5 votes vote down vote up
@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);
}
 
Example 9
Source File: SkinCompatViewInflater.java    From Android-skin-support with MIT License 5 votes vote down vote up
@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);
}
 
Example 10
Source File: SkinCompatViewInflater.java    From Android-skin-support with MIT License 5 votes vote down vote up
@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);
}
 
Example 11
Source File: DialogUtil.java    From letv with Apache License 2.0 5 votes vote down vote up
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();
        }
    }
}
 
Example 12
Source File: DialogUtil.java    From letv with Apache License 2.0 5 votes vote down vote up
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) {
            }
        }
    }
}
 
Example 13
Source File: SkinCompatViewInflater.java    From AndroidSkinAnimator with MIT License 5 votes vote down vote up
@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);
}
 
Example 14
Source File: FontsContract.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
}
 
Example 15
Source File: TextAppearanceSpan.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example 16
Source File: ResourcesCompat.java    From Carbon with Apache License 2.0 3 votes vote down vote up
/**
 * 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 */);
}
 
Example 17
Source File: ResourcesCompat.java    From Carbon with Apache License 2.0 3 votes vote down vote up
/**
 * 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 */);
}