convert utils to kotlin

This commit is contained in:
tibbi 2016-11-06 16:35:45 +01:00
parent 4f74709774
commit 3ec6bd3a94
8 changed files with 175 additions and 189 deletions

View file

@ -1,153 +0,0 @@
package com.simplemobiletools.gallery;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.webkit.MimeTypeMap;
import android.widget.Toast;
import com.simplemobiletools.gallery.models.Medium;
import java.io.File;
public class Utils {
public static String getFilename(final String path) {
return path.substring(path.lastIndexOf("/") + 1);
}
public static void showToast(Context context, int resId) {
Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show();
}
public static int getActionBarHeight(Context context, Resources res) {
final TypedValue tv = new TypedValue();
int height = 0;
if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
height = TypedValue.complexToDimensionPixelSize(tv.data, res.getDisplayMetrics());
}
return height;
}
public static int getStatusBarHeight(Resources res) {
int id = res.getIdentifier("status_bar_height", "dimen", "android");
if (id > 0) {
return res.getDimensionPixelSize(id);
}
return 0;
}
public static int getNavBarHeight(Resources res) {
int id = res.getIdentifier("navigation_bar_height", "dimen", "android");
if (id > 0) {
return res.getDimensionPixelSize(id);
}
return 0;
}
public static boolean hasNavBar(Activity act) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Display display = act.getWindowManager().getDefaultDisplay();
DisplayMetrics realDisplayMetrics = new DisplayMetrics();
display.getRealMetrics(realDisplayMetrics);
int realHeight = realDisplayMetrics.heightPixels;
int realWidth = realDisplayMetrics.widthPixels;
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels;
int displayWidth = displayMetrics.widthPixels;
return (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
} else {
boolean hasMenuKey = ViewConfiguration.get(act).hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
return !hasMenuKey && !hasBackKey;
}
}
public static boolean hasStoragePermission(Context cxt) {
return ContextCompat.checkSelfPermission(cxt, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
public static String getMimeType(String url) {
final String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return "";
}
public static void shareMedium(Medium medium, Activity activity) {
final String shareTitle = activity.getResources().getString(R.string.share_via);
final Intent intent = new Intent();
final File file = new File(medium.getPath());
final Uri uri = Uri.fromFile(file);
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType(getMimeType(medium));
activity.startActivity(Intent.createChooser(intent, shareTitle));
}
public static String getMimeType(Medium medium) {
if (medium.getIsVideo())
return "video/*";
else
return "image/*";
}
public static void showSystemUI(ActionBar actionbar, Window window) {
if (actionbar != null)
actionbar.show();
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
public static void hideSystemUI(ActionBar actionbar, Window window) {
if (actionbar != null)
actionbar.hide();
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_LOW_PROFILE |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE);
}
public static String getRealPathFromURI(Context context, Uri uri) {
Cursor cursor = null;
try {
String[] projection = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(uri, projection, null, null, null);
int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}

View file

@ -0,0 +1,139 @@
package com.simplemobiletools.gallery
import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.Resources
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.support.v4.content.ContextCompat
import android.support.v7.app.ActionBar
import android.util.DisplayMetrics
import android.util.TypedValue
import android.view.*
import android.webkit.MimeTypeMap
import android.widget.Toast
import com.simplemobiletools.gallery.models.Medium
import java.io.File
class Utils {
companion object {
fun getFilename(path: String): String {
return path.substring(path.lastIndexOf("/") + 1)
}
fun showToast(context: Context, resId: Int) {
Toast.makeText(context, context.resources.getString(resId), Toast.LENGTH_SHORT).show()
}
fun getActionBarHeight(context: Context, res: Resources): Int {
val tv = TypedValue()
var height = 0
if (context.theme.resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
height = TypedValue.complexToDimensionPixelSize(tv.data, res.displayMetrics)
}
return height
}
fun getStatusBarHeight(res: Resources): Int {
val id = res.getIdentifier("status_bar_height", "dimen", "android")
return if (id > 0) {
res.getDimensionPixelSize(id)
} else
0
}
fun getNavBarHeight(res: Resources): Int {
val id = res.getIdentifier("navigation_bar_height", "dimen", "android")
return if (id > 0) {
res.getDimensionPixelSize(id)
} else
0
}
fun hasNavBar(act: Activity): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
val display = act.windowManager.defaultDisplay
val realDisplayMetrics = DisplayMetrics()
display.getRealMetrics(realDisplayMetrics)
val realHeight = realDisplayMetrics.heightPixels
val realWidth = realDisplayMetrics.widthPixels
val displayMetrics = DisplayMetrics()
display.getMetrics(displayMetrics)
val displayHeight = displayMetrics.heightPixels
val displayWidth = displayMetrics.widthPixels
realWidth - displayWidth > 0 || realHeight - displayHeight > 0
} else {
val hasMenuKey = ViewConfiguration.get(act).hasPermanentMenuKey()
val hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK)
!hasMenuKey && !hasBackKey
}
}
fun hasStoragePermission(cxt: Context): Boolean {
return ContextCompat.checkSelfPermission(cxt, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
}
fun getMimeType(url: String): String {
val extension = MimeTypeMap.getFileExtensionFromUrl(url)
return if (extension != null) {
MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
} else
""
}
fun shareMedium(medium: Medium, activity: Activity) {
val shareTitle = activity.resources.getString(R.string.share_via)
val intent = Intent()
val file = File(medium.path)
val uri = Uri.fromFile(file)
intent.action = Intent.ACTION_SEND
intent.putExtra(Intent.EXTRA_STREAM, uri)
intent.type = getMimeType(medium)
activity.startActivity(Intent.createChooser(intent, shareTitle))
}
fun getMimeType(medium: Medium) = if (medium.isVideo) "video/*" else "image/*"
fun showSystemUI(actionbar: ActionBar?, window: Window) {
actionbar?.show()
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
}
fun hideSystemUI(actionbar: ActionBar?, window: Window) {
actionbar?.hide()
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_LOW_PROFILE or
View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_IMMERSIVE
}
fun getRealPathFromURI(context: Context, uri: Uri): String {
var cursor: Cursor? = null
try {
val projection = arrayOf(MediaStore.Images.Media.DATA)
cursor = context.contentResolver.query(uri, projection, null, null, null)
val index = cursor!!.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
cursor.moveToFirst()
return cursor.getString(index)
} finally {
cursor?.close()
}
}
}
}

View file

@ -148,7 +148,7 @@ public class MainActivity extends SimpleActivity
}
private void tryloadGallery() {
if (Utils.hasStoragePermission(getApplicationContext())) {
if (Utils.Companion.hasStoragePermission(getApplicationContext())) {
getDirectories();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION);
@ -163,7 +163,7 @@ public class MainActivity extends SimpleActivity
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getDirectories();
} else {
Utils.showToast(getApplicationContext(), R.string.no_permissions);
Utils.Companion.showToast(getApplicationContext(), R.string.no_permissions);
finish();
}
}
@ -183,7 +183,7 @@ public class MainActivity extends SimpleActivity
}
private void prepareForDeleting() {
Utils.showToast(this, R.string.deleting);
Utils.Companion.showToast(this, R.string.deleting);
final SparseBooleanArray items = mGridView.getCheckedItemPositions();
final int cnt = items.size();
int deletedCnt = 0;
@ -307,7 +307,7 @@ public class MainActivity extends SimpleActivity
final String newDirName = dirNameET.getText().toString().trim();
if (newDirName.isEmpty()) {
Utils.showToast(getApplicationContext(), R.string.rename_folder_empty);
Utils.Companion.showToast(getApplicationContext(), R.string.rename_folder_empty);
return;
}
@ -316,7 +316,7 @@ public class MainActivity extends SimpleActivity
final File newDir = new File(dir.getParent(), newDirName);
if (dir.renameTo(newDir)) {
Utils.showToast(getApplicationContext(), R.string.renaming_folder);
Utils.Companion.showToast(getApplicationContext(), R.string.renaming_folder);
alertDialog.dismiss();
mActionMode.finish();
@ -335,7 +335,7 @@ public class MainActivity extends SimpleActivity
}
});
} else {
Utils.showToast(getApplicationContext(), R.string.rename_folder_error);
Utils.Companion.showToast(getApplicationContext(), R.string.rename_folder_error);
}
}
});
@ -404,7 +404,7 @@ public class MainActivity extends SimpleActivity
final String path = data.getData().getPath();
final Uri uri = Uri.fromFile(new File(path));
if (mIsGetImageContentIntent || mIsGetVideoContentIntent || mIsGetAnyContentIntent) {
final String type = Utils.getMimeType(path);
final String type = Utils.Companion.getMimeType(path);
result.setDataAndTypeAndNormalize(uri, type);
result.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} else if (mIsPickImageIntent || mIsPickVideoIntent) {
@ -556,7 +556,7 @@ public class MainActivity extends SimpleActivity
runOnUiThread(new Runnable() {
@Override
public void run() {
Utils.showToast(getApplicationContext(), R.string.rename_folder_ok);
Utils.Companion.showToast(getApplicationContext(), R.string.rename_folder_ok);
}
});
}

