Move most of Util into library

This commit is contained in:
Moxie Marlinspike 2013-07-09 19:48:33 -07:00
parent 21eee19380
commit a200d29514
31 changed files with 92 additions and 95 deletions

View File

@ -1,6 +1,4 @@
package org.thoughtcrime.securesms.util;
import java.io.IOException;
package org.whispersystems.textsecure.util;
/**
* <p>Encodes and decodes to and from Base64 notation.</p>

View File

@ -0,0 +1,58 @@
package org.whispersystems.textsecure.util;
import android.app.AlertDialog;
import android.content.Context;
import android.widget.EditText;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class Util {
public static boolean isEmpty(String value) {
return value == null || value.trim().length() == 0;
}
public static boolean isEmpty(EditText value) {
return value == null || value.getText() == null || isEmpty(value.getText().toString());
}
public static boolean isEmpty(CharSequence value) {
return value == null || value.length() == 0;
}
public static void showAlertDialog(Context context, String title, String message) {
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(title);
dialog.setMessage(message);
dialog.setIcon(android.R.drawable.ic_dialog_alert);
dialog.setPositiveButton(android.R.string.ok, null);
dialog.show();
}
public static String getSecret(int size) {
try {
byte[] secret = new byte[size];
SecureRandom.getInstance("SHA1PRNG").nextBytes(secret);
return Base64.encodeBytes(secret);
} catch (NoSuchAlgorithmException nsae) {
throw new AssertionError(nsae);
}
}
public static String 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 new String(bout.toByteArray());
}
}

View File

@ -29,7 +29,6 @@ import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.telephony.PhoneNumberUtils;
import android.text.Editable;
@ -90,7 +89,7 @@ import org.thoughtcrime.securesms.util.EncryptedCharacterCalculator;
import org.thoughtcrime.securesms.util.InvalidMessageException;
import org.thoughtcrime.securesms.util.MemoryCleaner;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.util.Util;
import java.io.IOException;
import java.util.LinkedList;

View File

@ -21,7 +21,7 @@ import android.os.Bundle;
import android.widget.Toast;
import org.thoughtcrime.securesms.crypto.SerializableKey;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
import org.thoughtcrime.securesms.util.Dialogs;
import org.thoughtcrime.securesms.util.DynamicTheme;

View File

@ -16,10 +16,8 @@
*/
package org.thoughtcrime.securesms;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
@ -30,8 +28,8 @@ import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
import org.thoughtcrime.securesms.util.MemoryCleaner;
import org.thoughtcrime.securesms.util.Util;
import org.thoughtcrime.securesms.util.VersionTracker;
import org.whispersystems.textsecure.util.Util;
/**
* Activity for creating a user's local encryption passphrase.

View File

@ -24,8 +24,8 @@ import com.google.i18n.phonenumbers.AsYouTypeFormatter;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.util.PhoneNumberFormatter;
import org.whispersystems.textsecure.util.Util;
/**
* The register account activity. Begins the account registration process.

View File

@ -32,8 +32,8 @@ import com.actionbarsherlock.app.SherlockActivity;
import org.thoughtcrime.securesms.gcm.PushServiceSocket;
import org.thoughtcrime.securesms.gcm.RateLimitException;
import org.thoughtcrime.securesms.service.RegistrationService;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.util.PhoneNumberFormatter;
import org.whispersystems.textsecure.util.Util;
import java.io.IOException;

View File

@ -36,7 +36,7 @@ import android.util.Log;
import org.thoughtcrime.securesms.crypto.IdentityKey;
import org.thoughtcrime.securesms.crypto.InvalidKeyException;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
import java.io.IOException;
import java.lang.Long;

View File

@ -26,7 +26,7 @@ import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.agreement.ECDHBasicAgreement;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
import org.thoughtcrime.securesms.util.Conversions;
import org.thoughtcrime.securesms.util.InvalidMessageException;

View File

@ -30,7 +30,7 @@ import org.thoughtcrime.bouncycastle.asn1.ASN1Object;
import org.thoughtcrime.bouncycastle.asn1.ASN1Sequence;
import org.thoughtcrime.bouncycastle.asn1.DERInteger;
import org.thoughtcrime.bouncycastle.asn1.DERSequence;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
import org.thoughtcrime.securesms.util.Combiner;
import org.thoughtcrime.securesms.util.Conversions;

View File

@ -17,13 +17,11 @@
package org.thoughtcrime.securesms.crypto;
import android.content.Context;
import android.preference.PreferenceManager;
import android.util.Log;
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
import org.thoughtcrime.securesms.database.keys.LocalKeyRecord;
import org.thoughtcrime.securesms.protocol.Message;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
import org.thoughtcrime.securesms.util.Conversions;
import java.io.IOException;

View File

@ -33,7 +33,7 @@ import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
import org.thoughtcrime.securesms.util.Hex;
import org.thoughtcrime.securesms.util.InvalidMessageException;

View File

@ -23,7 +23,7 @@ import android.util.Log;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
import java.io.IOException;
import java.security.GeneralSecurityException;

View File

@ -31,9 +31,9 @@ import org.thoughtcrime.securesms.crypto.MasterCipher;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.keys.SessionRecord;
import org.thoughtcrime.securesms.notifications.MessageNotifier;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
import org.thoughtcrime.securesms.util.InvalidMessageException;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.util.Util;
import java.io.File;
import java.io.FileInputStream;

View File

@ -31,7 +31,7 @@ import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
import java.io.IOException;

View File

@ -44,7 +44,7 @@ import org.thoughtcrime.securesms.util.InvalidMessageException;
import org.thoughtcrime.securesms.util.LRUCache;
import org.thoughtcrime.securesms.util.ListenableFutureTask;
import org.thoughtcrime.securesms.util.Trimmer;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.util.Util;
import java.io.UnsupportedEncodingException;
import java.lang.ref.SoftReference;
@ -133,7 +133,7 @@ public class MmsDatabase extends Database implements MmsSmsColumns {
DELIVERY_TIME, DELIVERY_REPORT, BODY, PART_COUNT, ADDRESS
};
public static final ExecutorService slideResolver = Util.newSingleThreadedLifoExecutor();
public static final ExecutorService slideResolver = org.thoughtcrime.securesms.util.Util.newSingleThreadedLifoExecutor();
private static final Map<Long, SoftReference<SlideDeck>> slideCache =
Collections.synchronizedMap(new LRUCache<Long, SoftReference<SlideDeck>>(20));
@ -362,7 +362,7 @@ public class MmsDatabase extends Database implements MmsSmsColumns {
ContentValues contentValues = getContentValuesFromHeader(headers);
boolean unread = Util.isDefaultSmsProvider(context) || ((mailbox & Types.SECURE_MESSAGE_BIT) != 0);
if (!Util.isEmpty(retrieved.getCc())) {
if (!org.thoughtcrime.securesms.util.Util.isEmpty(retrieved.getCc())) {
try {
threadId = getThreadIdFor(retrieved);
} catch (RecipientFormattingException e) {
@ -773,10 +773,10 @@ public class MmsDatabase extends Database implements MmsSmsColumns {
byte[]transactionIdBytes = null;
if (!Util.isEmpty(contentLocation))
contentLocationBytes = Util.toIsoBytes(contentLocation);
contentLocationBytes = org.thoughtcrime.securesms.util.Util.toIsoBytes(contentLocation);
if (!Util.isEmpty(transactionId))
transactionIdBytes = Util.toIsoBytes(transactionId);
transactionIdBytes = org.thoughtcrime.securesms.util.Util.toIsoBytes(transactionId);
return new NotificationMmsMessageRecord(context, id, recipients, recipients.getPrimaryRecipient(),

View File

@ -37,7 +37,7 @@ import org.thoughtcrime.securesms.sms.IncomingKeyExchangeMessage;
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
import org.thoughtcrime.securesms.sms.OutgoingTextMessage;
import org.thoughtcrime.securesms.util.Trimmer;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.util.Util;
import java.util.LinkedList;
import java.util.List;

View File

@ -32,7 +32,7 @@ import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientFactory;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.InvalidMessageException;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.util.Util;
import java.util.Arrays;
import java.util.HashSet;

View File

@ -24,7 +24,7 @@ import android.text.style.StyleSpan;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.database.SmsDatabase;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.util.Util;
/**
* The message record model which represents thread heading messages.

View File

@ -13,7 +13,7 @@ import org.thoughtcrime.securesms.service.RegistrationService;
import org.thoughtcrime.securesms.service.SendReceiveService;
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.util.Util;
import java.io.IOException;
import java.util.ArrayList;

View File

@ -9,7 +9,7 @@ import com.google.thoughtcrimegson.Gson;
import org.thoughtcrime.securesms.Release;
import org.thoughtcrime.securesms.directory.DirectoryDescriptor;
import org.thoughtcrime.securesms.directory.NumberFilter;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.util.Util;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;

View File

@ -17,13 +17,11 @@
package org.thoughtcrime.securesms.mms;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.http.AndroidHttpClient;
import android.preference.PreferenceManager;
import android.util.Log;
import org.apache.http.HttpEntity;
@ -32,12 +30,11 @@ import org.apache.http.conn.params.ConnRouteParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.service.MmsDownloader;
import org.thoughtcrime.securesms.util.Conversions;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.util.Util;
import java.io.DataInputStream;
import java.io.IOException;

View File

@ -28,7 +28,7 @@ import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.thoughtcrime.securesms.service.MmscProcessor;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.util.Util;
import java.io.IOException;
import java.net.URI;

View File

@ -19,7 +19,7 @@ package org.thoughtcrime.securesms.mms;
import java.io.IOException;
import org.thoughtcrime.securesms.crypto.TransportDetails;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
public class TextTransport implements TransportDetails {

View File

@ -16,7 +16,7 @@
*/
package org.thoughtcrime.securesms.protocol;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

