FairEmail/app/src/main/java/eu/faircode/email/Helper.java

1307 lines
50 KiB
Java
Raw Normal View History

2018-08-02 13:33:06 +00:00
package eu.faircode.email;
/*
2018-08-14 05:53:24 +00:00
This file is part of FairEmail.
2018-08-02 13:33:06 +00:00
2018-08-14 05:53:24 +00:00
FairEmail is free software: you can redistribute it and/or modify
2018-08-02 13:33:06 +00:00
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2018-10-29 10:46:49 +00:00
FairEmail is distributed in the hope that it will be useful,
2018-08-02 13:33:06 +00:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
2018-10-29 10:46:49 +00:00
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
2018-08-02 13:33:06 +00:00
2020-01-05 17:32:53 +00:00
Copyright 2018-2020 by Marcel Bokhorst (M66B)
2018-08-02 13:33:06 +00:00
*/
2019-12-12 12:34:04 +00:00
import android.Manifest;
2019-12-05 14:18:53 +00:00
import android.app.Activity;
import android.app.KeyguardManager;
2018-11-08 19:51:38 +00:00
import android.content.ActivityNotFoundException;
2020-02-26 10:19:19 +00:00
import android.content.ComponentName;
2018-08-02 13:33:06 +00:00
import android.content.Context;
2018-12-01 12:17:33 +00:00
import android.content.DialogInterface;
import android.content.Intent;
2019-07-10 15:58:26 +00:00
import android.content.SharedPreferences;
2018-09-27 06:44:02 +00:00
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
2018-12-23 13:34:42 +00:00
import android.content.pm.ResolveInfo;
2019-12-01 11:24:03 +00:00
import android.content.res.Configuration;
import android.content.res.Resources;
2018-08-06 15:07:46 +00:00
import android.content.res.TypedArray;
2019-10-01 08:01:44 +00:00
import android.graphics.Color;
2018-09-19 11:03:44 +00:00
import android.net.Uri;
2019-07-12 17:33:40 +00:00
import android.os.Build;
2018-12-01 12:17:33 +00:00
import android.os.Bundle;
2019-10-19 19:53:19 +00:00
import android.os.Environment;
2019-07-10 15:58:26 +00:00
import android.os.Handler;
2019-12-01 11:24:03 +00:00
import android.os.LocaleList;
2019-04-11 07:47:49 +00:00
import android.os.Parcel;
2019-09-26 10:11:46 +00:00
import android.os.PowerManager;
2019-10-19 19:53:19 +00:00
import android.os.StatFs;
2020-03-19 10:00:12 +00:00
import android.provider.DocumentsContract;
2019-12-05 14:18:53 +00:00
import android.security.KeyChain;
import android.security.KeyChainAliasCallback;
import android.security.KeyChainException;
2019-05-13 06:02:56 +00:00
import android.text.Spannable;
import android.text.Spanned;
2019-10-20 10:17:04 +00:00
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.format.Time;
2019-05-13 12:29:52 +00:00
import android.util.TypedValue;
2019-10-20 10:17:04 +00:00
import android.view.KeyEvent;
import android.view.LayoutInflater;
2018-08-13 13:53:46 +00:00
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
2019-11-01 09:50:32 +00:00
import android.view.WindowManager;
2019-10-20 10:17:04 +00:00
import android.view.inputmethod.EditorInfo;
2019-11-20 11:40:56 +00:00
import android.webkit.MimeTypeMap;
2019-01-27 17:48:41 +00:00
import android.webkit.WebView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
2018-08-13 13:53:46 +00:00
import android.widget.ImageView;
2019-09-19 13:29:00 +00:00
import android.widget.RadioButton;
import android.widget.Spinner;
2019-06-28 19:22:20 +00:00
import android.widget.TextView;
2018-11-08 19:51:38 +00:00
import android.widget.Toast;
2018-08-02 13:33:06 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
2019-11-09 10:18:55 +00:00
import androidx.biometric.BiometricManager;
2019-07-10 15:58:26 +00:00
import androidx.biometric.BiometricPrompt;
2020-02-26 10:19:19 +00:00
import androidx.browser.customtabs.CustomTabsClient;
import androidx.browser.customtabs.CustomTabsIntent;
2020-02-26 10:19:19 +00:00
import androidx.browser.customtabs.CustomTabsServiceConnection;
2019-05-16 06:13:18 +00:00
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
2020-01-08 08:06:30 +00:00
import androidx.core.content.FileProvider;
2019-10-01 08:01:44 +00:00
import androidx.core.graphics.ColorUtils;
2019-07-10 15:58:26 +00:00
import androidx.fragment.app.FragmentActivity;
2019-12-29 16:10:25 +00:00
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
2019-12-29 16:10:25 +00:00
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.PreferenceManager;
2019-12-02 06:37:09 +00:00
import androidx.recyclerview.widget.RecyclerView;
2018-08-13 13:53:46 +00:00
import com.google.android.material.bottomnavigation.BottomNavigationView;
2020-01-25 09:49:59 +00:00
import org.jetbrains.annotations.NotNull;
2019-07-06 11:02:32 +00:00
import java.io.ByteArrayOutputStream;
2018-08-23 18:58:21 +00:00
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
2018-08-02 17:07:02 +00:00
import java.io.IOException;
2018-08-23 18:58:21 +00:00
import java.io.InputStream;
2019-11-30 11:50:39 +00:00
import java.io.OutputStream;
2020-01-05 08:27:34 +00:00
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
2018-08-25 11:27:54 +00:00
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
2018-12-24 10:47:21 +00:00
import java.text.DateFormat;
import java.text.DecimalFormat;
2018-12-24 10:47:21 +00:00
import java.text.SimpleDateFormat;
import java.util.ArrayList;
2020-01-25 09:49:59 +00:00
import java.util.Comparator;
2019-07-10 15:58:26 +00:00
import java.util.Date;
import java.util.List;
import java.util.Locale;
2019-02-26 10:05:21 +00:00
import java.util.Objects;
2019-10-10 11:26:44 +00:00
import java.util.concurrent.BlockingQueue;
2020-01-25 09:49:59 +00:00
import java.util.concurrent.ExecutionException;
2019-07-10 15:58:26 +00:00
import java.util.concurrent.ExecutorService;
2019-10-10 11:26:44 +00:00
import java.util.concurrent.LinkedBlockingQueue;
2020-01-25 09:49:59 +00:00
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.RunnableFuture;
2019-10-10 11:26:44 +00:00
import java.util.concurrent.SynchronousQueue;
2018-09-04 07:02:54 +00:00
import java.util.concurrent.ThreadFactory;
2019-10-10 11:26:44 +00:00
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
2020-01-25 09:49:59 +00:00
import java.util.concurrent.TimeoutException;
2019-06-07 06:07:40 +00:00
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
2020-03-05 13:45:29 +00:00
import java.util.regex.Pattern;
2018-09-04 07:02:54 +00:00
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
2018-12-23 13:34:42 +00:00
import static androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION;
2018-09-04 07:02:54 +00:00
2018-08-02 13:33:06 +00:00
public class Helper {
2019-02-15 07:51:14 +00:00
static final int NOTIFICATION_SYNCHRONIZE = 1;
static final int NOTIFICATION_SEND = 2;
static final int NOTIFICATION_EXTERNAL = 3;
static final int NOTIFICATION_UPDATE = 4;
2019-02-15 07:51:14 +00:00
2019-01-17 10:53:29 +00:00
static final float LOW_LIGHT = 0.6f;
2019-09-24 12:46:12 +00:00
static final int BUFFER_SIZE = 8192; // Same as in Files class
2019-01-17 10:53:29 +00:00
static final String PGP_BEGIN_MESSAGE = "-----BEGIN PGP MESSAGE-----";
static final String PGP_END_MESSAGE = "-----END PGP MESSAGE-----";
2019-07-12 14:57:56 +00:00
static final String FAQ_URI = "https://github.com/M66B/FairEmail/blob/master/FAQ.md";
2019-08-09 17:32:14 +00:00
static final String XDA_URI = "https://forum.xda-developers.com/showthread.php?t=3824168";
2020-01-05 08:27:34 +00:00
static final String SUPPORT_URI = "https://contact.faircode.eu/?product=fairemailsupport";
static final String TEST_URI = "https://play.google.com/apps/testing/" + BuildConfig.APPLICATION_ID;
2020-01-17 10:05:35 +00:00
static final String CROWDIN_URI = "https://crowdin.com/project/open-source-email";
static final String GRAVATAR_PRIVACY_URI = "https://meta.stackexchange.com/questions/44717/is-gravatar-a-privacy-risk";
2019-02-07 19:51:50 +00:00
2020-03-05 13:45:29 +00:00
static final Pattern EMAIL_ADDRESS
= Pattern.compile(
"[\\S]{1,256}" +
"\\@" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
"(" +
"\\." +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
")+"
);
2020-01-25 09:49:59 +00:00
static ExecutorService getBackgroundExecutor(int threads, final String name) {
2019-10-10 11:26:44 +00:00
ThreadFactory factory = new ThreadFactory() {
private final AtomicInteger threadId = new AtomicInteger();
@Override
public Thread newThread(@NonNull Runnable runnable) {
Thread thread = new Thread(runnable);
thread.setName("FairEmail_bg_" + name + "_" + threadId.getAndIncrement());
thread.setPriority(THREAD_PRIORITY_BACKGROUND);
return thread;
}
};
if (threads == 0)
return new ThreadPoolExecutorEx(
2020-01-30 16:05:15 +00:00
name,
2019-10-10 11:26:44 +00:00
0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
factory);
2020-01-25 09:49:59 +00:00
else if (threads == 1)
return new ThreadPoolExecutorEx(
2020-01-30 16:05:15 +00:00
name,
2020-01-25 09:49:59 +00:00
threads, threads,
0L, TimeUnit.MILLISECONDS,
new PriorityBlockingQueue<Runnable>(10, new PriorityComparator()),
factory) {
private final AtomicLong sequenceId = new AtomicLong();
2020-01-25 09:49:59 +00:00
@Override
protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
RunnableFuture<T> task = super.newTaskFor(runnable, value);
if (runnable instanceof PriorityRunnable)
return new PriorityFuture<T>(task,
((PriorityRunnable) runnable).getPriority(),
((PriorityRunnable) runnable).getOrder());
2020-01-25 09:49:59 +00:00
else
return new PriorityFuture<>(task, 0, sequenceId.getAndIncrement());
2020-01-25 09:49:59 +00:00
}
};
2019-10-10 11:26:44 +00:00
else
return new ThreadPoolExecutorEx(
2020-01-30 16:05:15 +00:00
name,
2019-10-10 11:26:44 +00:00
threads, threads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
factory);
}
private static class ThreadPoolExecutorEx extends ThreadPoolExecutor {
2020-01-30 16:05:15 +00:00
String name;
public ThreadPoolExecutorEx(
String name,
int corePoolSize, int maximumPoolSize,
long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory) {
2019-10-10 11:26:44 +00:00
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
2020-01-30 16:05:15 +00:00
this.name = name;
2019-10-10 11:26:44 +00:00
}
2019-06-07 06:07:40 +00:00
2018-09-04 07:02:54 +00:00
@Override
2019-10-10 11:26:44 +00:00
protected void beforeExecute(Thread t, Runnable r) {
2019-12-07 16:02:42 +00:00
Log.d("Executing " + t.getName());
2018-09-04 07:02:54 +00:00
}
2019-10-10 11:26:44 +00:00
}
2018-09-04 07:02:54 +00:00
2020-01-25 09:49:59 +00:00
private static class PriorityFuture<T> implements RunnableFuture<T> {
private int priority;
private long order;
2020-01-25 09:49:59 +00:00
private RunnableFuture<T> wrapped;
PriorityFuture(RunnableFuture<T> wrapped, int priority, long order) {
2020-01-25 09:49:59 +00:00
this.wrapped = wrapped;
this.priority = priority;
this.order = order;
2020-01-25 09:49:59 +00:00
}
public int getPriority() {
return this.priority;
2020-01-25 09:49:59 +00:00
}
public long getOrder() {
return this.order;
}
2020-01-25 09:49:59 +00:00
@Override
public void run() {
wrapped.run();
}
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
return wrapped.cancel(mayInterruptIfRunning);
}
@Override
public boolean isCancelled() {
return wrapped.isCancelled();
}
@Override
public boolean isDone() {
return wrapped.isDone();
}
@Override
public T get() throws ExecutionException, InterruptedException {
return wrapped.get();
}
@Override
public T get(long timeout, @NotNull TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException {
return wrapped.get(timeout, unit);
}
}
private static class PriorityComparator implements Comparator<Runnable> {
@Override
public int compare(Runnable r1, Runnable r2) {
if (r1 instanceof PriorityFuture<?> && r2 instanceof PriorityFuture<?>) {
Integer p1 = ((PriorityFuture<?>) r1).getPriority();
Integer p2 = ((PriorityFuture<?>) r2).getPriority();
int p = p1.compareTo(p2);
if (p == 0) {
Long o1 = ((PriorityFuture<?>) r1).getOrder();
Long o2 = ((PriorityFuture<?>) r2).getOrder();
return o1.compareTo(o2);
} else
return p;
2020-01-25 09:49:59 +00:00
} else
return 0;
}
}
static class PriorityRunnable implements Runnable {
private int priority;
private long order;
2020-01-25 09:49:59 +00:00
int getPriority() {
return this.priority;
}
long getOrder() {
return this.order;
2020-01-25 09:49:59 +00:00
}
PriorityRunnable(int priority, long order) {
2020-01-25 09:49:59 +00:00
this.priority = priority;
this.order = order;
2020-01-25 09:49:59 +00:00
}
@Override
public void run() {
Log.i("Run priority=" + priority);
}
}
2019-10-10 11:26:44 +00:00
private static final ExecutorService executor = getBackgroundExecutor(1, "helper");
2019-07-10 15:58:26 +00:00
2019-05-12 17:14:34 +00:00
// Features
2019-02-07 09:02:40 +00:00
static boolean hasPermission(Context context, String name) {
return (ContextCompat.checkSelfPermission(context, name) == PackageManager.PERMISSION_GRANTED);
}
2019-12-12 12:34:04 +00:00
static boolean hasPermissions(Context context, String[] permissions) {
for (String permission : permissions)
if (!hasPermission(context, permission))
return false;
return true;
}
static String[] getOAuthPermissions() {
List<String> permissions = new ArrayList<>();
permissions.add(Manifest.permission.READ_CONTACTS); // profile
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
permissions.add(Manifest.permission.GET_ACCOUNTS);
return permissions.toArray(new String[0]);
}
2019-05-12 17:14:34 +00:00
static boolean hasCustomTabs(Context context, Uri uri) {
PackageManager pm = context.getPackageManager();
Intent view = new Intent(Intent.ACTION_VIEW, uri);
for (ResolveInfo info : pm.queryIntentActivities(view, 0)) {
Intent intent = new Intent();
intent.setAction(ACTION_CUSTOM_TABS_CONNECTION);
intent.setPackage(info.activityInfo.packageName);
if (pm.resolveService(intent, 0) != null)
return true;
}
return false;
}
static boolean hasWebView(Context context) {
PackageManager pm = context.getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_WEBVIEW))
try {
new WebView(context);
return true;
} catch (Throwable ex) {
return false;
}
else
return false;
}
static boolean canPrint(Context context) {
PackageManager pm = context.getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_PRINTING);
}
2019-09-26 10:11:46 +00:00
static Boolean isIgnoringOptimizations(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm == null)
return null;
return pm.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID);
}
return null;
}
static boolean isPlayStoreInstall() {
return BuildConfig.PLAY_STORE_RELEASE;
}
static boolean isSecure(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean biometrics = prefs.getBoolean("biometrics", false);
String pin = prefs.getString("pin", null);
return (biometrics || !TextUtils.isEmpty(pin));
}
2019-05-12 17:14:34 +00:00
// View
static Intent getChooser(Context context, Intent intent) {
PackageManager pm = context.getPackageManager();
if (pm.queryIntentActivities(intent, 0).size() == 1)
return intent;
else
return Intent.createChooser(intent, context.getString(R.string.title_select_app));
}
2020-01-08 08:06:30 +00:00
static void share(Context context, File file, String type, String name) {
// https://developer.android.com/reference/android/support/v4/content/FileProvider
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file);
Log.i("uri=" + uri);
// Build intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndTypeAndNormalize(uri, type);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (!TextUtils.isEmpty(name))
intent.putExtra(Intent.EXTRA_TITLE, Helper.sanitizeFilename(name));
Log.i("Intent=" + intent + " type=" + type);
// Get targets
PackageManager pm = context.getPackageManager();
List<ResolveInfo> ris = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo ri : ris) {
Log.i("Target=" + ri);
context.grantUriPermission(ri.activityInfo.packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
// Check if viewer available
2020-01-08 10:44:28 +00:00
if (ris.size() == 0) {
String message = context.getString(R.string.title_no_viewer,
type != null ? type : name != null ? name : file.getName());
ToastEx.makeText(context, message, Toast.LENGTH_LONG).show();
} else
2020-01-08 08:06:30 +00:00
context.startActivity(intent);
}
2019-07-01 18:34:02 +00:00
static void view(Context context, Intent intent) {
Uri uri = intent.getData();
if ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme()))
2019-07-01 18:34:02 +00:00
view(context, intent.getData(), false);
else
context.startActivity(intent);
}
2019-07-01 18:34:02 +00:00
static void view(Context context, Uri uri, boolean browse) {
2018-12-24 12:27:45 +00:00
Log.i("View=" + uri);
2018-12-23 13:34:42 +00:00
2019-08-11 19:02:31 +00:00
if (browse || !hasCustomTabs(context, uri)) {
2018-12-23 13:34:42 +00:00
Intent view = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(getChooser(context, view));
2018-12-23 13:34:42 +00:00
} else {
// https://developer.chrome.com/multidevice/android/customtabs
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
2019-01-27 12:24:37 +00:00
builder.setToolbarColor(resolveColor(context, R.attr.colorPrimary));
2020-01-05 11:52:21 +00:00
builder.setSecondaryToolbarColor(resolveColor(context, R.attr.colorPrimaryDark));
builder.setColorScheme(Helper.isDarkTheme(context)
? CustomTabsIntent.COLOR_SCHEME_DARK : CustomTabsIntent.COLOR_SCHEME_LIGHT);
builder.addDefaultShareMenuItem();
2018-12-23 13:34:42 +00:00
CustomTabsIntent customTabsIntent = builder.build();
try {
customTabsIntent.launchUrl(context, uri);
} catch (ActivityNotFoundException ex) {
2019-02-19 16:14:07 +00:00
Log.w(ex);
2019-07-13 06:58:56 +00:00
ToastEx.makeText(context, context.getString(R.string.title_no_viewer, uri.toString()), Toast.LENGTH_LONG).show();
2018-12-23 13:34:42 +00:00
} catch (Throwable ex) {
2018-12-24 12:41:38 +00:00
Log.e(ex);
2019-12-06 07:50:46 +00:00
ToastEx.makeText(context, Log.formatThrowable(ex, false), Toast.LENGTH_LONG).show();
2018-12-23 13:34:42 +00:00
}
}
}
2020-02-26 10:19:19 +00:00
static void customTabsWarmup(Context context) {
try {
CustomTabsClient.bindCustomTabsService(context, "com.android.chrome", new CustomTabsServiceConnection() {
@Override
public void onCustomTabsServiceConnected(@NonNull ComponentName name, @NonNull CustomTabsClient client) {
2020-02-26 12:42:27 +00:00
Log.i("Warming up custom tabs");
2020-02-26 10:19:19 +00:00
try {
client.warmup(0);
} catch (Throwable ex) {
Log.w(ex);
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
// Do nothing
}
});
} catch (Throwable ex) {
Log.w(ex);
}
}
2019-08-15 05:58:20 +00:00
static void viewFAQ(Context context, int question) {
if (question == 0)
view(context, Uri.parse(FAQ_URI), false);
else
2019-12-10 10:51:03 +00:00
view(context, Uri.parse(FAQ_URI + "#user-content-faq" + question), false);
2019-01-14 10:29:47 +00:00
}
2018-10-29 15:47:27 +00:00
static Intent getIntentOpenKeychain() {
Intent intent = new Intent(Intent.ACTION_VIEW);
2019-11-06 09:29:25 +00:00
intent.setData(Uri.parse(BuildConfig.OPENKEYCHAIN_URI));
2018-10-29 15:47:27 +00:00
return intent;
}
2019-11-08 12:31:01 +00:00
static String getOpenKeychainPackage(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return prefs.getString("openpgp_provider", "org.sufficientlysecure.keychain");
}
static Intent getIntentIssue(Context context) {
2019-12-28 15:49:57 +00:00
if (ActivityBilling.isPro(context)) {
2019-06-08 19:50:17 +00:00
String version = BuildConfig.VERSION_NAME + "/" +
(Helper.hasValidFingerprint(context) ? "1" : "3") +
2019-07-22 15:19:18 +00:00
(BuildConfig.PLAY_STORE_RELEASE ? "p" : "") +
(BuildConfig.DEBUG ? "d" : "") +
2019-08-13 08:27:17 +00:00
(ActivityBilling.isPro(context) ? "+" : "");
2019-06-08 19:50:17 +00:00
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));
return intent;
} else
return new Intent(Intent.ACTION_VIEW, Uri.parse(XDA_URI));
2019-12-26 17:57:15 +00:00
}
static Intent getIntentRate(Context context) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + BuildConfig.APPLICATION_ID));
if (intent.resolveActivity(context.getPackageManager()) == null)
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID));
return intent;
}
2020-03-26 07:40:26 +00:00
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;
}
2019-05-12 17:14:34 +00:00
// Graphics
2018-12-30 14:35:19 +00:00
static int dp2pixels(Context context, int dp) {
2018-12-29 07:40:12 +00:00
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);
}
2018-12-30 14:35:19 +00:00
static float getTextSize(Context context, int zoom) {
2018-12-30 14:34:14 +00:00
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});
2018-12-30 16:17:22 +00:00
return ta.getDimension(0, 0);
2018-12-30 14:34:14 +00:00
} finally {
if (ta != null)
ta.recycle();
}
}
2018-08-02 13:33:06 +00:00
static int resolveColor(Context context, int attr) {
2018-08-06 15:07:46 +00:00
int[] attrs = new int[]{attr};
TypedArray a = context.getTheme().obtainStyledAttributes(attrs);
int color = a.getColor(0, 0xFF0000);
a.recycle();
return color;
2018-08-02 13:33:06 +00:00
}
static void setViewsEnabled(ViewGroup view, boolean enabled) {
for (int i = 0; i < view.getChildCount(); i++) {
View child = view.getChildAt(i);
if (child instanceof Spinner ||
child instanceof EditText ||
child instanceof CheckBox ||
child instanceof ImageView /* =ImageButton */ ||
2019-09-19 13:29:00 +00:00
child instanceof RadioButton ||
(child instanceof Button && "disable".equals(child.getTag())))
child.setEnabled(enabled);
2019-09-19 13:29:00 +00:00
else if (child instanceof BottomNavigationView) {
2018-08-13 13:53:46 +00:00
Menu menu = ((BottomNavigationView) child).getMenu();
menu.setGroupEnabled(0, enabled);
2019-12-02 06:37:09 +00:00
} else if (child instanceof RecyclerView)
; // do nothing
else if (child instanceof ViewGroup)
setViewsEnabled((ViewGroup) child, enabled);
}
}
2019-05-16 06:13:18 +00:00
static void hide(View view) {
view.setPadding(0, 0, 0, 0);
ViewGroup.LayoutParams lparam = view.getLayoutParams();
lparam.width = 0;
lparam.height = 0;
if (lparam instanceof ConstraintLayout.LayoutParams)
((ConstraintLayout.LayoutParams) lparam).setMargins(0, 0, 0, 0);
view.setLayoutParams(lparam);
}
2019-05-13 12:29:52 +00:00
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));
}
2019-10-01 08:01:44 +00:00
static int adjustLuminance(int color, boolean dark, float min) {
float lum = (float) ColorUtils.calculateLuminance(color);
if (dark ? lum < min : lum > 1 - min)
return ColorUtils.blendARGB(color,
dark ? Color.WHITE : Color.BLACK,
dark ? min - lum : lum - (1 - min));
return color;
}
2019-05-12 17:14:34 +00:00
// Formatting
2019-10-10 13:09:40 +00:00
private static final DecimalFormat df = new DecimalFormat("@@");
2019-05-12 17:14:34 +00:00
static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
2019-10-10 13:09:40 +00:00
return df.format(bytes / Math.pow(unit, exp)) + " " + pre + "B";
2019-05-12 17:14:34 +00:00
}
2019-11-22 10:36:49 +00:00
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);
}
2019-07-15 19:28:25 +00:00
// https://issuetracker.google.com/issues/37054851
static DateFormat getTimeInstance(Context context) {
return Helper.getTimeInstance(context, SimpleDateFormat.MEDIUM);
}
static DateFormat getDateInstance(Context context) {
return SimpleDateFormat.getDateInstance(SimpleDateFormat.MEDIUM);
}
2019-05-12 17:14:34 +00:00
static DateFormat getTimeInstance(Context context, int style) {
if (context != null &&
(style == SimpleDateFormat.SHORT || style == SimpleDateFormat.MEDIUM)) {
Locale locale = Locale.getDefault();
boolean is24Hour = android.text.format.DateFormat.is24HourFormat(context);
String skeleton = (is24Hour ? "Hm" : "hm");
if (style == SimpleDateFormat.MEDIUM)
skeleton += "s";
String pattern = android.text.format.DateFormat.getBestDateTimePattern(locale, skeleton);
return new SimpleDateFormat(pattern, locale);
} else
return SimpleDateFormat.getTimeInstance(style);
}
2019-07-15 19:28:25 +00:00
static DateFormat getDateTimeInstance(Context context) {
return Helper.getDateTimeInstance(context, SimpleDateFormat.MEDIUM, SimpleDateFormat.MEDIUM);
}
static DateFormat getDateTimeInstance(Context context, int dateStyle, int timeStyle) {
// TODO fix time format
return SimpleDateFormat.getDateTimeInstance(dateStyle, timeStyle);
}
2019-05-12 17:14:34 +00:00
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 String ellipsize(String text, int maxLen) {
if (text == null || text.length() < maxLen) {
return text;
}
return text.substring(0, maxLen) + "...";
}
2019-05-13 06:02:56 +00:00
static void clearComposingText(Spannable text) {
Object[] spans = text.getSpans(0, text.length(), Object.class);
for (Object span : spans)
if ((text.getSpanFlags(span) & Spanned.SPAN_COMPOSING) != 0)
text.removeSpan(span);
}
2019-07-19 06:27:44 +00:00
static String localizeFolderType(Context context, String type) {
int resid = context.getResources().getIdentifier(
2019-09-23 20:07:22 +00:00
"title_folder_" + type.toLowerCase(Locale.ROOT),
2019-07-19 06:27:44 +00:00
"string",
context.getPackageName());
return (resid > 0 ? context.getString(resid) : type);
}
2018-08-02 13:33:06 +00:00
static String localizeFolderName(Context context, String name) {
2020-02-15 14:53:11 +00:00
if (name != null && "INBOX".equals(name.toUpperCase(Locale.ROOT)))
2018-08-02 13:33:06 +00:00
return context.getString(R.string.title_folder_inbox);
else if ("OUTBOX".equals(name))
return context.getString(R.string.title_folder_outbox);
else
return name;
}
2019-09-26 10:11:46 +00:00
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));
}
});
}
}
2019-12-01 11:24:03 +00:00
static String[] getStrings(Context context, int resid, Object... formatArgs) {
List<String> result = new ArrayList<>();
Configuration configuration = new Configuration(context.getResources().getConfiguration());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
result.add(context.getString(resid, formatArgs));
if (!Locale.getDefault().getLanguage().equals("en")) {
configuration.setLocale(new Locale("en"));
Resources res = context.createConfigurationContext(configuration).getResources();
result.add(res.getString(resid, formatArgs));
}
} else {
LocaleList ll = context.getResources().getConfiguration().getLocales();
for (int i = 0; i < ll.size(); i++) {
configuration.setLocale(ll.get(i));
Resources res = context.createConfigurationContext(configuration).getResources();
result.add(res.getString(resid, formatArgs));
}
}
return result.toArray(new String[0]);
}
static boolean containsWhiteSpace(String text) {
return text.matches(".*\\s+.*");
}
static boolean containsControlChars(String text) {
for (int offset = 0; offset < text.length(); ) {
int 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 isUTF8(String text) {
// Get extended ASCII characters
byte[] octets = text.getBytes(StandardCharsets.ISO_8859_1);
for (int i = 0; i < octets.length; i++) {
int bytes;
if ((octets[i] & 0b10000000) == 0b00000000)
bytes = 1;
else if ((octets[i] & 0b11100000) == 0b11000000)
bytes = 2;
else if ((octets[i] & 0b11110000) == 0b11100000)
bytes = 3;
else if ((octets[i] & 0b11111000) == 0b11110000)
bytes = 4;
else if ((octets[i] & 0b11111100) == 0b11111000)
bytes = 5;
else if ((octets[i] & 0b11111110) == 0b11111100)
bytes = 6;
else
return false;
if (i + bytes > octets.length)
return false;
while (--bytes > 0)
if ((octets[++i] & 0b11000000) != 0b10000000)
return false;
}
return true;
}
2019-05-12 17:14:34 +00:00
// Files
2018-12-24 10:47:21 +00:00
2019-05-12 17:14:34 +00:00
static String sanitizeFilename(String name) {
2019-06-23 12:27:14 +00:00
if (name == null)
return null;
2019-07-30 07:03:54 +00:00
2019-07-30 12:46:02 +00:00
return name.replaceAll("[?:\"*|/\\\\<>]", "_");
2018-08-03 19:12:19 +00:00
}
2019-05-12 17:14:34 +00:00
static String getExtension(String filename) {
if (filename == null)
return null;
int index = filename.lastIndexOf(".");
if (index < 0)
return null;
return filename.substring(index + 1);
}
2018-08-23 14:36:19 +00:00
2019-11-20 11:40:56 +00:00
static String guessMimeType(String filename) {
String type = null;
String extension = Helper.getExtension(filename);
if (extension != null)
type = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(extension.toLowerCase(Locale.ROOT));
if (TextUtils.isEmpty(type))
type = "application/octet-stream";
return type;
}
2019-01-21 16:45:05 +00:00
static void writeText(File file, String content) throws IOException {
try (FileOutputStream out = new FileOutputStream(file)) {
if (content != null)
out.write(content.getBytes());
2019-01-21 16:45:05 +00:00
}
}
2019-07-06 11:02:32 +00:00
static String readStream(InputStream is, String charset) throws IOException {
2019-08-09 18:16:36 +00:00
ByteArrayOutputStream os = new ByteArrayOutputStream(Math.max(BUFFER_SIZE, is.available()));
byte[] buffer = new byte[BUFFER_SIZE];
2019-07-06 11:02:32 +00:00
for (int len = is.read(buffer); len != -1; len = is.read(buffer))
os.write(buffer, 0, len);
return new String(os.toByteArray(), charset);
}
2019-01-21 16:45:05 +00:00
static String readText(File file) throws IOException {
try (FileInputStream in = new FileInputStream(file)) {
return readStream(in, StandardCharsets.UTF_8.name());
2019-01-21 16:45:05 +00:00
}
}
2018-08-23 18:58:21 +00:00
static void copy(File src, File dst) throws IOException {
try (InputStream in = new FileInputStream(src)) {
try (FileOutputStream out = new FileOutputStream(dst)) {
2019-11-30 11:50:39 +00:00
copy(in, out);
2018-08-23 18:58:21 +00:00
}
}
}
2018-08-25 11:27:54 +00:00
2019-11-30 11:50:39 +00:00
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);
}
2020-02-23 18:45:05 +00:00
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;
}
2019-12-24 09:39:29 +00:00
static long getAvailableStorageSpace() {
2019-10-19 19:53:19 +00:00
StatFs stats = new StatFs(Environment.getDataDirectory().getAbsolutePath());
return stats.getAvailableBlocksLong() * stats.getBlockSizeLong();
}
2019-12-24 09:53:42 +00:00
static long getTotalStorageSpace() {
StatFs stats = new StatFs(Environment.getDataDirectory().getAbsolutePath());
return stats.getTotalBytes();
}
2019-12-01 17:19:17 +00:00
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);
2020-03-19 10:00:12 +00:00
File initial = Environment.getExternalStorageDirectory();
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, Uri.fromFile(initial));
2019-12-01 17:19:17 +00:00
}
2019-05-12 17:14:34 +00:00
// Cryptography
2018-08-25 11:27:54 +00:00
static String sha256(String data) throws NoSuchAlgorithmException {
return sha256(data.getBytes());
}
static String sha1(byte[] data) throws NoSuchAlgorithmException {
return sha("SHA-1", data);
}
2018-08-25 11:27:54 +00:00
static String sha256(byte[] data) throws NoSuchAlgorithmException {
return sha("SHA-256", data);
}
2020-01-17 15:59:29 +00:00
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) {
2018-08-25 11:27:54 +00:00
StringBuilder sb = new StringBuilder();
for (byte b : bytes)
sb.append(String.format("%02x", b));
return sb.toString();
}
2018-08-25 13:32:52 +00:00
2019-07-11 05:52:06 +00:00
static String getFingerprint(Context context) {
2018-09-27 06:44:02 +00:00
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("SHA1");
byte[] bytes = digest.digest(cert);
StringBuilder sb = new StringBuilder();
for (byte b : bytes)
2020-02-15 14:53:11 +00:00
sb.append(Integer.toString(b & 0xff, 16).toUpperCase(Locale.ROOT));
2018-09-27 06:44:02 +00:00
return sb.toString();
} catch (Throwable ex) {
2018-12-24 12:27:45 +00:00
Log.e(ex);
2018-09-27 06:44:02 +00:00
return null;
}
}
2019-07-11 05:52:06 +00:00
static boolean hasValidFingerprint(Context context) {
2018-09-27 06:44:02 +00:00
String signed = getFingerprint(context);
String expected = context.getString(R.string.fingerprint);
2019-02-26 10:05:21 +00:00
return Objects.equals(signed, expected);
2018-09-27 06:44:02 +00:00
}
2019-07-12 17:33:40 +00:00
static boolean canAuthenticate(Context context) {
2019-10-20 10:17:04 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String pin = prefs.getString("pin", null);
if (!TextUtils.isEmpty(pin))
return true;
2019-11-09 10:18:55 +00:00
BiometricManager bm = BiometricManager.from(context);
return (bm.canAuthenticate() == BiometricManager.BIOMETRIC_SUCCESS);
2019-07-12 17:33:40 +00:00
}
2019-07-10 15:58:26 +00:00
static boolean shouldAuthenticate(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean biometrics = prefs.getBoolean("biometrics", false);
2019-10-20 10:17:04 +00:00
String pin = prefs.getString("pin", null);
2019-07-10 15:58:26 +00:00
2019-10-20 10:17:04 +00:00
if (biometrics || !TextUtils.isEmpty(pin)) {
2019-07-10 17:58:02 +00:00
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));
2019-07-10 17:58:02 +00:00
if (last_authentication + biometrics_timeout < now)
2019-07-10 17:58:02 +00:00
return true;
2019-07-10 15:58:26 +00:00
2019-07-10 17:58:02 +00:00
prefs.edit().putLong("last_authentication", now).apply();
}
2019-07-10 15:58:26 +00:00
2019-07-10 17:58:02 +00:00
return false;
2019-07-10 15:58:26 +00:00
}
static void authenticate(final FragmentActivity activity,
Boolean enabled, final
Runnable authenticated, final Runnable cancelled) {
final Handler handler = new Handler();
2019-10-20 10:17:04 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
String pin = prefs.getString("pin", null);
2019-10-20 10:17:04 +00:00
if (enabled != null || TextUtils.isEmpty(pin)) {
BiometricPrompt.PromptInfo.Builder info = new BiometricPrompt.PromptInfo.Builder()
.setTitle(activity.getString(enabled == null ? R.string.app_name : R.string.title_setup_biometrics));
2019-10-20 10:17:04 +00:00
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));
2019-12-16 11:58:30 +00:00
info.setConfirmationRequired(false);
2019-10-20 10:17:04 +00:00
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));
BiometricPrompt prompt = new BiometricPrompt(activity, executor,
new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(final int errorCode, @NonNull final CharSequence errString) {
Log.w("Biometric error " + errorCode + ": " + errString);
if (errorCode != BiometricPrompt.ERROR_NEGATIVE_BUTTON &&
errorCode != BiometricPrompt.ERROR_CANCELED &&
errorCode != BiometricPrompt.ERROR_USER_CANCELED)
2019-10-21 07:13:40 +00:00
handler.post(new Runnable() {
@Override
public void run() {
ToastEx.makeText(activity,
"Error " + errorCode + ": " + errString,
Toast.LENGTH_LONG).show();
}
});
2019-07-10 15:58:26 +00:00
2019-10-20 10:17:04 +00:00
handler.post(cancelled);
}
@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
Log.i("Biometric succeeded");
setAuthenticated(activity);
handler.post(authenticated);
}
@Override
public void onAuthenticationFailed() {
Log.w("Biometric failed");
handler.post(cancelled);
}
});
prompt.authenticate(info.build());
} else {
final View dview = LayoutInflater.from(activity).inflate(R.layout.dialog_pin_ask, null);
final EditText etPin = dview.findViewById(R.id.etPin);
2019-11-01 09:50:32 +00:00
final AlertDialog dialog = new AlertDialog.Builder(activity)
2019-10-20 10:17:04 +00:00
.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();
if (pin.equals(entered)) {
setAuthenticated(activity);
handler.post(authenticated);
} else
handler.post(cancelled);
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
handler.post(cancelled);
}
})
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
handler.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;
}
});
2019-11-01 09:50:32 +00:00
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);
}
});
new Handler().post(new Runnable() {
@Override
public void run() {
etPin.requestFocus();
}
});
2019-10-20 10:17:04 +00:00
dialog.show();
}
}
2019-11-06 09:40:50 +00:00
static void setAuthenticated(Context context) {
2019-10-20 10:17:04 +00:00
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().putLong("last_authentication", new Date().getTime()).apply();
2019-07-10 15:58:26 +00:00
}
2019-07-11 06:13:49 +00:00
static void clearAuthentication(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.edit().remove("last_authentication").apply();
}
2020-01-29 19:02:45 +00:00
static void selectKeyAlias(final Activity activity, final LifecycleOwner owner, final String alias, final IKeyAlias intf) {
2019-12-05 14:18:53 +00:00
final Context context = activity.getApplicationContext();
final Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
2020-01-29 19:02:45 +00:00
if (alias != null)
2019-12-05 14:18:53 +00:00
try {
2020-01-29 19:02:45 +00:00
if (KeyChain.getPrivateKey(context, alias) != null) {
Log.i("Private key available alias=" + alias);
2019-12-05 14:18:53 +00:00
handler.post(new Runnable() {
@Override
public void run() {
2019-12-29 16:10:25 +00:00
if (owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED))
2020-01-29 19:02:45 +00:00
intf.onSelected(alias);
2019-12-05 14:18:53 +00:00
}
});
return;
}
} catch (KeyChainException ex) {
Log.w(ex);
} catch (Throwable ex) {
Log.e(ex);
}
handler.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);
handler.post(new Runnable() {
@Override
public void run() {
if (owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
2019-12-29 16:10:25 +00:00
if (alias == null)
intf.onNothingSelected();
else
intf.onSelected(alias);
} else {
owner.getLifecycle().addObserver(new LifecycleObserver() {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onStart() {
owner.getLifecycle().removeObserver(this);
if (alias == null)
intf.onNothingSelected();
else
intf.onSelected(alias);
}
});
}
2019-12-05 14:18:53 +00:00
}
});
}
},
2020-01-29 19:02:45 +00:00
null, null, null, -1, alias);
2019-12-05 14:18:53 +00:00
}
});
}
}).start();
}
interface IKeyAlias {
void onSelected(String alias);
void onNothingSelected();
}
2020-01-23 11:38:31 +00:00
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();
}
2019-05-12 17:14:34 +00:00
// Miscellaneous
2019-07-11 05:52:06 +00:00
static <T> List<List<T>> chunkList(List<T> list, int size) {
2019-05-22 11:17:42 +00:00
List<List<T>> 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<Long> list) {
long[] result = new long[list.size()];
for (int i = 0; i < list.size(); i++)
result[i] = list.get(i);
return result;
}
static List<Long> fromLongArray(long[] array) {
List<Long> result = new ArrayList<>();
for (int i = 0; i < array.length; i++)
result.add(array[i]);
return result;
}
2018-11-25 12:34:08 +00:00
static boolean equal(String[] a1, String[] a2) {
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;
}
2018-11-26 11:42:06 +00:00
2019-04-11 07:47:49 +00:00
static int getSize(Bundle bundle) {
Parcel p = Parcel.obtain();
bundle.writeToParcel(p, 0);
return p.dataSize();
}
2018-08-02 13:33:06 +00:00
}