move RenameItemDialog in a separate file
This commit is contained in:
parent
7ba442e32e
commit
a70e99da34
5 changed files with 107 additions and 80 deletions
|
@ -26,4 +26,7 @@ public class Constants {
|
||||||
public static final int SORT_BY_SIZE = 4;
|
public static final int SORT_BY_SIZE = 4;
|
||||||
|
|
||||||
public static final int SORT_DESCENDING = 1024;
|
public static final int SORT_DESCENDING = 1024;
|
||||||
|
|
||||||
|
// global intents
|
||||||
|
public static final int OPEN_DOCUMENT_TREE = 1000;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,17 +11,13 @@ import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
import android.support.v4.provider.DocumentFile;
|
|
||||||
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.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.EditText;
|
|
||||||
import android.widget.RelativeLayout;
|
import android.widget.RelativeLayout;
|
||||||
import android.widget.TextView;
|
|
||||||
|
|
||||||
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog;
|
import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog;
|
||||||
import com.simplemobiletools.gallery.Constants;
|
import com.simplemobiletools.gallery.Constants;
|
||||||
|
@ -29,10 +25,12 @@ import com.simplemobiletools.gallery.MyViewPager;
|
||||||
import com.simplemobiletools.gallery.R;
|
import com.simplemobiletools.gallery.R;
|
||||||
import com.simplemobiletools.gallery.Utils;
|
import com.simplemobiletools.gallery.Utils;
|
||||||
import com.simplemobiletools.gallery.adapters.MyPagerAdapter;
|
import com.simplemobiletools.gallery.adapters.MyPagerAdapter;
|
||||||
import com.simplemobiletools.gallery.dialogs.WritePermissionDialog;
|
import com.simplemobiletools.gallery.dialogs.RenameItemDialog;
|
||||||
import com.simplemobiletools.gallery.fragments.ViewPagerFragment;
|
import com.simplemobiletools.gallery.fragments.ViewPagerFragment;
|
||||||
import com.simplemobiletools.gallery.models.Medium;
|
import com.simplemobiletools.gallery.models.Medium;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -51,7 +49,6 @@ public class ViewPagerActivity extends SimpleActivity
|
||||||
|
|
||||||
private static final int EDIT_IMAGE = 1;
|
private static final int EDIT_IMAGE = 1;
|
||||||
private static final int SET_WALLPAPER = 2;
|
private static final int SET_WALLPAPER = 2;
|
||||||
private static final int OPEN_DOCUMENT_TREE = 3;
|
|
||||||
private static ActionBar mActionbar;
|
private static ActionBar mActionbar;
|
||||||
private static List<Medium> mMedia;
|
private static List<Medium> mMedia;
|
||||||
private static String mPath;
|
private static String mPath;
|
||||||
|
@ -237,7 +234,7 @@ public class ViewPagerActivity extends SimpleActivity
|
||||||
if (resultCode == RESULT_OK) {
|
if (resultCode == RESULT_OK) {
|
||||||
Utils.Companion.showToast(getApplicationContext(), R.string.wallpaper_set_successfully);
|
Utils.Companion.showToast(getApplicationContext(), R.string.wallpaper_set_successfully);
|
||||||
}
|
}
|
||||||
} else if (requestCode == OPEN_DOCUMENT_TREE && resultCode == Activity.RESULT_OK && data != null) {
|
} else if (requestCode == Constants.OPEN_DOCUMENT_TREE && resultCode == Activity.RESULT_OK && data != null) {
|
||||||
saveTreeUri(data);
|
saveTreeUri(data);
|
||||||
}
|
}
|
||||||
super.onActivityResult(requestCode, resultCode, data);
|
super.onActivityResult(requestCode, resultCode, data);
|
||||||
|
@ -302,81 +299,14 @@ public class ViewPagerActivity extends SimpleActivity
|
||||||
}
|
}
|
||||||
|
|
||||||
private void editMedium() {
|
private void editMedium() {
|
||||||
final File file = getCurrentFile();
|
new RenameItemDialog(this, getCurrentFile(), new RenameItemDialog.OnRenameItemListener() {
|
||||||
final String fullName = file.getName();
|
|
||||||
final int dotAt = fullName.lastIndexOf(".");
|
|
||||||
if (dotAt <= 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
final String name = fullName.substring(0, dotAt);
|
|
||||||
final String extension = fullName.substring(dotAt + 1, fullName.length());
|
|
||||||
|
|
||||||
final View renameFileView = getLayoutInflater().inflate(R.layout.rename_file, null);
|
|
||||||
final EditText fileNameET = (EditText) renameFileView.findViewById(R.id.file_name);
|
|
||||||
fileNameET.setText(name);
|
|
||||||
|
|
||||||
final EditText extensionET = (EditText) renameFileView.findViewById(R.id.extension);
|
|
||||||
extensionET.setText(extension);
|
|
||||||
|
|
||||||
final TextView filePath = (TextView) renameFileView.findViewById(R.id.file_path);
|
|
||||||
filePath.setText(file.getParent() + "/");
|
|
||||||
|
|
||||||
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
||||||
builder.setTitle(getResources().getString(R.string.rename_file));
|
|
||||||
builder.setView(renameFileView);
|
|
||||||
|
|
||||||
builder.setPositiveButton(R.string.ok, null);
|
|
||||||
builder.setNegativeButton(R.string.cancel, null);
|
|
||||||
|
|
||||||
final AlertDialog alertDialog = builder.create();
|
|
||||||
alertDialog.show();
|
|
||||||
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onRenameSuccess(@NotNull File newFile) {
|
||||||
final String fileName = fileNameET.getText().toString().trim();
|
|
||||||
final String extension = extensionET.getText().toString().trim();
|
|
||||||
|
|
||||||
if (fileName.isEmpty() || extension.isEmpty()) {
|
|
||||||
Utils.Companion.showToast(getApplicationContext(), R.string.rename_file_empty);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final File newFile = new File(file.getParent(), fileName + "." + extension);
|
|
||||||
|
|
||||||
if (Utils.Companion.needsStupidWritePermissions(getApplicationContext(), file.getAbsolutePath())) {
|
|
||||||
if (!file.canWrite() && mConfig.getTreeUri().isEmpty()) {
|
|
||||||
new WritePermissionDialog(ViewPagerActivity.this, new WritePermissionDialog.OnWritePermissionListener() {
|
|
||||||
@Override
|
|
||||||
public void onConfirmed() {
|
|
||||||
final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
|
|
||||||
startActivityForResult(intent, OPEN_DOCUMENT_TREE);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final DocumentFile document = Utils.Companion.getFileDocument(getApplicationContext(), file.getAbsolutePath());
|
|
||||||
if (document.canWrite())
|
|
||||||
document.renameTo(newFile.getName());
|
|
||||||
sendSuccess(file, newFile);
|
|
||||||
alertDialog.dismiss();
|
|
||||||
} else if (file.renameTo(newFile)) {
|
|
||||||
final int currItem = mPager.getCurrentItem();
|
|
||||||
mMedia.set(currItem, new Medium(newFile.getAbsolutePath(), mMedia.get(currItem).getIsVideo(), 0, file.length()));
|
|
||||||
sendSuccess(file, newFile);
|
|
||||||
alertDialog.dismiss();
|
|
||||||
} else {
|
|
||||||
Utils.Companion.showToast(getApplicationContext(), R.string.rename_file_error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sendSuccess(File currFile, File newFile) {
|
|
||||||
final String[] changedFiles = {currFile.getAbsolutePath(), newFile.getAbsolutePath()};
|
|
||||||
MediaScannerConnection.scanFile(getApplicationContext(), changedFiles, null, null);
|
|
||||||
mMedia.get(mPager.getCurrentItem()).setPath(newFile.getAbsolutePath());
|
mMedia.get(mPager.getCurrentItem()).setPath(newFile.getAbsolutePath());
|
||||||
updateActionbarTitle();
|
updateActionbarTitle();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private void reloadViewPager() {
|
private void reloadViewPager() {
|
||||||
final MyPagerAdapter adapter = (MyPagerAdapter) mPager.getAdapter();
|
final MyPagerAdapter adapter = (MyPagerAdapter) mPager.getAdapter();
|
||||||
|
|
|
@ -0,0 +1,89 @@
|
||||||
|
package com.simplemobiletools.gallery.dialogs
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
|
import android.content.Intent
|
||||||
|
import android.media.MediaScannerConnection
|
||||||
|
import android.support.v7.app.AlertDialog
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.WindowManager
|
||||||
|
import com.simplemobiletools.gallery.Config
|
||||||
|
import com.simplemobiletools.gallery.Constants
|
||||||
|
import com.simplemobiletools.gallery.R
|
||||||
|
import com.simplemobiletools.gallery.Utils
|
||||||
|
import com.simplemobiletools.gallery.extensions.toast
|
||||||
|
import com.simplemobiletools.gallery.extensions.value
|
||||||
|
import kotlinx.android.synthetic.main.rename_file.view.*
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class RenameItemDialog(val activity: Activity, val file: File, val listener: OnRenameItemListener) {
|
||||||
|
|
||||||
|
init {
|
||||||
|
val context = activity
|
||||||
|
val view = LayoutInflater.from(context).inflate(R.layout.rename_file, null)
|
||||||
|
val fullName = file.name
|
||||||
|
val dotAt = fullName.lastIndexOf(".")
|
||||||
|
var name = fullName
|
||||||
|
|
||||||
|
if (dotAt > 0) {
|
||||||
|
name = fullName.substring(0, dotAt)
|
||||||
|
val extension = fullName.substring(dotAt + 1)
|
||||||
|
view.file_extension.setText(extension)
|
||||||
|
}
|
||||||
|
|
||||||
|
view.file_name.setText(name)
|
||||||
|
view.file_path.text = "${file.parent}/"
|
||||||
|
|
||||||
|
AlertDialog.Builder(context)
|
||||||
|
.setTitle(context.resources.getString(R.string.rename_file))
|
||||||
|
.setView(view)
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.setNegativeButton(R.string.cancel, null)
|
||||||
|
.create().apply {
|
||||||
|
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
||||||
|
show()
|
||||||
|
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
|
||||||
|
val fileName = view.file_name.value
|
||||||
|
val extension = view.file_extension.value
|
||||||
|
|
||||||
|
if (fileName.isEmpty() || extension.isEmpty()) {
|
||||||
|
context.toast(R.string.rename_file_empty)
|
||||||
|
return@setOnClickListener
|
||||||
|
}
|
||||||
|
|
||||||
|
val newFile = File(file.parent, "$fileName.$extension")
|
||||||
|
|
||||||
|
if (Utils.needsStupidWritePermissions(context, file.absolutePath)) {
|
||||||
|
if (!file.canWrite() && Config.newInstance(context).treeUri.isEmpty()) {
|
||||||
|
WritePermissionDialog(activity, object : WritePermissionDialog.OnWritePermissionListener {
|
||||||
|
override fun onConfirmed() {
|
||||||
|
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
|
||||||
|
activity.startActivityForResult(intent, Constants.OPEN_DOCUMENT_TREE)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return@setOnClickListener
|
||||||
|
}
|
||||||
|
val document = Utils.Companion.getFileDocument(context, file.absolutePath)
|
||||||
|
if (document.canWrite())
|
||||||
|
document.renameTo(newFile.name)
|
||||||
|
sendSuccess(file, newFile)
|
||||||
|
dismiss()
|
||||||
|
} else if (file.renameTo(newFile)) {
|
||||||
|
sendSuccess(file, newFile)
|
||||||
|
dismiss()
|
||||||
|
} else {
|
||||||
|
context.toast(R.string.rename_file_error)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun sendSuccess(currFile: File, newFile: File) {
|
||||||
|
val changedFiles = arrayOf(currFile.absolutePath, newFile.absolutePath)
|
||||||
|
MediaScannerConnection.scanFile(activity.applicationContext, changedFiles, null, null)
|
||||||
|
listener.onRenameSuccess(newFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OnRenameItemListener {
|
||||||
|
fun onRenameSuccess(newFile: File)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.simplemobiletools.gallery.extensions
|
||||||
|
|
||||||
|
import android.widget.EditText
|
||||||
|
|
||||||
|
val EditText.value: String get() = this.text.toString().trim()
|
|
@ -19,13 +19,13 @@
|
||||||
android:singleLine="true"/>
|
android:singleLine="true"/>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/extension_label"
|
android:id="@+id/file_extension_label"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:text="@string/extension"/>
|
android:text="@string/extension"/>
|
||||||
|
|
||||||
<EditText
|
<EditText
|
||||||
android:id="@+id/extension"
|
android:id="@+id/file_extension"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_marginBottom="@dimen/activity_margin"
|
android:layout_marginBottom="@dimen/activity_margin"
|
||||||
|
|
Loading…
Reference in a new issue