View File

@ -19,7 +19,7 @@ import org.thoughtcrime.securesms.gcm.GcmIntentService;
import org.thoughtcrime.securesms.gcm.GcmRegistrationTimeoutException;
import org.thoughtcrime.securesms.gcm.PushServiceSocket;
import org.thoughtcrime.securesms.gcm.RateLimitException;
import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.util.Util;
import java.io.IOException;
import java.util.concurrent.ExecutorService;

View File

@ -18,7 +18,7 @@ package org.thoughtcrime.securesms.sms;
import android.util.Log;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
import java.io.IOException;
import java.util.ArrayList;

View File

@ -5,7 +5,7 @@ import android.util.Log;
import org.thoughtcrime.securesms.protocol.KeyExchangeWirePrefix;
import org.thoughtcrime.securesms.protocol.SecureMessageWirePrefix;
import org.thoughtcrime.securesms.protocol.WirePrefix;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
import org.thoughtcrime.securesms.util.Conversions;
import java.io.IOException;

View File

@ -21,7 +21,7 @@ import android.util.Log;
import org.thoughtcrime.securesms.crypto.SessionCipher;
import org.thoughtcrime.securesms.crypto.TransportDetails;
import org.thoughtcrime.securesms.protocol.WirePrefix;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
import java.io.IOException;

