convert MediaActivity to kotlin, initial version

This commit is contained in:
tibbi 2016-11-19 14:46:07 +01:00
parent 4b5063a1b0
commit e14f002596
2 changed files with 514 additions and 557 deletions

View File

@ -1,557 +0,0 @@
package com.simplemobiletools.gallery.activities;
import android.app.WallpaperManager;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.provider.DocumentFile;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask;
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog;
import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.adapters.MediaAdapter;
import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog;
import com.simplemobiletools.gallery.dialogs.CopyDialog;
import com.simplemobiletools.gallery.models.Medium;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MediaActivity extends SimpleActivity
implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener,
SwipeRefreshLayout.OnRefreshListener {
private static final String TAG = MediaActivity.class.getSimpleName();
@BindView(R.id.media_grid) GridView mGridView;
@BindView(R.id.media_holder) SwipeRefreshLayout mSwipeRefreshLayout;
private static List<Medium> mMedia;
private static String mPath;
private static Snackbar mSnackbar;
private static List<String> mToBeDeleted;
private static Parcelable mState;
private static boolean mIsSnackbarShown;
private static boolean mIsGetImageIntent;
private static boolean mIsGetVideoIntent;
private static boolean mIsGetAnyIntent;
private static int mSelectedItemsCnt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media);
ButterKnife.bind(this);
mIsGetImageIntent = getIntent().getBooleanExtra(Constants.INSTANCE.getGET_IMAGE_INTENT(), false);
mIsGetVideoIntent = getIntent().getBooleanExtra(Constants.INSTANCE.getGET_VIDEO_INTENT(), false);
mIsGetAnyIntent = getIntent().getBooleanExtra(Constants.INSTANCE.getGET_ANY_INTENT(), false);
mToBeDeleted = new ArrayList<>();
mSwipeRefreshLayout.setOnRefreshListener(this);
mPath = getIntent().getStringExtra(Constants.INSTANCE.getDIRECTORY());
mMedia = new ArrayList<>();
}
@Override
protected void onResume() {
super.onResume();
tryloadGallery();
if (mState != null && mGridView != null) {
mGridView.onRestoreInstanceState(mState);
}
}
@Override
protected void onPause() {
super.onPause();
deleteFiles();
if (mGridView != null && isChangingConfigurations()) {
mState = mGridView.onSaveInstanceState();
} else {
mState = null;
}
}
private void tryloadGallery() {
if (Utils.Companion.hasStoragePermission(getApplicationContext())) {
initializeGallery();
} else {
finish();
}
}
private void initializeGallery() {
final List<Medium> newMedia = getMedia();
if (newMedia.toString().equals(mMedia.toString())) {
return;
}
mMedia = newMedia;
if (isDirEmpty())
return;
final MediaAdapter adapter = new MediaAdapter(this, mMedia);
mGridView.setAdapter(adapter);
mGridView.setOnItemClickListener(this);
mGridView.setMultiChoiceModeListener(this);
mGridView.setOnTouchListener(this);
mIsSnackbarShown = false;
final String dirName = Utils.Companion.getFilename(this, mPath);
setTitle(dirName);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_media, menu);
final boolean isFolderHidden = mConfig.getIsFolderHidden(mPath);
menu.findItem(R.id.hide_folder).setVisible(!isFolderHidden);
menu.findItem(R.id.unhide_folder).setVisible(isFolderHidden);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.sort:
showSortingDialog();
return true;
case R.id.toggle_filename:
toggleFilenameVisibility();
return true;
case R.id.hide_folder:
hideDirectory();
return true;
case R.id.unhide_folder:
unhideDirectory();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void toggleFilenameVisibility() {
mConfig.setDisplayFileNames(!mConfig.getDisplayFileNames());
((MediaAdapter)mGridView.getAdapter()).updateDisplayFilenames(mConfig.getDisplayFileNames());
}
private void showSortingDialog() {
new ChangeSortingDialog(this, false, new ChangeSortingDialog.OnChangeSortingListener() {
@Override
public void sortingChanged() {
initializeGallery();
}
});
}
private void hideDirectory() {
mConfig.addHiddenDirectory(mPath);
if (!mConfig.getShowHiddenFolders())
finish();
else
invalidateOptionsMenu();
}
private void unhideDirectory() {
mConfig.removeHiddenDirectory(mPath);
invalidateOptionsMenu();
}
private void deleteDirectoryIfEmpty() {
final File file = new File(mPath);
if (file.isDirectory() && file.listFiles().length == 0) {
file.delete();
}
}
private List<Medium> getMedia() {
final List<Medium> media = new ArrayList<>();
final ArrayList<File> invalidFiles = new ArrayList<>();
for (int i = 0; i < 2; i++) {
if (mIsGetVideoIntent && i == 0)
continue;
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if (i == 1) {
if (mIsGetImageIntent)
continue;
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
}
final String where = MediaStore.Images.Media.DATA + " like ? ";
final String[] args = new String[]{mPath + "%"};
final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED};
final Cursor cursor = getContentResolver().query(uri, columns, where, args, null);
final String pattern = Pattern.quote(mPath) + "/[^/]*";
if (cursor != null) {
if (cursor.moveToFirst()) {
final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
do {
final String curPath = cursor.getString(pathIndex);
if (curPath == null)
continue;
if (curPath.matches(pattern) && !mToBeDeleted.contains(curPath)) {
final File file = new File(curPath);
if (file.exists()) {
final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED);
final long timestamp = cursor.getLong(dateIndex);
media.add(new Medium(file.getName(), curPath, (i == 1), timestamp, file.length()));
} else {
invalidFiles.add(file);
}
}
} while (cursor.moveToNext());
}
cursor.close();
}
}
Medium.Companion.setSorting(mConfig.getSorting());
Collections.sort(media);
Utils.Companion.scanFiles(getApplicationContext(), invalidFiles);
return media;
}
private boolean isDirEmpty() {
if (mMedia.size() <= 0) {
deleteDirectoryIfEmpty();
finish();
return true;
}
return false;
}
private void shareMedia() {
final List<Medium> selectedMedia = getSelectedMedia();
if (selectedMedia.size() <= 1) {
Utils.Companion.shareMedium(selectedMedia.get(0), this);
} else {
shareMedia(selectedMedia);
}
}
private void shareMedia(List<Medium> media) {
final String shareTitle = getResources().getString(R.string.share_via);
final Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.setType("image/* video/*");
final ArrayList<Uri> uris = new ArrayList<>(media.size());
for (Medium medium : media) {
final File file = new File(medium.getPath());
uris.add(Uri.fromFile(file));
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(intent, shareTitle));
}
private List<Medium> getSelectedMedia() {
final List<Medium> media = new ArrayList<>();
final SparseBooleanArray items = mGridView.getCheckedItemPositions();
final int cnt = items.size();
for (int i = 0; i < cnt; i++) {
if (items.valueAt(i)) {
final int id = items.keyAt(i);
media.add(mMedia.get(id));
}
}
return media;
}
private void prepareForDeleting() {
if (isShowingPermDialog(new File(mPath)))
return;
Utils.Companion.showToast(this, R.string.deleting);
final SparseBooleanArray items = mGridView.getCheckedItemPositions();
final int cnt = items.size();
int deletedCnt = 0;
for (int i = 0; i < cnt; i++) {
if (items.valueAt(i)) {
final int id = items.keyAt(i);
final String path = mMedia.get(id).getPath();
mToBeDeleted.add(path);
deletedCnt++;
}
}
notifyDeletion(deletedCnt);
}
private void notifyDeletion(int cnt) {
mMedia = getMedia();
if (mMedia.isEmpty()) {
deleteFiles();
} else {
final CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
final Resources res = getResources();
final String msg = res.getQuantityString(R.plurals.files_deleted, cnt, cnt);
mSnackbar = Snackbar.make(coordinator, msg, Snackbar.LENGTH_INDEFINITE);
mSnackbar.setAction(res.getString(R.string.undo), undoDeletion);
mSnackbar.setActionTextColor(Color.WHITE);
mSnackbar.show();
mIsSnackbarShown = true;
updateGridView();
}
}
private void deleteFiles() {
if (mToBeDeleted == null || mToBeDeleted.isEmpty())
return;
if (mSnackbar != null) {
mSnackbar.dismiss();
}
mIsSnackbarShown = false;
boolean wereFilesDeleted = false;
for (String delPath : mToBeDeleted) {
final File file = new File(delPath);
if (file.exists()) {
if (Utils.Companion.needsStupidWritePermissions(this, delPath)) {
if (isShowingPermDialog(file))
return;
final DocumentFile document = Utils.Companion.getFileDocument(this, delPath, mConfig.getTreeUri());
if (document.delete()) {
wereFilesDeleted = true;
}
} else {
if (file.delete())
wereFilesDeleted = true;
}
}
}
if (wereFilesDeleted) {
final String[] deletedPaths = mToBeDeleted.toArray(new String[mToBeDeleted.size()]);
MediaScannerConnection.scanFile(getApplicationContext(), deletedPaths, null, new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) {
if (mMedia != null && mMedia.isEmpty()) {
finish();
}
}
});
mToBeDeleted.clear();
}
}
private View.OnClickListener undoDeletion = new View.OnClickListener() {
@Override
public void onClick(View v) {
mSnackbar.dismiss();
mIsSnackbarShown = false;
mToBeDeleted.clear();
mMedia = getMedia();
updateGridView();
}
};
private void updateGridView() {
if (!isDirEmpty()) {
final MediaAdapter adapter = (MediaAdapter) mGridView.getAdapter();
adapter.updateItems(mMedia);
}
}
private void showProperties() {
final List<Medium> selectedMedia = getSelectedMedia();
if (selectedMedia.size() == 1) {
new PropertiesDialog(this, selectedMedia.get(0).getPath(), false);
} else {
final List<String> paths = new ArrayList<>(selectedMedia.size());
for (Medium medium : selectedMedia) {
paths.add(medium.getPath());
}
new PropertiesDialog(this, paths, false);
}
}
private boolean isSetWallpaperIntent() {
return getIntent().getBooleanExtra(Constants.INSTANCE.getSET_WALLPAPER_INTENT(), false);
}
private void displayCopyDialog() {
final ArrayList<File> files = new ArrayList<>();
final SparseBooleanArray items = mGridView.getCheckedItemPositions();
final int cnt = items.size();
for (int i = 0; i < cnt; i++) {
if (items.valueAt(i)) {
final int id = items.keyAt(i);
files.add(new File(mMedia.get(id).getPath()));
}
}
new CopyDialog(this, files, new CopyMoveTask.CopyMoveListener() {
@Override
public void copySucceeded(boolean deleted, boolean copiedAll) {
int msgId;
if (deleted) {
refreshDir();
msgId = copiedAll ? R.string.moving_success : R.string.moving_success_partial;
} else {
msgId = copiedAll? R.string.copying_success : R.string.copying_success_partial;
}
Utils.Companion.showToast(getApplicationContext(), msgId);
}
@Override
public void copyFailed() {
Utils.Companion.showToast(getApplicationContext(), R.string.copy_move_failed);
}
});
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final String curItemPath = mMedia.get(position).getPath();
if (isSetWallpaperIntent()) {
Utils.Companion.showToast(this, R.string.setting_wallpaper);
final int wantedWidth = getWallpaperDesiredMinimumWidth();
final int wantedHeight = getWallpaperDesiredMinimumHeight();
final float ratio = (float) wantedWidth / wantedHeight;
Glide.with(this).
load(new File(curItemPath)).
asBitmap().
override((int) (wantedWidth * ratio), wantedHeight).
fitCenter().
into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
try {
WallpaperManager.getInstance(getApplicationContext()).setBitmap(bitmap);
setResult(RESULT_OK);
} catch (IOException e) {
Log.e(TAG, "item click " + e.getMessage());
}
finish();
}
});
} else if (mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent) {
final Intent result = new Intent();
result.setData(Uri.parse(curItemPath));
setResult(RESULT_OK, result);
finish();
} else {
final Intent intent = new Intent(this, ViewPagerActivity.class);
intent.putExtra(Constants.INSTANCE.getMEDIUM(), curItemPath);
startActivity(intent);
}
}
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (checked) {
mSelectedItemsCnt++;
} else {
mSelectedItemsCnt--;
}
if (mSelectedItemsCnt > 0)
mode.setTitle(String.valueOf(mSelectedItemsCnt));
mode.invalidate();
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
final MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.cab_media, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.cab_properties:
showProperties();
return true;
case R.id.cab_share:
shareMedia();
return true;
case R.id.cab_delete:
prepareForDeleting();
mode.finish();
return true;
case R.id.cab_copy_move:
displayCopyDialog();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mSelectedItemsCnt = 0;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if (mIsSnackbarShown) {
deleteFiles();
}
return false;
}
@Override
public void onRefresh() {
refreshDir();
}
private void refreshDir() {
final File dir = new File(mPath);
if (dir.isDirectory()) {
Utils.Companion.scanPath(getApplicationContext(), mPath);
}
initializeGallery();
mSwipeRefreshLayout.setRefreshing(false);
}
}

