From 704ab37cadb2a071698b4bdf7e44f1fc75963db1 Mon Sep 17 00:00:00 2001 From: Ryan ZHAO Date: Tue, 23 Feb 2021 16:35:36 +1100 Subject: [PATCH] clean up unused utils --- .../securesms/jobs/PushMediaSendJob.java | 1 - .../securesms/jobs/PushTextSendJob.java | 1 - .../InsecureFallbackApprovalException.java | 11 -- .../transport/RetryLaterException.java | 2 - .../UndeliverableMessageException.java | 6 - .../securesms/util/CharacterCalculator.java | 9 - .../securesms/util/CommunicationActions.java | 12 -- .../securesms/util/FileProviderUtil.java | 4 - .../securesms/util/MediaUtil.java | 8 - .../securesms/util/MemoryFileUtil.java | 1 - .../securesms/util/PowerManagerCompat.java | 15 -- .../securesms/util/RedPhoneCallTypes.java | 23 --- .../util/SelectedRecipientsAdapter.java | 165 ------------------ .../securesms/util/TaggedFutureTask.java | 43 ----- .../securesms/util/TelephonyUtil.java | 48 ----- .../org/thoughtcrime/securesms/util/Util.java | 20 --- .../securesms/util/WakeLockUtil.java | 17 -- .../util/dualsim/SubscriptionInfoCompat.java | 35 ---- .../dualsim/SubscriptionManagerCompat.java | 152 ---------------- 19 files changed, 573 deletions(-) delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/transport/InsecureFallbackApprovalException.java delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/util/PowerManagerCompat.java delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/util/RedPhoneCallTypes.java delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/util/SelectedRecipientsAdapter.java delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/util/TaggedFutureTask.java delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/util/TelephonyUtil.java delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/util/dualsim/SubscriptionInfoCompat.java delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/util/dualsim/SubscriptionManagerCompat.java diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/PushMediaSendJob.java b/app/src/main/java/org/thoughtcrime/securesms/jobs/PushMediaSendJob.java index 9fcc30cdb7..5543739c28 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/PushMediaSendJob.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/PushMediaSendJob.java @@ -29,7 +29,6 @@ import org.thoughtcrime.securesms.mms.MmsException; import org.thoughtcrime.securesms.mms.OutgoingMediaMessage; import org.session.libsession.messaging.threads.recipients.Recipient; import org.thoughtcrime.securesms.service.ExpiringMessageManager; -import org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException; import org.thoughtcrime.securesms.transport.RetryLaterException; import org.thoughtcrime.securesms.transport.UndeliverableMessageException; import org.session.libsignal.libsignal.util.guava.Optional; diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/PushTextSendJob.java b/app/src/main/java/org/thoughtcrime/securesms/jobs/PushTextSendJob.java index 07fb242246..de8953f44e 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/PushTextSendJob.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/PushTextSendJob.java @@ -22,7 +22,6 @@ import org.thoughtcrime.securesms.dependencies.InjectableType; import org.thoughtcrime.securesms.jobmanager.Job; import org.thoughtcrime.securesms.loki.database.LokiMessageDatabase; import org.thoughtcrime.securesms.service.ExpiringMessageManager; -import org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException; import org.thoughtcrime.securesms.transport.RetryLaterException; import org.session.libsignal.libsignal.util.guava.Optional; import org.session.libsignal.service.api.SignalServiceMessageSender; diff --git a/app/src/main/java/org/thoughtcrime/securesms/transport/InsecureFallbackApprovalException.java b/app/src/main/java/org/thoughtcrime/securesms/transport/InsecureFallbackApprovalException.java deleted file mode 100644 index 15651696f9..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/transport/InsecureFallbackApprovalException.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.thoughtcrime.securesms.transport; - -public class InsecureFallbackApprovalException extends Exception { - public InsecureFallbackApprovalException(String detailMessage) { - super(detailMessage); - } - - public InsecureFallbackApprovalException(Throwable e) { - super(e); - } -} diff --git a/app/src/main/java/org/thoughtcrime/securesms/transport/RetryLaterException.java b/app/src/main/java/org/thoughtcrime/securesms/transport/RetryLaterException.java index 5117b45b8e..626bbafcdf 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/transport/RetryLaterException.java +++ b/app/src/main/java/org/thoughtcrime/securesms/transport/RetryLaterException.java @@ -1,7 +1,5 @@ package org.thoughtcrime.securesms.transport; -import java.io.IOException; - public class RetryLaterException extends Exception { public RetryLaterException() {} diff --git a/app/src/main/java/org/thoughtcrime/securesms/transport/UndeliverableMessageException.java b/app/src/main/java/org/thoughtcrime/securesms/transport/UndeliverableMessageException.java index 02987b1cdb..49e0f743dc 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/transport/UndeliverableMessageException.java +++ b/app/src/main/java/org/thoughtcrime/securesms/transport/UndeliverableMessageException.java @@ -1,17 +1,11 @@ package org.thoughtcrime.securesms.transport; public class UndeliverableMessageException extends Exception { - public UndeliverableMessageException() { - } public UndeliverableMessageException(String detailMessage) { super(detailMessage); } - public UndeliverableMessageException(String detailMessage, Throwable throwable) { - super(detailMessage, throwable); - } - public UndeliverableMessageException(Throwable throwable) { super(throwable); } diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/CharacterCalculator.java b/app/src/main/java/org/thoughtcrime/securesms/util/CharacterCalculator.java index 8bdf2d0c93..08080ffc3f 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/CharacterCalculator.java +++ b/app/src/main/java/org/thoughtcrime/securesms/util/CharacterCalculator.java @@ -23,15 +23,6 @@ public abstract class CharacterCalculator { public abstract CharacterState calculateCharacters(String messageBody); - public static CharacterCalculator readFromParcel(@NonNull Parcel in) { - switch (in.readInt()) { - case 1: return new SmsCharacterCalculator(); - case 2: return new MmsCharacterCalculator(); - case 3: return new PushCharacterCalculator(); - default: throw new IllegalArgumentException("Read an unsupported value for a calculator."); - } - } - public static void writeToParcel(@NonNull Parcel dest, @NonNull CharacterCalculator calculator) { if (calculator instanceof SmsCharacterCalculator) { dest.writeInt(1); diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/CommunicationActions.java b/app/src/main/java/org/thoughtcrime/securesms/util/CommunicationActions.java index 586da50de6..2487d8dba0 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/CommunicationActions.java +++ b/app/src/main/java/org/thoughtcrime/securesms/util/CommunicationActions.java @@ -20,10 +20,6 @@ import org.session.libsession.messaging.threads.recipients.Recipient; public class CommunicationActions { - public static void startConversation(@NonNull Context context, @NonNull Recipient recipient, @Nullable String text) { - startConversation(context, recipient, text, null); - } - public static void startConversation(@NonNull Context context, @NonNull Recipient recipient, @Nullable String text, @@ -56,14 +52,6 @@ public class CommunicationActions { }.execute(); } - public static void composeSmsThroughDefaultApp(@NonNull Context context, @NonNull Address address, @Nullable String text) { - Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + address.serialize())); - if (text != null) { - intent.putExtra("sms_body", text); - } - context.startActivity(intent); - } - public static void openBrowserLink(@NonNull Context context, @NonNull String link) { try { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(link)); diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/FileProviderUtil.java b/app/src/main/java/org/thoughtcrime/securesms/util/FileProviderUtil.java index 316476ff90..ed28c7c2e2 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/FileProviderUtil.java +++ b/app/src/main/java/org/thoughtcrime/securesms/util/FileProviderUtil.java @@ -20,10 +20,6 @@ public class FileProviderUtil { return Uri.fromFile(file); } - public static boolean isAuthority(@NonNull Uri uri) { - return AUTHORITY.equals(uri.getAuthority()); - } - public static boolean delete(@NonNull Context context, @NonNull Uri uri) { if (AUTHORITY.equals(uri.getAuthority())) { return context.getContentResolver().delete(uri, null, null) > 0; diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/MediaUtil.java b/app/src/main/java/org/thoughtcrime/securesms/util/MediaUtil.java index dee283588c..3c55996d64 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/MediaUtil.java +++ b/app/src/main/java/org/thoughtcrime/securesms/util/MediaUtil.java @@ -188,10 +188,6 @@ public class MediaUtil { return isVideoType(attachment.getContentType()); } - public static boolean isVideo(String contentType) { - return !TextUtils.isEmpty(contentType) && contentType.trim().startsWith("video/"); - } - public static boolean isVcard(String contentType) { return !TextUtils.isEmpty(contentType) && contentType.trim().equals(MediaTypes.VCARD); } @@ -208,10 +204,6 @@ public class MediaUtil { return !isGif(attachment) && !isImage(attachment) && !isAudio(attachment) && !isVideo(attachment); } - public static boolean isTextType(String contentType) { - return (null != contentType) && contentType.startsWith("text/"); - } - public static boolean isImageType(String contentType) { return (null != contentType) && contentType.startsWith("image/") diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/MemoryFileUtil.java b/app/src/main/java/org/thoughtcrime/securesms/util/MemoryFileUtil.java index 3c67478da5..4ee43e1e9c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/MemoryFileUtil.java +++ b/app/src/main/java/org/thoughtcrime/securesms/util/MemoryFileUtil.java @@ -1,7 +1,6 @@ package org.thoughtcrime.securesms.util; -import android.os.Build; import android.os.MemoryFile; import android.os.ParcelFileDescriptor; diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/PowerManagerCompat.java b/app/src/main/java/org/thoughtcrime/securesms/util/PowerManagerCompat.java deleted file mode 100644 index 6fa4ba027a..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/util/PowerManagerCompat.java +++ /dev/null @@ -1,15 +0,0 @@ -package org.thoughtcrime.securesms.util; - -import android.os.Build; -import android.os.PowerManager; -import androidx.annotation.NonNull; - -public class PowerManagerCompat { - - public static boolean isDeviceIdleMode(@NonNull PowerManager powerManager) { - if (Build.VERSION.SDK_INT >= 23) { - return powerManager.isDeviceIdleMode(); - } - return false; - } -} diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/RedPhoneCallTypes.java b/app/src/main/java/org/thoughtcrime/securesms/util/RedPhoneCallTypes.java deleted file mode 100644 index d5f1a136dc..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/util/RedPhoneCallTypes.java +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Copyright (C) 2011 Whisper Systems - * - * 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. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.thoughtcrime.securesms.util; - -public interface RedPhoneCallTypes { - public static final int INCOMING = 1023; - public static final int OUTGOING = 1024; - public static final int MISSED = 1025; -} diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/SelectedRecipientsAdapter.java b/app/src/main/java/org/thoughtcrime/securesms/util/SelectedRecipientsAdapter.java deleted file mode 100644 index 401777906a..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/util/SelectedRecipientsAdapter.java +++ /dev/null @@ -1,165 +0,0 @@ -package org.thoughtcrime.securesms.util; - -import android.content.Context; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.BaseAdapter; -import android.widget.ImageButton; -import android.widget.TextView; - -import network.loki.messenger.R; -import org.session.libsession.messaging.threads.recipients.Recipient; -import org.session.libsignal.libsignal.util.guava.Optional; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; - -public class SelectedRecipientsAdapter extends BaseAdapter { - @NonNull private Context context; - @Nullable private OnRecipientDeletedListener onRecipientDeletedListener; - @NonNull private List recipients; - - public SelectedRecipientsAdapter(@NonNull Context context) { - this(context, Collections.emptyList()); - } - - public SelectedRecipientsAdapter(@NonNull Context context, - @NonNull Collection existingRecipients) - { - this.context = context; - this.recipients = wrapExistingMembers(existingRecipients); - } - - public void add(@NonNull Recipient recipient, boolean isPush) { - if (!find(recipient).isPresent()) { - RecipientWrapper wrapper = new RecipientWrapper(recipient, true, isPush); - this.recipients.add(0, wrapper); - notifyDataSetChanged(); - } - } - - public Optional find(@NonNull Recipient recipient) { - RecipientWrapper found = null; - for (RecipientWrapper wrapper : recipients) { - if (wrapper.getRecipient().equals(recipient)) found = wrapper; - } - return Optional.fromNullable(found); - } - - public void remove(@NonNull Recipient recipient) { - Optional match = find(recipient); - if (match.isPresent()) { - recipients.remove(match.get()); - notifyDataSetChanged(); - } - } - - public Set getRecipients() { - final Set recipientSet = new HashSet<>(recipients.size()); - for (RecipientWrapper wrapper : recipients) { - recipientSet.add(wrapper.getRecipient()); - } - return recipientSet; - } - - @Override - public int getCount() { - return recipients.size(); - } - - public boolean hasNonPushMembers() { - for (RecipientWrapper wrapper : recipients) { - if (!wrapper.isPush()) return true; - } - return false; - } - - @Override - public Object getItem(int position) { - return recipients.get(position); - } - - @Override - public long getItemId(int position) { - return position; - } - - @Override - public View getView(final int position, View v, final ViewGroup parent) { - if (v == null) { - v = LayoutInflater.from(context).inflate(R.layout.selected_recipient_list_item, parent, false); - } - - final RecipientWrapper rw = (RecipientWrapper)getItem(position); - final Recipient p = rw.getRecipient(); - final boolean modifiable = rw.isModifiable(); - - TextView name = (TextView) v.findViewById(R.id.name); - TextView phone = (TextView) v.findViewById(R.id.phone); - ImageButton delete = (ImageButton) v.findViewById(R.id.delete); - - name.setText(p.getName()); - phone.setText(p.getAddress().serialize()); - delete.setVisibility(modifiable ? View.VISIBLE : View.GONE); - delete.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - if (onRecipientDeletedListener != null) { - onRecipientDeletedListener.onRecipientDeleted(recipients.get(position).getRecipient()); - } - } - }); - - return v; - } - - private static List wrapExistingMembers(Collection recipients) { - final LinkedList wrapperList = new LinkedList<>(); - for (Recipient recipient : recipients) { - wrapperList.add(new RecipientWrapper(recipient, false, true)); - } - return wrapperList; - } - - public void setOnRecipientDeletedListener(@Nullable OnRecipientDeletedListener listener) { - onRecipientDeletedListener = listener; - } - - public interface OnRecipientDeletedListener { - void onRecipientDeleted(Recipient recipient); - } - - public static class RecipientWrapper { - private final Recipient recipient; - private final boolean modifiable; - private final boolean push; - - public RecipientWrapper(final @NonNull Recipient recipient, - final boolean modifiable, - final boolean push) - { - this.recipient = recipient; - this.modifiable = modifiable; - this.push = push; - } - - public @NonNull Recipient getRecipient() { - return recipient; - } - - public boolean isModifiable() { - return modifiable; - } - - public boolean isPush() { - return push; - } - } -} \ No newline at end of file diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/TaggedFutureTask.java b/app/src/main/java/org/thoughtcrime/securesms/util/TaggedFutureTask.java deleted file mode 100644 index a20c13d433..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/util/TaggedFutureTask.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright (C) 2014 Open Whisper Systems - * - * 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. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.thoughtcrime.securesms.util; - -import java.util.concurrent.Callable; -import java.util.concurrent.FutureTask; - -/** - * FutureTask with a reference identifier tag. - * - * @author Jake McGinty - */ -public class TaggedFutureTask extends FutureTask { - private final Object tag; - public TaggedFutureTask(Runnable runnable, V result, Object tag) { - super(runnable, result); - this.tag = tag; - } - - public TaggedFutureTask(Callable callable, Object tag) { - super(callable); - this.tag = tag; - } - - public Object getTag() { - return tag; - } -} diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/TelephonyUtil.java b/app/src/main/java/org/thoughtcrime/securesms/util/TelephonyUtil.java deleted file mode 100644 index 71a78276d6..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/util/TelephonyUtil.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.thoughtcrime.securesms.util; - -import android.content.Context; -import android.content.res.Configuration; -import android.net.ConnectivityManager; -import androidx.annotation.NonNull; -import android.telephony.TelephonyManager; - -import org.session.libsignal.utilities.logging.Log; - -import java.util.Locale; - -public class TelephonyUtil { - private static final String TAG = TelephonyUtil.class.getSimpleName(); - - public static TelephonyManager getManager(final Context context) { - return (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); - } - - public static String getMccMnc(final Context context) { - final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); - final int configMcc = context.getResources().getConfiguration().mcc; - final int configMnc = context.getResources().getConfiguration().mnc; - if (tm.getSimState() == TelephonyManager.SIM_STATE_READY) { - Log.i(TAG, "Choosing MCC+MNC info from TelephonyManager.getSimOperator()"); - return tm.getSimOperator(); - } else if (tm.getPhoneType() != TelephonyManager.PHONE_TYPE_CDMA) { - Log.i(TAG, "Choosing MCC+MNC info from TelephonyManager.getNetworkOperator()"); - return tm.getNetworkOperator(); - } else if (configMcc != 0 && configMnc != 0) { - Log.i(TAG, "Choosing MCC+MNC info from current context's Configuration"); - return String.format(Locale.ROOT, "%03d%d", - configMcc, - configMnc == Configuration.MNC_ZERO ? 0 : configMnc); - } else { - return null; - } - } - - public static String getApn(final Context context) { - final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); - return cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS).getExtraInfo(); - } - - public static boolean isAnyPstnLineBusy(@NonNull Context context) { - return getManager(context).getCallState() != TelephonyManager.CALL_STATE_IDLE; - } -} 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 bd1802c839..2384fe482a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/Util.java +++ b/app/src/main/java/org/thoughtcrime/securesms/util/Util.java @@ -42,28 +42,8 @@ public class Util { return value == null || value.getText() == null || TextUtils.isEmpty(value.getTextTrimmed()); } - /** - * The app version. - *

- * This code should be used in all places that compare app versions rather than - * {@link #getManifestApkVersion(Context)} or {@link BuildConfig#VERSION_CODE}. - */ public static int getCanonicalVersionCode() { return BuildConfig.CANONICAL_VERSION_CODE; } - /** - * {@link BuildConfig#VERSION_CODE} may not be the actual version due to ABI split code adding a - * postfix after BuildConfig is generated. - *

- * However, in most cases you want to use {@link BuildConfig#CANONICAL_VERSION_CODE} via - * {@link #getCanonicalVersionCode()} - */ - public static int getManifestApkVersion(Context context) { - try { - return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode; - } catch (PackageManager.NameNotFoundException e) { - throw new AssertionError(e); - } - } } diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/WakeLockUtil.java b/app/src/main/java/org/thoughtcrime/securesms/util/WakeLockUtil.java index b876a084a4..c3e7654c88 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/util/WakeLockUtil.java +++ b/app/src/main/java/org/thoughtcrime/securesms/util/WakeLockUtil.java @@ -12,23 +12,6 @@ public class WakeLockUtil { private static final String TAG = WakeLockUtil.class.getSimpleName(); - /** - * Run a runnable with a wake lock. Ensures that the lock is safely acquired and released. - * - * @param tag will be prefixed with "signal:" if it does not already start with it. - */ - public static void runWithLock(@NonNull Context context, int lockType, long timeout, @NonNull String tag, @NonNull Runnable task) { - WakeLock wakeLock = null; - try { - wakeLock = acquire(context, lockType, timeout, tag); - task.run(); - } finally { - if (wakeLock != null) { - release(wakeLock, tag); - } - } - } - /** * @param tag will be prefixed with "signal:" if it does not already start with it. */ diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/dualsim/SubscriptionInfoCompat.java b/app/src/main/java/org/thoughtcrime/securesms/util/dualsim/SubscriptionInfoCompat.java deleted file mode 100644 index 4a2837668f..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/util/dualsim/SubscriptionInfoCompat.java +++ /dev/null @@ -1,35 +0,0 @@ -package org.thoughtcrime.securesms.util.dualsim; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -public class SubscriptionInfoCompat { - - private final int subscriptionId; - private final int mcc; - private final int mnc; - private final @Nullable CharSequence displayName; - - public SubscriptionInfoCompat(int subscriptionId, @Nullable CharSequence displayName, int mcc, int mnc) { - this.subscriptionId = subscriptionId; - this.displayName = displayName; - this.mcc = mcc; - this.mnc = mnc; - } - - public @NonNull CharSequence getDisplayName() { - return displayName != null ? displayName : ""; - } - - public int getSubscriptionId() { - return subscriptionId; - } - - public int getMnc() { - return mnc; - } - - public int getMcc() { - return mcc; - } -} diff --git a/app/src/main/java/org/thoughtcrime/securesms/util/dualsim/SubscriptionManagerCompat.java b/app/src/main/java/org/thoughtcrime/securesms/util/dualsim/SubscriptionManagerCompat.java deleted file mode 100644 index 03ba49f934..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/util/dualsim/SubscriptionManagerCompat.java +++ /dev/null @@ -1,152 +0,0 @@ -package org.thoughtcrime.securesms.util.dualsim; - -import android.content.Context; -import android.os.Build; -import androidx.annotation.NonNull; -import androidx.annotation.RequiresApi; -import android.telephony.SubscriptionInfo; -import android.telephony.SubscriptionManager; -import android.telephony.TelephonyManager; - -import network.loki.messenger.R; -import org.session.libsession.utilities.ServiceUtil; - -import org.session.libsignal.utilities.logging.Log; -import org.session.libsignal.libsignal.util.guava.Function; -import org.session.libsignal.libsignal.util.guava.Optional; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -public final class SubscriptionManagerCompat { - - private static final String TAG = SubscriptionManagerCompat.class.getSimpleName(); - - private final Context context; - - public SubscriptionManagerCompat(Context context) { - this.context = context.getApplicationContext(); - } - - public Optional getPreferredSubscriptionId() { - if (Build.VERSION.SDK_INT < 24) { - return Optional.absent(); - } - - return Optional.of(SubscriptionManager.getDefaultSmsSubscriptionId()); - } - - public Optional getActiveSubscriptionInfo(int subscriptionId) { - if (Build.VERSION.SDK_INT < 22) { - return Optional.absent(); - } - - return Optional.fromNullable(getActiveSubscriptionInfoMap(false).get(subscriptionId)); - } - - public @NonNull Collection getActiveAndReadySubscriptionInfos() { - if (Build.VERSION.SDK_INT < 22) { - return Collections.emptyList(); - } - - return getActiveSubscriptionInfoMap(true).values(); - } - - @RequiresApi(api = 22) - private @NonNull Map getActiveSubscriptionInfoMap(boolean excludeUnreadySubscriptions) { - List subscriptionInfos = getActiveSubscriptionInfoList(); - - if (subscriptionInfos.isEmpty()) { - return Collections.emptyMap(); - } - - Map descriptions = getDescriptionsFor(subscriptionInfos); - Map map = new LinkedHashMap<>(); - - for (SubscriptionInfo subscriptionInfo : subscriptionInfos) { - if (!excludeUnreadySubscriptions || isReady(subscriptionInfo)) { - map.put(subscriptionInfo.getSubscriptionId(), - new SubscriptionInfoCompat(subscriptionInfo.getSubscriptionId(), - descriptions.get(subscriptionInfo), - subscriptionInfo.getMcc(), - subscriptionInfo.getMnc())); - } - } - - return map; - } - - public boolean isMultiSim() { - if (Build.VERSION.SDK_INT < 22) { - return false; - } - - return getActiveSubscriptionInfoList().size() >= 2; - } - - @RequiresApi(api = 22) - private @NonNull List getActiveSubscriptionInfoList() { - SubscriptionManager subscriptionManager = ServiceUtil.getSubscriptionManager(context); - - if (subscriptionManager == null) { - Log.w(TAG, "Missing SubscriptionManager."); - return Collections.emptyList(); - } - - List list = subscriptionManager.getActiveSubscriptionInfoList(); - - return list != null? list : Collections.emptyList(); - } - - @RequiresApi(api = 22) - private Map getDescriptionsFor(@NonNull Collection subscriptions) { - Map descriptions; - - descriptions = createDescriptionMap(subscriptions, SubscriptionInfo::getDisplayName); - if (hasNoDuplicates(descriptions.values())) return descriptions; - - return createDescriptionMap(subscriptions, this::describeSimIndex); - } - - @RequiresApi(api = 22) - private String describeSimIndex(SubscriptionInfo info) { - return context.getString(R.string.conversation_activity__sim_n, info.getSimSlotIndex() + 1); - } - - private static Map createDescriptionMap(@NonNull Collection subscriptions, - @NonNull Function createDescription) - { - Map descriptions = new HashMap<>(); - for (SubscriptionInfo subscriptionInfo: subscriptions) { - descriptions.put(subscriptionInfo, createDescription.apply(subscriptionInfo)); - } - return descriptions; - } - - private static boolean hasNoDuplicates(Collection collection) { - final Set set = new HashSet<>(); - - for (T t : collection) { - if (!set.add(t)) { - return false; - } - } - return true; - } - - private boolean isReady(@NonNull SubscriptionInfo subscriptionInfo) { - if (Build.VERSION.SDK_INT < 24) return true; - - TelephonyManager telephonyManager = ServiceUtil.getTelephonyManager(context); - - TelephonyManager specificTelephonyManager = telephonyManager.createForSubscriptionId(subscriptionInfo.getSubscriptionId()); - - return specificTelephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY; - } -}