diff --git a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java deleted file mode 100644 index 9477e2f4b..000000000 --- a/app/src/main/java/com/simplemobiletools/gallery/activities/MainActivity.java +++ /dev/null @@ -1,608 +0,0 @@ -package com.simplemobiletools.gallery.activities; - -import android.Manifest; -import android.content.Intent; -import android.content.pm.PackageManager; -import android.content.res.Resources; -import android.graphics.Color; -import android.media.MediaScannerConnection; -import android.net.Uri; -import android.os.Bundle; -import android.os.Parcelable; -import android.provider.MediaStore; -import android.support.design.widget.CoordinatorLayout; -import android.support.design.widget.Snackbar; -import android.support.v4.app.ActivityCompat; -import android.support.v4.provider.DocumentFile; -import android.support.v4.widget.SwipeRefreshLayout; -import android.util.SparseBooleanArray; -import android.view.ActionMode; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.MotionEvent; -import android.view.View; -import android.widget.AdapterView; -import android.widget.GridView; - -import com.simplemobiletools.filepicker.asynctasks.CopyMoveTask; -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.DirectoryAdapter; -import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask; -import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog; -import com.simplemobiletools.gallery.dialogs.CopyDialog; -import com.simplemobiletools.gallery.dialogs.RenameDirectoryDialog; -import com.simplemobiletools.gallery.models.Directory; - -import org.jetbrains.annotations.NotNull; - -import java.io.File; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import butterknife.BindView; -import butterknife.ButterKnife; - -public class MainActivity extends SimpleActivity - implements AdapterView.OnItemClickListener, GridView.MultiChoiceModeListener, GridView.OnTouchListener, - SwipeRefreshLayout.OnRefreshListener, GetDirectoriesAsynctask.GetDirectoriesListener { - @BindView(R.id.directories_grid) GridView mGridView; - @BindView(R.id.directories_holder) SwipeRefreshLayout mSwipeRefreshLayout; - - private static final int STORAGE_PERMISSION = 1; - private static final int PICK_MEDIA = 2; - private static final int PICK_WALLPAPER = 3; - - private static List mDirs; - private static Snackbar mSnackbar; - private static List mToBeDeleted; - private static ActionMode mActionMode; - private static Parcelable mState; - - private static boolean mIsSnackbarShown; - private static boolean mIsPickImageIntent; - private static boolean mIsPickVideoIntent; - private static boolean mIsGetImageContentIntent; - private static boolean mIsGetVideoContentIntent; - private static boolean mIsGetAnyContentIntent; - private static boolean mIsSetWallpaperIntent; - private static boolean mIsThirdPartyIntent; - private static boolean mIsGettingDirs; - private static int mSelectedItemsCnt; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - ButterKnife.bind(this); - - final Intent intent = getIntent(); - mIsPickImageIntent = isPickImageIntent(intent); - mIsPickVideoIntent = isPickVideoIntent(intent); - mIsGetImageContentIntent = isGetImageContentIntent(intent); - mIsGetVideoContentIntent = isGetVideoContentIntent(intent); - mIsGetAnyContentIntent = isGetAnyContentIntent(intent); - mIsSetWallpaperIntent = isSetWallpaperIntent(intent); - mIsThirdPartyIntent = mIsPickImageIntent || mIsPickVideoIntent || mIsGetImageContentIntent || mIsGetVideoContentIntent || - mIsGetAnyContentIntent || mIsSetWallpaperIntent; - - mToBeDeleted = new ArrayList<>(); - mSwipeRefreshLayout.setOnRefreshListener(this); - mDirs = new ArrayList<>(); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - if (mIsThirdPartyIntent) - return false; - - getMenuInflater().inflate(R.menu.menu_main, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case R.id.sort: - showSortingDialog(); - return true; - case R.id.camera: - startActivity(new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA)); - return true; - case R.id.settings: - startActivity(new Intent(getApplicationContext(), SettingsActivity.class)); - return true; - case R.id.about: - startActivity(new Intent(getApplicationContext(), AboutActivity.class)); - return true; - default: - return super.onOptionsItemSelected(item); - } - } - - @Override - protected void onResume() { - super.onResume(); - tryloadGallery(); - if (mState != null && mGridView != null) - mGridView.onRestoreInstanceState(mState); - } - - @Override - protected void onPause() { - super.onPause(); - deleteDirs(); - if (mGridView != null) - mState = mGridView.onSaveInstanceState(); - } - - @Override - protected void onDestroy() { - super.onDestroy(); - mConfig.setFirstRun(false); - } - - private void tryloadGallery() { - if (Utils.Companion.hasStoragePermission(getApplicationContext())) { - getDirectories(); - } else { - ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, STORAGE_PERMISSION); - } - } - - @Override - public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { - super.onRequestPermissionsResult(requestCode, permissions, grantResults); - - if (requestCode == STORAGE_PERMISSION) { - if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { - getDirectories(); - } else { - Utils.Companion.showToast(getApplicationContext(), R.string.no_permissions); - finish(); - } - } - } - - private void getDirectories() { - if (mIsGettingDirs) - return; - - mIsGettingDirs = true; - new GetDirectoriesAsynctask(getApplicationContext(), mIsPickVideoIntent || mIsGetVideoContentIntent, mIsPickImageIntent || mIsGetImageContentIntent, - mToBeDeleted, this).execute(); - } - - private void showSortingDialog() { - new ChangeSortingDialog(this, true, new ChangeSortingDialog.OnChangeSortingListener() { - @Override - public void sortingChanged() { - getDirectories(); - } - }); - } - - private void prepareForDeleting() { - Utils.Companion.showToast(this, R.string.deleting); - final SparseBooleanArray items = mGridView.getCheckedItemPositions(); - final int cnt = items.size(); - int deletedCnt = 0; - for (int i = 0; i < cnt; i++) { - if (items.valueAt(i)) { - final int id = items.keyAt(i); - final String path = mDirs.get(id).getPath(); - mToBeDeleted.add(path); - deletedCnt++; - } - } - - for (String path : mToBeDeleted) { - if (isShowingPermDialog(new File(path))) { - return; - } - } - - notifyDeletion(deletedCnt); - } - - private void notifyDeletion(int cnt) { - getDirectories(); - - final CoordinatorLayout coordinator = (CoordinatorLayout) findViewById(R.id.coordinator_layout); - final Resources res = getResources(); - final String msg = res.getQuantityString(R.plurals.folders_deleted, cnt, cnt); - mSnackbar = Snackbar.make(coordinator, msg, Snackbar.LENGTH_INDEFINITE); - mSnackbar.setAction(res.getString(R.string.undo), undoDeletion); - mSnackbar.setActionTextColor(Color.WHITE); - mSnackbar.show(); - mIsSnackbarShown = true; - } - - private void deleteDirs() { - if (mToBeDeleted == null || mToBeDeleted.isEmpty()) - return; - - if (mSnackbar != null) { - mSnackbar.dismiss(); - } - - mIsSnackbarShown = false; - - final ArrayList updatedFiles = new ArrayList<>(); - for (String delPath : mToBeDeleted) { - final File dir = new File(delPath); - if (dir.exists()) { - final File[] files = dir.listFiles(); - for (File file : files) { - if (file.isFile() && Utils.Companion.isPhotoVideo(file)) { - updatedFiles.add(file); - deleteItem(file); - } - } - updatedFiles.add(dir); - if (dir.listFiles().length == 0) - deleteItem(dir); - } - } - - Utils.Companion.scanFiles(getApplicationContext(), updatedFiles); - mToBeDeleted.clear(); - } - - private void deleteItem(File file) { - if (Utils.Companion.needsStupidWritePermissions(this, file.getAbsolutePath())) { - if (!isShowingPermDialog(file)) { - final DocumentFile document = Utils.Companion.getFileDocument(this, file.getAbsolutePath(), mConfig.getTreeUri()); - document.delete(); - } - } else { - file.delete(); - } - } - - private View.OnClickListener undoDeletion = new View.OnClickListener() { - @Override - public void onClick(View v) { - mSnackbar.dismiss(); - mIsSnackbarShown = false; - mToBeDeleted.clear(); - getDirectories(); - } - }; - - private void showProperties() { - final SparseBooleanArray items = mGridView.getCheckedItemPositions(); - if (items.size() == 1) { - new PropertiesDialog(this, (String) getSelectedPaths().toArray()[0], false); - } else { - final List paths = new ArrayList<>(items.size()); - final int cnt = items.size(); - for (int i = 0; i < cnt; i++) { - if (items.valueAt(i)) { - final int id = items.keyAt(i); - paths.add(mDirs.get(id).getPath()); - } - } - - new PropertiesDialog(this, paths, false); - } - } - - private void editDirectory() { - final SparseBooleanArray items = mGridView.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 = mDirs.get(id).getPath(); - renameDir(path); - break; - } - } - } - - private void renameDir(final String path) { - final File dir = new File(path); - if (Utils.Companion.isAStorageRootFolder(this, path)) { - Utils.Companion.showToast(this, R.string.rename_folder_root); - return; - } - - new RenameDirectoryDialog(this, dir, new RenameDirectoryDialog.OnRenameDirListener() { - @Override - public void onRenameDirSuccess(@NotNull String[] changedFiles) { - mActionMode.finish(); - MediaScannerConnection.scanFile(getApplicationContext(), changedFiles, null, new MediaScannerConnection.OnScanCompletedListener() { - @Override - public void onScanCompleted(String path, Uri uri) { - scanCompleted(path); - } - }); - } - }); - } - - private void displayCopyDialog() { - final ArrayList files = new ArrayList<>(); - final SparseBooleanArray items = mGridView.getCheckedItemPositions(); - final int cnt = items.size(); - for (int i = 0; i < cnt; i++) { - if (items.valueAt(i)) { - final int id = items.keyAt(i); - final File dir = new File(mDirs.get(id).getPath()); - files.addAll(Arrays.asList(dir.listFiles())); - } - } - - new CopyDialog(this, files, new CopyMoveTask.CopyMoveListener() { - @Override - public void copySucceeded(boolean deleted, boolean copiedAll) { - int msgId; - if (deleted) { - getDirectories(); - 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 boolean isPickImageIntent(Intent intent) { - return isPickIntent(intent) && (hasImageContentData(intent) || isImageType(intent)); - } - - private boolean isPickVideoIntent(Intent intent) { - return isPickIntent(intent) && (hasVideoContentData(intent) || isVideoType(intent)); - } - - private boolean isPickIntent(Intent intent) { - return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_PICK); - } - - private boolean isGetContentIntent(Intent intent) { - return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_GET_CONTENT) && - intent.getType() != null; - } - - private boolean isGetImageContentIntent(Intent intent) { - return isGetContentIntent(intent) && - (intent.getType().startsWith("image/") || intent.getType().equals(MediaStore.Images.Media.CONTENT_TYPE)); - } - - private boolean isGetVideoContentIntent(Intent intent) { - return isGetContentIntent(intent) && - (intent.getType().startsWith("video/") || intent.getType().equals(MediaStore.Video.Media.CONTENT_TYPE)); - } - - private boolean isGetAnyContentIntent(Intent intent) { - return isGetContentIntent(intent) && intent.getType().equals("*/*"); - } - - private boolean isSetWallpaperIntent(Intent intent) { - return intent != null && intent.getAction() != null && intent.getAction().equals(Intent.ACTION_SET_WALLPAPER); - } - - private boolean hasImageContentData(Intent intent) { - final Uri data = intent.getData(); - return data != null && data.equals(MediaStore.Images.Media.EXTERNAL_CONTENT_URI); - } - - private boolean hasVideoContentData(Intent intent) { - final Uri data = intent.getData(); - return data != null && data.equals(MediaStore.Video.Media.EXTERNAL_CONTENT_URI); - } - - private boolean isImageType(Intent intent) { - final String type = intent.getType(); - return type != null && (type.startsWith("image/") || type.equals(MediaStore.Images.Media.CONTENT_TYPE)); - } - - private boolean isVideoType(Intent intent) { - final String type = intent.getType(); - return type != null && (type.startsWith("video/") || type.equals(MediaStore.Video.Media.CONTENT_TYPE)); - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent resultData) { - if (resultCode == RESULT_OK) { - if (requestCode == PICK_MEDIA && resultData != null) { - final Intent result = new Intent(); - final String path = resultData.getData().getPath(); - final Uri uri = Uri.fromFile(new File(path)); - if (mIsGetImageContentIntent || mIsGetVideoContentIntent || mIsGetAnyContentIntent) { - final String type = Utils.Companion.getMimeType(path); - result.setDataAndTypeAndNormalize(uri, type); - result.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); - } else if (mIsPickImageIntent || mIsPickVideoIntent) { - result.setData(uri); - result.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - } - - setResult(RESULT_OK, result); - finish(); - } else if (requestCode == PICK_WALLPAPER) { - setResult(RESULT_OK); - finish(); - } - } - super.onActivityResult(requestCode, resultCode, resultData); - } - - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - final Intent intent = new Intent(this, MediaActivity.class); - intent.putExtra(Constants.INSTANCE.getDIRECTORY(), mDirs.get(position).getPath()); - - if (mIsSetWallpaperIntent) { - intent.putExtra(Constants.INSTANCE.getSET_WALLPAPER_INTENT(), true); - startActivityForResult(intent, PICK_WALLPAPER); - } else { - intent.putExtra(Constants.INSTANCE.getGET_IMAGE_INTENT(), mIsPickImageIntent || mIsGetImageContentIntent); - intent.putExtra(Constants.INSTANCE.getGET_VIDEO_INTENT(), mIsPickVideoIntent || mIsGetVideoContentIntent); - intent.putExtra(Constants.INSTANCE.getGET_ANY_INTENT(), mIsGetAnyContentIntent); - startActivityForResult(intent, PICK_MEDIA); - } - } - - @Override - public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { - if (checked) { - mSelectedItemsCnt++; - } else { - mSelectedItemsCnt--; - } - - if (mSelectedItemsCnt > 0) { - mode.setTitle(String.valueOf(mSelectedItemsCnt)); - } - - mode.invalidate(); - } - - @Override - public boolean onCreateActionMode(ActionMode mode, Menu menu) { - final MenuInflater inflater = mode.getMenuInflater(); - inflater.inflate(R.menu.cab_directories, menu); - mActionMode = mode; - return true; - } - - @Override - public boolean onPrepareActionMode(ActionMode mode, Menu menu) { - menu.findItem(R.id.cab_edit).setVisible(mSelectedItemsCnt == 1); - - int hiddenCnt = 0; - int unhiddenCnt = 0; - final SparseBooleanArray items = mGridView.getCheckedItemPositions(); - final int cnt = items.size(); - for (int i = 0; i < cnt; i++) { - if (items.valueAt(i)) { - final int id = items.keyAt(i); - if (mConfig.getIsFolderHidden(mDirs.get(id).getPath())) - hiddenCnt++; - else - unhiddenCnt++; - } - } - - menu.findItem(R.id.cab_hide).setVisible(unhiddenCnt > 0); - menu.findItem(R.id.cab_unhide).setVisible(hiddenCnt > 0); - - return true; - } - - @Override - public boolean onActionItemClicked(ActionMode mode, MenuItem item) { - switch (item.getItemId()) { - case R.id.cab_properties: - showProperties(); - return true; - case R.id.cab_edit: - editDirectory(); - return true; - case R.id.cab_delete: - prepareForDeleting(); - mode.finish(); - return true; - case R.id.cab_hide: - hideFolders(); - mode.finish(); - return true; - case R.id.cab_unhide: - unhideFolders(); - mode.finish(); - return true; - case R.id.cab_copy_move: - displayCopyDialog(); - return true; - default: - return false; - } - } - - @Override - public void onDestroyActionMode(ActionMode mode) { - mSelectedItemsCnt = 0; - } - - @Override - public boolean onTouch(View v, MotionEvent event) { - if (mIsSnackbarShown) { - deleteDirs(); - } - - return false; - } - - private void hideFolders() { - mConfig.addHiddenDirectories(getSelectedPaths()); - getDirectories(); - } - - private void unhideFolders() { - mConfig.removeHiddenDirectories(getSelectedPaths()); - getDirectories(); - } - - private Set getSelectedPaths() { - final SparseBooleanArray items = mGridView.getCheckedItemPositions(); - final Set selectedPaths = new HashSet<>(); - final int cnt = items.size(); - for (int i = 0; i < cnt; i++) { - if (items.valueAt(i)) { - final int id = items.keyAt(i); - selectedPaths.add(mDirs.get(id).getPath()); - } - } - return selectedPaths; - } - - private void scanCompleted(final String path) { - final File dir = new File(path); - if (dir.isDirectory()) { - getDirectories(); - - runOnUiThread(new Runnable() { - @Override - public void run() { - Utils.Companion.showToast(getApplicationContext(), R.string.rename_folder_ok); - } - }); - } - } - - @Override - public void onRefresh() { - getDirectories(); - mSwipeRefreshLayout.setRefreshing(false); - } - - @Override - public void gotDirectories(@NotNull ArrayList dirs) { - mIsGettingDirs = false; - if (dirs.toString().equals(mDirs.toString())) { - return; - } - mDirs = dirs; - - final DirectoryAdapter adapter = new DirectoryAdapter(this, mDirs); - mGridView.setAdapter(adapter); - mGridView.setOnItemClickListener(this); - mGridView.setMultiChoiceModeListener(this); - mGridView.setOnTouchListener(this); - mGridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL); - } -} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/Utils.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/Utils.kt index 74706095a..68874fd6a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/Utils.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/Utils.kt @@ -97,10 +97,6 @@ class Utils { } } - fun isAStorageRootFolder(context: Context, path: String) = context.isAStorageRootFolder(path) - - fun isPhotoVideo(file: File) = file.isPhotoVideo() - fun needsStupidWritePermissions(context: Context, path: String) = context.needsStupidWritePermissions(path) fun getFileDocument(context: Context, path: String, treeUri: String) = context.getFileDocument(path, treeUri) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt new file mode 100644 index 000000000..a0cf72ba1 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -0,0 +1,532 @@ +package com.simplemobiletools.gallery.activities + +import android.Manifest +import android.app.Activity +import android.content.Intent +import android.content.pm.PackageManager +import android.graphics.Color +import android.net.Uri +import android.os.Bundle +import android.os.Parcelable +import android.provider.MediaStore +import android.support.design.widget.Snackbar +import android.support.v4.app.ActivityCompat +import android.support.v4.widget.SwipeRefreshLayout +import android.view.ActionMode +import android.view.Menu +import android.view.MenuItem +import android.view.View +import android.widget.AdapterView +import android.widget.GridView +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.DirectoryAdapter +import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask +import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog +import com.simplemobiletools.gallery.dialogs.CopyDialog +import com.simplemobiletools.gallery.dialogs.RenameDirectoryDialog +import com.simplemobiletools.gallery.models.Directory +import kotlinx.android.synthetic.main.activity_main.* +import java.io.File +import java.util.* + +class MainActivity : SimpleActivity(), AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener, GetDirectoriesAsynctask.GetDirectoriesListener { + companion object { + private val STORAGE_PERMISSION = 1 + private val PICK_MEDIA = 2 + private val PICK_WALLPAPER = 3 + + lateinit var mDirs: MutableList + private var mSnackbar: Snackbar? = null + lateinit var mToBeDeleted: MutableList + private var mActionMode: ActionMode? = null + private var mState: Parcelable? = null + + private var mIsSnackbarShown = false + private var mIsPickImageIntent = false + private var mIsPickVideoIntent = false + private var mIsGetImageContentIntent = false + private var mIsGetVideoContentIntent = false + private var mIsGetAnyContentIntent = false + private var mIsSetWallpaperIntent = false + private var mIsThirdPartyIntent = false + private var mIsGettingDirs = false + private var mSelectedItemsCnt = 0 + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + mIsPickImageIntent = isPickImageIntent(intent) + mIsPickVideoIntent = isPickVideoIntent(intent) + mIsGetImageContentIntent = isGetImageContentIntent(intent) + mIsGetVideoContentIntent = isGetVideoContentIntent(intent) + mIsGetAnyContentIntent = isGetAnyContentIntent(intent) + mIsSetWallpaperIntent = isSetWallpaperIntent(intent) + mIsThirdPartyIntent = mIsPickImageIntent || mIsPickVideoIntent || mIsGetImageContentIntent || mIsGetVideoContentIntent || + mIsGetAnyContentIntent || mIsSetWallpaperIntent + + mToBeDeleted = ArrayList() + directories_holder.setOnRefreshListener(this) + mDirs = ArrayList() + } + + override fun onCreateOptionsMenu(menu: Menu): Boolean { + if (mIsThirdPartyIntent) + return false + + menuInflater.inflate(R.menu.menu_main, menu) + return true + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + return when (item.itemId) { + R.id.sort -> { + showSortingDialog() + true + } + R.id.camera -> { + startActivity(Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA)) + true + } + R.id.settings -> { + startActivity(Intent(applicationContext, SettingsActivity::class.java)) + true + } + R.id.about -> { + startActivity(Intent(applicationContext, AboutActivity::class.java)) + true + } + else -> super.onOptionsItemSelected(item) + } + } + + override fun onResume() { + super.onResume() + tryloadGallery() + if (mState != null) + directories_grid.onRestoreInstanceState(mState) + } + + override fun onPause() { + super.onPause() + deleteDirs() + mState = directories_grid.onSaveInstanceState() + } + + override fun onDestroy() { + super.onDestroy() + mConfig.isFirstRun = false + } + + private fun tryloadGallery() { + if (hasStoragePermission()) { + getDirectories() + } else { + ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), STORAGE_PERMISSION) + } + } + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + + if (requestCode == STORAGE_PERMISSION) { + if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + getDirectories() + } else { + toast(R.string.no_permissions) + finish() + } + } + } + + private fun getDirectories() { + if (mIsGettingDirs) + return + + mIsGettingDirs = true + GetDirectoriesAsynctask(applicationContext, mIsPickVideoIntent || mIsGetVideoContentIntent, mIsPickImageIntent || mIsGetImageContentIntent, + mToBeDeleted, this).execute() + } + + private fun showSortingDialog() { + ChangeSortingDialog(this, true, object : ChangeSortingDialog.OnChangeSortingListener { + override fun sortingChanged() { + getDirectories() + } + }) + } + + private fun prepareForDeleting() { + toast(R.string.deleting) + val items = directories_grid.checkedItemPositions + val cnt = items.size() + var deletedCnt = 0 + for (i in 0..cnt - 1) { + if (items.valueAt(i)) { + val id = items.keyAt(i) + val path = mDirs[id].path + mToBeDeleted.add(path) + deletedCnt++ + } + } + + for (path in mToBeDeleted) { + if (isShowingPermDialog(File(path))) { + return + } + } + + notifyDeletion(deletedCnt) + } + + private fun notifyDeletion(cnt: Int) { + getDirectories() + + val res = resources + val msg = res.getQuantityString(R.plurals.folders_deleted, cnt, cnt) + mSnackbar = Snackbar.make(coordinator_layout, msg, Snackbar.LENGTH_INDEFINITE) + mSnackbar!!.apply { + setAction(res.getString(R.string.undo), undoDeletion) + setActionTextColor(Color.WHITE) + show() + } + mIsSnackbarShown = true + } + + private fun deleteDirs() { + if (mToBeDeleted.isEmpty()) + return + + mSnackbar?.dismiss() + mIsSnackbarShown = false + + val updatedFiles = ArrayList() + for (delPath in mToBeDeleted) { + val dir = File(delPath) + if (dir.exists()) { + val files = dir.listFiles() + for (file in files) { + if (file.isFile && file.isPhotoVideo()) { + updatedFiles.add(file) + deleteItem(file) + } + } + updatedFiles.add(dir) + if (dir.listFiles().size == 0) + deleteItem(dir) + } + } + + scanFiles(updatedFiles) {} + mToBeDeleted.clear() + } + + private fun deleteItem(file: File) { + if (needsStupidWritePermissions(file.absolutePath)) { + if (!isShowingPermDialog(file)) { + getFileDocument(file.absolutePath, mConfig.treeUri).delete() + } + } else { + file.delete() + } + } + + private val undoDeletion = View.OnClickListener { + mSnackbar!!.dismiss() + mIsSnackbarShown = false + mToBeDeleted.clear() + getDirectories() + } + + private fun showProperties() { + val items = directories_grid.checkedItemPositions + if (items.size() == 1) { + PropertiesDialog(this, selectedPaths.toTypedArray()[0], false) + } else { + val paths = ArrayList(items.size()) + val cnt = items.size() + for (i in 0..cnt - 1) { + if (items.valueAt(i)) { + val id = items.keyAt(i) + paths.add(mDirs[id].path) + } + } + + PropertiesDialog(this, paths, false) + } + } + + private fun editDirectory() { + val items = directories_grid.checkedItemPositions + val cnt = items.size() + for (i in 0..cnt - 1) { + if (items.valueAt(i)) { + val id = items.keyAt(i) + val path = mDirs[id].path + renameDir(path) + break + } + } + } + + private fun renameDir(path: String) { + val dir = File(path) + if (isAStorageRootFolder(path)) { + toast(R.string.rename_folder_root) + return + } + + RenameDirectoryDialog(this, dir, object : RenameDirectoryDialog.OnRenameDirListener { + override fun onRenameDirSuccess(changedPaths: ArrayList) { + mActionMode!!.finish() + applicationContext.scanPaths(changedPaths) { scanCompleted(path) } + } + }) + } + + private fun displayCopyDialog() { + val files = ArrayList() + val items = directories_grid.checkedItemPositions + val cnt = items.size() + for (i in 0..cnt - 1) { + if (items.valueAt(i)) { + val id = items.keyAt(i) + val dir = File(mDirs[id].path) + files.addAll(dir.listFiles()) + } + } + + CopyDialog(this, files, object : CopyMoveTask.CopyMoveListener { + override fun copySucceeded(deleted: Boolean, copiedAll: Boolean) { + if (deleted) { + getDirectories() + toast(if (copiedAll) R.string.moving_success else R.string.moving_success_partial) + } else { + toast(if (copiedAll) R.string.copying_success else R.string.copying_success_partial) + } + } + + override fun copyFailed() { + toast(R.string.copy_move_failed) + } + }) + } + + private fun isPickImageIntent(intent: Intent) = isPickIntent(intent) && (hasImageContentData(intent) || isImageType(intent)) + + private fun isPickVideoIntent(intent: Intent) = isPickIntent(intent) && (hasVideoContentData(intent) || isVideoType(intent)) + + private fun isPickIntent(intent: Intent) = intent.action == Intent.ACTION_PICK + + private fun isGetContentIntent(intent: Intent) = intent.action == Intent.ACTION_GET_CONTENT && intent.type != null + + private fun isGetImageContentIntent(intent: Intent) = isGetContentIntent(intent) && + (intent.type.startsWith("image/") || intent.type == MediaStore.Images.Media.CONTENT_TYPE) + + private fun isGetVideoContentIntent(intent: Intent) = isGetContentIntent(intent) && + (intent.type.startsWith("video/") || intent.type == MediaStore.Video.Media.CONTENT_TYPE) + + private fun isGetAnyContentIntent(intent: Intent) = isGetContentIntent(intent) && intent.type == "*/*" + + private fun isSetWallpaperIntent(intent: Intent?) = intent?.action == Intent.ACTION_SET_WALLPAPER + + private fun hasImageContentData(intent: Intent) = intent.data == MediaStore.Images.Media.EXTERNAL_CONTENT_URI + + private fun hasVideoContentData(intent: Intent) = intent.data == MediaStore.Video.Media.EXTERNAL_CONTENT_URI + + private fun isImageType(intent: Intent) = (intent.type?.startsWith("image/") == true || intent.type == MediaStore.Images.Media.CONTENT_TYPE) + + private fun isVideoType(intent: Intent) = (intent.type?.startsWith("video/") == true || intent.type == MediaStore.Video.Media.CONTENT_TYPE) + + override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { + if (resultCode == Activity.RESULT_OK) { + if (requestCode == PICK_MEDIA && resultData != null) { + Intent().apply { + val path = resultData.data.path + val uri = Uri.fromFile(File(path)) + if (mIsGetImageContentIntent || mIsGetVideoContentIntent || mIsGetAnyContentIntent) { + val type = Utils.getMimeType(path) + setDataAndTypeAndNormalize(uri, type) + flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + } else if (mIsPickImageIntent || mIsPickVideoIntent) { + data = uri + flags = Intent.FLAG_GRANT_READ_URI_PERMISSION + } + + setResult(Activity.RESULT_OK, this) + } + finish() + } else if (requestCode == PICK_WALLPAPER) { + setResult(Activity.RESULT_OK) + finish() + } + } + super.onActivityResult(requestCode, resultCode, resultData) + } + + override fun onItemClick(parent: AdapterView<*>, view: View, position: Int, id: Long) { + Intent(this, MediaActivity::class.java).apply { + putExtra(Constants.DIRECTORY, mDirs[position].path) + + if (mIsSetWallpaperIntent) { + putExtra(Constants.SET_WALLPAPER_INTENT, true) + startActivityForResult(this, PICK_WALLPAPER) + } else { + putExtra(Constants.GET_IMAGE_INTENT, mIsPickImageIntent || mIsGetImageContentIntent) + putExtra(Constants.GET_VIDEO_INTENT, mIsPickVideoIntent || mIsGetVideoContentIntent) + putExtra(Constants.GET_ANY_INTENT, mIsGetAnyContentIntent) + startActivityForResult(this, PICK_MEDIA) + } + } + } + + /*override fun onItemCheckedStateChanged(mode: ActionMode, position: Int, id: Long, checked: Boolean) { + if (checked) { + mSelectedItemsCnt++ + } else { + mSelectedItemsCnt-- + } + + if (mSelectedItemsCnt > 0) { + mode.title = mSelectedItemsCnt.toString() + } + + mode.invalidate() + } + + override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean { + val inflater = mode.menuInflater + inflater.inflate(R.menu.cab_directories, menu) + mActionMode = mode + return true + } + + override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean { + menu.findItem(R.id.cab_edit).isVisible = mSelectedItemsCnt == 1 + + var hiddenCnt = 0 + var unhiddenCnt = 0 + val items = directories_grid.checkedItemPositions + val cnt = items.size() + for (i in 0..cnt - 1) { + if (items.valueAt(i)) { + val id = items.keyAt(i) + if (mConfig.getIsFolderHidden(mDirs[id].path)) + hiddenCnt++ + else + unhiddenCnt++ + } + } + + menu.findItem(R.id.cab_hide).isVisible = unhiddenCnt > 0 + menu.findItem(R.id.cab_unhide).isVisible = hiddenCnt > 0 + + return true + } + + override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { + return when (item.itemId) { + R.id.cab_properties -> { + showProperties() + true + } + R.id.cab_edit -> { + editDirectory() + true + } + R.id.cab_delete -> { + prepareForDeleting() + mode.finish() + true + } + R.id.cab_hide -> { + hideFolders() + mode.finish() + true + } + R.id.cab_unhide -> { + unhideFolders() + mode.finish() + true + } + R.id.cab_copy_move -> { + displayCopyDialog() + true + } + else -> false + } + } + + override fun onDestroyActionMode(mode: ActionMode) { + mSelectedItemsCnt = 0 + } + + override fun onTouch(v: View, event: MotionEvent): Boolean { + if (mIsSnackbarShown) { + deleteDirs() + } + + return false + }*/ + + private fun hideFolders() { + mConfig.addHiddenDirectories(selectedPaths) + getDirectories() + } + + private fun unhideFolders() { + mConfig.removeHiddenDirectories(selectedPaths) + getDirectories() + } + + private val selectedPaths: Set + get() { + val items = directories_grid.checkedItemPositions + val selectedPaths = HashSet() + val cnt = items.size() + for (i in 0..cnt - 1) { + if (items.valueAt(i)) { + val id = items.keyAt(i) + selectedPaths.add(mDirs[id].path) + } + } + return selectedPaths + } + + private fun scanCompleted(path: String) { + val dir = File(path) + if (dir.isDirectory) { + getDirectories() + + runOnUiThread { toast(R.string.rename_folder_ok) } + } + } + + override fun onRefresh() { + getDirectories() + directories_holder.isRefreshing = false + } + + override fun gotDirectories(dirs: ArrayList) { + mIsGettingDirs = false + if (dirs.toString() == mDirs.toString()) { + return + } + mDirs = dirs + + val adapter = DirectoryAdapter(this, mDirs) + directories_grid.apply { + this@apply.adapter = adapter + onItemClickListener = this@MainActivity + //setMultiChoiceModeListener(this) + //setOnTouchListener(this) + choiceMode = GridView.CHOICE_MODE_MULTIPLE_MODAL + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameDirectoryDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameDirectoryDialog.kt index 2e09b1921..f5a635487 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameDirectoryDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/RenameDirectoryDialog.kt @@ -72,11 +72,10 @@ class RenameDirectoryDialog(val activity: SimpleActivity, val dir: File, val lis } updatedFiles.add(newDir.absolutePath) - val changedFiles = updatedFiles.toTypedArray() - listener.onRenameDirSuccess(changedFiles) + listener.onRenameDirSuccess(updatedFiles) } interface OnRenameDirListener { - fun onRenameDirSuccess(changedFiles: Array) + fun onRenameDirSuccess(changedPaths: ArrayList) } }