first
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package com.esafirm.imagepicker.helper;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.esafirm.imagepicker.R;
|
||||
import com.esafirm.imagepicker.features.ImagePickerConfig;
|
||||
import com.esafirm.imagepicker.features.IpCons;
|
||||
import com.esafirm.imagepicker.features.ReturnMode;
|
||||
import com.esafirm.imagepicker.features.common.BaseConfig;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class ConfigUtils {
|
||||
|
||||
public static ImagePickerConfig checkConfig(ImagePickerConfig config) {
|
||||
if (config == null) {
|
||||
throw new IllegalStateException("ImagePickerConfig cannot be null");
|
||||
}
|
||||
if (config.getMode() != IpCons.MODE_SINGLE
|
||||
&& (config.getReturnMode() == ReturnMode.GALLERY_ONLY
|
||||
|| config.getReturnMode() == ReturnMode.ALL)) {
|
||||
throw new IllegalStateException("ReturnMode.GALLERY_ONLY and ReturnMode.ALL is only applicable in Single Mode!");
|
||||
}
|
||||
if (config.getImageLoader() != null && !(config.getImageLoader() instanceof Serializable)) {
|
||||
throw new IllegalStateException("Custom image loader must be a class that implement ImageLoader." +
|
||||
" This limitation due to Serializeable");
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
public static boolean shouldReturn(BaseConfig config, boolean isCamera) {
|
||||
ReturnMode mode = config.getReturnMode();
|
||||
if (isCamera) {
|
||||
return mode == ReturnMode.ALL || mode == ReturnMode.CAMERA_ONLY;
|
||||
} else {
|
||||
return mode == ReturnMode.ALL || mode == ReturnMode.GALLERY_ONLY;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getFolderTitle(Context context, ImagePickerConfig config) {
|
||||
final String folderTitle = config.getFolderTitle();
|
||||
return ImagePickerUtils.isStringEmpty(folderTitle)
|
||||
? context.getString(R.string.ef_title_folder)
|
||||
: folderTitle;
|
||||
}
|
||||
|
||||
public static String getImageTitle(Context context, ImagePickerConfig config) {
|
||||
final String configImageTitle = config.getImageTitle();
|
||||
return ImagePickerUtils.isStringEmpty(configImageTitle)
|
||||
? context.getString(R.string.ef_title_select_image)
|
||||
: configImageTitle;
|
||||
}
|
||||
|
||||
public static String getDoneButtonText(Context context, ImagePickerConfig config) {
|
||||
final String doneButtonText = config.getDoneButtonText();
|
||||
return ImagePickerUtils.isStringEmpty(doneButtonText)
|
||||
? context.getString(R.string.ef_done)
|
||||
: doneButtonText;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.esafirm.imagepicker.helper;
|
||||
|
||||
import androidx.core.content.FileProvider;
|
||||
|
||||
public class ImagePickerFileProvider extends FileProvider {
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.esafirm.imagepicker.helper;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
|
||||
public class ImagePickerPreferences {
|
||||
|
||||
public static final String PREF_WRITE_EXTERNAL_STORAGE_REQUESTED = "writeExternalRequested";
|
||||
public static final String PREF_CAMERA_REQUESTED = "cameraRequested";
|
||||
|
||||
private Context context;
|
||||
|
||||
public ImagePickerPreferences(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a permission is requested
|
||||
*/
|
||||
public void setPermissionRequested(String permission) {
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
SharedPreferences.Editor editor = preferences.edit();
|
||||
editor.putBoolean(permission, true);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a permission is requestted or not (false by default)
|
||||
*/
|
||||
public boolean isPermissionRequested(String permission) {
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
return preferences.getBoolean(permission, false);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package com.esafirm.imagepicker.helper;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.text.TextUtils;
|
||||
import android.webkit.MimeTypeMap;
|
||||
|
||||
import com.esafirm.imagepicker.features.ImagePickerSavePath;
|
||||
import com.esafirm.imagepicker.model.Image;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URLConnection;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
public class ImagePickerUtils {
|
||||
|
||||
public static boolean isStringEmpty(@Nullable String str) {
|
||||
return str == null || str.length() == 0;
|
||||
}
|
||||
|
||||
public static File createImageFile(ImagePickerSavePath savePath) {
|
||||
// External sdcard location
|
||||
final String path = savePath.getPath();
|
||||
File mediaStorageDir = savePath.isFullPath()
|
||||
? new File(path)
|
||||
: new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), path);
|
||||
|
||||
// Create the storage directory if it does not exist
|
||||
if (!mediaStorageDir.exists()) {
|
||||
if (!mediaStorageDir.mkdirs()) {
|
||||
IpLogger.getInstance().d("Oops! Failed create " + path);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Create a media file name
|
||||
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
|
||||
String imageFileName = "IMG_" + timeStamp;
|
||||
|
||||
File imageFile = null;
|
||||
try {
|
||||
imageFile = File.createTempFile(imageFileName, ".jpg", mediaStorageDir);
|
||||
} catch (IOException e) {
|
||||
IpLogger.getInstance().d("Oops! Failed create " + imageFileName + " file");
|
||||
}
|
||||
return imageFile;
|
||||
}
|
||||
|
||||
public static String getNameFromFilePath(String path) {
|
||||
if (path.contains(File.separator)) {
|
||||
return path.substring(path.lastIndexOf(File.separator) + 1);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
public static void grantAppPermission(Context context, Intent intent, Uri fileUri) {
|
||||
List<ResolveInfo> resolvedIntentActivities = context.getPackageManager()
|
||||
.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
|
||||
|
||||
for (ResolveInfo resolvedIntentInfo : resolvedIntentActivities) {
|
||||
String packageName = resolvedIntentInfo.activityInfo.packageName;
|
||||
context.grantUriPermission(packageName, fileUri,
|
||||
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
}
|
||||
}
|
||||
|
||||
public static void revokeAppPermission(Context context, Uri fileUri) {
|
||||
context.revokeUriPermission(fileUri,
|
||||
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
}
|
||||
|
||||
public static boolean isGifFormat(Image image) {
|
||||
String extension = getExtension(image.getPath());
|
||||
return extension.equalsIgnoreCase("gif");
|
||||
}
|
||||
|
||||
public static boolean isVideoFormat(Image image) {
|
||||
String extension = getExtension(image.getPath());
|
||||
String mimeType = TextUtils.isEmpty(extension)
|
||||
? URLConnection.guessContentTypeFromName(image.getPath())
|
||||
: MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
||||
return mimeType != null && mimeType.startsWith("video");
|
||||
|
||||
}
|
||||
|
||||
private static String getExtension(String path) {
|
||||
String extension = MimeTypeMap.getFileExtensionFromUrl(path);
|
||||
if (!TextUtils.isEmpty(extension)) {
|
||||
return extension;
|
||||
}
|
||||
if (path.contains(".")) {
|
||||
return path.substring(path.lastIndexOf(".") + 1, path.length());
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.esafirm.imagepicker.helper;
|
||||
|
||||
public class IpCrasher {
|
||||
public static void openIssue() {
|
||||
throw new IllegalStateException("This should not happen. Please open an issue!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.esafirm.imagepicker.helper;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
public class IpLogger {
|
||||
|
||||
private static final String TAG = "ImagePicker";
|
||||
|
||||
private static IpLogger INSTANCE;
|
||||
|
||||
private boolean isEnable = true;
|
||||
|
||||
public static IpLogger getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new IpLogger();
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
private IpLogger() {
|
||||
}
|
||||
|
||||
public void setEnable(boolean enable) {
|
||||
isEnable = enable;
|
||||
}
|
||||
|
||||
public void d(String message) {
|
||||
if (isEnable) {
|
||||
Log.d(TAG, message);
|
||||
}
|
||||
}
|
||||
|
||||
public void e(String message) {
|
||||
if (isEnable) {
|
||||
Log.e(TAG, message);
|
||||
}
|
||||
}
|
||||
|
||||
public void w(String message) {
|
||||
if (isEnable) {
|
||||
Log.w(TAG, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.esafirm.imagepicker.helper;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Build;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
public class LocaleManager {
|
||||
|
||||
private static String language;
|
||||
|
||||
public static void setLanguange(String newLanguage) {
|
||||
language = newLanguage;
|
||||
}
|
||||
|
||||
private static String getLanguage() {
|
||||
return language != null && !language.isEmpty()
|
||||
? language
|
||||
: Locale.getDefault().getLanguage();
|
||||
}
|
||||
|
||||
public static Context updateResources(Context context) {
|
||||
Locale locale = new Locale(getLanguage());
|
||||
Locale.setDefault(locale);
|
||||
|
||||
Resources res = context.getResources();
|
||||
Configuration config = new Configuration(res.getConfiguration());
|
||||
if (Build.VERSION.SDK_INT >= 17) {
|
||||
config.setLocale(locale);
|
||||
context = context.createConfigurationContext(config);
|
||||
} else {
|
||||
config.locale = locale;
|
||||
res.updateConfiguration(config, res.getDisplayMetrics());
|
||||
}
|
||||
return context;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.esafirm.imagepicker.helper;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import android.view.View;
|
||||
import android.view.ViewTreeObserver;
|
||||
|
||||
import com.esafirm.imagepicker.R;
|
||||
|
||||
public class ViewUtils {
|
||||
|
||||
public static Drawable getArrowIcon(Context context) {
|
||||
final int backResourceId;
|
||||
if (Build.VERSION.SDK_INT >= 17 && context.getResources().getConfiguration().getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
|
||||
// For right-to-left layouts, pick the drawable that points to the right (forward).
|
||||
backResourceId = R.drawable.ef_ic_arrow_forward;
|
||||
} else {
|
||||
// For left-to-right layouts, pick the drawable that points to the left (back).
|
||||
backResourceId = R.drawable.ef_ic_arrow_back;
|
||||
}
|
||||
return ContextCompat.getDrawable(context.getApplicationContext(), backResourceId);
|
||||
}
|
||||
|
||||
public static void onPreDraw(final View view, final Runnable runnable) {
|
||||
view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
|
||||
@Override
|
||||
public boolean onPreDraw() {
|
||||
view.getViewTreeObserver().removeOnPreDrawListener(this);
|
||||
runnable.run();
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user