mirror of
https://github.com/FossifyOrg/Gallery.git
synced 2024-11-26 22:47:59 +01:00
convert ViewPagerActivity to kotlin
This commit is contained in:
parent
7ced072f9c
commit
719c510f59
4 changed files with 476 additions and 531 deletions
|
@ -20,9 +20,7 @@ import java.util.*
|
|||
|
||||
class Utils {
|
||||
companion object {
|
||||
fun getFilename(path: String): String {
|
||||
return path.substring(path.lastIndexOf("/") + 1)
|
||||
}
|
||||
fun getFilename(path: String) = path.substring(path.lastIndexOf("/") + 1)
|
||||
|
||||
fun showToast(context: Context, resId: Int) {
|
||||
context.toast(resId)
|
||||
|
@ -94,12 +92,10 @@ class Utils {
|
|||
val uri = Uri.fromFile(file)
|
||||
intent.action = Intent.ACTION_SEND
|
||||
intent.putExtra(Intent.EXTRA_STREAM, uri)
|
||||
intent.type = getMimeType(medium)
|
||||
intent.type = medium.getMimeType()
|
||||
activity.startActivity(Intent.createChooser(intent, shareTitle))
|
||||
}
|
||||
|
||||
fun getMimeType(medium: Medium) = if (medium.isVideo) "video/*" else "image/*"
|
||||
|
||||
fun showSystemUI(actionbar: ActionBar?, window: Window) {
|
||||
actionbar?.show()
|
||||
|
||||
|
|
|
@ -1,525 +0,0 @@
|
|||
package com.simplemobiletools.gallery.activities;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.database.Cursor;
|
||||
import android.media.MediaScannerConnection;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.provider.MediaStore;
|
||||
import android.support.v4.provider.DocumentFile;
|
||||
import android.support.v4.view.ViewPager;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.widget.RelativeLayout;
|
||||
|
||||
import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask;
|
||||
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog;
|
||||
import com.simplemobiletools.gallery.Constants;
|
||||
import com.simplemobiletools.gallery.MyViewPager;
|
||||
import com.simplemobiletools.gallery.R;
|
||||
import com.simplemobiletools.gallery.Utils;
|
||||
import com.simplemobiletools.gallery.adapters.MyPagerAdapter;
|
||||
import com.simplemobiletools.gallery.dialogs.CopyDialog;
|
||||
import com.simplemobiletools.gallery.dialogs.RenameFileDialog;
|
||||
import com.simplemobiletools.gallery.fragments.ViewPagerFragment;
|
||||
import com.simplemobiletools.gallery.models.Medium;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.OnClick;
|
||||
|
||||
public class ViewPagerActivity extends SimpleActivity
|
||||
implements ViewPager.OnPageChangeListener, View.OnSystemUiVisibilityChangeListener, ViewPager.OnTouchListener,
|
||||
ViewPagerFragment.FragmentClickListener {
|
||||
@BindView(R.id.undo_delete) View mUndoBtn;
|
||||
@BindView(R.id.view_pager) MyViewPager mPager;
|
||||
|
||||
private static final int EDIT_IMAGE = 1;
|
||||
private static final int SET_WALLPAPER = 2;
|
||||
private static ActionBar mActionbar;
|
||||
private static List<Medium> mMedia;
|
||||
private static String mPath;
|
||||
private static String mDirectory;
|
||||
private static String mToBeDeleted;
|
||||
private static String mBeingDeleted;
|
||||
|
||||
private static boolean mIsFullScreen;
|
||||
private static boolean mIsUndoShown;
|
||||
private static int mPos;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_medium);
|
||||
ButterKnife.bind(this);
|
||||
|
||||
if (!Utils.Companion.hasStoragePermission(getApplicationContext())) {
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
final Uri uri = getIntent().getData();
|
||||
if (uri != null) {
|
||||
Cursor cursor = null;
|
||||
try {
|
||||
final String[] proj = {MediaStore.Images.Media.DATA};
|
||||
cursor = getContentResolver().query(uri, proj, null, null, null);
|
||||
if (cursor != null) {
|
||||
final int dataIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
|
||||
cursor.moveToFirst();
|
||||
mPath = cursor.getString(dataIndex);
|
||||
}
|
||||
} finally {
|
||||
if (cursor != null) {
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
mPath = getIntent().getStringExtra(Constants.MEDIUM);
|
||||
}
|
||||
|
||||
if (mPath == null || mPath.isEmpty()) {
|
||||
Utils.Companion.showToast(getApplicationContext(), R.string.unknown_error);
|
||||
finish();
|
||||
return;
|
||||
}
|
||||
|
||||
mPos = 0;
|
||||
mIsFullScreen = true;
|
||||
mActionbar = getSupportActionBar();
|
||||
mToBeDeleted = "";
|
||||
mBeingDeleted = "";
|
||||
hideSystemUI();
|
||||
|
||||
Utils.Companion.scanPath(getApplicationContext(), mPath);
|
||||
addUndoMargin();
|
||||
mDirectory = new File(mPath).getParent();
|
||||
mMedia = getMedia();
|
||||
if (isDirEmpty())
|
||||
return;
|
||||
|
||||
final MyPagerAdapter adapter = new MyPagerAdapter(this, getSupportFragmentManager(), mMedia);
|
||||
mPager.setAdapter(adapter);
|
||||
mPager.setCurrentItem(mPos);
|
||||
mPager.addOnPageChangeListener(this);
|
||||
mPager.setOnTouchListener(this);
|
||||
|
||||
getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(this);
|
||||
updateActionbarTitle();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
if (!Utils.Companion.hasStoragePermission(getApplicationContext())) {
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
@OnClick(R.id.undo_delete)
|
||||
public void undoDeletion() {
|
||||
mIsUndoShown = false;
|
||||
mToBeDeleted = "";
|
||||
mBeingDeleted = "";
|
||||
mUndoBtn.setVisibility(View.GONE);
|
||||
reloadViewPager();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.viewpager_menu, menu);
|
||||
menu.findItem(R.id.menu_set_as_wallpaper).setVisible(getCurrentMedium().isImage());
|
||||
menu.findItem(R.id.menu_edit).setVisible(getCurrentMedium().isImage());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
deleteFile();
|
||||
switch (item.getItemId()) {
|
||||
case R.id.menu_set_as_wallpaper:
|
||||
setAsWallpaper();
|
||||
return true;
|
||||
case R.id.menu_copy_move:
|
||||
displayCopyDialog();
|
||||
return true;
|
||||
case R.id.menu_open_with:
|
||||
openWith();
|
||||
return true;
|
||||
case R.id.menu_share:
|
||||
shareMedium();
|
||||
return true;
|
||||
case R.id.menu_delete:
|
||||
notifyDeletion();
|
||||
return true;
|
||||
case R.id.menu_rename:
|
||||
editMedium();
|
||||
return true;
|
||||
case R.id.menu_edit:
|
||||
openEditor();
|
||||
return true;
|
||||
case R.id.menu_properties:
|
||||
showProperties();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConfigurationChanged(Configuration newConfig) {
|
||||
super.onConfigurationChanged(newConfig);
|
||||
final MyPagerAdapter adapter = (MyPagerAdapter) mPager.getAdapter();
|
||||
adapter.updateItems(mPos);
|
||||
}
|
||||
|
||||
private void displayCopyDialog() {
|
||||
final File file = getCurrentFile();
|
||||
final ArrayList<File> files = new ArrayList<>();
|
||||
files.add(file);
|
||||
new CopyDialog(this, files, new CopyMoveTask.CopyMoveListener() {
|
||||
@Override
|
||||
public void copySucceeded(boolean deleted, boolean copiedAll) {
|
||||
int msgId;
|
||||
if (deleted) {
|
||||
reloadViewPager();
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void openEditor() {
|
||||
final Intent intent = new Intent(Intent.ACTION_EDIT);
|
||||
intent.setDataAndType(Uri.fromFile(getCurrentFile()), "image/*");
|
||||
final Intent chooser = Intent.createChooser(intent, getString(R.string.edit_image_with));
|
||||
|
||||
if (intent.resolveActivity(getPackageManager()) != null) {
|
||||
startActivityForResult(chooser, EDIT_IMAGE);
|
||||
} else {
|
||||
Utils.Companion.showToast(getApplicationContext(), R.string.no_editor_found);
|
||||
}
|
||||
}
|
||||
|
||||
private void setAsWallpaper() {
|
||||
final Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
|
||||
intent.setDataAndType(Uri.fromFile(getCurrentFile()), "image/jpeg");
|
||||
final Intent chooser = Intent.createChooser(intent, getString(R.string.set_as_wallpaper_with));
|
||||
|
||||
if (intent.resolveActivity(getPackageManager()) != null) {
|
||||
startActivityForResult(chooser, SET_WALLPAPER);
|
||||
} else {
|
||||
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.Companion.getMimeType(getCurrentMedium()));
|
||||
final Intent chooser = Intent.createChooser(intent, getString(R.string.open_with));
|
||||
|
||||
if (intent.resolveActivity(getPackageManager()) != null) {
|
||||
startActivity(chooser);
|
||||
} else {
|
||||
Utils.Companion.showToast(getApplicationContext(), R.string.no_app_found);
|
||||
}
|
||||
}
|
||||
|
||||
private void showProperties() {
|
||||
new PropertiesDialog(this, getCurrentFile().getAbsolutePath(), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == EDIT_IMAGE) {
|
||||
if (resultCode == RESULT_OK && data != null) {
|
||||
final MyPagerAdapter adapter = (MyPagerAdapter) mPager.getAdapter();
|
||||
adapter.updateItems(mPos);
|
||||
}
|
||||
} else if (requestCode == SET_WALLPAPER) {
|
||||
if (resultCode == RESULT_OK) {
|
||||
Utils.Companion.showToast(getApplicationContext(), R.string.wallpaper_set_successfully);
|
||||
}
|
||||
}
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
private void shareMedium() {
|
||||
final Medium medium = getCurrentMedium();
|
||||
Utils.Companion.shareMedium(medium, this);
|
||||
}
|
||||
|
||||
private void notifyDeletion() {
|
||||
if (isShowingPermDialog(new File(mPath)))
|
||||
return;
|
||||
|
||||
mToBeDeleted = getCurrentFile().getAbsolutePath();
|
||||
if (mMedia.size() <= 1) {
|
||||
deleteFile();
|
||||
} else {
|
||||
Utils.Companion.showToast(this, R.string.file_deleted);
|
||||
mUndoBtn.setVisibility(View.VISIBLE);
|
||||
mIsUndoShown = true;
|
||||
reloadViewPager();
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteFile() {
|
||||
if (mToBeDeleted.isEmpty())
|
||||
return;
|
||||
|
||||
mIsUndoShown = false;
|
||||
mBeingDeleted = "";
|
||||
boolean mWasFileDeleted = false;
|
||||
|
||||
final File file = new File(mToBeDeleted);
|
||||
if (Utils.Companion.needsStupidWritePermissions(this, mToBeDeleted)) {
|
||||
if (!isShowingPermDialog(file)) {
|
||||
final DocumentFile document = Utils.Companion.getFileDocument(this, mToBeDeleted, mConfig.getTreeUri());
|
||||
if (document.canWrite()) {
|
||||
mWasFileDeleted = document.delete();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
mWasFileDeleted = file.delete();
|
||||
}
|
||||
|
||||
if (mWasFileDeleted) {
|
||||
mBeingDeleted = mToBeDeleted;
|
||||
final String[] deletedPath = new String[]{mToBeDeleted};
|
||||
MediaScannerConnection.scanFile(getApplicationContext(), deletedPath, null, new MediaScannerConnection.OnScanCompletedListener() {
|
||||
@Override
|
||||
public void onScanCompleted(String path, Uri uri) {
|
||||
scanCompleted();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
mToBeDeleted = "";
|
||||
mUndoBtn.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
private boolean isDirEmpty() {
|
||||
if (mMedia.size() <= 0) {
|
||||
deleteDirectoryIfEmpty();
|
||||
finish();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void editMedium() {
|
||||
new RenameFileDialog(this, getCurrentFile(), new RenameFileDialog.OnRenameFileListener() {
|
||||
@Override
|
||||
public void onRenameFileSuccess(@NotNull File newFile) {
|
||||
mMedia.get(mPager.getCurrentItem()).setPath(newFile.getAbsolutePath());
|
||||
updateActionbarTitle();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void reloadViewPager() {
|
||||
final MyPagerAdapter adapter = (MyPagerAdapter) mPager.getAdapter();
|
||||
final int curPos = mPager.getCurrentItem();
|
||||
mMedia = getMedia();
|
||||
if (isDirEmpty())
|
||||
return;
|
||||
|
||||
mPager.setAdapter(null);
|
||||
adapter.updateItems(mMedia);
|
||||
mPager.setAdapter(adapter);
|
||||
|
||||
final int newPos = Math.min(curPos, adapter.getCount());
|
||||
mPager.setCurrentItem(newPos);
|
||||
updateActionbarTitle();
|
||||
}
|
||||
|
||||
private void deleteDirectoryIfEmpty() {
|
||||
final File file = new File(mDirectory);
|
||||
if (file.isDirectory() && file.listFiles().length == 0) {
|
||||
file.delete();
|
||||
}
|
||||
|
||||
Utils.Companion.scanPath(getApplicationContext(), mDirectory);
|
||||
}
|
||||
|
||||
private List<Medium> getMedia() {
|
||||
final List<Medium> media = new ArrayList<>();
|
||||
final ArrayList<File> invalidFiles = new ArrayList<>();
|
||||
for (int i = 0; i < 2; i++) {
|
||||
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
|
||||
if (i == 1) {
|
||||
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
|
||||
}
|
||||
final String where = MediaStore.Images.Media.DATA + " like ? ";
|
||||
final String[] args = new String[]{mDirectory + "%"};
|
||||
final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media.SIZE};
|
||||
final Cursor cursor = getContentResolver().query(uri, columns, where, args, null);
|
||||
final String pattern = Pattern.quote(mDirectory) + "/[^/]*";
|
||||
|
||||
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;
|
||||
|
||||
final File file = new File(curPath);
|
||||
if (!file.exists()) {
|
||||
invalidFiles.add(file);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (curPath.matches(pattern) && !curPath.equals(mToBeDeleted) && !curPath.equals(mBeingDeleted)) {
|
||||
final int dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED);
|
||||
final long timestamp = cursor.getLong(dateIndex);
|
||||
|
||||
final int sizeIndex = cursor.getColumnIndex(MediaStore.Images.Media.SIZE);
|
||||
final long size = cursor.getLong(sizeIndex);
|
||||
media.add(new Medium(file.getName(), curPath, i == 1, timestamp, size));
|
||||
}
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
Utils.Companion.scanFiles(getApplicationContext(), invalidFiles);
|
||||
Medium.Companion.setSorting(mConfig.getSorting());
|
||||
Collections.sort(media);
|
||||
int j = 0;
|
||||
for (Medium medium : media) {
|
||||
if (medium.getPath().equals(mPath)) {
|
||||
mPos = j;
|
||||
break;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return media;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fragmentClicked() {
|
||||
deleteFile();
|
||||
mIsFullScreen = !mIsFullScreen;
|
||||
if (mIsFullScreen) {
|
||||
hideSystemUI();
|
||||
} else {
|
||||
showSystemUI();
|
||||
}
|
||||
}
|
||||
|
||||
private void hideSystemUI() {
|
||||
Utils.Companion.hideSystemUI(mActionbar, getWindow());
|
||||
}
|
||||
|
||||
private void showSystemUI() {
|
||||
Utils.Companion.showSystemUI(mActionbar, getWindow());
|
||||
}
|
||||
|
||||
private void updateActionbarTitle() {
|
||||
setTitle(Utils.Companion.getFilename(mMedia.get(mPager.getCurrentItem()).getPath()));
|
||||
}
|
||||
|
||||
private Medium getCurrentMedium() {
|
||||
if (mPos >= mMedia.size())
|
||||
mPos = mMedia.size() - 1;
|
||||
return mMedia.get(mPos);
|
||||
}
|
||||
|
||||
private File getCurrentFile() {
|
||||
return new File(getCurrentMedium().getPath());
|
||||
}
|
||||
|
||||
private void addUndoMargin() {
|
||||
final Resources res = getResources();
|
||||
final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mUndoBtn.getLayoutParams();
|
||||
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.Companion.getNavBarHeight(res);
|
||||
}
|
||||
|
||||
params.setMargins(params.leftMargin, topMargin, rightMargin, params.bottomMargin);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageSelected(int position) {
|
||||
updateActionbarTitle();
|
||||
mPos = position;
|
||||
supportInvalidateOptionsMenu();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageScrollStateChanged(int state) {
|
||||
if (state == ViewPager.SCROLL_STATE_DRAGGING) {
|
||||
final MyPagerAdapter adapter = (MyPagerAdapter) mPager.getAdapter();
|
||||
adapter.itemDragged(mPos);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSystemUiVisibilityChange(int visibility) {
|
||||
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
|
||||
mIsFullScreen = false;
|
||||
}
|
||||
|
||||
final MyPagerAdapter adapter = (MyPagerAdapter) mPager.getAdapter();
|
||||
adapter.updateUiVisibility(mIsFullScreen, mPos);
|
||||
}
|
||||
|
||||
private void scanCompleted() {
|
||||
mBeingDeleted = "";
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (mMedia != null && mMedia.size() <= 1) {
|
||||
reloadViewPager();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouch(View v, MotionEvent event) {
|
||||
if (mIsUndoShown) {
|
||||
deleteFile();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
deleteFile();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,472 @@
|
|||
package com.simplemobiletools.gallery.activities
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.content.res.Configuration
|
||||
import android.database.Cursor
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.provider.MediaStore
|
||||
import android.support.v4.view.ViewPager
|
||||
import android.support.v7.app.ActionBar
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.widget.RelativeLayout
|
||||
import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask
|
||||
import com.simplemobiletools.filepicker.extensions.*
|
||||
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.MyPagerAdapter
|
||||
import com.simplemobiletools.gallery.dialogs.CopyDialog
|
||||
import com.simplemobiletools.gallery.dialogs.RenameFileDialog
|
||||
import com.simplemobiletools.gallery.fragments.ViewPagerFragment
|
||||
import com.simplemobiletools.gallery.models.Medium
|
||||
import kotlinx.android.synthetic.main.activity_medium.*
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import java.util.regex.Pattern
|
||||
|
||||
class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View.OnSystemUiVisibilityChangeListener, ViewPagerFragment.FragmentClickListener {
|
||||
private var mActionbar: ActionBar? = null
|
||||
private var mMedia: MutableList<Medium>? = null
|
||||
private var mPath = ""
|
||||
private var mDirectory = ""
|
||||
private var mToBeDeleted = ""
|
||||
private var mBeingDeleted = ""
|
||||
|
||||
private var mIsFullScreen = false
|
||||
private var mIsUndoShown = false
|
||||
private var mPos = 0
|
||||
|
||||
companion object {
|
||||
private val EDIT_IMAGE = 1
|
||||
private val SET_WALLPAPER = 2
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_medium)
|
||||
|
||||
if (!hasStoragePermission()) {
|
||||
finish()
|
||||
return
|
||||
}
|
||||
|
||||
val uri = intent.data
|
||||
if (uri != null) {
|
||||
var cursor: Cursor? = null
|
||||
try {
|
||||
val proj = arrayOf(MediaStore.Images.Media.DATA)
|
||||
cursor = contentResolver.query(uri, proj, null, null, null)
|
||||
if (cursor != null) {
|
||||
val dataIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
|
||||
cursor.moveToFirst()
|
||||
mPath = cursor.getString(dataIndex)
|
||||
}
|
||||
} finally {
|
||||
cursor?.close()
|
||||
}
|
||||
} else {
|
||||
mPath = intent.getStringExtra(Constants.MEDIUM)
|
||||
}
|
||||
|
||||
if (mPath.isEmpty()) {
|
||||
toast(R.string.unknown_error)
|
||||
finish()
|
||||
return
|
||||
}
|
||||
|
||||
mPos = 0
|
||||
mIsFullScreen = true
|
||||
mActionbar = supportActionBar
|
||||
mToBeDeleted = ""
|
||||
mBeingDeleted = ""
|
||||
hideSystemUI()
|
||||
|
||||
scanPath(mPath) {}
|
||||
addUndoMargin()
|
||||
mDirectory = File(mPath).parent
|
||||
mMedia = getMedia()
|
||||
if (isDirEmpty())
|
||||
return
|
||||
|
||||
val pagerAdapter = MyPagerAdapter(this, supportFragmentManager, mMedia!!)
|
||||
view_pager.apply {
|
||||
adapter = pagerAdapter
|
||||
currentItem = mPos
|
||||
addOnPageChangeListener(this@ViewPagerActivity)
|
||||
}
|
||||
|
||||
window.decorView.setOnSystemUiVisibilityChangeListener(this)
|
||||
updateActionbarTitle()
|
||||
undo_delete.setOnClickListener { undoDeletion() }
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
if (!hasStoragePermission()) {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
fun undoDeletion() {
|
||||
mIsUndoShown = false
|
||||
mToBeDeleted = ""
|
||||
mBeingDeleted = ""
|
||||
undo_delete.visibility = View.GONE
|
||||
reloadViewPager()
|
||||
}
|
||||
|
||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||
menuInflater.inflate(R.menu.viewpager_menu, menu)
|
||||
menu.findItem(R.id.menu_set_as_wallpaper).isVisible = getCurrentMedium().isImage
|
||||
menu.findItem(R.id.menu_edit).isVisible = getCurrentMedium().isImage
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
deleteFile()
|
||||
return when (item.itemId) {
|
||||
R.id.menu_set_as_wallpaper -> {
|
||||
setAsWallpaper()
|
||||
true
|
||||
}
|
||||
R.id.menu_copy_move -> {
|
||||
displayCopyDialog()
|
||||
true
|
||||
}
|
||||
R.id.menu_open_with -> {
|
||||
openWith()
|
||||
true
|
||||
}
|
||||
R.id.menu_share -> {
|
||||
shareMedium()
|
||||
true
|
||||
}
|
||||
R.id.menu_delete -> {
|
||||
notifyDeletion()
|
||||
true
|
||||
}
|
||||
R.id.menu_rename -> {
|
||||
editMedium()
|
||||
true
|
||||
}
|
||||
R.id.menu_edit -> {
|
||||
openEditor()
|
||||
true
|
||||
}
|
||||
R.id.menu_properties -> {
|
||||
showProperties()
|
||||
true
|
||||
}
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onConfigurationChanged(newConfig: Configuration) {
|
||||
super.onConfigurationChanged(newConfig)
|
||||
val adapter = view_pager.adapter as MyPagerAdapter
|
||||
adapter.updateItems(mPos)
|
||||
}
|
||||
|
||||
private fun displayCopyDialog() {
|
||||
val files = ArrayList<File>()
|
||||
files.add(getCurrentFile())
|
||||
CopyDialog(this, files, object : CopyMoveTask.CopyMoveListener {
|
||||
override fun copySucceeded(deleted: Boolean, copiedAll: Boolean) {
|
||||
val msgId: Int
|
||||
if (deleted) {
|
||||
reloadViewPager()
|
||||
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
|
||||
}
|
||||
toast(msgId)
|
||||
}
|
||||
|
||||
override fun copyFailed() {
|
||||
toast(R.string.copy_move_failed)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun openEditor() {
|
||||
val intent = Intent(Intent.ACTION_EDIT)
|
||||
intent.setDataAndType(Uri.fromFile(getCurrentFile()), "image/*")
|
||||
val chooser = Intent.createChooser(intent, getString(R.string.edit_image_with))
|
||||
|
||||
if (intent.resolveActivity(packageManager) != null) {
|
||||
startActivityForResult(chooser, EDIT_IMAGE)
|
||||
} else {
|
||||
toast(R.string.no_editor_found)
|
||||
}
|
||||
}
|
||||
|
||||
private fun setAsWallpaper() {
|
||||
val intent = Intent(Intent.ACTION_ATTACH_DATA)
|
||||
intent.setDataAndType(Uri.fromFile(getCurrentFile()), "image/jpeg")
|
||||
val chooser = Intent.createChooser(intent, getString(R.string.set_as_wallpaper_with))
|
||||
|
||||
if (intent.resolveActivity(packageManager) != null) {
|
||||
startActivityForResult(chooser, SET_WALLPAPER)
|
||||
} else {
|
||||
toast(R.string.no_wallpaper_setter_found)
|
||||
}
|
||||
}
|
||||
|
||||
private fun openWith() {
|
||||
val intent = Intent(Intent.ACTION_VIEW)
|
||||
intent.setDataAndType(Uri.fromFile(getCurrentFile()), getCurrentMedium().getMimeType())
|
||||
val chooser = Intent.createChooser(intent, getString(R.string.open_with))
|
||||
|
||||
if (intent.resolveActivity(packageManager) != null) {
|
||||
startActivity(chooser)
|
||||
} else {
|
||||
toast(R.string.no_app_found)
|
||||
}
|
||||
}
|
||||
|
||||
private fun showProperties() {
|
||||
PropertiesDialog(this, getCurrentFile().absolutePath, false)
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
|
||||
if (requestCode == EDIT_IMAGE) {
|
||||
if (resultCode == Activity.RESULT_OK && resultData != null) {
|
||||
val adapter = view_pager.adapter as MyPagerAdapter
|
||||
adapter.updateItems(mPos)
|
||||
}
|
||||
} else if (requestCode == SET_WALLPAPER) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
toast(R.string.wallpaper_set_successfully)
|
||||
}
|
||||
}
|
||||
super.onActivityResult(requestCode, resultCode, resultData)
|
||||
}
|
||||
|
||||
private fun shareMedium() {
|
||||
Utils.shareMedium(getCurrentMedium(), this)
|
||||
}
|
||||
|
||||
private fun notifyDeletion() {
|
||||
if (isShowingPermDialog(File(mPath)))
|
||||
return
|
||||
|
||||
mToBeDeleted = getCurrentFile().absolutePath
|
||||
if (mMedia!!.size <= 1) {
|
||||
deleteFile()
|
||||
} else {
|
||||
toast(R.string.file_deleted)
|
||||
undo_delete.visibility = View.VISIBLE
|
||||
mIsUndoShown = true
|
||||
reloadViewPager()
|
||||
}
|
||||
}
|
||||
|
||||
private fun deleteFile() {
|
||||
if (mToBeDeleted.isEmpty())
|
||||
return
|
||||
|
||||
mIsUndoShown = false
|
||||
mBeingDeleted = ""
|
||||
var mWasFileDeleted = false
|
||||
|
||||
val file = File(mToBeDeleted)
|
||||
if (needsStupidWritePermissions(mToBeDeleted)) {
|
||||
if (!isShowingPermDialog(file)) {
|
||||
val document = getFileDocument(mToBeDeleted, mConfig.treeUri)
|
||||
if (document.canWrite()) {
|
||||
mWasFileDeleted = document.delete()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
mWasFileDeleted = file.delete()
|
||||
}
|
||||
|
||||
if (mWasFileDeleted) {
|
||||
mBeingDeleted = mToBeDeleted
|
||||
scanPath(mToBeDeleted) { scanCompleted() }
|
||||
}
|
||||
|
||||
mToBeDeleted = ""
|
||||
undo_delete.visibility = View.GONE
|
||||
}
|
||||
|
||||
private fun isDirEmpty(): Boolean {
|
||||
return if (mMedia!!.size <= 0) {
|
||||
deleteDirectoryIfEmpty()
|
||||
finish()
|
||||
true
|
||||
} else
|
||||
false
|
||||
}
|
||||
|
||||
private fun editMedium() {
|
||||
RenameFileDialog(this, getCurrentFile(), object : RenameFileDialog.OnRenameFileListener {
|
||||
override fun onRenameFileSuccess(newFile: File) {
|
||||
mMedia!![view_pager.currentItem].path = newFile.absolutePath
|
||||
updateActionbarTitle()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun reloadViewPager() {
|
||||
val adapter = view_pager.adapter as MyPagerAdapter
|
||||
val curPos = view_pager.currentItem
|
||||
mMedia = getMedia()
|
||||
if (isDirEmpty())
|
||||
return
|
||||
|
||||
view_pager.adapter = null
|
||||
adapter.updateItems(mMedia!!)
|
||||
view_pager.adapter = adapter
|
||||
|
||||
val newPos = Math.min(curPos, adapter.count)
|
||||
view_pager.currentItem = newPos
|
||||
updateActionbarTitle()
|
||||
}
|
||||
|
||||
private fun deleteDirectoryIfEmpty() {
|
||||
val file = File(mDirectory)
|
||||
if (file.isDirectory && file.listFiles().size == 0) {
|
||||
file.delete()
|
||||
}
|
||||
|
||||
scanPath(mDirectory) {}
|
||||
}
|
||||
|
||||
private fun getMedia(): MutableList<Medium> {
|
||||
val media = ArrayList<Medium>()
|
||||
val invalidFiles = ArrayList<File>()
|
||||
for (i in 0..1) {
|
||||
var uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
|
||||
if (i == 1) {
|
||||
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
|
||||
}
|
||||
|
||||
val where = "${MediaStore.Images.Media.DATA} like ? "
|
||||
val args = arrayOf("$mDirectory%")
|
||||
val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media.SIZE)
|
||||
val cursor = contentResolver.query(uri, columns, where, args, null)
|
||||
val pattern = "${Pattern.quote(mDirectory)}/[^/]*"
|
||||
|
||||
if (cursor?.moveToFirst() == true) {
|
||||
val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA)
|
||||
do {
|
||||
val curPath = cursor.getString(pathIndex) ?: continue
|
||||
|
||||
val file = File(curPath)
|
||||
if (!file.exists()) {
|
||||
invalidFiles.add(file)
|
||||
continue
|
||||
}
|
||||
|
||||
if (curPath.matches(pattern.toRegex()) && curPath != mToBeDeleted && curPath != mBeingDeleted) {
|
||||
val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)
|
||||
val timestamp = cursor.getLong(dateIndex)
|
||||
|
||||
val sizeIndex = cursor.getColumnIndex(MediaStore.Images.Media.SIZE)
|
||||
val size = cursor.getLong(sizeIndex)
|
||||
media.add(Medium(file.name, curPath, i == 1, timestamp, size))
|
||||
}
|
||||
} while (cursor.moveToNext())
|
||||
}
|
||||
cursor?.close()
|
||||
}
|
||||
|
||||
scanFiles(invalidFiles) {}
|
||||
Medium.sorting = mConfig.sorting
|
||||
Collections.sort(media)
|
||||
var j = 0
|
||||
for (medium in media) {
|
||||
if (medium.path == mPath) {
|
||||
mPos = j
|
||||
break
|
||||
}
|
||||
j++
|
||||
}
|
||||
return media
|
||||
}
|
||||
|
||||
override fun fragmentClicked() {
|
||||
deleteFile()
|
||||
mIsFullScreen = !mIsFullScreen
|
||||
if (mIsFullScreen) {
|
||||
hideSystemUI()
|
||||
} else {
|
||||
showSystemUI()
|
||||
}
|
||||
}
|
||||
|
||||
private fun hideSystemUI() = Utils.hideSystemUI(mActionbar, window)
|
||||
|
||||
private fun showSystemUI() = Utils.showSystemUI(mActionbar, window)
|
||||
|
||||
private fun updateActionbarTitle() {
|
||||
title = mMedia!![view_pager.currentItem].path.getFilenameFromPath()
|
||||
}
|
||||
|
||||
private fun getCurrentMedium(): Medium {
|
||||
if (mPos >= mMedia!!.size)
|
||||
mPos = mMedia!!.size - 1
|
||||
return mMedia!![mPos]
|
||||
}
|
||||
|
||||
private fun getCurrentFile() = File(getCurrentMedium().path)
|
||||
|
||||
private fun addUndoMargin() {
|
||||
val res = resources
|
||||
val params = undo_delete.layoutParams as RelativeLayout.LayoutParams
|
||||
val topMargin = Utils.getStatusBarHeight(res) + Utils.getActionBarHeight(applicationContext, res)
|
||||
var rightMargin = params.rightMargin
|
||||
|
||||
if (res.configuration.orientation != Configuration.ORIENTATION_PORTRAIT) {
|
||||
rightMargin += Utils.getNavBarHeight(res)
|
||||
}
|
||||
|
||||
params.setMargins(params.leftMargin, topMargin, rightMargin, params.bottomMargin)
|
||||
}
|
||||
|
||||
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
|
||||
|
||||
}
|
||||
|
||||
override fun onPageSelected(position: Int) {
|
||||
updateActionbarTitle()
|
||||
mPos = position
|
||||
supportInvalidateOptionsMenu()
|
||||
}
|
||||
|
||||
override fun onPageScrollStateChanged(state: Int) {
|
||||
if (state == ViewPager.SCROLL_STATE_DRAGGING) {
|
||||
val adapter = view_pager.adapter as MyPagerAdapter
|
||||
adapter.itemDragged(mPos)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSystemUiVisibilityChange(visibility: Int) {
|
||||
if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
|
||||
mIsFullScreen = false
|
||||
}
|
||||
|
||||
val adapter = view_pager.adapter as MyPagerAdapter
|
||||
adapter.updateUiVisibility(mIsFullScreen, mPos)
|
||||
}
|
||||
|
||||
private fun scanCompleted() {
|
||||
mBeingDeleted = ""
|
||||
runOnUiThread {
|
||||
if (mMedia != null && mMedia!!.size <= 1) {
|
||||
reloadViewPager()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
deleteFile()
|
||||
}
|
||||
}
|
|
@ -11,6 +11,8 @@ class Medium(val name: String, var path: String, val isVideo: Boolean, val times
|
|||
val isImage: Boolean
|
||||
get() = !isGif && !isVideo
|
||||
|
||||
fun getMimeType() = if (isVideo) "video/*" else "image/*"
|
||||
|
||||
override fun compareTo(other: Medium): Int {
|
||||
var res: Int
|
||||
if (sorting and Constants.SORT_BY_NAME != 0) {
|
||||
|
|
Loading…
Reference in a new issue