View File

@ -1,7 +1,7 @@
package org.thoughtcrime.securesms.transport;
import org.thoughtcrime.securesms.crypto.TransportDetails;
import org.thoughtcrime.securesms.util.Base64;
import org.whispersystems.textsecure.util.Base64;
import java.io.IOException;

View File

@ -16,8 +16,6 @@
*/
package org.thoughtcrime.securesms.util;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
@ -27,12 +25,7 @@ import android.widget.EditText;
import android.os.Build;
import android.provider.Telephony;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@ -100,18 +93,6 @@ public class Util {
return executor;
}
public static boolean isEmpty(String value) {
return value == null || value.trim().length() == 0;
}
public static boolean isEmpty(EditText value) {
return value == null || value.getText() == null || isEmpty(value.getText().toString());
}
public static boolean isEmpty(CharSequence value) {
return value == null || value.length() == 0;
}
public static boolean isEmpty(EncodedStringValue[] value) {
return value == null || value.length == 0;
}
@ -153,38 +134,6 @@ public class Util {
}
}
public static void showAlertDialog(Context context, String title, String message) {
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(title);
dialog.setMessage(message);
dialog.setIcon(android.R.drawable.ic_dialog_alert);
dialog.setPositiveButton(android.R.string.ok, null);
dialog.show();
}
public static String getSecret(int size) {
try {
byte[] secret = new byte[size];
SecureRandom.getInstance("SHA1PRNG").nextBytes(secret);
return Base64.encodeBytes(secret);
} catch (NoSuchAlgorithmException nsae) {
throw new AssertionError(nsae);
}
}
public static String 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 new String(bout.toByteArray());
}
public static boolean isDefaultSmsProvider(Context context){
return (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) ||