Java Code Examples for android.content.res.Resources#getSystem()

The following examples show how to use android.content.res.Resources#getSystem() . 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: BitmapHelper.java    From android-chromium with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Decode and sample down a bitmap resource to the requested width and height.
 *
 * @param name The resource name of the bitmap to decode.
 * @param reqWidth The requested width of the resulting bitmap.
 * @param reqHeight The requested height of the resulting bitmap.
 * @return A bitmap sampled down from the original with the same aspect ratio and dimensions.
 *         that are equal to or greater than the requested width and height.
 */
@CalledByNative
private static Bitmap decodeDrawableResource(String name,
                                             int reqWidth,
                                             int reqHeight) {
    Resources res = Resources.getSystem();
    int resId = res.getIdentifier(name, null, null);

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}
 
Example 2
Source File: ClickableToast.java    From Overchan-Android with GNU General Public License v3.0 6 votes vote down vote up
public WindowManager.LayoutParams getWmParams(){
    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
    params.alpha = 1;
    params.format = PixelFormat.RGBA_8888;
    if (gravityTop) {
        params.gravity = Gravity.TOP;
    } else {
        params.gravity = Gravity.BOTTOM;
        params.verticalMargin = isNarrow() ? 0.05f : 0.07f;
    }
    params.flags =  WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    //params.type = WindowManager.LayoutParams.LAST_SYSTEM_WINDOW + 30;
    try {
        if (internalResources == null) internalResources = Resources.getSystem();
        params.windowAnimations = internalResources.getIdentifier("Animation.Toast", "style", "android");
        params.y = getResources().getDimensionPixelSize(internalResources.getIdentifier("toast_y_offset", "dimen", "android"));
    } catch (Exception e) {
        Logger.e(TAG, e);
    }
    return params;
}
 
Example 3
Source File: RtlTestUtils.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/**
 * @see org.robolectric.RuntimeEnvironment#setQualifiers(String)
 */
@RequiresApi(api = VERSION_CODES.JELLY_BEAN_MR1)
@SuppressWarnings("deprecation")
private static void setLocale(Locale locale) {
  Configuration configuration = new Configuration(Resources.getSystem().getConfiguration());
  DisplayMetrics displayMetrics = new DisplayMetrics();
  displayMetrics.setTo(Resources.getSystem().getDisplayMetrics());

  configuration.setLocale(locale);

  Resources systemResources = Resources.getSystem();
  systemResources.updateConfiguration(configuration, displayMetrics);
  ApplicationProvider
      .getApplicationContext()
      .getResources()
      .updateConfiguration(configuration, displayMetrics);
}
 
Example 4
Source File: IconShapeOverride.java    From LaunchEnr with GNU General Public License v3.0 6 votes vote down vote up
public static void apply(Context context) {
    if (!AndroidVersion.isAtLeastOreo()) {
        return;
    }
    String path = getAppliedValue(context);
    if (TextUtils.isEmpty(path)) {
        return;
    }
    if (!isSupported(context)) {
        return;
    }

    // magic
    try {
        Resources override =
                new ResourcesOverride(Resources.getSystem(), getConfigResId(), path);
        getSystemResField().set(null, override);
    } catch (Exception e) {
        // revert value.
        prefs(context).edit().remove(KEY_PREFERENCE).apply();
    }
}
 
Example 5
Source File: AutofitHelper.java    From kAndroid with Apache License 2.0 6 votes vote down vote up
/**
 * Set the original text size of the View.
 *
 * @see TextView#setTextSize(int, float)
 */
public void setTextSize(int unit, float size) {
    if (mIsAutofitting) {
        // We don't want to update the TextView's actual textSize while we're autofitting
        // since it'd get set to the autofitTextSize
        return;
    }
    Context context = mTextView.getContext();
    Resources r = Resources.getSystem();

    if (context != null) {
        r = context.getResources();
    }

    setRawTextSize(TypedValue.applyDimension(unit, size, r.getDisplayMetrics()));
}
 
Example 6
Source File: ToggleButton.java    From ToggleButton with MIT License 6 votes vote down vote up
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	
	final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
	final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
	
	int widthSize = MeasureSpec.getSize(widthMeasureSpec);
	int heightSize = MeasureSpec.getSize(heightMeasureSpec);
	
	Resources r = Resources.getSystem();
	if(widthMode == MeasureSpec.UNSPECIFIED || widthMode == MeasureSpec.AT_MOST){
		widthSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, r.getDisplayMetrics());
		widthMeasureSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
	}
	
	if(heightMode == MeasureSpec.UNSPECIFIED || heightSize == MeasureSpec.AT_MOST){
		heightSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30, r.getDisplayMetrics());
		heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
	}
	
	
	super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
 
