/** * Check if this is a foss build (requires BuildConfig field) */ public boolean isFossBuild() { return bcbool("IS_FOSS_BUILD", false); }
/** * Get a {@link Bitmap} out of a {@link DrawableRes} */ public Bitmap drawableToBitmap(@DrawableRes int drawableId) { return drawableToBitmap(ContextCompat.getDrawable(_context, drawableId)); }
/** * Get a BuildConfig string value */ public Integer bcint(String fieldName, int defaultValue) { Object field = getBuildConfigValue(fieldName); if (field != null && field instanceof Integer) { return (Integer) field; } return defaultValue; }
pref.setSummary(String.format(locale, "%s\nVersion v%s (%d)", _cu.getPackageName(), _cu.getAppVersionName(), _cu.bcint("VERSION_CODE", 0))); String summary = String.format(locale, "\n<b>Package:</b> %s\n<b>Version:</b> v%s (%d)", _cu.getPackageName(), _cu.getAppVersionName(), _cu.bcint("VERSION_CODE", 0)); summary += (tmp = _cu.bcstr("FLAVOR", "")).isEmpty() ? "" : ("\n<b>Flavor:</b> " + tmp.replace("flavor", "")); summary += (tmp = _cu.bcstr("BUILD_TYPE", "")).isEmpty() ? "" : (" (" + tmp + ")"); summary += (tmp = _cu.bcstr("BUILD_DATE", "")).isEmpty() ? "" : ("\n<b>Build date:</b> " + tmp); summary += (tmp = _cu.getAppInstallationSource()).isEmpty() ? "" : ("\n<b>ISource:</b> " + tmp); summary += (tmp = _cu.bcstr("GITHASH", "")).isEmpty() ? "" : ("\n<b>VCS Hash:</b> " + tmp); pref.setSummary(_cu.htmlToSpanned(summary.trim().replace("\n", "<br/>"))); String[] data = (_cu.readTextfileFromRawRes(R.raw.project_team, "", "").trim() + "\n\n").split("\n"); for (int i = 0; i + 2 < data.length; i += 4) { Preference person = new Preference(context);
private void loadLangs(Context context, @Nullable AttributeSet attrs) { setDefaultValue(SYSTEM_LANGUAGE_CODE); // Fetch readable details ContextUtils contextUtils = new ContextUtils(context); List<String> languages = new ArrayList<>(); Object bcof = contextUtils.getBuildConfigValue("DETECTED_ANDROID_LOCALES"); if (bcof instanceof String[]) { for (String langId : (String[]) bcof) { Locale locale = contextUtils.getLocaleByAndroidCode(langId); languages.add(summarizeLocale(locale, langId) + ";" + langId); } } // Sort languages naturally Collections.sort(languages); // Show in UI String[] entries = new String[languages.size() + 2]; String[] entryval = new String[languages.size() + 2]; for (int i = 0; i < languages.size(); i++) { entries[i + 2] = languages.get(i).split(";")[0]; entryval[i + 2] = languages.get(i).split(";")[1]; } entryval[0] = SYSTEM_LANGUAGE_CODE; entries[0] = _systemLanguageName + " ยป " + summarizeLocale(context.getResources().getConfiguration().locale, ""); entryval[1] = _defaultLanguageCode; entries[1] = summarizeLocale(contextUtils.getLocaleByAndroidCode(_defaultLanguageCode), _defaultLanguageCode); setEntries(entries); setEntryValues(entryval); }
@Override public boolean callChangeListener(Object newValue) { if (newValue instanceof String) { // Does not apply to existing UI, use recreate() new ContextUtils(getContext()).setAppLanguage((String) newValue); } return super.callChangeListener(newValue); }
/** * Share the given bitmap with given format * * @param bitmap Image * @param format A {@link Bitmap.CompressFormat}, supporting JPEG,PNG,WEBP * @param imageName Filename without extension * @param quality Quality of the exported image [0-100] * @return if success, true */ public boolean shareImage(Bitmap bitmap, Bitmap.CompressFormat format, int quality, String imageName) { try { String ext = format.name().toLowerCase(); File file = File.createTempFile(imageName, "." + ext.replace("jpeg", "jpg"), _context.getExternalCacheDir()); if (bitmap != null && new ContextUtils(_context).writeImageToFile(file, bitmap, format, quality)) { shareStream(file, "image/" + ext); return true; } } catch (IOException e) { e.printStackTrace(); } return false; }
@Override public CharSequence getSummary() { Locale locale = new ContextUtils(getContext()).getLocaleByAndroidCode(getValue()); String prefix = TextUtils.isEmpty(super.getSummary()) ? "" : super.getSummary() + "\n\n"; return prefix + summarizeLocale(locale, getValue()); }
@Override @Deprecated public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { Activity activity = getActivity(); _appSettings = getAppSettings(activity); _cu = new ContextUtils(activity); getPreferenceManager().setSharedPreferencesName(getSharedPreferencesName()); addPreferencesFromResource(getPreferenceResourceForInflation()); if (activity != null && activity.getTheme() != null) { TypedArray array = activity.getTheme().obtainStyledAttributes(new int[]{android.R.attr.colorBackground}); int bgcolor = array.getColor(0, 0xFFFFFFFF); _defaultIconTintColor = _cu.shouldColorOnTopBeLight(bgcolor) ? Color.WHITE : Color.BLACK; } // on bottom afterOnCreate(savedInstanceState, activity); }
switch (keyToStringResId(preference)) { case R.string.pref_key__more_info__app: { _cu.openWebpageInExternalBrowser(getString(R.string.app_web_url)); return true; _cu.openWebpageInExternalBrowser(getString(R.string.app_community_url)); return true; _cu.openWebpageInExternalBrowser(getString(R.string.app_donate_url)); return true; _cu.openWebpageInExternalBrowser(getString(R.string.app_bugreport_url)); return true; _cu.openWebpageInExternalBrowser(getString(R.string.app_translate_url)); return true; _cu.openWebpageInExternalBrowser(getString(R.string.app_contribution_info_url)); return true; _cu.openWebpageInExternalBrowser( String.format("https://gsantner.net/android-contribution-guide/?packageid=%s&name=%s&web=%s", _cu.context().getPackageName(), getString(R.string.app_name), getString(R.string.app_web_url).replace("=", "%3D"))); return true; _cu.openWebpageInExternalBrowser(getString(R.string.app_source_code_url)); return true;
/** * Get a {@link Bitmap} from a given {@code imagePath} on the filesystem * Specifying a {@code maxDimen} is also possible and a value below 2000 * is recommended, otherwise a {@link OutOfMemoryError} may occur */ public Bitmap loadImageFromFilesystem(File imagePath, int maxDimen) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(imagePath.getAbsolutePath(), options); options.inSampleSize = calculateInSampleSize(options, maxDimen); options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(imagePath.getAbsolutePath(), options); }
/** * Get a BuildConfig bool value */ public Boolean bcbool(String fieldName, Boolean defaultValue) { Object field = getBuildConfigValue(fieldName); if (field != null && field instanceof Boolean) { return (Boolean) field; } return defaultValue; }
/** * Draw text in the center of the given {@link DrawableRes} * This may be useful for e.g. badge counts */ public Bitmap drawTextOnDrawable(@DrawableRes int drawableRes, String text, int textSize) { Resources resources = _context.getResources(); float scale = resources.getDisplayMetrics().density; Bitmap bitmap = drawableToBitmap(drawableRes); bitmap = bitmap.copy(bitmap.getConfig(), true); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.rgb(61, 61, 61)); paint.setTextSize((int) (textSize * scale)); paint.setShadowLayer(1f, 0f, 1f, Color.WHITE); Rect bounds = new Rect(); paint.getTextBounds(text, 0, text.length(), bounds); int x = (bitmap.getWidth() - bounds.width()) / 2; int y = (bitmap.getHeight() + bounds.height()) / 2; canvas.drawText(text, x, y, paint); return bitmap; }
/** * Check if this is a gplay build (requires BuildConfig field) */ public boolean isGooglePlayBuild() { return bcbool("IS_GPLAY_BUILD", true); }
/** * Get a BuildConfig string value */ public String bcstr(String fieldName, String defaultValue) { Object field = getBuildConfigValue(fieldName); if (field != null && field instanceof String) { return (String) field; } return defaultValue; }