View file

@ -100,7 +100,7 @@ public class MediaActivity extends SimpleActivity
}
private void tryloadGallery() {
if (Utils.hasStoragePermission(getApplicationContext())) {
if (Utils.Companion.hasStoragePermission(getApplicationContext())) {
initializeGallery();
} else {
finish();
@ -124,7 +124,7 @@ public class MediaActivity extends SimpleActivity
mGridView.setOnTouchListener(this);
mIsSnackbarShown = false;
final String dirName = Utils.getFilename(mPath);
final String dirName = Utils.Companion.getFilename(mPath);
setTitle(dirName);
}
@ -257,7 +257,7 @@ public class MediaActivity extends SimpleActivity
private void shareMedia() {
final List<Medium> selectedMedia = getSelectedMedia();
if (selectedMedia.size() <= 1) {
Utils.shareMedium(selectedMedia.get(0), this);
Utils.Companion.shareMedium(selectedMedia.get(0), this);
} else {
shareMedia(selectedMedia);
}
@ -292,7 +292,7 @@ public class MediaActivity extends SimpleActivity
}
private void prepareForDeleting() {
Utils.showToast(this, R.string.deleting);
Utils.Companion.showToast(this, R.string.deleting);
final SparseBooleanArray items = mGridView.getCheckedItemPositions();
final int cnt = items.size();
int deletedCnt = 0;
@ -393,7 +393,7 @@ public class MediaActivity extends SimpleActivity
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String curItemPath = mMedia.get(position).getPath();
if (isSetWallpaperIntent()) {
Utils.showToast(this, R.string.setting_wallpaper);
Utils.Companion.showToast(this, R.string.setting_wallpaper);
final int wantedWidth = getWallpaperDesiredMinimumWidth();
final int wantedHeight = getWallpaperDesiredMinimumHeight();

View file

@ -52,7 +52,7 @@ public class PhotoVideoActivity extends SimpleActivity implements ViewPagerFragm
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_holder, mFragment).commit();
}
hideSystemUI();
setTitle(Utils.getFilename(mUri.toString()));
setTitle(Utils.Companion.getFilename(mUri.toString()));
}
@Override
@ -98,10 +98,10 @@ public class PhotoVideoActivity extends SimpleActivity implements ViewPagerFragm
}
private void hideSystemUI() {
Utils.hideSystemUI(mActionbar, getWindow());
Utils.Companion.hideSystemUI(mActionbar, getWindow());
}
private void showSystemUI() {
Utils.showSystemUI(mActionbar, getWindow());
Utils.Companion.showSystemUI(mActionbar, getWindow());
}
}