Example 7
Source File: DiskInfo.java    From FireFiles with Apache License 2.0 6 votes vote down vote up
public String getDescription() {
    final Resources res = Resources.getSystem();
    if ((flags & FLAG_SD) != 0) {
        if (isInteresting(label)) {
            return res.getString(R.string.storage_sd_card_label, label);
        } else {
            return res.getString(R.string.storage_sd_card);
        }
    } else if ((flags & FLAG_USB) != 0) {
        if (isInteresting(label)) {
            return res.getString(R.string.storage_usb_drive_label, label);
        } else {
            return res.getString(R.string.storage_usb_drive);
        }
    } else {
        return null;
    }
}
 
Example 8
Source File: PixelUtil.java    From Android-Application-ZJB with Apache License 2.0 5 votes vote down vote up
/**
 * sp转px.
 *
 * @param value the value
 * @return the int
 */
public static int sp2px(float value) {
    Resources r;
    if (gContext == null) {
        r = Resources.getSystem();
    } else {
        r = gContext.getResources();
    }
    float spvalue = value * r.getDisplayMetrics().scaledDensity;
    return (int) (spvalue + 0.5f);
}
 
Example 9
Source File: AutofitHelper.java    From kAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * Set the maximum text size to a given unit and value. See TypedValue for the possible
 * dimension units.
 *
 * @param unit The desired dimension unit.
 * @param size The desired size in the given units.
 *
 * @attr ref android.R.styleable#TextView_textSize
 */
public AutofitHelper setMaxTextSize(int unit, float size) {
    Context context = mTextView.getContext();
    Resources r = Resources.getSystem();

    if (context != null) {
        r = context.getResources();
    }

    setRawMaxTextSize(TypedValue.applyDimension(unit, size, r.getDisplayMetrics()));
    return this;
}
 
Example 10
Source File: AutofitHelper.java    From kAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * Set the minimum text size to a given unit and value. See TypedValue for the possible
 * dimension units.
 *
 * @param unit The desired dimension unit.
 * @param size The desired size in the given units.
 *
 * @attr ref me.grantland.R.styleable#AutofitTextView_minTextSize
 */
public AutofitHelper setMinTextSize(int unit, float size) {
    Context context = mTextView.getContext();
    Resources r = Resources.getSystem();

    if (context != null) {
        r = context.getResources();
    }

    setRawMinTextSize(TypedValue.applyDimension(unit, size, r.getDisplayMetrics()));
    return this;
}
 
Example 11
Source File: AutofitTextView.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void setTextSize(int unit, float size) {
    Context context = getContext();
    Resources r = Resources.getSystem();

    if (context != null) {
        r = context.getResources();
    }

    setRawTextSize(TypedValue.applyDimension(unit, size, r.getDisplayMetrics()));
}
 
Example 12
Source File: AutoFitTextView.java    From android_gisapp with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setTextSize(final int unit,final float size)
{
    final Context c=getContext();
    Resources r;
    if(c==null)
        r=Resources.getSystem();
    else r=c.getResources();
    _maxTextSize=TypedValue.applyDimension(unit,size,r.getDisplayMetrics());
    _textCachedSizes.clear();
    adjustTextSize();
}
 
Example 13
Source File: DateUtils.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static void initFormatStringsLocked() {
    Resources r = Resources.getSystem();
    Configuration cfg = r.getConfiguration();
    if (sLastConfig == null || !sLastConfig.equals(cfg)) {
        sLastConfig = cfg;
        sElapsedFormatMMSS = r.getString(com.android.internal.R.string.elapsed_time_short_format_mm_ss);
        sElapsedFormatHMMSS = r.getString(com.android.internal.R.string.elapsed_time_short_format_h_mm_ss);
    }
}
 
