This commit is contained in:
Ryan ZHAO 2021-02-02 15:10:23 +11:00
parent 809ef0b6bc
commit 94913a334c
7 changed files with 23 additions and 99 deletions

View File

@ -194,7 +194,7 @@ import org.thoughtcrime.securesms.util.DateUtils;
import org.thoughtcrime.securesms.util.MediaUtil; import org.thoughtcrime.securesms.util.MediaUtil;
import org.thoughtcrime.securesms.util.PushCharacterCalculator; import org.thoughtcrime.securesms.util.PushCharacterCalculator;
import org.session.libsession.utilities.ServiceUtil; import org.session.libsession.utilities.ServiceUtil;
import org.thoughtcrime.securesms.util.Util; // Cannot be modified import org.session.libsession.utilities.Util;
import org.session.libsession.messaging.sending_receiving.contacts.Contact; import org.session.libsession.messaging.sending_receiving.contacts.Contact;
import org.session.libsession.messaging.sending_receiving.linkpreview.LinkPreview; import org.session.libsession.messaging.sending_receiving.linkpreview.LinkPreview;
@ -466,7 +466,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
return; return;
} }
if (!Util.isEmpty(composeText) || attachmentManager.isAttachmentPresent()) { if (!org.thoughtcrime.securesms.util.Util.isEmpty(composeText) || attachmentManager.isAttachmentPresent()) {
saveDraft(); saveDraft();
attachmentManager.clear(glideRequests, false); attachmentManager.clear(glideRequests, false);
silentlySetComposeText(""); silentlySetComposeText("");
@ -1371,7 +1371,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
new AsyncTask<Void, Void, Boolean>() { new AsyncTask<Void, Void, Boolean>() {
@Override @Override
protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) {
return Util.isMmsCapable(ConversationActivity.this); return org.thoughtcrime.securesms.util.Util.isMmsCapable(ConversationActivity.this);
} }
@Override @Override
@ -1723,7 +1723,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity
private Drafts getDraftsForCurrentState() { private Drafts getDraftsForCurrentState() {
Drafts drafts = new Drafts(); Drafts drafts = new Drafts();
if (!Util.isEmpty(composeText)) { if (!org.thoughtcrime.securesms.util.Util.isEmpty(composeText)) {
drafts.add(new Draft(Draft.TEXT, composeText.getTextTrimmed())); drafts.add(new Draft(Draft.TEXT, composeText.getTextTrimmed()));
} }

View File

@ -55,7 +55,7 @@ import network.loki.messenger.R;
import org.thoughtcrime.securesms.contactshare.SimpleTextWatcher; import org.thoughtcrime.securesms.contactshare.SimpleTextWatcher;
import org.thoughtcrime.securesms.logging.Log; import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.logsubmit.util.Scrubber; import org.thoughtcrime.securesms.logsubmit.util.Scrubber;
import org.thoughtcrime.securesms.util.Util; import org.session.libsession.utilities.Util;
import org.session.libsession.utilities.task.ProgressDialogAsyncTask; import org.session.libsession.utilities.task.ProgressDialogAsyncTask;
import java.io.BufferedReader; import java.io.BufferedReader;
@ -507,7 +507,7 @@ public class SubmitLogFragment extends Fragment {
.append(" ") .append(" ")
.append(pm.getPackageInfo(context.getPackageName(), 0).versionName) .append(pm.getPackageInfo(context.getPackageName(), 0).versionName)
.append(" (") .append(" (")
.append(Util.getManifestApkVersion(context)) .append(org.thoughtcrime.securesms.util.Util.getManifestApkVersion(context))
.append(")\n"); .append(")\n");
} catch (PackageManager.NameNotFoundException nnfe) { } catch (PackageManager.NameNotFoundException nnfe) {
builder.append("Unknown\n"); builder.append("Unknown\n");

View File

@ -44,81 +44,10 @@ import network.loki.messenger.BuildConfig;
public class Util { public class Util {
private static volatile Handler handler;
public static String join(String[] list, String delimiter) {
return join(Arrays.asList(list), delimiter);
}
public static String join(Collection<String> list, String delimiter) {
StringBuilder result = new StringBuilder();
int i = 0;
for (String item : list) {
result.append(item);
if (++i < list.size())
result.append(delimiter);
}
return result.toString();
}
public static boolean isEmpty(ComposeText value) { public static boolean isEmpty(ComposeText value) {
return value == null || value.getText() == null || TextUtils.isEmpty(value.getTextTrimmed()); return value == null || value.getText() == null || TextUtils.isEmpty(value.getTextTrimmed());
} }
public static boolean isEmpty(Collection collection) {
return collection == null || collection.isEmpty();
}
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;
}
public static boolean isOwnNumber(Context context, Address address) {
if (address.isGroup()) return false;
return TextSecurePreferences.getLocalNumber(context).equals(address.serialize());
}
public static byte[] readFully(InputStream in) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int read;
while ((read = in.read(buffer)) != -1) {
bout.write(buffer, 0, read);
}
in.close();
return bout.toByteArray();
}
public static String readFullyAsString(InputStream in) throws IOException {
return new String(readFully(in));
}
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) {
int subListSize = Math.min(partitionSize, list.size() - index);
results.add(list.subList(index, index + subListSize));
}
return results;
}
/** /**
* The app version. * The app version.
* <p> * <p>
@ -148,24 +77,4 @@ public class Util {
public static boolean isMmsCapable(Context context) { public static boolean isMmsCapable(Context context) {
return (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) || OutgoingLegacyMmsConnection.isConnectionPossible(context); return (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) || OutgoingLegacyMmsConnection.isConnectionPossible(context);
} }
public static boolean isMainThread() {
return Looper.myLooper() == Looper.getMainLooper();
}
public static void runOnMain(final @NonNull Runnable runnable) {
if (isMainThread()) runnable.run();
else getHandler().post(runnable);
}
private static Handler getHandler() {
if (handler == null) {
synchronized (Util.class) {
if (handler == null) {
handler = new Handler(Looper.getMainLooper());
}
}
}
return handler;
}
} }

View File

@ -1,8 +1,8 @@
package org.thoughtcrime.securesms.jobmanager.impl; package org.thoughtcrime.securesms.jobmanager.impl;
import org.junit.Test; import org.junit.Test;
import org.thoughtcrime.securesms.jobmanager.Data; import org.session.libsession.messaging.jobs.Data;
import org.thoughtcrime.securesms.util.Util; import org.session.libsession.utilities.Util;
import java.io.IOException; import java.io.IOException;

View File

@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.util;
import org.junit.Test; import org.junit.Test;
import org.thoughtcrime.securesms.BaseUnitTest; import org.thoughtcrime.securesms.BaseUnitTest;
import org.session.libsession.utilities.Util;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;

View File

@ -5,6 +5,8 @@ import org.junit.Test;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.session.libsession.utilities.Util;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class UtilTest { public class UtilTest {

View File

@ -155,6 +155,18 @@ object Util {
return sb.toString() return sb.toString()
} }
@JvmStatic
fun <E> chunk(list: List<E>, chunkSize: Int): List<List<E>> {
val chunks: MutableList<List<E>> = ArrayList(list.size / chunkSize)
var i = 0
while (i < list.size) {
val chunk = list.subList(i, Math.min(list.size, i + chunkSize))
chunks.add(chunk)
i += chunkSize
}
return chunks
}
@JvmStatic @JvmStatic
fun equals(a: Any?, b: Any?): Boolean { fun equals(a: Any?, b: Any?): Boolean {
return a === b || a != null && a == b return a === b || a != null && a == b