From 94913a334ccdcb84b27f95e86299f8245daf51a4 Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Tue, 2 Feb 2021 15:10:23 +1100 Subject: [PATCH] clean --- .../conversation/ConversationActivity.java | 8 +- .../logsubmit/SubmitLogFragment.java | 4 +- .../org/thoughtcrime/securesms/util/Util.java | 91 ------------------- .../impl/JsonDataSerializerTest.java | 4 +- .../securesms/util/ListPartitionTest.java | 1 + .../thoughtcrime/securesms/util/UtilTest.java | 2 + .../org/session/libsession/utilities/Util.kt | 12 +++ 7 files changed, 23 insertions(+), 99 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActivity.java b/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActivity.java index b644f998f9..2dda54c36b 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActivity.java +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActivity.java @@ -194,7 +194,7 @@ import org.thoughtcrime.securesms.util.DateUtils; import org.thoughtcrime.securesms.util.MediaUtil; import org.thoughtcrime.securesms.util.PushCharacterCalculator; 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.linkpreview.LinkPreview; @@ -466,7 +466,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity return; } - if (!Util.isEmpty(composeText) || attachmentManager.isAttachmentPresent()) { + if (!org.thoughtcrime.securesms.util.Util.isEmpty(composeText) || attachmentManager.isAttachmentPresent()) { saveDraft(); attachmentManager.clear(glideRequests, false); silentlySetComposeText(""); @@ -1371,7 +1371,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity new AsyncTask() { @Override protected Boolean doInBackground(Void... params) { - return Util.isMmsCapable(ConversationActivity.this); + return org.thoughtcrime.securesms.util.Util.isMmsCapable(ConversationActivity.this); } @Override @@ -1723,7 +1723,7 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity private Drafts getDraftsForCurrentState() { Drafts drafts = new Drafts(); - if (!Util.isEmpty(composeText)) { + if (!org.thoughtcrime.securesms.util.Util.isEmpty(composeText)) { drafts.add(new Draft(Draft.TEXT, composeText.getTextTrimmed())); } diff --git a/app/src/main/java/org/thoughtcrime/securesms/logsubmit/SubmitLogFragment.java b/app/src/main/java/org/thoughtcrime/securesms/logsubmit/SubmitLogFragment.java index 572dd8ff5e..2609af8bb7 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/logsubmit/SubmitLogFragment.java +++ b/app/src/main/java/org/thoughtcrime/securesms/logsubmit/SubmitLogFragment.java @@ -55,7 +55,7 @@ import network.loki.messenger.R; import org.thoughtcrime.securesms.contactshare.SimpleTextWatcher; import org.thoughtcrime.securesms.logging.Log; 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 java.io.BufferedReader; @@ -507,7 +507,7 @@ public class SubmitLogFragment extends Fragment { .append(" ") .append(pm.getPackageInfo(context.getPackageName(), 0).versionName) .append(" (") - .append(Util.getManifestApkVersion(context)) + .append(org.thoughtcrime.securesms.util.Util.getManifestApkVersion(context)) .append(")\n"); } catch (PackageManager.NameNotFoundException nnfe) { builder.append("Unknown\n"); diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/Util.java b/app/src/main/java/org/thoughtcrime/securesms/util/Util.java index c0baa3f370..9415de0104 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/Util.java +++ b/app/src/main/java/org/thoughtcrime/securesms/util/Util.java @@ -44,81 +44,10 @@ import network.loki.messenger.BuildConfig; 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 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) { return value == null || value.getText() == null || TextUtils.isEmpty(value.getTextTrimmed()); } - public static boolean isEmpty(Collection collection) { - return collection == null || collection.isEmpty(); - } - - public static List> chunk(@NonNull List list, int chunkSize) { - List> chunks = new ArrayList<>(list.size() / chunkSize); - - for (int i = 0; i < list.size(); i += chunkSize) { - List 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 List> partition(List list, int partitionSize) { - List> results = new LinkedList<>(); - - for (int index=0;index @@ -148,24 +77,4 @@ public class Util { public static boolean isMmsCapable(Context 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; - } } diff --git a/app/src/test/java/org/thoughtcrime/securesms/jobmanager/impl/JsonDataSerializerTest.java b/app/src/test/java/org/thoughtcrime/securesms/jobmanager/impl/JsonDataSerializerTest.java index cd2a66e624..8b2c8ff4fb 100644 --- a/app/src/test/java/org/thoughtcrime/securesms/jobmanager/impl/JsonDataSerializerTest.java +++ b/app/src/test/java/org/thoughtcrime/securesms/jobmanager/impl/JsonDataSerializerTest.java @@ -1,8 +1,8 @@ package org.thoughtcrime.securesms.jobmanager.impl; import org.junit.Test; -import org.thoughtcrime.securesms.jobmanager.Data; -import org.thoughtcrime.securesms.util.Util; +import org.session.libsession.messaging.jobs.Data; +import org.session.libsession.utilities.Util; import java.io.IOException; diff --git a/app/src/test/java/org/thoughtcrime/securesms/util/ListPartitionTest.java b/app/src/test/java/org/thoughtcrime/securesms/util/ListPartitionTest.java index 1ef955a831..01988ee966 100644 --- a/app/src/test/java/org/thoughtcrime/securesms/util/ListPartitionTest.java +++ b/app/src/test/java/org/thoughtcrime/securesms/util/ListPartitionTest.java @@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.util; import org.junit.Test; import org.thoughtcrime.securesms.BaseUnitTest; +import org.session.libsession.utilities.Util; import java.util.LinkedList; import java.util.List; diff --git a/app/src/test/java/org/thoughtcrime/securesms/util/UtilTest.java b/app/src/test/java/org/thoughtcrime/securesms/util/UtilTest.java index b34fc5d95a..0d33dd996e 100644 --- a/app/src/test/java/org/thoughtcrime/securesms/util/UtilTest.java +++ b/app/src/test/java/org/thoughtcrime/securesms/util/UtilTest.java @@ -5,6 +5,8 @@ import org.junit.Test; import java.util.Arrays; import java.util.List; +import org.session.libsession.utilities.Util; + import static org.junit.Assert.assertEquals; public class UtilTest { diff --git a/libsession/src/main/java/org/session/libsession/utilities/Util.kt b/libsession/src/main/java/org/session/libsession/utilities/Util.kt index acf7d961d4..61f008e287 100644 --- a/libsession/src/main/java/org/session/libsession/utilities/Util.kt +++ b/libsession/src/main/java/org/session/libsession/utilities/Util.kt @@ -155,6 +155,18 @@ object Util { return sb.toString() } + @JvmStatic + fun chunk(list: List, chunkSize: Int): List> { + val chunks: MutableList> = 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 fun equals(a: Any?, b: Any?): Boolean { return a === b || a != null && a == b