diff --git a/app/src/main/java/com/simplemobiletools/gallery/Constants.java b/app/src/main/java/com/simplemobiletools/gallery/Constants.java index 7b892bffe..70eb11b92 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/Constants.java +++ b/app/src/main/java/com/simplemobiletools/gallery/Constants.java @@ -26,4 +26,7 @@ public class Constants { public static final int SORT_BY_SIZE = 4; public static final int SORT_DESCENDING = 1024; + + // global intents + public static final int OPEN_DOCUMENT_TREE = 1000; } diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java index 364746735..c14121b0f 100644 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java +++ b/app/src/main/java/com/simplemobiletools/gallery/activities/ViewPagerActivity.java @@ -11,17 +11,13 @@ import android.net.Uri; import android.os.Build; 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.support.v7.app.AlertDialog; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; -import android.widget.EditText; import android.widget.RelativeLayout; -import android.widget.TextView; import com.simplemobiletools.fileproperties.dialogs.PropertiesDialog; import com.simplemobiletools.gallery.Constants; @@ -29,10 +25,12 @@ 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.WritePermissionDialog; +import com.simplemobiletools.gallery.dialogs.RenameItemDialog; 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; @@ -51,7 +49,6 @@ public class ViewPagerActivity extends SimpleActivity private static final int EDIT_IMAGE = 1; private static final int SET_WALLPAPER = 2; - private static final int OPEN_DOCUMENT_TREE = 3; private static ActionBar mActionbar; private static List mMedia; private static String mPath; @@ -237,7 +234,7 @@ public class ViewPagerActivity extends SimpleActivity if (resultCode == RESULT_OK) { 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); } super.onActivityResult(requestCode, resultCode, data); @@ -302,82 +299,15 @@ public class ViewPagerActivity extends SimpleActivity } private void editMedium() { - final File file = getCurrentFile(); - 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() { + new RenameItemDialog(this, getCurrentFile(), new RenameItemDialog.OnRenameItemListener() { @Override - public void onClick(View v) { - 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); - } + public void onRenameSuccess(@NotNull File newFile) { + mMedia.get(mPager.getCurrentItem()).setPath(newFile.getAbsolutePath()); + updateActionbarTitle(); } }); } - 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()); - updateActionbarTitle(); - } - private void reloadViewPager() { final MyPagerAdapter adapter = (MyPagerAdapter) mPager.getAdapter(); final int curPos = mPager.getCurrentItem(); diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameItemDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameItemDialog.kt new file mode 100644 index 000000000..40c6a885e --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameItemDialog.kt @@ -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) + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/editText.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/editText.kt new file mode 100644 index 000000000..6bf3b3df9 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/editText.kt @@ -0,0 +1,5 @@ +package com.simplemobiletools.gallery.extensions + +import android.widget.EditText + +val EditText.value: String get() = this.text.toString().trim() diff --git a/app/src/main/res/layout/rename_file.xml b/app/src/main/res/layout/rename_file.xml index 9403fdd99..79ff50716 100644 --- a/app/src/main/res/layout/rename_file.xml +++ b/app/src/main/res/layout/rename_file.xml @@ -19,13 +19,13 @@ android:singleLine="true"/>