2017-11-25 06:00:30 +00:00
|
|
|
/*
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-10-01 02:56:29 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* 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.
|
2012-10-01 02:56:29 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
2014-01-11 11:34:11 +00:00
|
|
|
import android.annotation.SuppressLint;
|
2014-12-29 22:01:02 +00:00
|
|
|
import android.annotation.TargetApi;
|
2015-06-04 21:19:10 +00:00
|
|
|
import android.app.ActivityManager;
|
2016-11-15 03:50:29 +00:00
|
|
|
import android.content.ClipData;
|
|
|
|
import android.content.ClipboardManager;
|
2013-07-17 22:13:00 +00:00
|
|
|
import android.content.Context;
|
2014-07-23 22:40:45 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2013-02-08 19:57:54 +00:00
|
|
|
import android.graphics.Typeface;
|
2017-11-26 18:45:39 +00:00
|
|
|
import android.net.Uri;
|
2014-02-19 21:46:49 +00:00
|
|
|
import android.os.Build;
|
2014-12-29 22:01:02 +00:00
|
|
|
import android.os.Build.VERSION;
|
|
|
|
import android.os.Build.VERSION_CODES;
|
2015-05-22 01:27:31 +00:00
|
|
|
import android.os.Handler;
|
2015-05-15 04:08:37 +00:00
|
|
|
import android.os.Looper;
|
2014-02-19 21:46:49 +00:00
|
|
|
import android.provider.Telephony;
|
2015-08-24 22:24:31 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2015-05-18 15:38:48 +00:00
|
|
|
import android.support.annotation.Nullable;
|
2017-11-25 06:00:30 +00:00
|
|
|
import android.support.annotation.RequiresPermission;
|
2014-11-12 19:15:05 +00:00
|
|
|
import android.telephony.TelephonyManager;
|
2013-02-08 19:57:54 +00:00
|
|
|
import android.text.Spannable;
|
|
|
|
import android.text.SpannableString;
|
2014-11-12 19:15:05 +00:00
|
|
|
import android.text.TextUtils;
|
2013-02-08 19:57:54 +00:00
|
|
|
import android.text.style.StyleSpan;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2013-02-04 08:13:07 +00:00
|
|
|
|
2017-05-08 22:32:59 +00:00
|
|
|
import com.google.android.mms.pdu_alt.CharacterSets;
|
|
|
|
import com.google.android.mms.pdu_alt.EncodedStringValue;
|
2017-08-02 19:51:46 +00:00
|
|
|
import com.google.i18n.phonenumbers.NumberParseException;
|
2015-01-08 22:56:04 +00:00
|
|
|
import com.google.i18n.phonenumbers.PhoneNumberUtil;
|
2017-08-02 19:51:46 +00:00
|
|
|
import com.google.i18n.phonenumbers.Phonenumber;
|
2015-01-08 22:56:04 +00:00
|
|
|
|
2015-01-12 04:27:34 +00:00
|
|
|
import org.thoughtcrime.securesms.BuildConfig;
|
2018-02-02 14:56:25 +00:00
|
|
|
import org.thoughtcrime.securesms.components.ComposeText;
|
2017-07-26 16:59:15 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2014-12-29 22:01:02 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingLegacyMmsConnection;
|
2017-08-02 19:51:46 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2013-07-17 02:52:02 +00:00
|
|
|
|
2013-07-19 00:42:45 +00:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2018-07-26 14:10:46 +00:00
|
|
|
import java.io.Closeable;
|
|
|
|
import java.io.EOFException;
|
2013-07-19 00:42:45 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2014-11-12 19:15:05 +00:00
|
|
|
import java.io.OutputStream;
|
2013-04-26 18:23:43 +00:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2014-11-12 19:15:05 +00:00
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.SecureRandom;
|
2017-03-28 19:05:30 +00:00
|
|
|
import java.text.DecimalFormat;
|
2019-02-06 08:36:55 +00:00
|
|
|
import java.util.ArrayList;
|
2015-05-18 15:38:48 +00:00
|
|
|
import java.util.Arrays;
|
2014-11-12 19:15:05 +00:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2018-10-29 22:14:31 +00:00
|
|
|
import java.util.Map;
|
2015-10-27 00:20:09 +00:00
|
|
|
import java.util.concurrent.CountDownLatch;
|
2012-12-27 20:00:14 +00:00
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
public class Util {
|
2015-11-22 18:44:44 +00:00
|
|
|
private static final String TAG = Util.class.getSimpleName();
|
|
|
|
|
2019-02-06 08:36:55 +00:00
|
|
|
private static volatile Handler handler;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2017-08-07 21:24:53 +00:00
|
|
|
public static <T> List<T> asList(T... elements) {
|
|
|
|
List<T> result = new LinkedList<>();
|
|
|
|
Collections.addAll(result, elements);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-24 16:40:04 +00:00
|
|
|
public static String join(String[] list, String delimiter) {
|
|
|
|
return join(Arrays.asList(list), delimiter);
|
|
|
|
}
|
|
|
|
|
2014-11-12 19:15:05 +00:00
|
|
|
public static String join(Collection<String> list, String delimiter) {
|
|
|
|
StringBuilder result = new StringBuilder();
|
2015-05-22 01:27:31 +00:00
|
|
|
int i = 0;
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2014-11-12 19:15:05 +00:00
|
|
|
for (String item : list) {
|
|
|
|
result.append(item);
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2014-11-12 19:15:05 +00:00
|
|
|
if (++i < list.size())
|
|
|
|
result.append(delimiter);
|
|
|
|
}
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2014-11-12 19:15:05 +00:00
|
|
|
return result.toString();
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-10-01 02:56:29 +00:00
|
|
|
|
2015-06-09 14:37:20 +00:00
|
|
|
public static String join(long[] list, String delimeter) {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
for (int j=0;j<list.length;j++) {
|
|
|
|
if (j != 0) sb.append(delimeter);
|
|
|
|
sb.append(list[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
2012-12-27 20:00:14 +00:00
|
|
|
public static ExecutorService newSingleThreadedLifoExecutor() {
|
2013-01-06 23:46:26 +00:00
|
|
|
ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingLifoQueue<Runnable>());
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
executor.execute(() -> {
|
2013-01-06 23:46:26 +00:00
|
|
|
// Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
|
2017-11-25 06:00:30 +00:00
|
|
|
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
|
2013-01-06 23:46:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return executor;
|
2012-12-27 20:00:14 +00:00
|
|
|
}
|
|
|
|
|
2013-04-26 01:59:49 +00:00
|
|
|
public static boolean isEmpty(EncodedStringValue[] value) {
|
|
|
|
return value == null || value.length == 0;
|
|
|
|
}
|
|
|
|
|
2018-02-02 14:56:25 +00:00
|
|
|
public static boolean isEmpty(ComposeText value) {
|
|
|
|
return value == null || value.getText() == null || TextUtils.isEmpty(value.getTextTrimmed());
|
2013-02-08 19:57:54 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 17:59:23 +00:00
|
|
|
public static boolean isEmpty(Collection collection) {
|
|
|
|
return collection == null || collection.isEmpty();
|
|
|
|
}
|
|
|
|
|
2018-10-29 22:14:31 +00:00
|
|
|
public static <K, V> V getOrDefault(@NonNull Map<K, V> map, K key, V defaultValue) {
|
|
|
|
return map.containsKey(key) ? map.get(key) : defaultValue;
|
|
|
|
}
|
|
|
|
|
2019-02-27 03:29:52 +00:00
|
|
|
public static String getFirstNonEmpty(String... values) {
|
|
|
|
for (String value : values) {
|
|
|
|
if (!TextUtils.isEmpty(value)) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2019-02-06 08:36:55 +00:00
|
|
|
public static <E> List<List<E>> chunk(@NonNull List<E> list, int chunkSize) {
|
|
|
|
List<List<E>> chunks = new ArrayList<>(list.size() / chunkSize);
|
|
|
|
|
|
|
|
for (int i = 0; i < list.size(); i += chunkSize) {
|
|
|
|
List<E> chunk = list.subList(i, Math.min(list.size(), i + chunkSize));
|
|
|
|
chunks.add(chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
return chunks;
|
|
|
|
}
|
|
|
|
|
2014-11-12 19:15:05 +00:00
|
|
|
public static CharSequence getBoldedString(String value) {
|
2013-02-08 19:57:54 +00:00
|
|
|
SpannableString spanned = new SpannableString(value);
|
2014-11-12 19:15:05 +00:00
|
|
|
spanned.setSpan(new StyleSpan(Typeface.BOLD), 0,
|
2013-02-08 19:57:54 +00:00
|
|
|
spanned.length(),
|
|
|
|
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
|
|
|
|
return spanned;
|
|
|
|
}
|
|
|
|
|
2015-08-24 22:24:31 +00:00
|
|
|
public static @NonNull String toIsoString(byte[] bytes) {
|
2013-04-26 18:23:43 +00:00
|
|
|
try {
|
|
|
|
return new String(bytes, CharacterSets.MIMENAME_ISO_8859_1);
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
2014-04-20 21:18:17 +00:00
|
|
|
throw new AssertionError("ISO_8859_1 must be supported!");
|
2013-04-26 18:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[] toIsoBytes(String isoString) {
|
|
|
|
try {
|
|
|
|
return isoString.getBytes(CharacterSets.MIMENAME_ISO_8859_1);
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
2014-04-20 21:18:17 +00:00
|
|
|
throw new AssertionError("ISO_8859_1 must be supported!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[] toUtf8Bytes(String utf8String) {
|
|
|
|
try {
|
|
|
|
return utf8String.getBytes(CharacterSets.MIMENAME_UTF_8);
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
throw new AssertionError("UTF_8 must be supported!");
|
2013-04-26 18:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
public static void wait(Object lock, long timeout) {
|
2013-07-17 02:52:02 +00:00
|
|
|
try {
|
|
|
|
lock.wait(timeout);
|
|
|
|
} catch (InterruptedException ie) {
|
|
|
|
throw new AssertionError(ie);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 14:10:46 +00:00
|
|
|
public static void close(Closeable closeable) {
|
2017-03-28 19:05:30 +00:00
|
|
|
try {
|
2018-07-26 14:10:46 +00:00
|
|
|
closeable.close();
|
2015-11-22 18:44:44 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 19:05:30 +00:00
|
|
|
public static long getStreamLength(InputStream in) throws IOException {
|
|
|
|
byte[] buffer = new byte[4096];
|
|
|
|
int totalSize = 0;
|
|
|
|
|
|
|
|
int read;
|
|
|
|
|
|
|
|
while ((read = in.read(buffer)) != -1) {
|
|
|
|
totalSize += read;
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalSize;
|
|
|
|
}
|
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
public static boolean isOwnNumber(Context context, Address address) {
|
|
|
|
if (address.isGroup()) return false;
|
|
|
|
if (address.isEmail()) return false;
|
2014-10-21 04:06:34 +00:00
|
|
|
|
2017-07-26 16:59:15 +00:00
|
|
|
return TextSecurePreferences.getLocalNumber(context).equals(address.toPhoneString());
|
2016-02-14 13:58:39 +00:00
|
|
|
}
|
|
|
|
|
2018-02-26 17:58:18 +00:00
|
|
|
public static void readFully(InputStream in, byte[] buffer) throws IOException {
|
2018-07-26 14:10:46 +00:00
|
|
|
readFully(in, buffer, buffer.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void readFully(InputStream in, byte[] buffer, int len) throws IOException {
|
2018-02-26 17:58:18 +00:00
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
for (;;) {
|
2018-07-26 14:10:46 +00:00
|
|
|
int read = in.read(buffer, offset, len - offset);
|
|
|
|
if (read == -1) throw new EOFException("Stream ended early");
|
2018-02-26 17:58:18 +00:00
|
|
|
|
2018-07-26 14:10:46 +00:00
|
|
|
if (read + offset < len) offset += read;
|
|
|
|
else return;
|
2018-02-26 17:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 09:03:24 +00:00
|
|
|
public static byte[] readFully(InputStream in) throws IOException {
|
2014-11-12 19:15:05 +00:00
|
|
|
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
|
|
|
byte[] buffer = new byte[4096];
|
|
|
|
int read;
|
|
|
|
|
|
|
|
while ((read = in.read(buffer)) != -1) {
|
|
|
|
bout.write(buffer, 0, read);
|
|
|
|
}
|
2013-07-19 00:42:45 +00:00
|
|
|
|
2014-11-12 19:15:05 +00:00
|
|
|
in.close();
|
|
|
|
|
2014-12-12 09:03:24 +00:00
|
|
|
return bout.toByteArray();
|
2014-11-12 19:15:05 +00:00
|
|
|
}
|
|
|
|
|
2014-12-12 09:03:24 +00:00
|
|
|
public static String readFullyAsString(InputStream in) throws IOException {
|
|
|
|
return new String(readFully(in));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long copy(InputStream in, OutputStream out) throws IOException {
|
2018-01-25 03:17:44 +00:00
|
|
|
byte[] buffer = new byte[8192];
|
2013-07-19 00:42:45 +00:00
|
|
|
int read;
|
2014-12-12 09:03:24 +00:00
|
|
|
long total = 0;
|
2013-07-19 00:42:45 +00:00
|
|
|
|
|
|
|
while ((read = in.read(buffer)) != -1) {
|
2014-11-12 19:15:05 +00:00
|
|
|
out.write(buffer, 0, read);
|
2014-12-12 09:03:24 +00:00
|
|
|
total += read;
|
2013-07-19 00:42:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
in.close();
|
2014-11-12 19:15:05 +00:00
|
|
|
out.close();
|
2014-12-12 09:03:24 +00:00
|
|
|
|
|
|
|
return total;
|
2014-11-12 19:15:05 +00:00
|
|
|
}
|
|
|
|
|
2017-11-25 06:00:30 +00:00
|
|
|
@RequiresPermission(anyOf = {
|
|
|
|
android.Manifest.permission.READ_PHONE_STATE,
|
|
|
|
android.Manifest.permission.READ_SMS,
|
|
|
|
android.Manifest.permission.READ_PHONE_NUMBERS
|
|
|
|
})
|
|
|
|
@SuppressLint("MissingPermission")
|
2017-08-02 19:51:46 +00:00
|
|
|
public static Optional<Phonenumber.PhoneNumber> getDeviceNumber(Context context) {
|
|
|
|
try {
|
|
|
|
final String localNumber = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();
|
|
|
|
final Optional<String> countryIso = getSimCountryIso(context);
|
2014-11-12 19:15:05 +00:00
|
|
|
|
2017-08-02 19:51:46 +00:00
|
|
|
if (TextUtils.isEmpty(localNumber)) return Optional.absent();
|
|
|
|
if (!countryIso.isPresent()) return Optional.absent();
|
2014-11-12 19:15:05 +00:00
|
|
|
|
2017-08-02 19:51:46 +00:00
|
|
|
return Optional.fromNullable(PhoneNumberUtil.getInstance().parse(localNumber, countryIso.get()));
|
|
|
|
} catch (NumberParseException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return Optional.absent();
|
|
|
|
}
|
2015-01-08 22:56:04 +00:00
|
|
|
}
|
2014-11-12 19:15:05 +00:00
|
|
|
|
2017-08-02 19:51:46 +00:00
|
|
|
public static Optional<String> getSimCountryIso(Context context) {
|
2015-01-08 22:56:04 +00:00
|
|
|
String simCountryIso = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getSimCountryIso();
|
2017-08-02 19:51:46 +00:00
|
|
|
return Optional.fromNullable(simCountryIso != null ? simCountryIso.toUpperCase() : null);
|
2014-11-12 19:15:05 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 17:02:19 +00:00
|
|
|
public static <T> List<List<T>> partition(List<T> list, int partitionSize) {
|
|
|
|
List<List<T>> results = new LinkedList<>();
|
|
|
|
|
|
|
|
for (int index=0;index<list.size();index+=partitionSize) {
|
2015-04-12 17:07:30 +00:00
|
|
|
int subListSize = Math.min(partitionSize, list.size() - index);
|
2015-04-02 17:02:19 +00:00
|
|
|
|
2015-04-12 17:07:30 +00:00
|
|
|
results.add(list.subList(index, index + subListSize));
|
2015-04-02 17:02:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2014-11-12 19:15:05 +00:00
|
|
|
public static List<String> split(String source, String delimiter) {
|
|
|
|
List<String> results = new LinkedList<>();
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(source)) {
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
String[] elements = source.split(delimiter);
|
|
|
|
Collections.addAll(results, elements);
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[][] split(byte[] input, int firstLength, int secondLength) {
|
|
|
|
byte[][] parts = new byte[2][];
|
|
|
|
|
|
|
|
parts[0] = new byte[firstLength];
|
|
|
|
System.arraycopy(input, 0, parts[0], 0, firstLength);
|
|
|
|
|
|
|
|
parts[1] = new byte[secondLength];
|
|
|
|
System.arraycopy(input, firstLength, parts[1], 0, secondLength);
|
|
|
|
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[] combine(byte[]... elements) {
|
|
|
|
try {
|
|
|
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
|
|
|
|
for (byte[] element : elements) {
|
|
|
|
baos.write(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
return baos.toByteArray();
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[] trim(byte[] input, int length) {
|
|
|
|
byte[] result = new byte[length];
|
|
|
|
System.arraycopy(input, 0, result, 0, result.length);
|
|
|
|
|
|
|
|
return result;
|
2013-07-19 00:42:45 +00:00
|
|
|
}
|
2013-03-26 04:26:03 +00:00
|
|
|
|
2014-01-11 11:34:11 +00:00
|
|
|
@SuppressLint("NewApi")
|
2013-11-11 21:06:58 +00:00
|
|
|
public static boolean isDefaultSmsProvider(Context context){
|
2019-03-20 22:09:27 +00:00
|
|
|
return context.getPackageName().equals(Telephony.Sms.getDefaultSmsPackage(context));
|
2013-11-11 21:06:58 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 22:40:45 +00:00
|
|
|
public static int getCurrentApkReleaseVersion(Context context) {
|
|
|
|
try {
|
|
|
|
return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
2014-11-12 19:15:05 +00:00
|
|
|
|
|
|
|
public static String getSecret(int size) {
|
|
|
|
byte[] secret = getSecretBytes(size);
|
|
|
|
return Base64.encodeBytes(secret);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte[] getSecretBytes(int size) {
|
|
|
|
byte[] secret = new byte[size];
|
|
|
|
getSecureRandom().nextBytes(secret);
|
|
|
|
return secret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SecureRandom getSecureRandom() {
|
2018-02-26 17:58:18 +00:00
|
|
|
return new SecureRandom();
|
2014-11-12 19:15:05 +00:00
|
|
|
}
|
|
|
|
|
2015-10-28 14:46:47 +00:00
|
|
|
public static int getDaysTillBuildExpiry() {
|
2019-03-20 22:09:27 +00:00
|
|
|
int age = (int) TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - BuildConfig.BUILD_TIMESTAMP);
|
|
|
|
return 90 - age;
|
2015-01-12 04:27:34 +00:00
|
|
|
}
|
2014-12-29 22:01:02 +00:00
|
|
|
|
|
|
|
@TargetApi(VERSION_CODES.LOLLIPOP)
|
|
|
|
public static boolean isMmsCapable(Context context) {
|
|
|
|
return (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) || OutgoingLegacyMmsConnection.isConnectionPossible(context);
|
|
|
|
}
|
2015-05-15 04:08:37 +00:00
|
|
|
|
2015-05-22 01:27:31 +00:00
|
|
|
public static boolean isMainThread() {
|
|
|
|
return Looper.myLooper() == Looper.getMainLooper();
|
|
|
|
}
|
|
|
|
|
2015-05-15 04:08:37 +00:00
|
|
|
public static void assertMainThread() {
|
2015-05-22 01:27:31 +00:00
|
|
|
if (!isMainThread()) {
|
2015-05-15 04:08:37 +00:00
|
|
|
throw new AssertionError("Main-thread assertion failed.");
|
|
|
|
}
|
|
|
|
}
|
2015-05-18 15:38:48 +00:00
|
|
|
|
2018-10-16 18:47:58 +00:00
|
|
|
public static void postToMain(final @NonNull Runnable runnable) {
|
2019-02-06 08:36:55 +00:00
|
|
|
getHandler().post(runnable);
|
2018-10-16 18:47:58 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 00:20:09 +00:00
|
|
|
public static void runOnMain(final @NonNull Runnable runnable) {
|
2015-05-22 01:27:31 +00:00
|
|
|
if (isMainThread()) runnable.run();
|
2019-02-06 08:36:55 +00:00
|
|
|
else getHandler().post(runnable);
|
2015-05-22 01:27:31 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 06:46:48 +00:00
|
|
|
public static void runOnMainDelayed(final @NonNull Runnable runnable, long delayMillis) {
|
2019-02-06 08:36:55 +00:00
|
|
|
getHandler().postDelayed(runnable, delayMillis);
|
2017-09-10 06:46:48 +00:00
|
|
|
}
|
|
|
|
|
2018-10-29 22:14:31 +00:00
|
|
|
public static void cancelRunnableOnMain(@NonNull Runnable runnable) {
|
2019-02-06 08:36:55 +00:00
|
|
|
getHandler().removeCallbacks(runnable);
|
2018-10-29 22:14:31 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 00:20:09 +00:00
|
|
|
public static void runOnMainSync(final @NonNull Runnable runnable) {
|
|
|
|
if (isMainThread()) {
|
|
|
|
runnable.run();
|
|
|
|
} else {
|
|
|
|
final CountDownLatch sync = new CountDownLatch(1);
|
2017-11-25 06:00:30 +00:00
|
|
|
runOnMain(() -> {
|
|
|
|
try {
|
|
|
|
runnable.run();
|
|
|
|
} finally {
|
|
|
|
sync.countDown();
|
2015-10-27 00:20:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
sync.await();
|
|
|
|
} catch (InterruptedException ie) {
|
|
|
|
throw new AssertionError(ie);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-17 02:05:07 +00:00
|
|
|
public static <T> T getRandomElement(T[] elements) {
|
2019-03-15 22:31:52 +00:00
|
|
|
return elements[new SecureRandom().nextInt(elements.length)];
|
2016-10-17 02:05:07 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 15:38:48 +00:00
|
|
|
public static boolean equals(@Nullable Object a, @Nullable Object b) {
|
|
|
|
return a == b || (a != null && a.equals(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int hashCode(@Nullable Object... objects) {
|
|
|
|
return Arrays.hashCode(objects);
|
|
|
|
}
|
2015-06-04 21:19:10 +00:00
|
|
|
|
2017-11-26 18:45:39 +00:00
|
|
|
public static @Nullable Uri uri(@Nullable String uri) {
|
|
|
|
if (uri == null) return null;
|
|
|
|
else return Uri.parse(uri);
|
|
|
|
}
|
|
|
|
|
2015-06-04 21:19:10 +00:00
|
|
|
@TargetApi(VERSION_CODES.KITKAT)
|
|
|
|
public static boolean isLowMemory(Context context) {
|
|
|
|
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
|
|
|
|
|
|
|
return (VERSION.SDK_INT >= VERSION_CODES.KITKAT && activityManager.isLowRamDevice()) ||
|
2017-07-21 23:01:32 +00:00
|
|
|
activityManager.getLargeMemoryClass() <= 64;
|
2015-06-04 21:19:10 +00:00
|
|
|
}
|
2015-07-17 19:50:00 +00:00
|
|
|
|
|
|
|
public static int clamp(int value, int min, int max) {
|
|
|
|
return Math.min(Math.max(value, min), max);
|
|
|
|
}
|
2015-07-24 20:22:28 +00:00
|
|
|
|
|
|
|
public static float clamp(float value, float min, float max) {
|
|
|
|
return Math.min(Math.max(value, min), max);
|
|
|
|
}
|
2016-11-15 03:50:29 +00:00
|
|
|
|
|
|
|
public static @Nullable String readTextFromClipboard(@NonNull Context context) {
|
2017-11-25 06:00:30 +00:00
|
|
|
{
|
2016-11-15 03:50:29 +00:00
|
|
|
ClipboardManager clipboardManager = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
|
|
|
|
|
|
|
|
if (clipboardManager.hasPrimaryClip() && clipboardManager.getPrimaryClip().getItemCount() > 0) {
|
|
|
|
return clipboardManager.getPrimaryClip().getItemAt(0).getText().toString();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void writeTextToClipboard(@NonNull Context context, @NonNull String text) {
|
2017-11-25 06:00:30 +00:00
|
|
|
{
|
2016-11-15 03:50:29 +00:00
|
|
|
ClipboardManager clipboardManager = (ClipboardManager)context.getSystemService(Context.CLIPBOARD_SERVICE);
|
|
|
|
clipboardManager.setPrimaryClip(ClipData.newPlainText("Safety numbers", text));
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 21:37:27 +00:00
|
|
|
|
|
|
|
public static int toIntExact(long value) {
|
|
|
|
if ((int)value != value) {
|
|
|
|
throw new ArithmeticException("integer overflow");
|
|
|
|
}
|
|
|
|
return (int)value;
|
|
|
|
}
|
|
|
|
|
2017-01-05 20:42:28 +00:00
|
|
|
public static boolean isStringEquals(String first, String second) {
|
|
|
|
if (first == null) return second == null;
|
|
|
|
return first.equals(second);
|
|
|
|
}
|
2016-11-09 17:37:40 +00:00
|
|
|
|
|
|
|
public static boolean isEquals(@Nullable Long first, long second) {
|
|
|
|
return first != null && first == second;
|
|
|
|
}
|
2017-03-28 19:05:30 +00:00
|
|
|
|
|
|
|
public static String getPrettyFileSize(long sizeBytes) {
|
|
|
|
if (sizeBytes <= 0) return "0";
|
|
|
|
|
|
|
|
String[] units = new String[]{"B", "kB", "MB", "GB", "TB"};
|
|
|
|
int digitGroups = (int) (Math.log10(sizeBytes) / Math.log10(1024));
|
|
|
|
|
|
|
|
return new DecimalFormat("#,##0.#").format(sizeBytes/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
|
|
|
|
}
|
2019-02-06 08:36:55 +00:00
|
|
|
|
|
|
|
private static Handler getHandler() {
|
|
|
|
if (handler == null) {
|
|
|
|
synchronized (Util.class) {
|
|
|
|
if (handler == null) {
|
|
|
|
handler = new Handler(Looper.getMainLooper());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return handler;
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|