mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-28 21:33:53 +01:00
Remove unused code from the app (#3276)
This commit is contained in:
parent
bb0a21929e
commit
2f9a71911a
27 changed files with 49 additions and 610 deletions
|
|
@ -2,20 +2,18 @@ package fr.free.nrw.commons.filepicker;
|
|||
|
||||
import android.app.Activity;
|
||||
import android.content.ClipData;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.net.Uri;
|
||||
import android.os.Parcelable;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.provider.MediaStore;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
|
@ -25,10 +23,8 @@ import java.util.List;
|
|||
|
||||
import static fr.free.nrw.commons.filepicker.PickedFiles.singleFileList;
|
||||
|
||||
@SuppressWarnings({"unused", "FieldCanBeLocal", "ResultOfMethodCallIgnored"})
|
||||
public class FilePicker implements Constants {
|
||||
|
||||
private static final boolean SHOW_GALLERY_IN_CHOOSER = false;
|
||||
private static final String KEY_PHOTO_URI = "photo_uri";
|
||||
private static final String KEY_VIDEO_URI = "video_uri";
|
||||
private static final String KEY_LAST_CAMERA_PHOTO = "last_photo";
|
||||
|
|
@ -45,23 +41,6 @@ public class FilePicker implements Constants {
|
|||
return uri;
|
||||
}
|
||||
|
||||
private static Uri createCameraVideoFile(@NonNull Context context) throws IOException {
|
||||
File imagePath = PickedFiles.getCameraVideoLocation(context);
|
||||
Uri uri = PickedFiles.getUriToFile(context, imagePath);
|
||||
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
|
||||
editor.putString(KEY_VIDEO_URI, uri.toString());
|
||||
editor.putString(KEY_LAST_CAMERA_VIDEO, imagePath.toString());
|
||||
editor.apply();
|
||||
return uri;
|
||||
}
|
||||
|
||||
private static Intent createDocumentsIntent(@NonNull Context context, int type) {
|
||||
storeType(context, type);
|
||||
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
||||
intent.setType("image/*");
|
||||
return intent;
|
||||
}
|
||||
|
||||
private static Intent createGalleryIntent(@NonNull Context context, int type) {
|
||||
storeType(context, type);
|
||||
return plainGalleryPickerIntent()
|
||||
|
|
@ -84,22 +63,6 @@ public class FilePicker implements Constants {
|
|||
return intent;
|
||||
}
|
||||
|
||||
private static Intent createCameraForVideoIntent(@NonNull Context context, int type) {
|
||||
storeType(context, type);
|
||||
|
||||
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
|
||||
try {
|
||||
Uri capturedImageUri = createCameraVideoFile(context);
|
||||
//We have to explicitly grant the write permission since Intent.setFlag works only on API Level >=20
|
||||
grantWritePermission(context, intent, capturedImageUri);
|
||||
intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return intent;
|
||||
}
|
||||
|
||||
private static void revokeWritePermission(@NonNull Context context, Uri uri) {
|
||||
context.revokeUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
}
|
||||
|
|
@ -112,41 +75,6 @@ public class FilePicker implements Constants {
|
|||
}
|
||||
}
|
||||
|
||||
private static Intent createChooserIntent(@NonNull Context context, @Nullable String chooserTitle, int type) throws IOException {
|
||||
return createChooserIntent(context, chooserTitle, SHOW_GALLERY_IN_CHOOSER, type);
|
||||
}
|
||||
|
||||
private static Intent createChooserIntent(@NonNull Context context, @Nullable String chooserTitle, boolean showGallery, int type) throws IOException {
|
||||
storeType(context, type);
|
||||
|
||||
Uri outputFileUri = createCameraPictureFile(context);
|
||||
List<Intent> cameraIntents = new ArrayList<>();
|
||||
Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
List<ResolveInfo> camList = packageManager.queryIntentActivities(captureIntent, 0);
|
||||
for (ResolveInfo res : camList) {
|
||||
final String packageName = res.activityInfo.packageName;
|
||||
final Intent intent = new Intent(captureIntent);
|
||||
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
|
||||
intent.setPackage(packageName);
|
||||
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
|
||||
grantWritePermission(context, intent, outputFileUri);
|
||||
cameraIntents.add(intent);
|
||||
}
|
||||
Intent galleryIntent;
|
||||
|
||||
if (showGallery) {
|
||||
galleryIntent = createGalleryIntent(context, type);
|
||||
} else {
|
||||
galleryIntent = createDocumentsIntent(context, type);
|
||||
}
|
||||
|
||||
Intent chooserIntent = Intent.createChooser(galleryIntent, chooserTitle);
|
||||
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
|
||||
|
||||
return chooserIntent;
|
||||
}
|
||||
|
||||
private static void storeType(@NonNull Context context, int type) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit().putInt(KEY_TYPE, type).apply();
|
||||
}
|
||||
|
|
@ -155,75 +83,6 @@ public class FilePicker implements Constants {
|
|||
return PreferenceManager.getDefaultSharedPreferences(context).getInt(KEY_TYPE, 0);
|
||||
}
|
||||
|
||||
public static void openChooserWithDocuments(Activity activity, @Nullable String chooserTitle, int type) {
|
||||
try {
|
||||
Intent intent = createChooserIntent(activity, chooserTitle, type);
|
||||
activity.startActivityForResult(intent, RequestCodes.SOURCE_CHOOSER | RequestCodes.PICK_PICTURE_FROM_DOCUMENTS);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void openChooserWithDocuments(Fragment fragment, @Nullable String chooserTitle, int type) {
|
||||
try {
|
||||
Intent intent = createChooserIntent(fragment.getActivity(), chooserTitle, type);
|
||||
fragment.startActivityForResult(intent, RequestCodes.SOURCE_CHOOSER | RequestCodes.PICK_PICTURE_FROM_DOCUMENTS);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void openChooserWithDocuments(android.app.Fragment fragment, @Nullable String chooserTitle, int type) {
|
||||
try {
|
||||
Intent intent = createChooserIntent(fragment.getActivity(), chooserTitle, type);
|
||||
fragment.startActivityForResult(intent, RequestCodes.SOURCE_CHOOSER | RequestCodes.PICK_PICTURE_FROM_DOCUMENTS);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void openChooserWithGallery(Activity activity, @Nullable String chooserTitle, int type) {
|
||||
try {
|
||||
Intent intent = createChooserIntent(activity, chooserTitle, true, type);
|
||||
activity.startActivityForResult(intent, RequestCodes.SOURCE_CHOOSER | RequestCodes.PICK_PICTURE_FROM_GALLERY);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void openChooserWithGallery(Fragment fragment, @Nullable String chooserTitle, int type) {
|
||||
try {
|
||||
Intent intent = createChooserIntent(fragment.getActivity(), chooserTitle, true, type);
|
||||
fragment.startActivityForResult(intent, RequestCodes.SOURCE_CHOOSER | RequestCodes.PICK_PICTURE_FROM_GALLERY);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void openChooserWithGallery(android.app.Fragment fragment, @Nullable String chooserTitle, int type) {
|
||||
try {
|
||||
Intent intent = createChooserIntent(fragment.getActivity(), chooserTitle, true, type);
|
||||
fragment.startActivityForResult(intent, RequestCodes.SOURCE_CHOOSER | RequestCodes.PICK_PICTURE_FROM_GALLERY);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static void openDocuments(Activity activity, int type) {
|
||||
Intent intent = createDocumentsIntent(activity, type);
|
||||
activity.startActivityForResult(intent, RequestCodes.PICK_PICTURE_FROM_DOCUMENTS);
|
||||
}
|
||||
|
||||
public static void openDocuments(Fragment fragment, int type) {
|
||||
Intent intent = createDocumentsIntent(fragment.getContext(), type);
|
||||
fragment.startActivityForResult(intent, RequestCodes.PICK_PICTURE_FROM_DOCUMENTS);
|
||||
}
|
||||
|
||||
public static void openDocuments(android.app.Fragment fragment, int type) {
|
||||
Intent intent = createDocumentsIntent(fragment.getActivity(), type);
|
||||
fragment.startActivityForResult(intent, RequestCodes.PICK_PICTURE_FROM_DOCUMENTS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens default galery or a available galleries picker if there is no default
|
||||
*
|
||||
|
|
@ -234,58 +93,13 @@ public class FilePicker implements Constants {
|
|||
activity.startActivityForResult(intent, RequestCodes.PICK_PICTURE_FROM_GALLERY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens default galery or a available galleries picker if there is no default
|
||||
*
|
||||
* @param type Custom type of your choice, which will be returned with the images
|
||||
*/
|
||||
public static void openGallery(Fragment fragment, int type) {
|
||||
Intent intent = createGalleryIntent(fragment.getContext(), type);
|
||||
fragment.startActivityForResult(intent, RequestCodes.PICK_PICTURE_FROM_GALLERY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens default galery or a available galleries picker if there is no default
|
||||
*
|
||||
* @param type Custom type of your choice, which will be returned with the images
|
||||
*/
|
||||
public static void openGallery(android.app.Fragment fragment, int type) {
|
||||
Intent intent = createGalleryIntent(fragment.getActivity(), type);
|
||||
fragment.startActivityForResult(intent, RequestCodes.PICK_PICTURE_FROM_GALLERY);
|
||||
}
|
||||
|
||||
public static void openCameraForImage(Activity activity, int type) {
|
||||
Intent intent = createCameraForImageIntent(activity, type);
|
||||
activity.startActivityForResult(intent, RequestCodes.TAKE_PICTURE);
|
||||
}
|
||||
|
||||
public static void openCameraForImage(Fragment fragment, int type) {
|
||||
Intent intent = createCameraForImageIntent(fragment.getActivity(), type);
|
||||
fragment.startActivityForResult(intent, RequestCodes.TAKE_PICTURE);
|
||||
}
|
||||
|
||||
public static void openCameraForImage(android.app.Fragment fragment, int type) {
|
||||
Intent intent = createCameraForImageIntent(fragment.getActivity(), type);
|
||||
fragment.startActivityForResult(intent, RequestCodes.TAKE_PICTURE);
|
||||
}
|
||||
|
||||
public static void openCameraForVideo(Activity activity, int type) {
|
||||
Intent intent = createCameraForVideoIntent(activity, type);
|
||||
activity.startActivityForResult(intent, RequestCodes.CAPTURE_VIDEO);
|
||||
}
|
||||
|
||||
public static void openCameraForVideo(Fragment fragment, int type) {
|
||||
Intent intent = createCameraForVideoIntent(fragment.getActivity(), type);
|
||||
fragment.startActivityForResult(intent, RequestCodes.CAPTURE_VIDEO);
|
||||
}
|
||||
|
||||
public static void openCameraForVideo(android.app.Fragment fragment, int type) {
|
||||
Intent intent = createCameraForVideoIntent(fragment.getActivity(), type);
|
||||
fragment.startActivityForResult(intent, RequestCodes.CAPTURE_VIDEO);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static UploadableFile takenCameraPicture(Context context) throws IOException, URISyntaxException {
|
||||
private static UploadableFile takenCameraPicture(Context context) throws URISyntaxException {
|
||||
String lastCameraPhoto = PreferenceManager.getDefaultSharedPreferences(context).getString(KEY_LAST_CAMERA_PHOTO, null);
|
||||
if (lastCameraPhoto != null) {
|
||||
return new UploadableFile(new File(lastCameraPhoto));
|
||||
|
|
@ -295,7 +109,7 @@ public class FilePicker implements Constants {
|
|||
}
|
||||
|
||||
@Nullable
|
||||
private static UploadableFile takenCameraVideo(Context context) throws IOException, URISyntaxException {
|
||||
private static UploadableFile takenCameraVideo(Context context) throws URISyntaxException {
|
||||
String lastCameraPhoto = PreferenceManager.getDefaultSharedPreferences(context).getString(KEY_LAST_CAMERA_VIDEO, null);
|
||||
if (lastCameraPhoto != null) {
|
||||
return new UploadableFile(new File(lastCameraPhoto));
|
||||
|
|
@ -352,49 +166,12 @@ public class FilePicker implements Constants {
|
|||
return data == null || (data.getData() == null && data.getClipData() == null);
|
||||
}
|
||||
|
||||
public static boolean willHandleActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == RequestCodes.SOURCE_CHOOSER || requestCode == RequestCodes.PICK_PICTURE_FROM_GALLERY || requestCode == RequestCodes.TAKE_PICTURE || requestCode == RequestCodes.PICK_PICTURE_FROM_DOCUMENTS) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static Intent plainGalleryPickerIntent() {
|
||||
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
||||
intent.setType("image/*");
|
||||
return intent;
|
||||
}
|
||||
|
||||
public static boolean canDeviceHandleGallery(@NonNull Context context) {
|
||||
return plainGalleryPickerIntent().resolveActivity(context.getPackageManager()) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param context context
|
||||
* @return File containing lastly taken (using camera) photo. Returns null if there was no photo taken or it doesn't exist anymore.
|
||||
*/
|
||||
public static File lastlyTakenButCanceledPhoto(@NonNull Context context) {
|
||||
String filePath = PreferenceManager.getDefaultSharedPreferences(context).getString(KEY_LAST_CAMERA_PHOTO, null);
|
||||
if (filePath == null) return null;
|
||||
File file = new File(filePath);
|
||||
if (file.exists()) {
|
||||
return file;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static File lastlyTakenButCanceledVideo(@NonNull Context context) {
|
||||
String filePath = PreferenceManager.getDefaultSharedPreferences(context).getString(KEY_LAST_CAMERA_VIDEO, null);
|
||||
if (filePath == null) return null;
|
||||
File file = new File(filePath);
|
||||
if (file.exists()) {
|
||||
return file;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static void onPictureReturnedFromDocuments(Intent data, Activity activity, @NonNull FilePicker.Callbacks callbacks) {
|
||||
try {
|
||||
Uri photoPath = data.getData();
|
||||
|
|
@ -508,20 +285,6 @@ public class FilePicker implements Constants {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to clear configuration. Would likely be used in onDestroy(), onDestroyView()...
|
||||
*
|
||||
* @param context context
|
||||
*/
|
||||
public static void clearConfiguration(@NonNull Context context) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit()
|
||||
.remove(BundleKeys.FOLDER_NAME)
|
||||
.remove(BundleKeys.ALLOW_MULTIPLE)
|
||||
.remove(BundleKeys.COPY_TAKEN_PHOTOS)
|
||||
.remove(BundleKeys.COPY_PICKED_IMAGES)
|
||||
.apply();
|
||||
}
|
||||
|
||||
public static FilePickerConfiguration configuration(@NonNull Context context) {
|
||||
return new FilePickerConfiguration(context);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,13 +11,6 @@ public class FilePickerConfiguration implements Constants {
|
|||
this.context = context;
|
||||
}
|
||||
|
||||
public FilePickerConfiguration setImagesFolderName(String folderName) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context)
|
||||
.edit().putString(BundleKeys.FOLDER_NAME, folderName)
|
||||
.apply();
|
||||
return this;
|
||||
}
|
||||
|
||||
public FilePickerConfiguration setAllowMultiplePickInGallery(boolean allowMultiple) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit()
|
||||
.putBoolean(BundleKeys.ALLOW_MULTIPLE, allowMultiple)
|
||||
|
|
@ -32,13 +25,6 @@ public class FilePickerConfiguration implements Constants {
|
|||
return this;
|
||||
}
|
||||
|
||||
public FilePickerConfiguration setCopyPickedImagesToPublicGalleryAppFolder(boolean copy) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit()
|
||||
.putBoolean(BundleKeys.COPY_PICKED_IMAGES, copy)
|
||||
.apply();
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getFolderName() {
|
||||
return PreferenceManager.getDefaultSharedPreferences(context).getString(BundleKeys.FOLDER_NAME, DEFAULT_FOLDER_NAME);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,11 +15,6 @@ public class MimeTypeMapWrapper {
|
|||
"image/heif", "heif",
|
||||
"image/heic", "heic");
|
||||
|
||||
private static final Map<String, String> sExtensionToMimeTypeMap =
|
||||
ImmutableMap.of(
|
||||
"heif", "image/heif",
|
||||
"heic", "image/heic");
|
||||
|
||||
public static String getExtensionFromMimeType(String mimeType) {
|
||||
String result = sMimeTypeToExtensionMap.get(mimeType);
|
||||
if (result != null) {
|
||||
|
|
@ -28,19 +23,4 @@ public class MimeTypeMapWrapper {
|
|||
return sMimeTypeMap.getExtensionFromMimeType(mimeType);
|
||||
}
|
||||
|
||||
public static String getMimeTypeFromExtension(String extension) {
|
||||
String result = sExtensionToMimeTypeMap.get(extension);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
return sMimeTypeMap.getMimeTypeFromExtension(extension);
|
||||
}
|
||||
|
||||
public static boolean hasExtension(String extension) {
|
||||
return sExtensionToMimeTypeMap.containsKey(extension) || sMimeTypeMap.hasExtension(extension);
|
||||
}
|
||||
|
||||
public static boolean hasMimeType(String mimeType) {
|
||||
return sMimeTypeToExtensionMap.containsKey(mimeType) || sMimeTypeMap.hasMimeType(mimeType);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,9 +5,10 @@ import android.content.Context;
|
|||
import android.media.MediaScannerConnection;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.content.FileProvider;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
|
|
@ -100,11 +101,9 @@ class PickedFiles implements Constants {
|
|||
|
||||
MediaScannerConnection.scanFile(context,
|
||||
paths, null,
|
||||
new MediaScannerConnection.OnScanCompletedListener() {
|
||||
public void onScanCompleted(String path, Uri uri) {
|
||||
Timber.d("Scanned " + path + ":");
|
||||
Timber.d("-> uri=%s", uri);
|
||||
}
|
||||
(path, uri) -> {
|
||||
Timber.d("Scanned " + path + ":");
|
||||
Timber.d("-> uri=%s", uri);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -122,11 +121,6 @@ class PickedFiles implements Constants {
|
|||
return File.createTempFile(UUID.randomUUID().toString(), ".jpg", dir);
|
||||
}
|
||||
|
||||
static File getCameraVideoLocation(@NonNull Context context) throws IOException {
|
||||
File dir = tempImageDirectory(context);
|
||||
return File.createTempFile(UUID.randomUUID().toString(), ".mp4", dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* To find out the extension of required object in given uri
|
||||
* Solution by http://stackoverflow.com/a/36514823/1171484
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue