allow renaming the directories

This commit is contained in:
tibbi 2016-03-07 21:41:55 +01:00
parent e4127b61d9
commit f626ee1843
11 changed files with 120 additions and 20 deletions

View file

@ -3,7 +3,7 @@ package com.simplemobiletools.gallery;
import android.content.Context;
import android.widget.Toast;
public class Helpers {
public class Utils {
public static String getFilename(final String path) {
return path.substring(path.lastIndexOf("/") + 1);
}

View file

@ -14,6 +14,7 @@ import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
@ -23,13 +24,14 @@ import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;
import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.Directory;
import com.simplemobiletools.gallery.Helpers;
import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.adapters.DirectoryAdapter;
import java.io.File;
@ -39,7 +41,8 @@ import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity
implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener {
implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener,
MediaScannerConnection.OnScanCompletedListener {
private final int STORAGE_PERMISSION = 1;
private List<Directory> dirs;
private GridView gridView;
@ -47,6 +50,7 @@ public class MainActivity extends AppCompatActivity
private Snackbar snackbar;
private boolean isSnackbarShown;
private List<String> toBeDeleted;
private ActionMode actionMode;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -118,7 +122,7 @@ public class MainActivity extends AppCompatActivity
final int newImageCnt = directory.getPhotoCnt() + 1;
directory.setPhotoCnt(newImageCnt);
} else if (!toBeDeleted.contains(fileDir)) {
final String dirName = Helpers.getFilename(fileDir);
final String dirName = Utils.getFilename(fileDir);
directories.put(fileDir, new Directory(fileDir, path, dirName, 1));
}
} while (cursor.moveToNext());
@ -129,7 +133,7 @@ public class MainActivity extends AppCompatActivity
}
private void prepareForDeleting() {
Helpers.showToast(this, R.string.deleting);
Utils.showToast(this, R.string.deleting);
final SparseBooleanArray items = gridView.getCheckedItemPositions();
int cnt = items.size();
int deletedCnt = 0;
@ -204,6 +208,56 @@ public class MainActivity extends AppCompatActivity
adapter.updateItems(dirs);
}
private void renameDirectory() {
final SparseBooleanArray items = gridView.getCheckedItemPositions();
final int cnt = items.size();
for (int i = 0; i < cnt; i++) {
if (items.valueAt(i)) {
final int id = items.keyAt(i);
final String path = dirs.get(id).getPath();
final File dir = new File(path);
final View renameFileView = getLayoutInflater().inflate(R.layout.rename_directory, null);
final EditText dirNameET = (EditText) renameFileView.findViewById(R.id.directory_name);
dirNameET.setText(dir.getName());
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getResources().getString(R.string.rename_folder));
builder.setView(renameFileView);
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String newDirName = dirNameET.getText().toString().trim();
if (!newDirName.isEmpty()) {
final File newDir = new File(dir.getParent(), newDirName);
if (dir.renameTo(newDir)) {
Utils.showToast(getApplicationContext(), R.string.rename_folder_ok);
alertDialog.dismiss();
actionMode.finish();
final String[] newDirPath = new String[]{newDir.getAbsolutePath()};
MediaScannerConnection.scanFile(getApplicationContext(), newDirPath, null, MainActivity.this);
} else {
Utils.showToast(getApplicationContext(), R.string.rename_folder_error);
}
} else {
Utils.showToast(getApplicationContext(), R.string.rename_folder_error);
}
}
});
break;
}
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Intent intent = new Intent(this, PhotosActivity.class);
@ -228,17 +282,23 @@ public class MainActivity extends AppCompatActivity
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
final MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.directories_menu, menu);
actionMode = mode;
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
final MenuItem menuItem = menu.findItem(R.id.cab_edit);
menuItem.setVisible(selectedItemsCnt == 1);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.cab_edit:
renameDirectory();
return true;
case R.id.cab_remove:
prepareForDeleting();
mode.finish();
@ -261,4 +321,16 @@ public class MainActivity extends AppCompatActivity
return false;
}
@Override
public void onScanCompleted(String path, Uri uri) {
runOnUiThread(new Runnable() {
@Override
public void run() {
dirs = new ArrayList<>(getDirectories().values());
updateGridView();
gridView.requestLayout();
}
});
}
}

View file

@ -27,8 +27,8 @@ import android.widget.GridView;
import android.widget.Toast;
import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.Helpers;
import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.adapters.PhotosAdapter;
import java.io.File;
@ -102,7 +102,7 @@ public class PhotosActivity extends AppCompatActivity
gridView.setOnTouchListener(this);
isSnackbarShown = false;
final String dirName = Helpers.getFilename(path);
final String dirName = Utils.getFilename(path);
setTitle(dirName);
}
@ -146,7 +146,7 @@ public class PhotosActivity extends AppCompatActivity
}
private void prepareForDeleting() {
Helpers.showToast(this, R.string.deleting);
Utils.showToast(this, R.string.deleting);
final SparseBooleanArray items = gridView.getCheckedItemPositions();
int cnt = items.size();
int deletedCnt = 0;

View file

@ -23,7 +23,7 @@ import android.widget.EditText;
import android.widget.Toast;
import com.simplemobiletools.gallery.Constants;
import com.simplemobiletools.gallery.Helpers;
import com.simplemobiletools.gallery.Utils;
import com.simplemobiletools.gallery.MyViewPager;
import com.simplemobiletools.gallery.R;
import com.simplemobiletools.gallery.adapters.MyPagerAdapter;
@ -200,7 +200,7 @@ public class ViewPagerActivity extends AppCompatActivity
MediaScannerConnection.scanFile(getApplicationContext(), changedFiles, null, null);
updateActionbarTitle();
} else {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.rename_error), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), getResources().getString(R.string.rename_file_error), Toast.LENGTH_SHORT).show();
}
}
});
@ -297,7 +297,7 @@ public class ViewPagerActivity extends AppCompatActivity
}
private void updateActionbarTitle() {
setTitle(Helpers.getFilename(photos.get(pager.getCurrentItem())));
setTitle(Utils.getFilename(photos.get(pager.getCurrentItem())));
}
private File getCurrentFile() {

View file

@ -3,13 +3,13 @@
android:id="@+id/coordinator_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:background="@android:color/black">
<GridView
android:id="@+id/directories_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:choiceMode="multipleChoiceModal"
android:columnWidth="@dimen/dir_tmb_size"
android:horizontalSpacing="1dp"

View file

@ -3,13 +3,13 @@
android:id="@+id/coordinator_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:background="@android:color/black">
<GridView
android:id="@+id/photos_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:choiceMode="multipleChoiceModal"
android:columnWidth="@dimen/photo_tmb_size"
android:horizontalSpacing="1dp"

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/folder_name"/>
<EditText
android:id="@+id/directory_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"/>
</LinearLayout>

View file

@ -1,9 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/cab_edit"
android:icon="@android:drawable/ic_menu_edit"
android:title="@string/edit"
app:showAsAction="ifRoom"/>
<item
android:id="@+id/cab_remove"
android:icon="@android:drawable/ic_menu_delete"
android:title="@string/remove"
app:showAsAction="always"/>
app:showAsAction="ifRoom"/>
</menu>

View file

@ -5,5 +5,5 @@
android:id="@+id/cab_remove"
android:icon="@android:drawable/ic_menu_delete"
android:title="@string/remove"
app:showAsAction="always"/>
app:showAsAction="ifRoom"/>
</menu>

View file

@ -15,5 +15,5 @@
android:id="@+id/menu_share"
android:icon="@android:drawable/ic_menu_share"
android:title="Share"
app:showAsAction="always"/>
app:showAsAction="ifRoom"/>
</menu>

View file

@ -7,8 +7,12 @@
<string name="edit">Edit</string>
<string name="undo">Undo</string>
<string name="rename_file">Rename file</string>
<string name="rename_error">Could not rename the file</string>
<string name="rename_folder">Rename folder</string>
<string name="rename_file_error">Could not rename the file</string>
<string name="rename_folder_error">Could not rename the folder</string>
<string name="rename_folder_ok">Folder renamed successfully</string>
<string name="file_name">File name</string>
<string name="folder_name">Folder name</string>
<string name="extension">Extension</string>
<string name="file_deleted">deleted</string>