View file

@ -63,7 +63,7 @@ public class ViewPagerActivity extends SimpleActivity
setContentView(R.layout.activity_medium);
ButterKnife.bind(this);
if (!Utils.hasStoragePermission(getApplicationContext())) {
if (!Utils.Companion.hasStoragePermission(getApplicationContext())) {
finish();
return;
}
@ -89,7 +89,7 @@ public class ViewPagerActivity extends SimpleActivity
}
if (mPath == null || mPath.isEmpty()) {
Utils.showToast(getApplicationContext(), R.string.unknown_error);
Utils.Companion.showToast(getApplicationContext(), R.string.unknown_error);
finish();
return;
}
@ -121,7 +121,7 @@ public class ViewPagerActivity extends SimpleActivity
@Override
protected void onResume() {
super.onResume();
if (!Utils.hasStoragePermission(getApplicationContext())) {
if (!Utils.Companion.hasStoragePermission(getApplicationContext())) {
finish();
}
}
@ -188,7 +188,7 @@ public class ViewPagerActivity extends SimpleActivity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(chooser, EDIT_IMAGE);
} else {
Utils.showToast(getApplicationContext(), R.string.no_editor_found);
Utils.Companion.showToast(getApplicationContext(), R.string.no_editor_found);
}
}
@ -200,19 +200,19 @@ public class ViewPagerActivity extends SimpleActivity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(chooser, SET_WALLPAPER);
} else {
Utils.showToast(getApplicationContext(), R.string.no_wallpaper_setter_found);
Utils.Companion.showToast(getApplicationContext(), R.string.no_wallpaper_setter_found);
}
}
private void openWith() {
final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(getCurrentFile()), Utils.getMimeType(getCurrentMedium()));
intent.setDataAndType(Uri.fromFile(getCurrentFile()), Utils.Companion.getMimeType(getCurrentMedium()));
final Intent chooser = Intent.createChooser(intent, getString(R.string.open_with));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
} else {
Utils.showToast(getApplicationContext(), R.string.no_app_found);
Utils.Companion.showToast(getApplicationContext(), R.string.no_app_found);
}
}
@ -229,7 +229,7 @@ public class ViewPagerActivity extends SimpleActivity
}
} else if (requestCode == SET_WALLPAPER) {
if (resultCode == RESULT_OK) {
Utils.showToast(getApplicationContext(), R.string.wallpaper_set_successfully);
Utils.Companion.showToast(getApplicationContext(), R.string.wallpaper_set_successfully);
}
}
super.onActivityResult(requestCode, resultCode, data);
@ -237,7 +237,7 @@ public class ViewPagerActivity extends SimpleActivity
private void shareMedium() {
final Medium medium = getCurrentMedium();
Utils.shareMedium(medium, this);
Utils.Companion.shareMedium(medium, this);
}
private void notifyDeletion() {
@ -246,7 +246,7 @@ public class ViewPagerActivity extends SimpleActivity
if (mMedia.size() <= 1) {
deleteFile();
} else {
Utils.showToast(this, R.string.file_deleted);
Utils.Companion.showToast(this, R.string.file_deleted);
mUndoBtn.setVisibility(View.VISIBLE);
mIsUndoShown = true;
reloadViewPager();
@ -320,7 +320,7 @@ public class ViewPagerActivity extends SimpleActivity
final String extension = extensionET.getText().toString().trim();
if (fileName.isEmpty() || extension.isEmpty()) {
Utils.showToast(getApplicationContext(), R.string.rename_file_empty);
Utils.Companion.showToast(getApplicationContext(), R.string.rename_file_empty);
return;
}
@ -335,7 +335,7 @@ public class ViewPagerActivity extends SimpleActivity
updateActionbarTitle();
alertDialog.dismiss();
} else {
Utils.showToast(getApplicationContext(), R.string.rename_file_error);
Utils.Companion.showToast(getApplicationContext(), R.string.rename_file_error);
}
}
});
@ -425,15 +425,15 @@ public class ViewPagerActivity extends SimpleActivity
}
private void hideSystemUI() {
Utils.hideSystemUI(mActionbar, getWindow());
Utils.Companion.hideSystemUI(mActionbar, getWindow());
}
private void showSystemUI() {
Utils.showSystemUI(mActionbar, getWindow());
Utils.Companion.showSystemUI(mActionbar, getWindow());
}
private void updateActionbarTitle() {
setTitle(Utils.getFilename(mMedia.get(mPager.getCurrentItem()).getPath()));
setTitle(Utils.Companion.getFilename(mMedia.get(mPager.getCurrentItem()).getPath()));
}
private Medium getCurrentMedium() {
@ -449,11 +449,11 @@ public class ViewPagerActivity extends SimpleActivity
private void addUndoMargin() {
final Resources res = getResources();
final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mUndoBtn.getLayoutParams();
final int topMargin = Utils.getStatusBarHeight(res) + Utils.getActionBarHeight(getApplicationContext(), res);
final int topMargin = Utils.Companion.getStatusBarHeight(res) + Utils.Companion.getActionBarHeight(getApplicationContext(), res);
int rightMargin = params.rightMargin;
if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_PORTRAIT) {
rightMargin += Utils.getNavBarHeight(res);
rightMargin += Utils.Companion.getNavBarHeight(res);
}
params.setMargins(params.leftMargin, topMargin, rightMargin, params.bottomMargin);

View file

@ -26,7 +26,7 @@ public class PhotoFragment extends ViewPagerFragment implements View.OnClickList
mMedium = (Medium) getArguments().getSerializable(Constants.MEDIUM);
if (mMedium.getPath().startsWith("content://"))
mMedium.setPath(Utils.getRealPathFromURI(getContext(), Uri.parse(mMedium.getPath())));
mMedium.setPath(Utils.Companion.getRealPathFromURI(getContext(), Uri.parse(mMedium.getPath())));
if (mMedium == null)
return view;

View file

@ -121,13 +121,13 @@ public class VideoFragment extends ViewPagerFragment
private void initTimeHolder() {
mTimeHolder = mView.findViewById(R.id.video_time_holder);
final Resources res = getResources();
final int height = Utils.getNavBarHeight(res);
final int height = Utils.Companion.getNavBarHeight(res);
final int left = mTimeHolder.getPaddingLeft();
final int top = mTimeHolder.getPaddingTop();
int right = (int) getResources().getDimension(R.dimen.timer_padding);
int bottom = 0;
if (Utils.hasNavBar(getActivity())) {
if (Utils.Companion.hasNavBar(getActivity())) {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
bottom += height;
} else {