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
|
|
|
|
2021-01-01 07:56:36 +00:00
|
|
|
Copyright 2018-2021 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;
|
2021-02-02 16:43:46 +00:00
|
|
|
import android.app.ActivityManager;
|
2019-10-11 10:32:46 +00:00
|
|
|
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;
|
2020-07-19 16:06:57 +00:00
|
|
|
import android.content.ContentResolver;
|
2018-08-02 13:33:06 +00:00
|
|
|
import android.content.Context;
|
2018-12-01 12:17:33 +00:00
|
|
|
import android.content.DialogInterface;
|
2018-09-19 11:19:16 +00:00
|
|
|
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;
|
2020-10-31 09:27:42 +00:00
|
|
|
import android.os.BatteryManager;
|
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-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-07-19 16:06:57 +00:00
|
|
|
import android.provider.Settings;
|
2019-12-05 14:18:53 +00:00
|
|
|
import android.security.KeyChain;
|
|
|
|
import android.security.KeyChainAliasCallback;
|
|
|
|
import android.security.KeyChainException;
|
2021-04-04 12:53:25 +00:00
|
|
|
import android.text.Layout;
|
|
|
|
import android.text.Spannable;
|
2019-10-20 10:17:04 +00:00
|
|
|
import android.text.TextUtils;
|
2019-04-19 11:08:30 +00:00
|
|
|
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;
|
2021-04-04 12:53:25 +00:00
|
|
|
import android.view.MotionEvent;
|
2018-08-10 11:05:38 +00:00
|
|
|
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;
|
2019-01-29 09:02:34 +00:00
|
|
|
import android.widget.Button;
|
2018-08-10 11:05:38 +00:00
|
|
|
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;
|
2018-08-10 11:05:38 +00:00
|
|
|
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
|
|
|
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2019-07-01 12:06:15 +00:00
|
|
|
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;
|
2021-01-17 03:05:29 +00:00
|
|
|
import androidx.browser.customtabs.CustomTabColorSchemeParams;
|
2020-02-26 10:19:19 +00:00
|
|
|
import androidx.browser.customtabs.CustomTabsClient;
|
2019-04-17 18:21:44 +00:00
|
|
|
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;
|
2019-04-17 18:21:44 +00:00
|
|
|
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;
|
2020-01-11 07:39:38 +00:00
|
|
|
import androidx.lifecycle.LifecycleObserver;
|
2019-12-29 16:10:25 +00:00
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
2020-01-11 07:39:38 +00:00
|
|
|
import androidx.lifecycle.OnLifecycleEvent;
|
2019-04-17 18:21:44 +00:00
|
|
|
import androidx.preference.PreferenceManager;
|
2019-12-02 06:37:09 +00:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2019-04-17 18:21:44 +00:00
|
|
|
|
2018-08-13 13:53:46 +00:00
|
|
|
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
|
|
|
|
2020-06-19 18:44:19 +00:00
|
|
|
import org.openintents.openpgp.util.OpenPgpApi;
|
2020-01-25 09:49:59 +00:00
|
|
|
|
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;
|
2021-01-20 19:00:44 +00:00
|
|
|
import java.nio.charset.Charset;
|
2019-07-26 06:39:05 +00:00
|
|
|
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;
|
2018-09-16 10:44:13 +00:00
|
|
|
import java.text.DecimalFormat;
|
2018-12-24 10:47:21 +00:00
|
|
|
import java.text.SimpleDateFormat;
|
2018-10-17 07:51:29 +00:00
|
|
|
import java.util.ArrayList;
|
2020-07-20 12:50:10 +00:00
|
|
|
import java.util.Arrays;
|
2020-08-03 17:48:02 +00:00
|
|
|
import java.util.Collections;
|
2020-01-25 09:49:59 +00:00
|
|
|
import java.util.Comparator;
|
2019-07-10 15:58:26 +00:00
|
|
|
import java.util.Date;
|
2018-10-17 07:51:29 +00:00
|
|
|
import java.util.List;
|
2019-04-19 11:08:30 +00:00
|
|
|
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;
|
2020-03-08 11:32:52 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicLong;
|
2020-03-05 13:45:29 +00:00
|
|
|
import java.util.regex.Pattern;
|
2018-08-15 07:40:18 +00:00
|
|
|
|
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;
|
2019-02-27 13:03:17 +00:00
|
|
|
static final int NOTIFICATION_SEND = 2;
|
|
|
|
static final int NOTIFICATION_EXTERNAL = 3;
|
2019-06-22 14:34:46 +00:00
|
|
|
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
|
|
|
|
2019-07-26 06:39:05 +00:00
|
|
|
static final int BUFFER_SIZE = 8192; // Same as in Files class
|
2021-01-25 09:05:22 +00:00
|
|
|
static final long MIN_REQUIRED_SPACE = 250 * 1024L * 1024L;
|
2019-01-17 10:53:29 +00:00
|
|
|
|
2019-09-25 08:48:25 +00:00
|
|
|
static final String PGP_BEGIN_MESSAGE = "-----BEGIN PGP MESSAGE-----";
|
|
|
|
static final String PGP_END_MESSAGE = "-----END PGP MESSAGE-----";
|
|
|
|
|
2021-04-05 11:14:25 +00:00
|
|
|
static final String FAQ_URI = "https://email.faircode.eu/faq/";
|
2021-02-12 14:57:49 +00:00
|
|
|
static final String XDA_URI = "https://forum.xda-developers.com/showthread.php?t=3824168";
|
2021-04-06 06:59:35 +00:00
|
|
|
static final String SUPPORT_URI = "https://contact.faircode.eu/?product=fairemailsupport&version=" + BuildConfig.VERSION_NAME;
|
2019-09-29 15:51:03 +00:00
|
|
|
static final String TEST_URI = "https://play.google.com/apps/testing/" + BuildConfig.APPLICATION_ID;
|
2020-01-23 16:02:52 +00:00
|
|
|
static final String GRAVATAR_PRIVACY_URI = "https://meta.stackexchange.com/questions/44717/is-gravatar-a-privacy-risk";
|
2020-08-18 15:25:19 +00:00
|
|
|
static final String LICENSE_URI = "https://www.gnu.org/licenses/gpl-3.0.html";
|
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-08-03 17:48:02 +00:00
|
|
|
// https://developer.android.com/guide/topics/media/media-formats#image-formats
|
|
|
|
static final List<String> IMAGE_TYPES = Collections.unmodifiableList(Arrays.asList(
|
|
|
|
"image/bmp",
|
|
|
|
"image/gif",
|
|
|
|
"image/jpeg",
|
|
|
|
"image/jpg",
|
|
|
|
"image/png",
|
|
|
|
"image/webp"
|
|
|
|
));
|
|
|
|
|
|
|
|
static final List<String> IMAGE_TYPES8 = Collections.unmodifiableList(Arrays.asList(
|
|
|
|
"image/heic",
|
|
|
|
"image/heif"
|
|
|
|
));
|
|
|
|
|
2020-04-23 15:56:02 +00:00
|
|
|
private static final ExecutorService executor = getBackgroundExecutor(1, "helper");
|
|
|
|
|
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) {
|
2020-03-08 11:32:52 +00:00
|
|
|
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)
|
2020-02-01 08:46:21 +00:00
|
|
|
return new PriorityFuture<T>(task,
|
|
|
|
((PriorityRunnable) runnable).getPriority(),
|
|
|
|
((PriorityRunnable) runnable).getOrder());
|
2020-01-25 09:49:59 +00:00
|
|
|
else
|
2020-03-08 11:32:52 +00:00
|
|
|
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-04-05 12:10:19 +00:00
|
|
|
private String name;
|
2020-01-30 16:05:15 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2020-04-05 12:10:19 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void afterExecute(Runnable r, Throwable t) {
|
|
|
|
Log.d("Executed " + name + " pending=" + getQueue().size());
|
|
|
|
}
|
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;
|
2020-02-01 08:46:21 +00:00
|
|
|
private long order;
|
2020-01-25 09:49:59 +00:00
|
|
|
private RunnableFuture<T> wrapped;
|
|
|
|
|
2020-02-01 08:46:21 +00:00
|
|
|
PriorityFuture(RunnableFuture<T> wrapped, int priority, long order) {
|
2020-01-25 09:49:59 +00:00
|
|
|
this.wrapped = wrapped;
|
2020-02-01 08:46:21 +00:00
|
|
|
this.priority = priority;
|
|
|
|
this.order = order;
|
2020-01-25 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getPriority() {
|
2020-02-01 08:46:21 +00:00
|
|
|
return this.priority;
|
2020-01-25 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 08:46:21 +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
|
2020-09-10 09:14:51 +00:00
|
|
|
public T get(long timeout, @NonNull TimeUnit unit) throws ExecutionException, InterruptedException, TimeoutException {
|
2020-01-25 09:49:59 +00:00
|
|
|
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();
|
2020-02-01 08:46:21 +00:00
|
|
|
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;
|
2020-02-01 08:46:21 +00:00
|
|
|
private long order;
|
2020-01-25 09:49:59 +00:00
|
|
|
|
|
|
|
int getPriority() {
|
2020-02-01 08:46:21 +00:00
|
|
|
return this.priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
long getOrder() {
|
|
|
|
return this.order;
|
2020-01-25 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 08:46:21 +00:00
|
|
|
PriorityRunnable(int priority, long order) {
|
2020-01-25 09:49:59 +00:00
|
|
|
this.priority = priority;
|
2020-02-01 08:46:21 +00:00
|
|
|
this.order = order;
|
2020-01-25 09:49:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Log.i("Run priority=" + priority);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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<>();
|
2020-08-08 10:36:32 +00:00
|
|
|
//permissions.add(Manifest.permission.READ_CONTACTS); // profile
|
2019-12-12 12:34:04 +00:00
|
|
|
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) {
|
2020-06-14 16:34:13 +00:00
|
|
|
String scheme = (uri == null ? null : uri.getScheme());
|
|
|
|
if (!"http".equals(scheme) && !"https".equals(scheme))
|
|
|
|
return false;
|
|
|
|
|
2019-05-12 17:14:34 +00:00
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
Intent view = new Intent(Intent.ACTION_VIEW, uri);
|
|
|
|
|
2020-06-14 16:34:13 +00:00
|
|
|
List<ResolveInfo> ris = pm.queryIntentActivities(view, 0); // action whitelisted
|
2020-06-13 14:32:18 +00:00
|
|
|
for (ResolveInfo info : ris) {
|
2019-05-12 17:14:34 +00:00
|
|
|
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) {
|
2020-07-10 07:12:24 +00:00
|
|
|
try {
|
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
if (pm.hasSystemFeature(PackageManager.FEATURE_WEBVIEW)) {
|
2019-05-12 17:14:34 +00:00
|
|
|
new WebView(context);
|
|
|
|
return true;
|
2020-07-10 07:12:24 +00:00
|
|
|
} else
|
2019-05-12 17:14:34 +00:00
|
|
|
return false;
|
2020-07-10 07:12:24 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
/*
|
|
|
|
Caused by: java.lang.RuntimeException: Package manager has died
|
|
|
|
at android.app.ApplicationPackageManager.hasSystemFeature(ApplicationPackageManager.java:414)
|
|
|
|
at eu.faircode.email.Helper.hasWebView(SourceFile:375)
|
|
|
|
at eu.faircode.email.ApplicationEx.onCreate(SourceFile:110)
|
|
|
|
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1014)
|
|
|
|
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4751)
|
|
|
|
*/
|
2019-05-12 17:14:34 +00:00
|
|
|
return false;
|
2020-07-10 07:12:24 +00:00
|
|
|
}
|
2019-05-12 17:14:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-11-04 14:15:08 +00:00
|
|
|
static Integer getBatteryLevel(Context context) {
|
|
|
|
try {
|
|
|
|
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
|
2020-11-04 19:46:20 +00:00
|
|
|
if (bm == null)
|
|
|
|
return null;
|
2020-11-04 14:15:08 +00:00
|
|
|
return bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 09:27:42 +00:00
|
|
|
static boolean isCharging(Context context) {
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
|
|
|
|
return false;
|
|
|
|
try {
|
|
|
|
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);
|
2020-11-04 19:46:20 +00:00
|
|
|
if (bm == null)
|
|
|
|
return false;
|
2020-10-31 09:27:42 +00:00
|
|
|
return bm.isCharging();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 10:11:46 +00:00
|
|
|
static boolean isPlayStoreInstall() {
|
|
|
|
return BuildConfig.PLAY_STORE_RELEASE;
|
|
|
|
}
|
|
|
|
|
2020-02-26 11:13:34 +00:00
|
|
|
static boolean isSecure(Context context) {
|
2020-07-19 16:06:57 +00:00
|
|
|
try {
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
|
|
|
ContentResolver resolver = context.getContentResolver();
|
2021-01-17 03:05:29 +00:00
|
|
|
int enabled = Settings.System.getInt(resolver, Settings.Secure.LOCK_PATTERN_ENABLED, 0);
|
2020-07-19 16:06:57 +00:00
|
|
|
return (enabled != 0);
|
|
|
|
} else {
|
|
|
|
KeyguardManager kgm = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
|
|
|
|
return (kgm != null && kgm.isDeviceSecure());
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-26 11:13:34 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 18:44:19 +00:00
|
|
|
static boolean isOpenKeychainInstalled(Context context) {
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
String provider = prefs.getString("openpgp_provider", "org.sufficientlysecure.keychain");
|
|
|
|
|
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
Intent intent = new Intent(OpenPgpApi.SERVICE_INTENT_2);
|
|
|
|
intent.setPackage(provider);
|
|
|
|
List<ResolveInfo> ris = pm.queryIntentServices(intent, 0);
|
|
|
|
|
2020-09-07 13:58:16 +00:00
|
|
|
return (ris != null && ris.size() > 0);
|
2020-06-19 18:44:19 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 12:46:14 +00:00
|
|
|
static boolean isComponentEnabled(Context context, Class<?> clazz) {
|
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
int state = pm.getComponentEnabledSetting(new ComponentName(context, clazz));
|
2021-01-07 07:05:47 +00:00
|
|
|
return (state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
|
2020-12-11 12:46:14 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 14:03:14 +00:00
|
|
|
static void enableComponent(Context context, Class<?> clazz, boolean whether) {
|
|
|
|
enableComponent(context, clazz.getName(), whether);
|
2020-09-10 13:57:33 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 14:03:14 +00:00
|
|
|
static void enableComponent(Context context, String name, boolean whether) {
|
2020-09-10 13:57:33 +00:00
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
pm.setComponentEnabledSetting(
|
|
|
|
new ComponentName(context, name),
|
2020-09-10 14:03:14 +00:00
|
|
|
whether
|
2021-01-07 07:05:47 +00:00
|
|
|
? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
2020-09-10 13:57:33 +00:00
|
|
|
: PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
|
|
|
PackageManager.DONT_KILL_APP);
|
|
|
|
}
|
|
|
|
|
2021-01-17 08:56:37 +00:00
|
|
|
static void setKeyboardIncognitoMode(EditText view, Context context) {
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean incognito_keyboard = prefs.getBoolean("incognito_keyboard", false);
|
|
|
|
if (incognito_keyboard)
|
|
|
|
try {
|
|
|
|
view.setImeOptions(view.getImeOptions() | EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 17:14:34 +00:00
|
|
|
// View
|
|
|
|
|
|
|
|
static Intent getChooser(Context context, Intent intent) {
|
2020-06-14 16:34:13 +00:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
|
|
|
|
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));
|
|
|
|
} else
|
2019-05-12 17:14:34 +00:00
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
2020-01-08 08:06:30 +00:00
|
|
|
static void share(Context context, File file, String type, String name) {
|
2020-10-29 07:32:23 +00:00
|
|
|
try {
|
|
|
|
_share(context, file, type, name);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
// java.lang.IllegalArgumentException: Failed to resolve canonical path for ...
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _share(Context context, File file, String type, String name) {
|
2020-01-08 08:06:30 +00:00
|
|
|
// 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);
|
2021-03-06 18:42:23 +00:00
|
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
|
|
|
|
|
if (!("message/rfc822".equals(type) ||
|
|
|
|
"message/delivery-status".equals(type) ||
|
|
|
|
"message/disposition-notification".equals(type) ||
|
|
|
|
"text/rfc822-headers".equals(type)))
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
2021-02-12 10:40:25 +00:00
|
|
|
|
2020-01-08 08:06:30 +00:00
|
|
|
if (!TextUtils.isEmpty(name))
|
|
|
|
intent.putExtra(Intent.EXTRA_TITLE, Helper.sanitizeFilename(name));
|
|
|
|
Log.i("Intent=" + intent + " type=" + type);
|
|
|
|
|
2020-06-14 16:34:13 +00:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
|
|
|
|
// Get targets
|
2020-08-30 12:55:26 +00:00
|
|
|
List<ResolveInfo> ris = null;
|
|
|
|
try {
|
|
|
|
PackageManager pm = context.getPackageManager();
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
/*
|
|
|
|
java.lang.RuntimeException: Package manager has died
|
|
|
|
at android.app.ApplicationPackageManager.queryIntentActivitiesAsUser(ApplicationPackageManager.java:571)
|
|
|
|
at android.app.ApplicationPackageManager.queryIntentActivities(ApplicationPackageManager.java:557)
|
|
|
|
at eu.faircode.email.Helper.share(SourceFile:489)
|
|
|
|
*/
|
2020-06-14 16:34:13 +00:00
|
|
|
}
|
2020-01-08 08:06:30 +00:00
|
|
|
|
2020-06-14 16:34:13 +00:00
|
|
|
// Check if viewer available
|
2021-01-30 10:13:29 +00:00
|
|
|
if (ris == null || ris.size() == 0)
|
2020-10-18 07:33:25 +00:00
|
|
|
if (isTnef(type, null))
|
2020-06-14 16:34:13 +00:00
|
|
|
viewFAQ(context, 155);
|
2021-01-30 10:13:29 +00:00
|
|
|
else
|
|
|
|
reportNoViewer(context, intent);
|
|
|
|
else
|
2020-06-14 16:34:13 +00:00
|
|
|
context.startActivity(intent);
|
2020-10-17 09:44:47 +00:00
|
|
|
} else
|
|
|
|
context.startActivity(intent);
|
2020-01-08 08:06:30 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 07:33:25 +00:00
|
|
|
static boolean isTnef(String type, String name) {
|
2020-10-04 10:17:37 +00:00
|
|
|
// https://en.wikipedia.org/wiki/Transport_Neutral_Encapsulation_Format
|
2020-10-18 07:33:25 +00:00
|
|
|
if ("application/ms-tnef".equals(type) ||
|
|
|
|
"application/vnd.ms-tnef".equals(type))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if ("application/octet-stream".equals(type) &&
|
|
|
|
"winmail.dat".equals(name))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2020-10-04 10:17:37 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 18:34:02 +00:00
|
|
|
static void view(Context context, Intent intent) {
|
2018-09-19 11:19:16 +00:00
|
|
|
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);
|
2018-09-19 11:19:16 +00:00
|
|
|
else
|
2020-06-14 16:34:13 +00:00
|
|
|
try {
|
|
|
|
context.startActivity(intent);
|
|
|
|
} catch (ActivityNotFoundException ex) {
|
|
|
|
Log.w(ex);
|
2021-01-30 10:13:29 +00:00
|
|
|
reportNoViewer(context, intent);
|
2020-06-14 16:34:13 +00:00
|
|
|
}
|
2018-09-19 11:19:16 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 18:34:02 +00:00
|
|
|
static void view(Context context, Uri uri, boolean browse) {
|
2020-06-18 11:53:32 +00:00
|
|
|
view(context, uri, browse, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void view(Context context, Uri uri, boolean browse, boolean task) {
|
2020-10-30 10:59:28 +00:00
|
|
|
if (context == null) {
|
|
|
|
Log.e(new Throwable("view"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:38:22 +00:00
|
|
|
boolean has = hasCustomTabs(context, uri);
|
|
|
|
Log.i("View=" + uri + " browse=" + browse + " task=" + task + " has=" + has);
|
2018-12-23 13:34:42 +00:00
|
|
|
|
2020-07-21 20:38:22 +00:00
|
|
|
if (browse || !has) {
|
2020-04-23 06:20:37 +00:00
|
|
|
try {
|
|
|
|
Intent view = new Intent(Intent.ACTION_VIEW, uri);
|
2020-06-18 11:53:32 +00:00
|
|
|
if (task)
|
|
|
|
view.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2020-07-21 20:40:37 +00:00
|
|
|
context.startActivity(view);
|
|
|
|
} catch (ActivityNotFoundException ex) {
|
|
|
|
Log.w(ex);
|
2021-01-30 10:13:29 +00:00
|
|
|
reportNoViewer(context, uri);
|
2020-04-23 06:20:37 +00:00
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
ToastEx.makeText(context, Log.formatThrowable(ex, false), Toast.LENGTH_LONG).show();
|
|
|
|
}
|
2018-12-23 13:34:42 +00:00
|
|
|
} else {
|
2021-04-05 13:48:30 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
boolean navbar_colorize = prefs.getBoolean("navbar_colorize", false);
|
|
|
|
int colorPrimary = resolveColor(context, R.attr.colorPrimary);
|
|
|
|
int colorPrimaryDark = resolveColor(context, R.attr.colorPrimaryDark);
|
|
|
|
|
|
|
|
CustomTabColorSchemeParams.Builder schemes = new CustomTabColorSchemeParams.Builder()
|
|
|
|
.setToolbarColor(colorPrimary)
|
|
|
|
.setSecondaryToolbarColor(colorPrimaryDark);
|
|
|
|
if (navbar_colorize)
|
|
|
|
schemes.setNavigationBarColor(colorPrimaryDark);
|
|
|
|
|
2018-12-23 13:34:42 +00:00
|
|
|
// https://developer.chrome.com/multidevice/android/customtabs
|
2021-04-05 13:48:30 +00:00
|
|
|
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder()
|
|
|
|
.setDefaultColorSchemeParams(schemes.build())
|
|
|
|
.setColorScheme(Helper.isDarkTheme(context)
|
|
|
|
? CustomTabsIntent.COLOR_SCHEME_DARK
|
|
|
|
: CustomTabsIntent.COLOR_SCHEME_LIGHT)
|
|
|
|
.setShareState(CustomTabsIntent.SHARE_STATE_ON)
|
|
|
|
.setUrlBarHidingEnabled(true)
|
|
|
|
.setStartAnimations(context, R.anim.activity_open_enter, R.anim.activity_open_exit)
|
|
|
|
.setExitAnimations(context, R.anim.activity_close_enter, R.anim.activity_close_exit);
|
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);
|
2021-01-30 10:13:29 +00:00
|
|
|
reportNoViewer(context, uri);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-19 11:19:16 +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)
|
2021-04-05 11:14:25 +00:00
|
|
|
view(context, Uri.parse(FAQ_URI + "#top"), false);
|
2019-08-15 05:58:20 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2021-04-06 06:59:35 +00:00
|
|
|
static Uri getSupportUri(Context context) {
|
|
|
|
return Uri.parse(SUPPORT_URI)
|
|
|
|
.buildUpon()
|
|
|
|
.appendQueryParameter("installed", Helper.hasValidFingerprint(context) ? "" : "Other")
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2019-05-09 11:32:42 +00:00
|
|
|
static Intent getIntentIssue(Context context) {
|
2020-05-13 14:03:08 +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);
|
2021-03-25 07:03:25 +00:00
|
|
|
//intent.setPackage(BuildConfig.APPLICATION_ID);
|
2019-06-08 19:50:17 +00:00
|
|
|
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;
|
2021-02-12 14:58:59 +00:00
|
|
|
} else {
|
|
|
|
if (Helper.hasValidFingerprint(context))
|
2021-04-06 06:59:35 +00:00
|
|
|
return new Intent(Intent.ACTION_VIEW, getSupportUri(context));
|
2021-02-12 14:58:59 +00:00
|
|
|
else
|
|
|
|
return new Intent(Intent.ACTION_VIEW, Uri.parse(XDA_URI));
|
|
|
|
}
|
2019-12-26 17:57:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Intent getIntentRate(Context context) {
|
2020-06-14 16:34:13 +00:00
|
|
|
return new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + BuildConfig.APPLICATION_ID));
|
2019-05-09 11:32:42 +00:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-01-25 17:22:18 +00:00
|
|
|
static boolean isSurfaceDuo() {
|
2020-10-11 10:31:51 +00:00
|
|
|
return ("Microsoft".equalsIgnoreCase(Build.MANUFACTURER) && "Surface Duo".equals(Build.MODEL));
|
|
|
|
}
|
|
|
|
|
2021-01-30 10:13:29 +00:00
|
|
|
static void reportNoViewer(Context context, Uri uri) {
|
|
|
|
reportNoViewer(context, new Intent().setData(uri));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reportNoViewer(Context context, Intent intent) {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
String title = intent.getStringExtra(Intent.EXTRA_TITLE);
|
|
|
|
if (TextUtils.isEmpty(title)) {
|
|
|
|
Uri data = intent.getData();
|
|
|
|
if (data == null)
|
|
|
|
sb.append(intent.toString());
|
|
|
|
else
|
|
|
|
sb.append(data.toString());
|
|
|
|
} else
|
|
|
|
sb.append(title);
|
|
|
|
|
|
|
|
String type = intent.getType();
|
|
|
|
if (!TextUtils.isEmpty(type))
|
|
|
|
sb.append(' ').append(type);
|
|
|
|
|
|
|
|
String message = context.getString(R.string.title_no_viewer, sb.toString());
|
|
|
|
ToastEx.makeText(context, message, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
|
2021-02-02 16:43:46 +00:00
|
|
|
static void excludeFromRecents(Context context) {
|
2021-02-02 20:05:17 +00:00
|
|
|
try {
|
|
|
|
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
|
|
|
if (am == null)
|
|
|
|
return;
|
|
|
|
|
2021-02-02 16:43:46 +00:00
|
|
|
List<ActivityManager.AppTask> tasks = am.getAppTasks();
|
2021-02-02 20:05:17 +00:00
|
|
|
if (tasks == null || tasks.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tasks.get(0).setExcludeFromRecents(true);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
2021-02-02 16:43:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 12:53:25 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-10-04 18:06:59 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-10 11:05:38 +00:00
|
|
|
static void setViewsEnabled(ViewGroup view, boolean enabled) {
|
|
|
|
for (int i = 0; i < view.getChildCount(); i++) {
|
|
|
|
View child = view.getChildAt(i);
|
2020-11-23 14:23:39 +00:00
|
|
|
if ("ignore".equals(child.getTag()))
|
|
|
|
continue;
|
2018-08-12 08:07:34 +00:00
|
|
|
if (child instanceof Spinner ||
|
|
|
|
child instanceof EditText ||
|
|
|
|
child instanceof CheckBox ||
|
2019-01-29 09:02:34 +00:00
|
|
|
child instanceof ImageView /* =ImageButton */ ||
|
2019-09-19 13:29:00 +00:00
|
|
|
child instanceof RadioButton ||
|
2019-01-29 09:02:34 +00:00
|
|
|
(child instanceof Button && "disable".equals(child.getTag())))
|
2018-08-10 11:05:38 +00:00
|
|
|
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)
|
2018-08-10 11:05:38 +00:00
|
|
|
setViewsEnabled((ViewGroup) child, enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 06:13:18 +00:00
|
|
|
static void hide(View view) {
|
2020-11-01 13:42:38 +00:00
|
|
|
view.setPadding(0, 1, 0, 0);
|
2019-05-16 06:13:18 +00:00
|
|
|
|
|
|
|
ViewGroup.LayoutParams lparam = view.getLayoutParams();
|
|
|
|
lparam.width = 0;
|
2020-10-08 12:14:19 +00:00
|
|
|
lparam.height = 1;
|
2019-05-16 06:13:18 +00:00
|
|
|
if (lparam instanceof ConstraintLayout.LayoutParams)
|
|
|
|
((ConstraintLayout.LayoutParams) lparam).setMargins(0, 0, 0, 0);
|
|
|
|
view.setLayoutParams(lparam);
|
|
|
|
}
|
|
|
|
|
2021-02-28 08:32:21 +00:00
|
|
|
static boolean isNight(Context context) {
|
|
|
|
// https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#configuration_changes
|
|
|
|
int uiMode = context.getResources().getConfiguration().uiMode;
|
2021-03-27 11:11:58 +00:00
|
|
|
Log.i("UI mode=" + Integer.toHexString(uiMode));
|
|
|
|
return ((uiMode & Configuration.UI_MODE_NIGHT_YES) != 0);
|
2021-02-28 08:32:21 +00:00
|
|
|
}
|
|
|
|
|
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("@@");
|
|
|
|
|
2020-07-02 08:19:01 +00:00
|
|
|
static String humanReadableByteCount(long bytes) {
|
|
|
|
return humanReadableByteCount(bytes, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String humanReadableByteCount(long bytes, boolean si) {
|
2019-05-12 17:14:34 +00:00
|
|
|
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
|
|
|
|
|
2021-01-30 14:28:43 +00:00
|
|
|
static boolean isRtl(String text) {
|
|
|
|
if (TextUtils.isEmpty(text))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int rtl = 0;
|
|
|
|
int ltr = 0;
|
|
|
|
for (int i = 0; i < text.length(); i++)
|
|
|
|
switch (Character.getDirectionality(text.charAt(i))) {
|
|
|
|
case java.lang.Character.DIRECTIONALITY_RIGHT_TO_LEFT:
|
|
|
|
case java.lang.Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC:
|
|
|
|
case java.lang.Character.DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING:
|
|
|
|
case java.lang.Character.DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE:
|
|
|
|
rtl++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case java.lang.Character.DIRECTIONALITY_LEFT_TO_RIGHT:
|
|
|
|
case java.lang.Character.DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING:
|
|
|
|
case java.lang.Character.DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE:
|
|
|
|
ltr++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (rtl > ltr);
|
|
|
|
}
|
|
|
|
|
2019-07-15 19:28:25 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:40:44 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-12-01 11:24:03 +00:00
|
|
|
static String[] getStrings(Context context, int resid, Object... formatArgs) {
|
2020-03-26 13:44:53 +00:00
|
|
|
return getStrings(context, null, resid, formatArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static String[] getStrings(Context context, String language, int resid, Object... formatArgs) {
|
|
|
|
List<Locale> locales = new ArrayList<>();
|
|
|
|
|
|
|
|
if (language != null)
|
|
|
|
locales.add(new Locale(language));
|
2019-12-01 11:24:03 +00:00
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
2020-03-26 13:44:53 +00:00
|
|
|
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"));
|
2019-12-01 11:24:03 +00:00
|
|
|
} else {
|
|
|
|
LocaleList ll = context.getResources().getConfiguration().getLocales();
|
|
|
|
for (int i = 0; i < ll.size(); i++) {
|
2020-03-26 13:44:53 +00:00
|
|
|
Locale l = ll.get(i);
|
|
|
|
if (!l.getLanguage().equals(language))
|
|
|
|
locales.add(l);
|
2019-12-01 11:24:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 13:44:53 +00:00
|
|
|
List<String> 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);
|
|
|
|
}
|
|
|
|
|
2019-12-01 11:24:03 +00:00
|
|
|
return result.toArray(new String[0]);
|
|
|
|
}
|
|
|
|
|
2020-07-20 12:50:10 +00:00
|
|
|
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<String> names = new ArrayList<>();
|
|
|
|
String[] c = name.split("\\.");
|
|
|
|
List<String> assets = Arrays.asList(list);
|
|
|
|
|
|
|
|
List<Locale> 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;
|
|
|
|
}
|
|
|
|
|
2020-01-08 12:21:53 +00:00
|
|
|
static boolean containsWhiteSpace(String text) {
|
|
|
|
return text.matches(".*\\s+.*");
|
|
|
|
}
|
|
|
|
|
|
|
|
static boolean containsControlChars(String text) {
|
2020-04-23 14:03:56 +00:00
|
|
|
int codePoint;
|
2020-01-08 12:21:53 +00:00
|
|
|
for (int offset = 0; offset < text.length(); ) {
|
2020-04-23 14:03:56 +00:00
|
|
|
codePoint = text.codePointAt(offset);
|
2020-01-08 12:21:53 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-13 12:20:19 +00:00
|
|
|
static boolean isSingleScript(String s) {
|
|
|
|
// https://en.wikipedia.org/wiki/IDN_homograph_attack
|
2021-04-04 11:45:32 +00:00
|
|
|
|
|
|
|
if (TextUtils.isEmpty(s))
|
|
|
|
return true;
|
|
|
|
|
2020-04-13 12:20:19 +00:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
|
|
|
|
return true;
|
|
|
|
|
2020-04-23 14:03:56 +00:00
|
|
|
int codepoint;
|
|
|
|
Character.UnicodeScript us;
|
2020-04-13 12:20:19 +00:00
|
|
|
Character.UnicodeScript script = null;
|
|
|
|
for (int i = 0; i < s.length(); ) {
|
2020-04-23 14:03:56 +00:00
|
|
|
codepoint = s.codePointAt(i);
|
2020-04-13 12:20:19 +00:00
|
|
|
i += Character.charCount(codepoint);
|
2020-04-23 14:03:56 +00:00
|
|
|
us = Character.UnicodeScript.of(codepoint);
|
2020-04-13 12:20:19 +00:00
|
|
|
if (us.equals(Character.UnicodeScript.COMMON))
|
|
|
|
continue;
|
|
|
|
if (script == null)
|
|
|
|
script = us;
|
|
|
|
else if (!us.equals(script))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-06 17:00:11 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-10-29 10:50:06 +00:00
|
|
|
return name
|
|
|
|
// Canonical files names cannot contain NUL
|
|
|
|
.replace("\0", "")
|
|
|
|
.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-15 07:40:18 +00:00
|
|
|
}
|
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);
|
2020-07-14 13:07:09 +00:00
|
|
|
if (extension != null) {
|
|
|
|
extension = extension.toLowerCase(Locale.ROOT);
|
2021-02-05 12:00:57 +00:00
|
|
|
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
2020-07-14 13:07:09 +00:00
|
|
|
}
|
2019-11-20 11:40:56 +00:00
|
|
|
|
|
|
|
if (TextUtils.isEmpty(type))
|
2020-07-14 13:07:09 +00:00
|
|
|
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
|
|
|
|
return "application/octet-stream";
|
2019-11-20 11:40:56 +00:00
|
|
|
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2020-07-14 13:07:09 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-21 16:45:05 +00:00
|
|
|
static void writeText(File file, String content) throws IOException {
|
2019-07-26 06:39:05 +00:00
|
|
|
try (FileOutputStream out = new FileOutputStream(file)) {
|
|
|
|
if (content != null)
|
|
|
|
out.write(content.getBytes());
|
2019-01-21 16:45:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:00:44 +00:00
|
|
|
static String readStream(InputStream is) throws IOException {
|
|
|
|
return readStream(is, StandardCharsets.UTF_8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static String readStream(InputStream is, Charset charset) throws IOException {
|
2019-08-09 18:16:36 +00:00
|
|
|
ByteArrayOutputStream os = new ByteArrayOutputStream(Math.max(BUFFER_SIZE, is.available()));
|
2019-07-26 06:39:05 +00:00
|
|
|
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 {
|
2019-06-14 11:06:30 +00:00
|
|
|
try (FileInputStream in = new FileInputStream(file)) {
|
2021-01-20 19:00:44 +00:00
|
|
|
return readStream(in);
|
2019-01-21 16:45:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 18:30:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:58:21 +00:00
|
|
|
static void copy(File src, File dst) throws IOException {
|
2019-07-26 06:39:05 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-01-22 14:01:20 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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-05-11 08:04:22 +00:00
|
|
|
//File initial = Environment.getExternalStorageDirectory();
|
|
|
|
//intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, Uri.fromFile(initial));
|
2019-12-01 17:19:17 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 17:48:02 +00:00
|
|
|
static boolean isImage(String mimeType) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
|
|
|
if (IMAGE_TYPES8.contains(mimeType))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return IMAGE_TYPES.contains(mimeType);
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2019-12-15 18:26:01 +00:00
|
|
|
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 {
|
2019-12-15 18:26:01 +00:00
|
|
|
return sha("SHA-256", data);
|
|
|
|
}
|
|
|
|
|
2020-01-17 15:59:29 +00:00
|
|
|
static String md5(byte[] data) throws NoSuchAlgorithmException {
|
|
|
|
return sha("MD5", data);
|
|
|
|
}
|
|
|
|
|
2019-12-15 18:26:01 +00:00
|
|
|
static String sha(String digest, byte[] data) throws NoSuchAlgorithmException {
|
|
|
|
byte[] bytes = MessageDigest.getInstance(digest).digest(data);
|
2020-01-31 19:20:44 +00:00
|
|
|
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);
|
2020-08-20 16:54:56 +00:00
|
|
|
return (bm.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK) == 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);
|
2019-07-24 05:38:34 +00:00
|
|
|
long biometrics_timeout = prefs.getInt("biometrics_timeout", 2) * 60 * 1000L;
|
2019-07-11 15:06:49 +00:00
|
|
|
Log.i("Authentication valid until=" + new Date(last_authentication + biometrics_timeout));
|
2019-07-10 17:58:02 +00:00
|
|
|
|
2019-07-11 15:06:49 +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
|
|
|
}
|
|
|
|
|
2020-05-30 13:59:51 +00:00
|
|
|
static void authenticate(final FragmentActivity activity, final LifecycleOwner owner,
|
2019-07-10 15:58:26 +00:00
|
|
|
Boolean enabled, final
|
|
|
|
Runnable authenticated, final Runnable cancelled) {
|
2019-10-20 10:17:04 +00:00
|
|
|
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
|
|
|
|
String pin = prefs.getString("pin", null);
|
2019-10-11 10:32:46 +00:00
|
|
|
|
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-11 10:32:46 +00:00
|
|
|
|
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));
|
|
|
|
|
2020-05-30 13:59:51 +00:00
|
|
|
final BiometricPrompt prompt = new BiometricPrompt(activity, executor,
|
2019-10-20 10:17:04 +00:00
|
|
|
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)
|
2020-08-23 15:34:14 +00:00
|
|
|
ApplicationEx.getMainHandler().post(new Runnable() {
|
2019-10-21 07:13:40 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
ToastEx.makeText(activity,
|
|
|
|
"Error " + errorCode + ": " + errString,
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
});
|
2019-07-10 15:58:26 +00:00
|
|
|
|
2020-08-23 15:34:14 +00:00
|
|
|
ApplicationEx.getMainHandler().post(cancelled);
|
2019-10-20 10:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
|
|
|
|
Log.i("Biometric succeeded");
|
|
|
|
setAuthenticated(activity);
|
2020-08-23 15:34:14 +00:00
|
|
|
ApplicationEx.getMainHandler().post(authenticated);
|
2019-10-20 10:17:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAuthenticationFailed() {
|
|
|
|
Log.w("Biometric failed");
|
2020-08-23 15:34:14 +00:00
|
|
|
ApplicationEx.getMainHandler().post(cancelled);
|
2019-10-20 10:17:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
prompt.authenticate(info.build());
|
2020-05-30 13:59:51 +00:00
|
|
|
|
2020-06-01 06:32:03 +00:00
|
|
|
final Runnable cancelPrompt = new Runnable() {
|
2020-05-31 12:42:20 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
prompt.cancelAuthentication();
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-23 15:34:14 +00:00
|
|
|
ApplicationEx.getMainHandler().postDelayed(cancelPrompt, 60 * 1000L);
|
2020-05-31 12:42:20 +00:00
|
|
|
|
2020-05-30 13:59:51 +00:00
|
|
|
owner.getLifecycle().addObserver(new LifecycleObserver() {
|
2020-05-30 14:21:25 +00:00
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
|
|
public void onDestroy() {
|
2020-08-23 15:34:14 +00:00
|
|
|
ApplicationEx.getMainHandler().post(cancelPrompt);
|
2020-05-30 13:59:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-10-20 10:17:04 +00:00
|
|
|
} 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);
|
2020-08-23 15:34:14 +00:00
|
|
|
ApplicationEx.getMainHandler().post(authenticated);
|
2019-10-20 10:17:04 +00:00
|
|
|
} else
|
2020-08-23 15:34:14 +00:00
|
|
|
ApplicationEx.getMainHandler().post(cancelled);
|
2019-10-20 10:17:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2020-08-23 15:34:14 +00:00
|
|
|
ApplicationEx.getMainHandler().post(cancelled);
|
2019-10-20 10:17:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.setOnDismissListener(new DialogInterface.OnDismissListener() {
|
|
|
|
@Override
|
|
|
|
public void onDismiss(DialogInterface dialog) {
|
2020-08-23 15:34:14 +00:00
|
|
|
ApplicationEx.getMainHandler().post(cancelled);
|
2019-10-20 10:17:04 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-23 15:34:14 +00:00
|
|
|
ApplicationEx.getMainHandler().post(new Runnable() {
|
2019-11-01 09:50:32 +00:00
|
|
|
@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();
|
|
|
|
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);
|
2020-04-02 09:18:16 +00:00
|
|
|
deliver(alias);
|
2019-12-05 14:18:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} catch (KeyChainException ex) {
|
|
|
|
Log.w(ex);
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
|
2020-08-23 15:34:14 +00:00
|
|
|
ApplicationEx.getMainHandler().post(new Runnable() {
|
2019-12-05 14:18:53 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
KeyChain.choosePrivateKeyAlias(activity, new KeyChainAliasCallback() {
|
|
|
|
@Override
|
|
|
|
public void alias(@Nullable final String alias) {
|
|
|
|
Log.i("Selected key alias=" + alias);
|
2020-04-02 09:18:16 +00:00
|
|
|
deliver(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
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2020-04-02 09:18:16 +00:00
|
|
|
|
|
|
|
private void deliver(final String selected) {
|
2020-08-23 15:34:14 +00:00
|
|
|
ApplicationEx.getMainHandler().post(new Runnable() {
|
2020-04-02 09:18:16 +00:00
|
|
|
@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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
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
|
|
|
|
|
2021-03-06 10:14:23 +00:00
|
|
|
static void gc() {
|
2021-03-10 19:25:56 +00:00
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
|
Runtime.getRuntime().gc();
|
|
|
|
try {
|
|
|
|
Thread.sleep(50);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2021-03-06 10:14:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-17 07:51:29 +00:00
|
|
|
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) {
|
2020-06-25 07:14:05 +00:00
|
|
|
if (a1 == null && a2 == null)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (a1 == null || a2 == null)
|
|
|
|
return false;
|
|
|
|
|
2018-11-25 12:34:08 +00:00
|
|
|
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
|
|
|
}
|