Media download controls

Closes #3991
// FREEBIE
This commit is contained in:
Jake McGinty
2015-08-24 15:24:31 -07:00
committed by Moxie Marlinspike
parent 7e2c9f97e9
commit 80ce83ef9d
54 changed files with 606 additions and 204 deletions

View File

@@ -3,6 +3,8 @@ package org.thoughtcrime.securesms.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;
@@ -108,6 +110,11 @@ public class MediaUtil {
return ContentType.isVideoType(Util.toIsoString(part.getContentType()));
}
public static @Nullable String getDiscreteMimeType(@NonNull PduPart part) {
final String[] sections = (Util.toIsoString(part.getContentType()).split("/", 2));
return sections.length > 1 ? sections[0] : null;
}
public static class ThumbnailData {
Bitmap bitmap;
float aspectRatio;

View File

@@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.util;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
@@ -13,4 +14,8 @@ public class ServiceUtil {
public static WindowManager getWindowManager(Context context) {
return (WindowManager) context.getSystemService(Activity.WINDOW_SERVICE);
}
public static ConnectivityManager getConnectivityManager(Context context) {
return (ConnectivityManager) context.getSystemService(Activity.CONNECTIVITY_SERVICE);
}
}

View File

@@ -4,11 +4,19 @@ import android.content.Context;
import android.os.Build;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.annotation.ArrayRes;
import android.support.annotation.NonNull;
import android.util.Log;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.preferences.NotificationPrivacyPreference;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class TextSecurePreferences {
@@ -74,6 +82,10 @@ public class TextSecurePreferences {
public static final String REPEAT_ALERTS_PREF = "pref_repeat_alerts";
public static final String NOTIFICATION_PRIVACY_PREF = "pref_notification_privacy";
public static final String MEDIA_DOWNLOAD_MOBILE_PREF = "pref_media_download_mobile";
public static final String MEDIA_DOWNLOAD_WIFI_PREF = "pref_media_download_wifi";
public static final String MEDIA_DOWNLOAD_ROAMING_PREF = "pref_media_download_roaming";
public static NotificationPrivacyPreference getNotificationPrivacy(Context context) {
return new NotificationPrivacyPreference(getStringPreference(context, NOTIFICATION_PRIVACY_PREF, "all"));
}
@@ -433,6 +445,25 @@ public class TextSecurePreferences {
return Integer.parseInt(getStringPreference(context, THREAD_TRIM_LENGTH, "500"));
}
public static @NonNull Set<String> getMobileMediaDownloadAllowed(Context context) {
return getMediaDownloadAllowed(context, MEDIA_DOWNLOAD_MOBILE_PREF, R.array.pref_media_download_mobile_data_default);
}
public static @NonNull Set<String> getWifiMediaDownloadAllowed(Context context) {
return getMediaDownloadAllowed(context, MEDIA_DOWNLOAD_WIFI_PREF, R.array.pref_media_download_wifi_default);
}
public static @NonNull Set<String> getRoamingMediaDownloadAllowed(Context context) {
return getMediaDownloadAllowed(context, MEDIA_DOWNLOAD_ROAMING_PREF, R.array.pref_media_download_roaming_default);
}
private static @NonNull Set<String> getMediaDownloadAllowed(Context context, String key, @ArrayRes int defaultValuesRes) {
return getStringSetPreference(context,
key,
new HashSet<>(Arrays.asList(context.getResources().getStringArray(defaultValuesRes))));
}
public static void setBooleanPreference(Context context, String key, boolean value) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(key, value).apply();
}
@@ -468,4 +499,8 @@ public class TextSecurePreferences {
private static void setLongPreference(Context context, String key, long value) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(key, value).apply();
}
private static Set<String> getStringSetPreference(Context context, String key, Set<String> defaultValues) {
return PreferenceManager.getDefaultSharedPreferences(context).getStringSet(key, defaultValues);
}
}

View File

@@ -28,6 +28,7 @@ import android.os.Build.VERSION_CODES;
import android.os.Handler;
import android.os.Looper;
import android.provider.Telephony;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.telephony.TelephonyManager;
import android.text.Spannable;
@@ -119,7 +120,7 @@ public class Util {
return spanned;
}
public static String toIsoString(byte[] bytes) {
public static @NonNull String toIsoString(byte[] bytes) {
try {
return new String(bytes, CharacterSets.MIMENAME_ISO_8859_1);
} catch (UnsupportedEncodingException e) {