use annotations for android sdk constraints

This commit is contained in:
Vincent Breitmoser
2015-09-28 18:50:20 +02:00
parent 0e613aff2e
commit c630fe651f
4 changed files with 36 additions and 42 deletions

View File

@@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ClipDescription;
import android.content.Context;
@@ -36,6 +37,7 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.widget.DefaultItemAnimator;
@@ -249,6 +251,7 @@ public class DecryptListFragment
}
}
@TargetApi(VERSION_CODES.KITKAT)
private void saveFileDialog(InputDataResult result, int index) {
Activity activity = getActivity();
@@ -265,8 +268,8 @@ public class DecryptListFragment
filename = "decrypted" + (ext != null ? "."+ext : "");
}
FileHelper.saveDocument(this, filename, metadata.getMimeType(),
REQUEST_CODE_OUTPUT);
// requires >=kitkat
FileHelper.saveDocument(this, filename, metadata.getMimeType(), REQUEST_CODE_OUTPUT);
}
private void saveFile(Uri saveUri) {

View File

@@ -26,6 +26,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
@@ -35,6 +36,7 @@ import android.graphics.Bitmap;
import android.graphics.Point;
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.DefaultItemAnimator;
@@ -217,6 +219,7 @@ public class EncryptFilesFragment
mSelectedFiles.requestFocus();
}
@TargetApi(VERSION_CODES.KITKAT)
private void showOutputFileDialog() {
if (mFilesAdapter.getModelCount() != 1) {
throw new IllegalStateException();
@@ -225,8 +228,7 @@ public class EncryptFilesFragment
String targetName =
(mEncryptFilenames ? "1" : FileHelper.getFilename(getActivity(), model.inputUri))
+ (mUseArmor ? Constants.FILE_EXTENSION_ASC : Constants.FILE_EXTENSION_PGP_MAIN);
FileHelper.saveDocument(this, targetName,
REQUEST_CODE_OUTPUT);
FileHelper.saveDocument(this, targetName, REQUEST_CODE_OUTPUT);
}
public void addFile(Intent data) {

View File

@@ -133,10 +133,7 @@ public class LinkedIdCreateHttpsStep2Fragment extends LinkedIdCreateFinalFragmen
String targetName = "pgpkey.txt";
// TODO: not supported on Android < 4.4
FileHelper.saveDocument(this,
targetName,
"text/plain",
REQUEST_CODE_OUTPUT);
FileHelper.saveDocument(this, targetName, "text/plain", REQUEST_CODE_OUTPUT);
}
private void saveFile(Uri uri) {

View File

@@ -17,6 +17,16 @@
package org.sufficientlysecure.keychain.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import android.annotation.TargetApi;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
@@ -31,21 +41,11 @@ import android.os.Build.VERSION_CODES;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.OpenableColumns;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.widget.Toast;
import org.sufficientlysecure.keychain.R;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
/** This class offers a number of helper functions for saving documents.
*
@@ -73,6 +73,22 @@ import java.text.DecimalFormat;
*/
public class FileHelper {
@TargetApi(VERSION_CODES.KITKAT)
public static void saveDocument(Fragment fragment, String targetName, int requestCode) {
saveDocument(fragment, targetName, "*/*", requestCode);
}
/** Opens the storage browser on Android 4.4 or later for saving a file. */
@TargetApi(VERSION_CODES.KITKAT)
public static void saveDocument(Fragment fragment, String mimeType, String suggestedName, int requestCode) {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(mimeType);
intent.putExtra("android.content.extra.SHOW_ADVANCED", true); // Note: This is not documented, but works
intent.putExtra(Intent.EXTRA_TITLE, suggestedName);
fragment.startActivityForResult(intent, requestCode);
}
public static void openDocument(Fragment fragment, Uri last, String mimeType, boolean multiple, int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
openDocumentKitKat(fragment, mimeType, multiple, requestCode);
@@ -81,19 +97,6 @@ public class FileHelper {
}
}
public static void saveDocument(Fragment fragment, String targetName, int requestCode) {
saveDocument(fragment, targetName, "*/*", requestCode);
}
public static void saveDocument(Fragment fragment, String targetName, String mimeType,
int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
saveDocumentKitKat(fragment, mimeType, targetName, requestCode);
} else {
throw new RuntimeException("saveDocument does not support Android < 4.4!");
}
}
/** Opens the preferred installed file manager on Android and shows a toast
* if no manager is installed. */
private static void openDocumentPreKitKat(
@@ -127,17 +130,6 @@ public class FileHelper {
fragment.startActivityForResult(intent, requestCode);
}
/** Opens the storage browser on Android 4.4 or later for saving a file. */
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void saveDocumentKitKat(Fragment fragment, String mimeType, String suggestedName, int requestCode) {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(mimeType);
intent.putExtra("android.content.extra.SHOW_ADVANCED", true); // Note: This is not documented, but works
intent.putExtra(Intent.EXTRA_TITLE, suggestedName);
fragment.startActivityForResult(intent, requestCode);
}
public static String getFilename(Context context, Uri uri) {
String filename = null;
try {