View File

@ -0,0 +1,514 @@
package com.simplemobiletools.gallery.activities
import android.app.Activity
import android.app.WallpaperManager
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.support.design.widget.CoordinatorLayout
import android.support.design.widget.Snackbar
import android.support.v4.widget.SwipeRefreshLayout
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.animation.GlideAnimation
import com.bumptech.glide.request.target.SimpleTarget
import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask
import com.simplemobiletools.filepicker.extensions.scanPath
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog
import com.simplemobiletools.gallery.Constants
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.Utils
import com.simplemobiletools.gallery.adapters.MediaAdapter
import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog
import com.simplemobiletools.gallery.dialogs.CopyDialog
import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.activity_media.*
import java.io.File
import java.io.IOException
import java.util.*
import java.util.regex.Pattern
class MediaActivity : SimpleActivity(), AdapterView.OnItemClickListener/*, GridView.MultiChoiceModeListener, GridView.OnTouchListener*/, SwipeRefreshLayout.OnRefreshListener {
companion object {
private val TAG = MediaActivity::class.java.simpleName
private var mSnackbar: Snackbar? = null
lateinit var mToBeDeleted: MutableList<String>
lateinit var mMedia: MutableList<Medium>
private var mPath = ""
private var mIsSnackbarShown = false
private var mIsGetImageIntent = false
private var mIsGetVideoIntent = false
private var mIsGetAnyIntent = false
private var mSelectedItemsCnt = 0
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_media)
mIsGetImageIntent = intent.getBooleanExtra(Constants.GET_IMAGE_INTENT, false)
mIsGetVideoIntent = intent.getBooleanExtra(Constants.GET_VIDEO_INTENT, false)
mIsGetAnyIntent = intent.getBooleanExtra(Constants.GET_ANY_INTENT, false)
media_holder.setOnRefreshListener(this)
mPath = intent.getStringExtra(Constants.DIRECTORY)
mToBeDeleted = ArrayList<String>()
mMedia = ArrayList<Medium>()
}
override fun onResume() {
super.onResume()
tryloadGallery()
}
override fun onPause() {
super.onPause()
deleteFiles()
}
private fun tryloadGallery() {
if (Utils.hasStoragePermission(applicationContext)) {
initializeGallery()
} else {
finish()
}
}
private fun initializeGallery() {
val newMedia = getMedia()
if (newMedia.toString() == mMedia.toString()) {
return
}
mMedia = newMedia
if (isDirEmpty)
return
val adapter = MediaAdapter(this, mMedia)
media_grid.adapter = adapter
media_grid.onItemClickListener = this
/*mGridView!!.setMultiChoiceModeListener(this)
mGridView!!.setOnTouchListener(this)*/
mIsSnackbarShown = false
val dirName = Utils.getFilename(this, mPath)
title = dirName
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_media, menu)
val isFolderHidden = mConfig.getIsFolderHidden(mPath)
menu.findItem(R.id.hide_folder).isVisible = !isFolderHidden
menu.findItem(R.id.unhide_folder).isVisible = isFolderHidden
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.sort -> {
showSortingDialog()
true
}
R.id.toggle_filename -> {
toggleFilenameVisibility()
true
}
R.id.hide_folder -> {
hideDirectory()
true
}
R.id.unhide_folder -> {
unhideDirectory()
true
}
else -> super.onOptionsItemSelected(item)
}
}
private fun toggleFilenameVisibility() {
mConfig.displayFileNames = !mConfig.displayFileNames
(media_grid.adapter as MediaAdapter).updateDisplayFilenames(mConfig.displayFileNames)
}
private fun showSortingDialog() {
ChangeSortingDialog(this, false, object : ChangeSortingDialog.OnChangeSortingListener {
override fun sortingChanged() {
initializeGallery()
}
})
}
private fun hideDirectory() {
mConfig.addHiddenDirectory(mPath)
if (!mConfig.showHiddenFolders)
finish()
else
invalidateOptionsMenu()
}
private fun unhideDirectory() {
mConfig.removeHiddenDirectory(mPath)
invalidateOptionsMenu()
}
private fun deleteDirectoryIfEmpty() {
val file = File(mPath)
if (file.isDirectory && file.listFiles().isEmpty()) {
file.delete()
}
}
private fun getMedia(): MutableList<Medium> {
val media = ArrayList<Medium>()
val invalidFiles = ArrayList<File>()
for (i in 0..1) {
if (mIsGetVideoIntent && i == 0)
continue
var uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
if (i == 1) {
if (mIsGetImageIntent)
continue
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
}
val where = "${MediaStore.Images.Media.DATA} like ? "
val args = arrayOf("$mPath%")
val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED)
val cursor = contentResolver.query(uri, columns, where, args, null)
val pattern = "${Pattern.quote(mPath)}/[^/]*"
if (cursor != null) {
if (cursor.moveToFirst()) {
val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA)
do {
val curPath = cursor.getString(pathIndex) ?: continue
if (curPath.matches(pattern.toRegex()) && !mToBeDeleted.contains(curPath)) {
val file = File(curPath)
if (file.exists()) {
val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)
val timestamp = cursor.getLong(dateIndex)
media.add(Medium(file.name, curPath, i == 1, timestamp, file.length()))
} else {
invalidFiles.add(file)
}
}
} while (cursor.moveToNext())
}
cursor.close()
}
}
Medium.sorting = mConfig.sorting
Collections.sort(media)
Utils.scanFiles(applicationContext, invalidFiles)
return media
}
private val isDirEmpty: Boolean
get() {
if (mMedia.size <= 0) {
deleteDirectoryIfEmpty()
finish()
return true
}
return false
}
private fun shareMedia() {
val selectedMedia = selectedMedia
if (selectedMedia.size <= 1) {
Utils.shareMedium(selectedMedia[0], this)
} else {
shareMedia(selectedMedia)
}
}
private fun shareMedia(media: List<Medium>) {
val shareTitle = resources.getString(R.string.share_via)
val intent = Intent()
intent.action = Intent.ACTION_SEND_MULTIPLE
intent.type = "image/* video/*"
val uris = ArrayList<Uri>(media.size)
for (medium in media) {
val file = File(medium.path)
uris.add(Uri.fromFile(file))
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris)
startActivity(Intent.createChooser(intent, shareTitle))
}
private val selectedMedia: List<Medium>
get() {
val media = ArrayList<Medium>()
val items = media_grid.checkedItemPositions
val cnt = items.size()
for (i in 0..cnt - 1) {
if (items.valueAt(i)) {
val id = items.keyAt(i)
media.add(mMedia[id])
}
}
return media
}
private fun prepareForDeleting() {
if (isShowingPermDialog(File(mPath)))
return
Utils.showToast(this, R.string.deleting)
val items = media_grid.checkedItemPositions
val cnt = items.size()
var deletedCnt = 0
for (i in 0..cnt - 1) {
if (items.valueAt(i)) {
val id = items.keyAt(i)
val path = mMedia[id].path
mToBeDeleted.add(path)
deletedCnt++
}
}
notifyDeletion(deletedCnt)
}
private fun notifyDeletion(cnt: Int) {
mMedia = getMedia()
if (mMedia.isEmpty()) {
deleteFiles()
} else {
val coordinator = findViewById(R.id.coordinator_layout) as CoordinatorLayout
val res = resources
val msg = res.getQuantityString(R.plurals.files_deleted, cnt, cnt)
mSnackbar = Snackbar.make(coordinator, msg, Snackbar.LENGTH_INDEFINITE)
mSnackbar!!.apply {
setAction(res.getString(R.string.undo), undoDeletion)
setActionTextColor(Color.WHITE)
show()
}
mIsSnackbarShown = true
updateGridView()
}
}
private fun deleteFiles() {
if (mToBeDeleted.isEmpty())
return
if (mSnackbar != null) {
mSnackbar!!.dismiss()
}
mIsSnackbarShown = false
var wereFilesDeleted = false
for (delPath in mToBeDeleted) {
val file = File(delPath)
if (file.exists()) {
if (Utils.needsStupidWritePermissions(this, delPath)) {
if (isShowingPermDialog(file))
return
val document = Utils.getFileDocument(this, delPath, mConfig.treeUri)
if (document.delete()) {
wereFilesDeleted = true
}
} else {
if (file.delete())
wereFilesDeleted = true
}
}
}
if (wereFilesDeleted) {
val deletedPaths = mToBeDeleted.toTypedArray()
MediaScannerConnection.scanFile(applicationContext, deletedPaths, null) { path, uri ->
if (mMedia.isEmpty()) {
finish()
}
}
mToBeDeleted.clear()
}
}
private val undoDeletion = View.OnClickListener {
mSnackbar!!.dismiss()
mIsSnackbarShown = false
mToBeDeleted.clear()
mMedia = getMedia()
updateGridView()
}
private fun updateGridView() {
if (!isDirEmpty) {
val adapter = media_grid.adapter as MediaAdapter
adapter.updateItems(mMedia)
}
}
private fun showProperties() {
val selectedMedia = selectedMedia
if (selectedMedia.size == 1) {
PropertiesDialog(this, selectedMedia[0].path, false)
} else {
val paths = ArrayList<String>(selectedMedia.size)
for (medium in selectedMedia) {
paths.add(medium.path)
}
PropertiesDialog(this, paths, false)
}
}
private val isSetWallpaperIntent: Boolean
get() = intent.getBooleanExtra(Constants.SET_WALLPAPER_INTENT, false)
private fun displayCopyDialog() {
val files = ArrayList<File>()
val items = media_grid.checkedItemPositions
val cnt = items.size()
for (i in 0..cnt - 1) {
if (items.valueAt(i)) {
val id = items.keyAt(i)
files.add(File(mMedia[id].path))
}
}
CopyDialog(this, files, object : CopyMoveTask.CopyMoveListener {
override fun copySucceeded(deleted: Boolean, copiedAll: Boolean) {
val msgId: Int
if (deleted) {
refreshDir()
msgId = if (copiedAll) R.string.moving_success else R.string.moving_success_partial
} else {
msgId = if (copiedAll) R.string.copying_success else R.string.copying_success_partial
}
Utils.showToast(applicationContext, msgId)
}
override fun copyFailed() {
Utils.showToast(applicationContext, R.string.copy_move_failed)
}
})
}
override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) {
val curItemPath = mMedia[position].path
if (isSetWallpaperIntent) {
Utils.showToast(this, R.string.setting_wallpaper)
val wantedWidth = wallpaperDesiredMinimumWidth
val wantedHeight = wallpaperDesiredMinimumHeight
val ratio = wantedWidth.toFloat() / wantedHeight
Glide.with(this).load(File(curItemPath)).asBitmap().override((wantedWidth * ratio).toInt(), wantedHeight).fitCenter().into(object : SimpleTarget<Bitmap>() {
override fun onResourceReady(bitmap: Bitmap?, glideAnimation: GlideAnimation<in Bitmap>?) {
try {
WallpaperManager.getInstance(applicationContext).setBitmap(bitmap)
setResult(Activity.RESULT_OK)
} catch (e: IOException) {
Log.e(TAG, "item click " + e.message)
}
finish()
}
})
} else if (mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent) {
val result = Intent()
result.data = Uri.parse(curItemPath)
setResult(Activity.RESULT_OK, result)
finish()
} else {
val intent = Intent(this, ViewPagerActivity::class.java)
intent.putExtra(Constants.MEDIUM, curItemPath)
startActivity(intent)
}
}
/*override fun onItemCheckedStateChanged(mode: ActionMode, position: Int, id: Long, checked: Boolean) {
if (checked) {
mSelectedItemsCnt++
} else {
mSelectedItemsCnt--
}
if (mSelectedItemsCnt > 0)
mode.title = mSelectedItemsCnt.toString()
mode.invalidate()
}
override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
val inflater = mode.menuInflater
inflater.inflate(R.menu.cab_media, menu)
return true
}
override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
return false
}
override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
when (item.itemId) {
R.id.cab_properties -> {
showProperties()
return true
}
R.id.cab_share -> {
shareMedia()
return true
}
R.id.cab_delete -> {
prepareForDeleting()
mode.finish()
return true
}
R.id.cab_copy_move -> {
displayCopyDialog()
return true
}
else -> return false
}
}
override fun onDestroyActionMode(mode: ActionMode) {
mSelectedItemsCnt = 0
}
override fun onTouch(v: View, event: MotionEvent): Boolean {
if (mIsSnackbarShown) {
deleteFiles()
}
return false
}*/
override fun onRefresh() {
refreshDir()
}
private fun refreshDir() {
val dir = File(mPath)
if (dir.isDirectory) {
scanPath(mPath) {}
}
initializeGallery()
media_holder.isRefreshing = false
}
}