allow Undoing file deleting from viewpager
This commit is contained in:
parent
09e4f00f27
commit
304f6010da
4 changed files with 91 additions and 22 deletions
|
@ -3,6 +3,7 @@ package com.simplemobiletools.gallery.activities;
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.graphics.Color;
|
import android.graphics.Color;
|
||||||
import android.media.MediaScannerConnection;
|
import android.media.MediaScannerConnection;
|
||||||
|
@ -162,9 +163,10 @@ public class PhotosActivity extends AppCompatActivity
|
||||||
|
|
||||||
private void notifyDeletion(int cnt) {
|
private void notifyDeletion(int cnt) {
|
||||||
final CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
|
final CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
|
||||||
final String msg = getResources().getQuantityString(R.plurals.files_deleted, cnt, cnt);
|
final Resources res = getResources();
|
||||||
|
final String msg = res.getQuantityString(R.plurals.files_deleted, cnt, cnt);
|
||||||
snackbar = Snackbar.make(coordinator, msg, Snackbar.LENGTH_INDEFINITE);
|
snackbar = Snackbar.make(coordinator, msg, Snackbar.LENGTH_INDEFINITE);
|
||||||
snackbar.setAction(getResources().getString(R.string.undo), undoDeletion);
|
snackbar.setAction(res.getString(R.string.undo), undoDeletion);
|
||||||
snackbar.setActionTextColor(Color.WHITE);
|
snackbar.setActionTextColor(Color.WHITE);
|
||||||
snackbar.show();
|
snackbar.show();
|
||||||
isSnackbarShown = true;
|
isSnackbarShown = true;
|
||||||
|
|
|
@ -2,17 +2,22 @@ package com.simplemobiletools.gallery.activities;
|
||||||
|
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.res.Resources;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.media.MediaScannerConnection;
|
import android.media.MediaScannerConnection;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
|
import android.support.design.widget.CoordinatorLayout;
|
||||||
|
import android.support.design.widget.Snackbar;
|
||||||
import android.support.v4.view.ViewPager;
|
import android.support.v4.view.ViewPager;
|
||||||
import android.support.v7.app.ActionBar;
|
import android.support.v7.app.ActionBar;
|
||||||
import android.support.v7.app.AlertDialog;
|
import android.support.v7.app.AlertDialog;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
@ -29,7 +34,8 @@ import java.util.List;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class ViewPagerActivity extends AppCompatActivity
|
public class ViewPagerActivity extends AppCompatActivity
|
||||||
implements ViewPager.OnPageChangeListener, View.OnSystemUiVisibilityChangeListener, MediaScannerConnection.OnScanCompletedListener {
|
implements ViewPager.OnPageChangeListener, View.OnSystemUiVisibilityChangeListener, MediaScannerConnection.OnScanCompletedListener,
|
||||||
|
ViewPager.OnTouchListener {
|
||||||
private int pos;
|
private int pos;
|
||||||
private boolean isFullScreen;
|
private boolean isFullScreen;
|
||||||
private ActionBar actionbar;
|
private ActionBar actionbar;
|
||||||
|
@ -37,6 +43,9 @@ public class ViewPagerActivity extends AppCompatActivity
|
||||||
private MyViewPager pager;
|
private MyViewPager pager;
|
||||||
private String path;
|
private String path;
|
||||||
private String directory;
|
private String directory;
|
||||||
|
private Snackbar snackbar;
|
||||||
|
private boolean isSnackbarShown;
|
||||||
|
private String toBeDeleted;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -46,6 +55,7 @@ public class ViewPagerActivity extends AppCompatActivity
|
||||||
pos = 0;
|
pos = 0;
|
||||||
isFullScreen = true;
|
isFullScreen = true;
|
||||||
actionbar = getSupportActionBar();
|
actionbar = getSupportActionBar();
|
||||||
|
toBeDeleted = "";
|
||||||
hideSystemUI();
|
hideSystemUI();
|
||||||
|
|
||||||
path = getIntent().getStringExtra(Constants.PHOTO);
|
path = getIntent().getStringExtra(Constants.PHOTO);
|
||||||
|
@ -60,6 +70,7 @@ public class ViewPagerActivity extends AppCompatActivity
|
||||||
pager.setAdapter(adapter);
|
pager.setAdapter(adapter);
|
||||||
pager.setCurrentItem(pos);
|
pager.setCurrentItem(pos);
|
||||||
pager.addOnPageChangeListener(this);
|
pager.addOnPageChangeListener(this);
|
||||||
|
pager.setOnTouchListener(this);
|
||||||
|
|
||||||
getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(this);
|
getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(this);
|
||||||
updateActionbarTitle();
|
updateActionbarTitle();
|
||||||
|
@ -73,12 +84,13 @@ public class ViewPagerActivity extends AppCompatActivity
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
|
deleteFile();
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
case R.id.menu_share:
|
case R.id.menu_share:
|
||||||
shareImage();
|
shareImage();
|
||||||
return true;
|
return true;
|
||||||
case R.id.menu_remove:
|
case R.id.menu_remove:
|
||||||
deleteImage();
|
notifyDeletion();
|
||||||
return true;
|
return true;
|
||||||
case R.id.menu_edit:
|
case R.id.menu_edit:
|
||||||
editImage();
|
editImage();
|
||||||
|
@ -99,15 +111,45 @@ public class ViewPagerActivity extends AppCompatActivity
|
||||||
startActivity(Intent.createChooser(sendIntent, shareTitle));
|
startActivity(Intent.createChooser(sendIntent, shareTitle));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteImage() {
|
private void notifyDeletion() {
|
||||||
Helpers.showToast(this, R.string.deleting);
|
final CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
|
||||||
final File file = getCurrentFile();
|
final Resources res = getResources();
|
||||||
|
snackbar = Snackbar.make(coordinator, res.getString(R.string.file_deleted), Snackbar.LENGTH_INDEFINITE);
|
||||||
|
snackbar.setAction(res.getString(R.string.undo), undoDeletion);
|
||||||
|
snackbar.setActionTextColor(Color.WHITE);
|
||||||
|
snackbar.show();
|
||||||
|
isSnackbarShown = true;
|
||||||
|
toBeDeleted = getCurrentFile().getAbsolutePath();
|
||||||
|
reloadViewPager();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteFile() {
|
||||||
|
if (toBeDeleted.isEmpty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (snackbar == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
snackbar.dismiss();
|
||||||
|
isSnackbarShown = false;
|
||||||
|
|
||||||
|
final File file = new File(toBeDeleted);
|
||||||
if (file.delete()) {
|
if (file.delete()) {
|
||||||
final String[] deletedPath = new String[]{file.getAbsolutePath()};
|
final String[] deletedPath = new String[]{toBeDeleted};
|
||||||
MediaScannerConnection.scanFile(this, deletedPath, null, this);
|
MediaScannerConnection.scanFile(this, deletedPath, null, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private View.OnClickListener undoDeletion = new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
snackbar.dismiss();
|
||||||
|
isSnackbarShown = false;
|
||||||
|
toBeDeleted = "";
|
||||||
|
reloadViewPager();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private boolean isDirEmpty() {
|
private boolean isDirEmpty() {
|
||||||
if (photos.size() <= 0) {
|
if (photos.size() <= 0) {
|
||||||
deleteDirectoryIfEmpty();
|
deleteDirectoryIfEmpty();
|
||||||
|
@ -120,12 +162,12 @@ public class ViewPagerActivity extends AppCompatActivity
|
||||||
private void editImage() {
|
private void editImage() {
|
||||||
final File file = getCurrentFile();
|
final File file = getCurrentFile();
|
||||||
final String fullName = file.getName();
|
final String fullName = file.getName();
|
||||||
final int pos = fullName.lastIndexOf(".");
|
final int dotAt = fullName.lastIndexOf(".");
|
||||||
if (pos <= 0)
|
if (dotAt <= 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
final String name = fullName.substring(0, pos);
|
final String name = fullName.substring(0, dotAt);
|
||||||
final String extension = fullName.substring(pos + 1, fullName.length());
|
final String extension = fullName.substring(dotAt + 1, fullName.length());
|
||||||
|
|
||||||
final View renameFileView = getLayoutInflater().inflate(R.layout.rename_file, null);
|
final View renameFileView = getLayoutInflater().inflate(R.layout.rename_file, null);
|
||||||
final EditText fileNameET = (EditText) renameFileView.findViewById(R.id.file_name);
|
final EditText fileNameET = (EditText) renameFileView.findViewById(R.id.file_name);
|
||||||
|
@ -163,7 +205,7 @@ public class ViewPagerActivity extends AppCompatActivity
|
||||||
|
|
||||||
private void reloadViewPager() {
|
private void reloadViewPager() {
|
||||||
final MyPagerAdapter adapter = (MyPagerAdapter) pager.getAdapter();
|
final MyPagerAdapter adapter = (MyPagerAdapter) pager.getAdapter();
|
||||||
final int pos = pager.getCurrentItem();
|
final int curPos = pager.getCurrentItem();
|
||||||
photos = getPhotos();
|
photos = getPhotos();
|
||||||
if (isDirEmpty())
|
if (isDirEmpty())
|
||||||
return;
|
return;
|
||||||
|
@ -172,7 +214,7 @@ public class ViewPagerActivity extends AppCompatActivity
|
||||||
adapter.updateItems(photos);
|
adapter.updateItems(photos);
|
||||||
pager.setAdapter(adapter);
|
pager.setAdapter(adapter);
|
||||||
|
|
||||||
final int newPos = Math.min(pos, adapter.getCount());
|
final int newPos = Math.min(curPos, adapter.getCount());
|
||||||
pager.setCurrentItem(newPos);
|
pager.setCurrentItem(newPos);
|
||||||
updateActionbarTitle();
|
updateActionbarTitle();
|
||||||
}
|
}
|
||||||
|
@ -182,6 +224,9 @@ public class ViewPagerActivity extends AppCompatActivity
|
||||||
if (file.isDirectory() && file.listFiles().length == 0) {
|
if (file.isDirectory() && file.listFiles().length == 0) {
|
||||||
file.delete();
|
file.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final String[] toBeDeleted = new String[]{directory};
|
||||||
|
MediaScannerConnection.scanFile(getApplicationContext(), toBeDeleted, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> getPhotos() {
|
private List<String> getPhotos() {
|
||||||
|
@ -200,7 +245,7 @@ public class ViewPagerActivity extends AppCompatActivity
|
||||||
do {
|
do {
|
||||||
final String curPath = cursor.getString(pathIndex);
|
final String curPath = cursor.getString(pathIndex);
|
||||||
|
|
||||||
if (curPath.matches(pattern)) {
|
if (curPath.matches(pattern) && !curPath.equals(toBeDeleted)) {
|
||||||
photos.add(curPath);
|
photos.add(curPath);
|
||||||
|
|
||||||
if (curPath.equals(path))
|
if (curPath.equals(path))
|
||||||
|
@ -283,4 +328,19 @@ public class ViewPagerActivity extends AppCompatActivity
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onTouch(View v, MotionEvent event) {
|
||||||
|
if (isSnackbarShown) {
|
||||||
|
deleteFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
deleteFile();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,19 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<LinearLayout
|
<android.support.design.widget.CoordinatorLayout
|
||||||
|
android:id="@+id/coordinator_layout"
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent">
|
||||||
android:background="@android:color/black">
|
|
||||||
|
|
||||||
<com.simplemobiletools.gallery.MyViewPager
|
<LinearLayout
|
||||||
android:id="@+id/view_pager"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"/>
|
android:layout_height="match_parent"
|
||||||
|
android:background="@android:color/black">
|
||||||
|
|
||||||
</LinearLayout>
|
<com.simplemobiletools.gallery.MyViewPager
|
||||||
|
android:id="@+id/view_pager"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
</android.support.design.widget.CoordinatorLayout>
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
<string name="rename_error">Could not rename the file</string>
|
<string name="rename_error">Could not rename the file</string>
|
||||||
<string name="file_name">File name</string>
|
<string name="file_name">File name</string>
|
||||||
<string name="extension">Extension</string>
|
<string name="extension">Extension</string>
|
||||||
|
<string name="file_deleted">File deleted</string>
|
||||||
|
|
||||||
<plurals name="files_deleted">
|
<plurals name="files_deleted">
|
||||||
<item quantity="one">1 file deleted</item>
|
<item quantity="one">1 file deleted</item>
|
||||||
|
|
Loading…
Reference in a new issue