Example 14
Source File: CustomHtmlToSpannedConverter.java    From zulip-android with Apache License 2.0 5 votes vote down vote up
private static void endFont(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Font.class);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);

    if (where != len) {
        Font f = (Font) obj;

        if (!TextUtils.isEmpty(f.mColor)) {
            if (f.mColor.startsWith("@")) {
                Resources res = Resources.getSystem();
                String name = f.mColor.substring(1);
                int colorRes = res.getIdentifier(name, "color", "android");
                if (colorRes != 0) {
                    ColorStateList colors = res.getColorStateList(colorRes);
                    text.setSpan(new TextAppearanceSpan(null, 0, 0, colors,
                                    null), where, len,
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            } else {
                int c = getHtmlColor(f.mColor);
                if (c != -1) {
                    text.setSpan(new ForegroundColorSpan(c | 0xFF000000),
                            where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }

        if (f.mFace != null) {
            text.setSpan(new TypefaceSpan(f.mFace), where, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
 
Example 15
Source File: DefaultMediaDecoder.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@NonNull
public static DefaultMediaDecoder create() {
    return new DefaultMediaDecoder(Resources.getSystem());
}
 
Example 16
Source File: SwitchButton.java    From FastAndroid with Apache License 2.0 4 votes vote down vote up
private static float dp2px(float dp) {
    Resources r = Resources.getSystem();
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
}
 
Example 17
Source File: Tool.java    From openlauncher with Apache License 2.0 4 votes vote down vote up
public static int sp2px(float sp) {
    Resources resources = Resources.getSystem();
    float px = sp * resources.getDisplayMetrics().scaledDensity;
    return (int) Math.ceil(px);
}
 
Example 18
Source File: SwitchButton.java    From SmartOrnament with Apache License 2.0 4 votes vote down vote up
/*******************************************************/
private static float dp2px(float dp){
    Resources r = Resources.getSystem();
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
}
 
Example 19
Source File: CommUtil.java    From Viewer with Apache License 2.0 3 votes vote down vote up
/**
 * 获取当前分辨率下指定单位对应的像素大小(根据设备信息)
 * px,dip,sp -> px
 * 
 * Paint.setTextSize()单位为px
 * 
 * 代码摘自:TextView.setTextSize()
 * 
 * @param unit  TypedValue.COMPLEX_UNIT_*
 * @param size
 * @return
 */
public static float getRawSize(Context context,int unit, float size) {
       Resources r;

       if (context == null)
           r = Resources.getSystem();
       else
           r = context.getResources();
        
       return TypedValue.applyDimension(unit, size, r.getDisplayMetrics());
}
 
Example 20
Source File: PromptUtilsUnitTest.java    From MaterialTapTargetPrompt with Apache License 2.0 2 votes vote down vote up
@SuppressLint("RtlHardcoded")
@Test
public void testGetTextAlignment()
{
    final Resources resources = Resources.getSystem();

    assertEquals(Layout.Alignment.ALIGN_NORMAL, PromptUtils.getTextAlignment(resources, Gravity.START, null));
    assertEquals(Layout.Alignment.ALIGN_NORMAL, PromptUtils.getTextAlignment(resources, Gravity.LEFT, null));

    assertEquals(Layout.Alignment.ALIGN_NORMAL, PromptUtils.getTextAlignment(resources, Gravity.START, "abc"));
    assertEquals(Layout.Alignment.ALIGN_NORMAL, PromptUtils.getTextAlignment(resources, Gravity.LEFT, "abc"));

    assertEquals(Layout.Alignment.ALIGN_OPPOSITE, PromptUtils.getTextAlignment(resources, Gravity.END, "abc"));
    assertEquals(Layout.Alignment.ALIGN_OPPOSITE, PromptUtils.getTextAlignment(resources, Gravity.RIGHT, "abc"));

    assertEquals(Layout.Alignment.ALIGN_CENTER, PromptUtils.getTextAlignment(resources, Gravity.CENTER_HORIZONTAL, "abc"));

    assertEquals(Layout.Alignment.ALIGN_NORMAL, PromptUtils.getTextAlignment(resources, Gravity.START, "جبا"));
    assertEquals(Layout.Alignment.ALIGN_NORMAL, PromptUtils.getTextAlignment(resources, Gravity.LEFT, "جبا"));

    assertEquals(Layout.Alignment.ALIGN_OPPOSITE, PromptUtils.getTextAlignment(resources, Gravity.END, "جبا"));
    assertEquals(Layout.Alignment.ALIGN_OPPOSITE, PromptUtils.getTextAlignment(resources, Gravity.RIGHT, "جبا"));

    assertEquals(Layout.Alignment.ALIGN_CENTER, PromptUtils.getTextAlignment(resources, Gravity.CENTER_HORIZONTAL, "جبا"));

    ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 16);

    assertEquals(Layout.Alignment.ALIGN_NORMAL, PromptUtils.getTextAlignment(resources, Gravity.START, "abc"));
    assertEquals(Layout.Alignment.ALIGN_NORMAL, PromptUtils.getTextAlignment(resources, Gravity.LEFT, "abc"));

    assertEquals(Layout.Alignment.ALIGN_OPPOSITE, PromptUtils.getTextAlignment(resources, Gravity.END, "abc"));
    assertEquals(Layout.Alignment.ALIGN_OPPOSITE, PromptUtils.getTextAlignment(resources, Gravity.RIGHT, "abc"));

    assertEquals(Layout.Alignment.ALIGN_CENTER, PromptUtils.getTextAlignment(resources, Gravity.CENTER_HORIZONTAL, "abc"));

    assertEquals(Layout.Alignment.ALIGN_NORMAL, PromptUtils.getTextAlignment(resources, Gravity.START, "جبا"));
    assertEquals(Layout.Alignment.ALIGN_NORMAL, PromptUtils.getTextAlignment(resources, Gravity.LEFT, "جبا"));

    assertEquals(Layout.Alignment.ALIGN_OPPOSITE, PromptUtils.getTextAlignment(resources, Gravity.END, "جبا"));
    assertEquals(Layout.Alignment.ALIGN_OPPOSITE, PromptUtils.getTextAlignment(resources, Gravity.RIGHT, "جبا"));

    assertEquals(Layout.Alignment.ALIGN_CENTER, PromptUtils.getTextAlignment(resources, Gravity.CENTER_HORIZONTAL, "جبا"));
}