return Uri.parse(PRIVACY_URI)
.buildUpon()
.appendQueryParameter("language", Locale.getDefault().getLanguage())
.appendQueryParameter("tag", Locale.getDefault().toLanguageTag())
.build();
}
static Uri getSupportUri(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String language = prefs.getString("language", null);
Locale slocale = Resources.getSystem().getConfiguration().locale;
return Uri.parse(SUPPORT_URI)
.buildUpon()
.appendQueryParameter("product", "fairemailsupport")
.appendQueryParameter("version", BuildConfig.VERSION_NAME)
.appendQueryParameter("locale", slocale.toString())
.appendQueryParameter("language", language == null ? "" : language)
.appendQueryParameter("installed", Helper.hasValidFingerprint(context) ? "" : "Other")
.build();
}
static Intent getIntentIssue(Context context) {
if (ActivityBilling.isPro(context)) {
String version = BuildConfig.VERSION_NAME + "/" +
(Helper.hasValidFingerprint(context) ? "1" : "3") +
(BuildConfig.PLAY_STORE_RELEASE ? "p" : "") +
(BuildConfig.DEBUG ? "d" : "") +
(ActivityBilling.isPro(context) ? "+" : "");
Intent intent = new Intent(Intent.ACTION_SEND);
//intent.setPackage(BuildConfig.APPLICATION_ID);
intent.setType("text/plain");
try {
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{Log.myAddress().getAddress()});
} catch (UnsupportedEncodingException ex) {
Log.w(ex);
}
intent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.title_issue_subject, version));
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String language = prefs.getString("language", null);
String uuid = prefs.getString("uuid", null);
Locale slocale = Resources.getSystem().getConfiguration().locale;
String html = "
";
html += "";
html += "Locale: " + Html.escapeHtml(slocale.toString()) + "
";
if (language != null)
html += "Language: " + Html.escapeHtml(language) + "
";
if (uuid != null)
html += "UUID: " + Html.escapeHtml(uuid) + "
";
html += "
";
intent.putExtra(Intent.EXTRA_TEXT, HtmlHelper.getText(context, html));
intent.putExtra(Intent.EXTRA_HTML_TEXT, html);
return intent;
} else {
if (Helper.hasValidFingerprint(context))
return new Intent(Intent.ACTION_VIEW, getSupportUri(context));
else
return new Intent(Intent.ACTION_VIEW, Uri.parse(XDA_URI));
}
}
static Intent getIntentRate(Context context) {
return new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + BuildConfig.APPLICATION_ID));
}
static long getInstallTime(Context context) {
try {
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(BuildConfig.APPLICATION_ID, 0);
if (pi != null)
return pi.firstInstallTime;
} catch (Throwable ex) {
Log.e(ex);
}
return 0;
}
static boolean isSupportedDevice() {
if ("Amazon".equals(Build.BRAND) && Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
/*
java.lang.IllegalArgumentException: Comparison method violates its general contract!
java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeHi(TimSort.java:864)
at java.util.TimSort.mergeAt(TimSort.java:481)
at java.util.TimSort.mergeCollapse(TimSort.java:406)
at java.util.TimSort.sort(TimSort.java:210)
at java.util.TimSort.sort(TimSort.java:169)
at java.util.Arrays.sort(Arrays.java:2010)
at java.util.Collections.sort(Collections.java:1883)
at android.view.ViewGroup$ChildListForAccessibility.init(ViewGroup.java:7181)
at android.view.ViewGroup$ChildListForAccessibility.obtain(ViewGroup.java:7138)
at android.view.ViewGroup.dispatchPopulateAccessibilityEventInternal(ViewGroup.java:2734)
at android.view.View.dispatchPopulateAccessibilityEvent(View.java:5617)
at android.view.View.sendAccessibilityEventUncheckedInternal(View.java:5582)
at android.view.View.sendAccessibilityEventUnchecked(View.java:5566)
at android.view.View.sendAccessibilityEventInternal(View.java:5543)
at android.view.View.sendAccessibilityEvent(View.java:5512)
at android.view.View.onFocusChanged(View.java:5449)
at android.view.View.handleFocusGainInternal(View.java:5229)
at android.view.ViewGroup.handleFocusGainInternal(ViewGroup.java:651)
at android.view.View.requestFocusNoSearch(View.java:7950)
at android.view.View.requestFocus(View.java:7929)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2612)
at android.view.ViewGroup.onRequestFocusInDescendants(ViewGroup.java:2657)
at android.view.ViewGroup.requestFocus(ViewGroup.java:2613)
at android.view.View.requestFocus(View.java:7896)
at android.view.View.requestFocus(View.java:7875)
at androidx.recyclerview.widget.RecyclerView.recoverFocusFromState(SourceFile:3788)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep3(SourceFile:4023)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(SourceFile:3652)
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(SourceFile:1877)
at androidx.recyclerview.widget.RecyclerView$w.run(SourceFile:5044)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:781)
at android.view.Choreographer.doCallbacks(Choreographer.java:592)
at android.view.Choreographer.doFrame(Choreographer.java:559)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:767)
*/
return false;
}
return true;
}
static boolean isSamsung() {
return "Samsung".equalsIgnoreCase(Build.MANUFACTURER);
}
static boolean isOnePlus() {
return "OnePlus".equalsIgnoreCase(Build.MANUFACTURER);
}
static boolean isHuawei() {
return "HUAWEI".equalsIgnoreCase(Build.MANUFACTURER);
}
static boolean isXiaomi() {
return "Xiaomi".equalsIgnoreCase(Build.MANUFACTURER);
}
static boolean isMeizu() {
return "Meizu".equalsIgnoreCase(Build.MANUFACTURER);
}
static boolean isAsus() {
return "asus".equalsIgnoreCase(Build.MANUFACTURER);
}
static boolean isWiko() {
return "WIKO".equalsIgnoreCase(Build.MANUFACTURER);
}
static boolean isLenovo() {
return "LENOVO".equalsIgnoreCase(Build.MANUFACTURER);
}
static boolean isOppo() {
return "OPPO".equalsIgnoreCase(Build.MANUFACTURER);
}
static boolean isRealme() {
return "realme".equalsIgnoreCase(Build.MANUFACTURER);
}
static boolean isBlackview() {
return "Blackview".equalsIgnoreCase(Build.MANUFACTURER);
}
static boolean isSony() {
return "sony".equalsIgnoreCase(Build.MANUFACTURER);
}
static boolean isStaminaEnabled(Context context) {
// https://dontkillmyapp.com/sony
if (BuildConfig.DEBUG)
return true;
if (!isSony())
return false;
try {
ContentResolver resolver = context.getContentResolver();
return (Settings.Secure.getInt(resolver, "somc.stamina_mode", 0) > 0);
} catch (Throwable ex) {
Log.e(ex);
return false;
}
}
static boolean isSurfaceDuo() {
return ("Microsoft".equalsIgnoreCase(Build.MANUFACTURER) && "Surface Duo".equals(Build.MODEL));
}
static boolean isKilling() {
// https://dontkillmyapp.com/
return (isSamsung() ||
isOnePlus() ||
isHuawei() ||
isXiaomi() ||
isMeizu() ||
isAsus() ||
isWiko() ||
isLenovo() ||
isOppo() ||
// Vivo
isRealme() ||
isBlackview() ||
isSony() ||
BuildConfig.DEBUG);
}
static boolean isDozeRequired() {
return (Build.VERSION.SDK_INT > Build.VERSION_CODES.R && false);
}
static void reportNoViewer(Context context, Uri uri) {
reportNoViewer(context, new Intent().setData(uri));
}
static void reportNoViewer(Context context, Intent intent) {
View dview = LayoutInflater.from(context).inflate(R.layout.dialog_no_viewer, null);
TextView tvName = dview.findViewById(R.id.tvName);
TextView tvFullName = dview.findViewById(R.id.tvFullName);
TextView tvType = dview.findViewById(R.id.tvType);
String title = intent.getStringExtra(Intent.EXTRA_TITLE);
Uri data = intent.getData();
String type = intent.getType();
String fullName = (data == null ? intent.toString() : data.toString());
String extension = (data == null ? null : getExtension(data.getLastPathSegment()));
tvName.setText(title == null ? fullName : title);
tvFullName.setText(fullName);
tvFullName.setVisibility(title == null ? View.GONE : View.VISIBLE);
tvType.setText(type);
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setView(dview)
.setNegativeButton(android.R.string.cancel, null);
if (hasPlayStore(context) && !TextUtils.isEmpty(extension)) {
builder.setNeutralButton(R.string.title_no_viewer_search, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
try {
Uri search = Uri.parse(PLAY_STORE_SEARCH)
.buildUpon()
.appendQueryParameter("q", extension)
.build();
Intent intent = new Intent(Intent.ACTION_VIEW, search);
context.startActivity(intent);
} catch (Throwable ex) {
Log.e(ex);
ToastEx.makeText(context, ex.toString(), Toast.LENGTH_LONG).show();
}
}
});
}
builder.show();
}
static void excludeFromRecents(Context context) {
try {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (am == null)
return;
List tasks = am.getAppTasks();
if (tasks == null || tasks.size() == 0)
return;
tasks.get(0).setExcludeFromRecents(true);
} catch (Throwable ex) {
Log.e(ex);
}
}
static int getOffset(TextView widget, Spannable buffer, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
return layout.getOffsetForHorizontal(line, x);
}
// Graphics
static int dp2pixels(Context context, int dp) {
float scale = context.getResources().getDisplayMetrics().density;
return Math.round(dp * scale);
}
static int pixels2dp(Context context, float pixels) {
float scale = context.getResources().getDisplayMetrics().density;
return Math.round(pixels / scale);
}
static float getTextSize(Context context, int zoom) {
TypedArray ta = null;
try {
if (zoom == 0)
ta = context.obtainStyledAttributes(
R.style.TextAppearance_AppCompat_Small, new int[]{android.R.attr.textSize});
else if (zoom == 2)
ta = context.obtainStyledAttributes(
R.style.TextAppearance_AppCompat_Large, new int[]{android.R.attr.textSize});
else
ta = context.obtainStyledAttributes(
R.style.TextAppearance_AppCompat_Medium, new int[]{android.R.attr.textSize});
return ta.getDimension(0, 0);
} finally {
if (ta != null)
ta.recycle();
}
}
static int resolveColor(Context context, int attr) {
return resolveColor(context, attr, 0xFF0000);
}
static int resolveColor(Context context, int attr, int def) {
int[] attrs = new int[]{attr};
TypedArray a = context.getTheme().obtainStyledAttributes(attrs);
int color = a.getColor(0, def);
a.recycle();
return color;
}
static void setViewsEnabled(ViewGroup view, boolean enabled) {
for (int i = 0; i < view.getChildCount(); i++) {
View child = view.getChildAt(i);
if ("ignore".equals(child.getTag()))
continue;
if (child instanceof Spinner ||
child instanceof EditText ||
child instanceof CheckBox ||
child instanceof ImageView /* =ImageButton */ ||
child instanceof RadioButton ||
(child instanceof Button && "disable".equals(child.getTag())))
child.setEnabled(enabled);
else if (child instanceof BottomNavigationView) {
Menu menu = ((BottomNavigationView) child).getMenu();
menu.setGroupEnabled(0, enabled);
} else if (child instanceof RecyclerView)
; // do nothing
else if (child instanceof ViewGroup)
setViewsEnabled((ViewGroup) child, enabled);
}
}
static void hide(View view) {
view.setPadding(0, 1, 0, 0);
ViewGroup.LayoutParams lparam = view.getLayoutParams();
lparam.width = 0;
lparam.height = 1;
if (lparam instanceof ConstraintLayout.LayoutParams)
((ConstraintLayout.LayoutParams) lparam).setMargins(0, 0, 0, 0);
view.setLayoutParams(lparam);
}
static boolean isNight(Context context) {
// https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#configuration_changes
int uiMode = context.getResources().getConfiguration().uiMode;
Log.i("UI mode=0x" + Integer.toHexString(uiMode));
return ((uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES);
}
static boolean isDarkTheme(Context context) {
TypedValue tv = new TypedValue();
context.getTheme().resolveAttribute(R.attr.themeName, tv, true);
return (tv.string != null && !"light".contentEquals(tv.string));
}
static void showKeyboard(final View view) {
final Context context = view.getContext();
InputMethodManager imm =
(InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm == null)
return;
Log.i("showKeyboard view=" + view);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
static void hideKeyboard(final View view) {
final Context context = view.getContext();
InputMethodManager imm =
(InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm == null)
return;
Log.i("hideKeyboard view=" + view);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
// Formatting
private static final DecimalFormat df = new DecimalFormat("@@");
static String humanReadableByteCount(long bytes) {
return humanReadableByteCount(bytes, true);
}
static String humanReadableByteCount(long bytes, boolean si) {
int sign = (int) Math.signum(bytes);
bytes = Math.abs(bytes);
int unit = (si ? 1000 : 1024);
if (bytes < unit)
return sign * bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
return df.format(sign * bytes / Math.pow(unit, exp)) + " " + pre + "B";
}
static boolean isPrintableChar(char c) {
Character.UnicodeBlock block = Character.UnicodeBlock.of(c);
if (block == null || block == Character.UnicodeBlock.SPECIALS)
return false;
return !Character.isISOControl(c);
}
// https://issuetracker.google.com/issues/37054851
static DateFormat getTimeInstance(Context context) {
return getTimeInstance(context, SimpleDateFormat.MEDIUM);
}
static DateFormat getTimeInstance(Context context, int style) {
if (context != null &&
(style == SimpleDateFormat.SHORT || style == SimpleDateFormat.MEDIUM))
return new SimpleDateFormat(getTimePattern(context, style));
else
return SimpleDateFormat.getTimeInstance(style);
}
static DateFormat getDateInstance(Context context) {
return getDateInstance(context, SimpleDateFormat.MEDIUM);
}
private static DateFormat getDateInstance(Context context, int style) {
return SimpleDateFormat.getDateInstance(style);
}
static DateFormat getDateTimeInstance(Context context) {
return getDateTimeInstance(context, SimpleDateFormat.MEDIUM, SimpleDateFormat.MEDIUM);
}
static DateFormat getDateTimeInstance(Context context, int dateStyle, int timeStyle) {
if (context != null &&
(timeStyle == SimpleDateFormat.SHORT || timeStyle == SimpleDateFormat.MEDIUM)) {
DateFormat dateFormat = getDateInstance(context, dateStyle);
if (dateFormat instanceof SimpleDateFormat) {
String datePattern = ((SimpleDateFormat) dateFormat).toPattern();
String timePattern = getTimePattern(context, timeStyle);
return new SimpleDateFormat(datePattern + " " + timePattern);
}
}
return SimpleDateFormat.getDateTimeInstance(dateStyle, timeStyle);
}
private static String getTimePattern(Context context, int style) {
// https://issuetracker.google.com/issues/37054851
boolean is24Hour = android.text.format.DateFormat.is24HourFormat(context);
String skeleton = (is24Hour ? "Hm" : "hm");
if (style == SimpleDateFormat.MEDIUM)
skeleton += "s";
return android.text.format.DateFormat.getBestDateTimePattern(Locale.getDefault(), skeleton);
}
static CharSequence getRelativeTimeSpanString(Context context, long millis) {
long now = System.currentTimeMillis();
long span = Math.abs(now - millis);
Time nowTime = new Time();
Time thenTime = new Time();
nowTime.set(now);
thenTime.set(millis);
if (span < DateUtils.DAY_IN_MILLIS && nowTime.weekDay == thenTime.weekDay)
return getTimeInstance(context, SimpleDateFormat.SHORT).format(millis);
else
return DateUtils.getRelativeTimeSpanString(context, millis);
}
static void linkPro(final TextView tv) {
if (ActivityBilling.isPro(tv.getContext()) && !BuildConfig.DEBUG)
hide(tv);
else {
tv.getPaint().setUnderlineText(true);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv.getContext().startActivity(new Intent(tv.getContext(), ActivityBilling.class));
}
});
}
}
static String getString(Context context, String language, int resid, Object... formatArgs) {
if (language == null)
return context.getString(resid, formatArgs);
Configuration configuration = new Configuration(context.getResources().getConfiguration());
configuration.setLocale(new Locale(language));
Resources res = context.createConfigurationContext(configuration).getResources();
return res.getString(resid, formatArgs);
}
static String[] getStrings(Context context, int resid, Object... formatArgs) {
return getStrings(context, null, resid, formatArgs);
}
static String[] getStrings(Context context, String language, int resid, Object... formatArgs) {
List locales = new ArrayList<>();
if (language != null)
locales.add(new Locale(language));
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
Locale l = Locale.getDefault();
if (!l.getLanguage().equals(language))
locales.add(l);
if (!"en".equals(language) && !"en".equals(l.getLanguage()))
locales.add(new Locale("en"));
} else {
LocaleList ll = context.getResources().getConfiguration().getLocales();
for (int i = 0; i < ll.size(); i++) {
Locale l = ll.get(i);
if (!l.getLanguage().equals(language))
locales.add(l);
}
}
List result = new ArrayList<>();
Configuration configuration = new Configuration(context.getResources().getConfiguration());
for (Locale locale : locales) {
configuration.setLocale(locale);
Resources res = context.createConfigurationContext(configuration).getResources();
String text = res.getString(resid, formatArgs);
result.add(text);
}
return result.toArray(new String[0]);
}
static String getLocalizedAsset(Context context, String name) throws IOException {
if (name == null || !name.contains("."))
throw new IllegalArgumentException(name);
String[] list = context.getResources().getAssets().list("");
if (list == null)
throw new IllegalArgumentException();
List names = new ArrayList<>();
String[] c = name.split("\\.");
List assets = Arrays.asList(list);
List locales = new ArrayList<>();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
locales.add(Locale.getDefault());
else {
LocaleList ll = context.getResources().getConfiguration().getLocales();
for (int i = 0; i < ll.size(); i++)
locales.add(ll.get(i));
}
for (Locale locale : locales) {
String language = locale.getLanguage();
String country = locale.getCountry();
if ("en".equals(language) && "US".equals(country))
names.add(name);
else {
String localized = c[0] + "-" + language + "-r" + country + "." + c[1];
if (assets.contains(localized))
names.add(localized);
}
}
for (Locale locale : locales) {
String prefix = c[0] + "-" + locale.getLanguage();
for (String asset : assets)
if (asset.startsWith(prefix))
names.add(asset);
}
names.add(name);
String asset = names.get(0);
Log.i("Using " + asset +
" of " + TextUtils.join(",", names) +
" (" + TextUtils.join(",", locales) + ")");
return asset;
}
static boolean containsWhiteSpace(String text) {
return text.matches(".*\\s+.*");
}
static boolean containsControlChars(String text) {
int codePoint;
for (int offset = 0; offset < text.length(); ) {
codePoint = text.codePointAt(offset);
offset += Character.charCount(codePoint);
switch (Character.getType(codePoint)) {
case Character.CONTROL: // \p{Cc}
case Character.FORMAT: // \p{Cf}
case Character.PRIVATE_USE: // \p{Co}
case Character.SURROGATE: // \p{Cs}
case Character.UNASSIGNED: // \p{Cn}
return true;
}
}
return false;
}
static boolean isSingleScript(String s) {
// https://en.wikipedia.org/wiki/IDN_homograph_attack
if (TextUtils.isEmpty(s))
return true;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
return true;
int codepoint;
Character.UnicodeScript us;
Character.UnicodeScript script = null;
for (int i = 0; i < s.length(); ) {
codepoint = s.codePointAt(i);
i += Character.charCount(codepoint);
us = Character.UnicodeScript.of(codepoint);
if (us.equals(Character.UnicodeScript.COMMON))
continue;
if (script == null)
script = us;
else if (!us.equals(script))
return false;
}
return true;
}
static Integer parseInt(String text) {
if (TextUtils.isEmpty(text))
return null;
if (!TextUtils.isDigitsOnly(text))
return null;
try {
return Integer.parseInt(text);
} catch (NumberFormatException ignored) {
return null;
}
}
// Files
static String sanitizeFilename(String name) {
if (name == null)
return null;
return name
// Canonical files names cannot contain NUL
.replace("\0", "")
.replaceAll("[?:\"*|/\\\\<>]", "_");
}
static String getExtension(String filename) {
if (filename == null)
return null;
int index = filename.lastIndexOf(".");
if (index < 0)
return null;
return filename.substring(index + 1);
}
static String guessMimeType(String filename) {
String type = null;
String extension = Helper.getExtension(filename);
if (extension != null) {
extension = extension.toLowerCase(Locale.ROOT);
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
if (TextUtils.isEmpty(type))
if ("csv".equals(extension))
return "text/csv";
else if ("eml".equals(extension))
return "message/rfc822";
else if ("gpx".equals(extension))
return "application/gpx+xml";
else if ("log".equals(extension))
return "text/plain";
else if ("ovpn".equals(extension))
return "application/x-openvpn-profile";
else if ("mbox".equals(extension))
return "application/mbox"; // https://tools.ietf.org/html/rfc4155
else
return "application/octet-stream";
return type;
}
static String guessExtension(String mimeType) {
String extension = null;
if (mimeType != null) {
mimeType = mimeType.toLowerCase(Locale.ROOT);
extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
}
if (TextUtils.isEmpty(extension))
if ("text/csv".equals(mimeType))
return "csv";
else if ("message/rfc822".equals(mimeType))
return "eml";
else if ("application/gpx+xml".equals(mimeType))
return "gpx";
else if ("application/x-openvpn-profile".equals(mimeType))
return "ovpn";
return extension;
}
static void writeText(File file, String content) throws IOException {
try (FileOutputStream out = new FileOutputStream(file)) {
if (content != null)
out.write(content.getBytes());
}
}
static String readStream(InputStream is) throws IOException {
return readStream(is, StandardCharsets.UTF_8);
}
static String readStream(InputStream is, Charset charset) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream(Math.max(BUFFER_SIZE, is.available()));
byte[] buffer = new byte[BUFFER_SIZE];
for (int len = is.read(buffer); len != -1; len = is.read(buffer))
os.write(buffer, 0, len);
return new String(os.toByteArray(), charset);
}
static String readText(File file) throws IOException {
try (FileInputStream in = new FileInputStream(file)) {
return readStream(in);
}
}
public static void readBuffer(InputStream is, byte[] buffer) throws IOException {
int left = buffer.length;
while (left > 0) {
int count = is.read(buffer, buffer.length - left, left);
if (count < 0)
throw new IOException("EOF");
left -= count;
}
}
static void copy(File src, File dst) throws IOException {
try (InputStream in = new FileInputStream(src)) {
try (FileOutputStream out = new FileOutputStream(dst)) {
copy(in, out);
}
}
}
static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buf = new byte[BUFFER_SIZE];
int len;
while ((len = in.read(buf)) > 0)
out.write(buf, 0, len);
}
static long copy(Context context, Uri uri, File file) throws IOException {
long size = 0;
InputStream is = null;
OutputStream os = null;
try {
is = context.getContentResolver().openInputStream(uri);
os = new FileOutputStream(file);
byte[] buffer = new byte[Helper.BUFFER_SIZE];
for (int len = is.read(buffer); len != -1; len = is.read(buffer)) {
size += len;
os.write(buffer, 0, len);
}
} finally {
try {
if (is != null)
is.close();
} finally {
if (os != null)
os.close();
}
}
return size;
}
static long getAvailableStorageSpace() {
StatFs stats = new StatFs(Environment.getDataDirectory().getAbsolutePath());
return stats.getAvailableBlocksLong() * stats.getBlockSizeLong();
}
static long getTotalStorageSpace() {
StatFs stats = new StatFs(Environment.getDataDirectory().getAbsolutePath());
return stats.getTotalBytes();
}
static long getSize(File dir) {
long size = 0;
File[] listed = dir.listFiles();
if (listed != null)
for (File file : listed)
if (file.isDirectory())
size += getSize(file);
else
size += file.length();
return size;
}
static void openAdvanced(Intent intent) {
// https://issuetracker.google.com/issues/72053350
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
intent.putExtra("android.content.extra.FANCY", true);
intent.putExtra("android.content.extra.SHOW_FILESIZE", true);
intent.putExtra("android.provider.extra.SHOW_ADVANCED", true);
//File initial = Environment.getExternalStorageDirectory();
//intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, Uri.fromFile(initial));
}
static HttpURLConnection openUrlRedirect(Context context, String source, int timeout) throws IOException {
int redirects = 0;
URL url = new URL(source);
while (true) {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(false);
urlConnection.setReadTimeout(timeout);
urlConnection.setConnectTimeout(timeout);
urlConnection.setInstanceFollowRedirects(true);
urlConnection.setRequestProperty("User-Agent", WebViewEx.getUserAgent(context));
urlConnection.connect();
try {
int status = urlConnection.getResponseCode();
if (status == HttpURLConnection.HTTP_MOVED_PERM ||
status == HttpURLConnection.HTTP_MOVED_TEMP ||
status == HttpURLConnection.HTTP_SEE_OTHER ||
status == 307 /* Temporary redirect */ ||
status == 308 /* Permanent redirect */) {
if (++redirects > MAX_REDIRECTS)
throw new IOException("Too many redirects");
String header = urlConnection.getHeaderField("Location");
if (header == null)
throw new IOException("Location header missing");
String location = URLDecoder.decode(header, StandardCharsets.UTF_8.name());
url = new URL(url, location);
Log.i("Redirect #" + redirects + " to " + url);
urlConnection.disconnect();
continue;
}
if (status != HttpURLConnection.HTTP_OK)
throw new IOException("Error " + status + ": " + urlConnection.getResponseMessage());
return urlConnection;
} catch (IOException ex) {
urlConnection.disconnect();
throw ex;
}
}
}
// Cryptography
static String sha256(String data) throws NoSuchAlgorithmException {
return sha256(data.getBytes());
}
static String sha1(byte[] data) throws NoSuchAlgorithmException {
return sha("SHA-1", data);
}
static String sha256(byte[] data) throws NoSuchAlgorithmException {
return sha("SHA-256", data);
}
static String md5(byte[] data) throws NoSuchAlgorithmException {
return sha("MD5", data);
}
static String sha(String digest, byte[] data) throws NoSuchAlgorithmException {
byte[] bytes = MessageDigest.getInstance(digest).digest(data);
return hex(bytes);
}
static String hex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes)
sb.append(String.format("%02x", b));
return sb.toString();
}
static String getFingerprint(Context context) {
return getFingerprint(context, "SHA1");
}
static String getFingerprint(Context context, String hash) {
try {
PackageManager pm = context.getPackageManager();
String pkg = context.getPackageName();
PackageInfo info = pm.getPackageInfo(pkg, PackageManager.GET_SIGNATURES);
byte[] cert = info.signatures[0].toByteArray();
MessageDigest digest = MessageDigest.getInstance(hash);
byte[] bytes = digest.digest(cert);
StringBuilder sb = new StringBuilder();
for (byte b : bytes)
sb.append(String.format("%02X", b));
return sb.toString();
} catch (Throwable ex) {
Log.e(ex);
return null;
}
}
static boolean hasValidFingerprint(Context context) {
if (hasValidFingerprint == null) {
hasValidFingerprint = false;
String signed = getFingerprint(context);
String[] fingerprints = new String[]{
context.getString(R.string.fingerprint),
context.getString(R.string.fingerprint_amazon)
};
for (String fingerprint : fingerprints)
if (Objects.equals(signed, fingerprint)) {
hasValidFingerprint = true;
break;
}
}
return hasValidFingerprint;
}
static boolean canAuthenticate(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String pin = prefs.getString("pin", null);
if (!TextUtils.isEmpty(pin))
return true;
BiometricManager bm = BiometricManager.from(context);
return (bm.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK) == BiometricManager.BIOMETRIC_SUCCESS);
}
static boolean shouldAuthenticate(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean biometrics = prefs.getBoolean("biometrics", false);
String pin = prefs.getString("pin", null);
if (biometrics || !TextUtils.isEmpty(pin)) {
long now = new Date().getTime();
long last_authentication = prefs.getLong("last_authentication", 0);
long biometrics_timeout = prefs.getInt("biometrics_timeout", 2) * 60 * 1000L;
Log.i("Authentication valid until=" + new Date(last_authentication + biometrics_timeout));
if (last_authentication + biometrics_timeout < now)
return true;
prefs.edit().putLong("last_authentication", now).apply();
}
return false;
}
static void authenticate(final FragmentActivity activity, final LifecycleOwner owner,
Boolean enabled, final
Runnable authenticated, final Runnable cancelled) {
Log.i("Authenticate " + activity);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
String pin = prefs.getString("pin", null);
if (enabled != null || TextUtils.isEmpty(pin)) {
Log.i("Authenticate biometric enabled=" + enabled);
BiometricPrompt.PromptInfo.Builder info = new BiometricPrompt.PromptInfo.Builder()
.setTitle(activity.getString(enabled == null ? R.string.app_name : R.string.title_setup_biometrics));
KeyguardManager kgm = (KeyguardManager) activity.getSystemService(Context.KEYGUARD_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && kgm != null && kgm.isDeviceSecure())
info.setDeviceCredentialAllowed(true);
else
info.setNegativeButtonText(activity.getString(android.R.string.cancel));
info.setConfirmationRequired(false);
info.setSubtitle(activity.getString(enabled == null ? R.string.title_setup_biometrics_unlock
: enabled
? R.string.title_setup_biometrics_disable
: R.string.title_setup_biometrics_enable));
final BiometricPrompt prompt = new BiometricPrompt(activity, executor,
new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(final int errorCode, @NonNull final CharSequence errString) {
Log.w("Authenticate biometric error " + errorCode + ": " + errString);
if (errorCode != BiometricPrompt.ERROR_NEGATIVE_BUTTON &&
errorCode != BiometricPrompt.ERROR_CANCELED &&
errorCode != BiometricPrompt.ERROR_USER_CANCELED)
ApplicationEx.getMainHandler().post(new Runnable() {
@Override
public void run() {
ToastEx.makeText(activity,
"Error " + errorCode + ": " + errString,
Toast.LENGTH_LONG).show();
}
});
ApplicationEx.getMainHandler().post(cancelled);
}
@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
Log.i("Authenticate biometric succeeded");
setAuthenticated(activity);
ApplicationEx.getMainHandler().post(authenticated);
}
@Override
public void onAuthenticationFailed() {
Log.w("Authenticate biometric failed");
ApplicationEx.getMainHandler().post(cancelled);
}
});
prompt.authenticate(info.build());
final Runnable cancelPrompt = new Runnable() {
@Override
public void run() {
try {
prompt.cancelAuthentication();
} catch (Throwable ex) {
Log.e(ex);
}
}
};
ApplicationEx.getMainHandler().postDelayed(cancelPrompt, 60 * 1000L);
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onDestroy() {
Log.i("Authenticate destroyed");
ApplicationEx.getMainHandler().post(cancelPrompt);
}
});
} else {
Log.i("Authenticate PIN");
final View dview = LayoutInflater.from(activity).inflate(R.layout.dialog_pin_ask, null);
final EditText etPin = dview.findViewById(R.id.etPin);
final AlertDialog dialog = new AlertDialog.Builder(activity)
.setView(dview)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
String pin = prefs.getString("pin", "");
String entered = etPin.getText().toString();
Log.i("Authenticate PIN ok=" + pin.equals(entered));
if (pin.equals(entered)) {
setAuthenticated(activity);
ApplicationEx.getMainHandler().post(authenticated);
} else {
ApplicationEx.getMainHandler().post(cancelled);
}
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("Authenticate PIN cancelled");
ApplicationEx.getMainHandler().post(cancelled);
}
})
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
Log.i("Authenticate PIN dismissed");
if (shouldAuthenticate(activity)) // Some Android versions call dismiss on OK
ApplicationEx.getMainHandler().post(cancelled);
}
})
.create();
etPin.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
return true;
} else
return false;
}
});
etPin.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
});
ApplicationEx.getMainHandler().post(new Runnable() {
@Override
public void run() {
etPin.requestFocus();
}
});
try {
dialog.show();
} catch (Throwable ex) {
Log.e(ex);
/*
java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.faircode.email/eu.faircode.email.ActivityMain}: java.lang.RuntimeException: InputChannel is not initialized.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3477)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3620)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2183)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:7604)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
Caused by: java.lang.RuntimeException: InputChannel is not initialized.
at android.view.InputEventReceiver.nativeInit(Native Method)
at android.view.InputEventReceiver.(InputEventReceiver.java:71)
at android.view.ViewRootImpl$WindowInputEventReceiver.(ViewRootImpl.java:7758)
at android.view.ViewRootImpl.setView(ViewRootImpl.java:1000)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:393)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
at android.app.Dialog.show(Dialog.java:342)
at eu.faircode.email.Helper.authenticate(SourceFile:15)
at eu.faircode.email.ActivityMain.onCreate(SourceFile:24)
at android.app.Activity.performCreate(Activity.java:7822)
*/
}
}
}
static void setAuthenticated(Context context) {
Date now = new Date();
Log.i("Authenticated now=" + now);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putLong("last_authentication", now.getTime()).apply();
}
static void clearAuthentication(Context context) {
Log.i("Authenticate clear");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().remove("last_authentication").apply();
}
static void selectKeyAlias(final Activity activity, final LifecycleOwner owner, final String alias, final IKeyAlias intf) {
final Context context = activity.getApplicationContext();
new Thread(new Runnable() {
@Override
public void run() {
if (alias != null)
try {
if (KeyChain.getPrivateKey(context, alias) != null) {
Log.i("Private key available alias=" + alias);
deliver(alias);
return;
}
} catch (KeyChainException ex) {
Log.w(ex);
} catch (Throwable ex) {
Log.e(ex);
}
ApplicationEx.getMainHandler().post(new Runnable() {
@Override
public void run() {
KeyChain.choosePrivateKeyAlias(activity, new KeyChainAliasCallback() {
@Override
public void alias(@Nullable final String alias) {
Log.i("Selected key alias=" + alias);
deliver(alias);
}
},
null, null, null, -1, alias);
}
});
}
private void deliver(final String selected) {
ApplicationEx.getMainHandler().post(new Runnable() {
@Override
public void run() {
if (owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
if (selected == null)
intf.onNothingSelected();
else
intf.onSelected(selected);
} else {
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
owner.getLifecycle().removeObserver(this);
if (selected == null)
intf.onNothingSelected();
else
intf.onSelected(selected);
}
});
}
}
});
}
}).start();
}
interface IKeyAlias {
void onSelected(String alias);
void onNothingSelected();
}
public static String HMAC(String algo, int blocksize, byte[] key, byte[] text) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance(algo);
if (key.length > blocksize)
key = md.digest(key);
byte[] ipad = new byte[blocksize];
byte[] opad = new byte[blocksize];
for (int i = 0; i < key.length; i++) {
ipad[i] = key[i];
opad[i] = key[i];
}
for (int i = 0; i < blocksize; i++) {
ipad[i] ^= 0x36;
opad[i] ^= 0x5c;
}
byte[] digest;
md.update(ipad);
md.update(text);
digest = md.digest();
md.update(opad);
md.update(digest);
digest = md.digest();
StringBuilder sb = new StringBuilder();
for (byte b : digest)
sb.append(String.format("%02x", b));
return sb.toString();
}
// Miscellaneous
static void gc() {
if (BuildConfig.DEBUG) {
Runtime.getRuntime().gc();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static List> chunkList(List list, int size) {
List> result = new ArrayList<>(list.size() / size);
for (int i = 0; i < list.size(); i += size)
result.add(list.subList(i, i + size < list.size() ? i + size : list.size()));
return result;
}
static long[] toLongArray(List list) {
long[] result = new long[list.size()];
for (int i = 0; i < list.size(); i++)
result[i] = list.get(i);
return result;
}
static List fromLongArray(long[] array) {
List result = new ArrayList<>();
for (int i = 0; i < array.length; i++)
result.add(array[i]);
return result;
}
static boolean equal(String[] a1, String[] a2) {
if (a1 == null && a2 == null)
return true;
if (a1 == null || a2 == null)
return false;
if (a1.length != a2.length)
return false;
for (int i = 0; i < a1.length; i++)
if (!a1[i].equals(a2[i]))
return false;
return true;
}
static int getSize(Bundle bundle) {
Parcel p = Parcel.obtain();
bundle.writeToParcel(p, 0);
return p.dataSize();
}
}