From 1df15a8273accec80b5b234e0aa0de8a0833bf68 Mon Sep 17 00:00:00 2001 From: dnldsht Date: Thu, 16 Jun 2016 17:41:54 +0200 Subject: [PATCH] clean --- .../java/com/horaapps/leafpic/Base/Album.java | 123 +++++++----------- .../com/horaapps/leafpic/MainActivity.java | 101 +++++++------- 2 files changed, 96 insertions(+), 128 deletions(-) diff --git a/app/src/main/java/com/horaapps/leafpic/Base/Album.java b/app/src/main/java/com/horaapps/leafpic/Base/Album.java index 78dcc9e11..f20cff94f 100644 --- a/app/src/main/java/com/horaapps/leafpic/Base/Album.java +++ b/app/src/main/java/com/horaapps/leafpic/Base/Album.java @@ -5,24 +5,18 @@ import android.media.MediaScannerConnection; import android.net.Uri; import android.preference.PreferenceManager; -import android.util.Log; import com.horaapps.leafpic.Adapters.PhotosAdapter; -import com.horaapps.leafpic.MyApplication; import com.horaapps.leafpic.R; import com.horaapps.leafpic.utils.ContentHelper; import com.horaapps.leafpic.utils.StringUtils; +import org.jetbrains.annotations.NotNull; + import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.io.OutputStream; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.Comparator; -import java.util.List; /** * Created by dnld on 26/04/16. @@ -33,38 +27,35 @@ public class Album { private String path = null; private int count = -1; private boolean selected = false; - private int filter_photos; + private int filter; public AlbumSettings settings = new AlbumSettings(); - private int current = -1; + private int currentMediaIndex = 0; private String storageRootPath; public ArrayList media = new ArrayList(); public ArrayList selectedMedias = new ArrayList(); - public Album() { - media = new ArrayList(); - selectedMedias = new ArrayList(); - } + public Album() { } - public Album(String path, String name, int count, String storageRootPath) { + public Album(String path, String name) { media = new ArrayList(); selectedMedias = new ArrayList(); this.path = path; this.name = name; - this.count = count; + } + + public Album(String path, String name, int count, String storageRootPath) { + this(path, name, count); this.storageRootPath = storageRootPath; } public Album(String path, String name, int count) { - media = new ArrayList(); - selectedMedias = new ArrayList(); - this.path = path; - this.name = name; + this(path, name); this.count = count; } - public Album(Context context, File mediaPath) { + public Album(Context context, @NotNull File mediaPath) { File folder = mediaPath.getParentFile(); media = new ArrayList(); selectedMedias = new ArrayList(); @@ -81,17 +72,10 @@ public Album(Context context, Uri mediaUri) { setCurrentPhotoIndex(0); } - public Album(String path, String name) { - media = new ArrayList(); - selectedMedias = new ArrayList(); - this.path = path; - this.name = name; - } - public void updatePhotos(Context context) { SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(context); ArrayList mediaArrayList = new ArrayList(); - File[] images = new File(getPath()).listFiles(new ImageFileFilter(filter_photos, SP.getBoolean("set_include_video",true))); + File[] images = new File(getPath()).listFiles(new ImageFileFilter(filter, SP.getBoolean("set_include_video",true))); for (File image : images) mediaArrayList.add(0, new Media(image)); media = mediaArrayList; @@ -118,23 +102,8 @@ public int compare(String lhs, String rhs) { return result; } - /* public void updatePhotos(PhotosAdapter adapter) { - media = new ArrayList(); - File[] images = new File(getPath()).listFiles(new ImageFileFilter(filter_photos)); - for (int i = 0; i < images.length; i++) { - media.add(0, new Media(images[i].getAbsolutePath(), images[i].lastModified(), images[i].length())); - adapter.notifyItemInserted(i); - } - sortPhotos(); - adapter.notifyDataSetChanged(); - }*/ - - public boolean areFiltersActive() { - return filter_photos != ImageFileFilter.FILTER_ALL; - } - public void filterMedias(Context context, int filter) { - filter_photos = filter; + this.filter = filter; updatePhotos(context); } @@ -157,11 +126,11 @@ public boolean isSelected() { public Media getMedia(int index) { return media.get(index); } - public void setCurrentPhotoIndex(int index){ current = index; } + public void setCurrentPhotoIndex(int index){ currentMediaIndex = index; } - public Media getCurrentMedia() { return getMedia(current); } + public Media getCurrentMedia() { return getMedia(currentMediaIndex); } - public int getCurrentMediaIndex() { return current; } + public int getCurrentMediaIndex() { return currentMediaIndex; } public String getContentDescription(Context c) { return c.getString(R.string.media); @@ -209,19 +178,22 @@ public void setSelectedPhotoAsPreview(Context context) { private void setCurrentPhoto(String path) { for (int i = 0; i < media.size(); i++) - if (media.get(i).getPath().equals(path)) current = i; + if (media.get(i).getPath().equals(path)) currentMediaIndex = i; } public int getSelectedCount() { return selectedMedias.size(); } + public boolean areMediaSelected() { return getSelectedCount() != 0;} + public void selectAllPhotos() { - for (int i = 0; i < media.size(); i++) + for (int i = 0; i < media.size(); i++) { if (!media.get(i).isSelected()) { media.get(i).setSelected(true); selectedMedias.add(media.get(i)); } + } } public int toggleSelectPhoto(int index) { @@ -241,31 +213,32 @@ public void setDefaultSortingMode(Context context, int column) { settings.columnSortingMode = column; } - public void renameCurrentMedia(Context context, String newName) { + public boolean renameCurrentMedia(Context context, String newName) { + boolean success = false; try { File from = new File(getCurrentMedia().getPath()); File to = new File(StringUtils.getPhotoPathRenamed(getCurrentMedia().getPath(), newName)); - if (from.renameTo(to)) { + if (success = ContentHelper.moveFile(context, from, to)) { scanFile(context, new String[]{ to.getAbsolutePath(), from.getAbsolutePath() }); getCurrentMedia().path = to.getAbsolutePath(); } - } catch (Exception e) { - e.printStackTrace(); - } + } catch (Exception e) { e.printStackTrace(); } + return success; } - public int moveCurrentPhoto(Context context, String newName) { + public boolean moveCurrentPhoto(Context context, String newName) { + boolean success = false; try { File from = new File(getCurrentMedia().getPath()); File to = new File(StringUtils.getPhotoPathMoved(getCurrentMedia().getPath(), newName)); - if (ContentHelper.moveFile(context, from, to)) { + if (success = ContentHelper.moveFile(context, from, to)) { scanFile(context, new String[]{ to.getAbsolutePath(), from.getAbsolutePath() }); getCurrentMedia().path = to.getAbsolutePath(); media.remove(getCurrentMediaIndex()); setCount(media.size()); } } catch (Exception e) { e.printStackTrace(); } - return getCurrentMediaIndex(); + return success; } public void setDefaultSortingAscending(Context context, Boolean ascending) { @@ -311,9 +284,8 @@ public void selectAllPhotosUpTo(int targetIndex, PhotosAdapter adapter) { } public void clearSelectedPhotos() { - for (Media m : media) { + for (Media m : media) m.setSelected(false); - } selectedMedias.clear(); } @@ -341,39 +313,30 @@ public void copySelectedPhotos(Context context, String folderPath) { copyPhoto(context, media.getPath(), folderPath); } - public void copyPhoto(Context context, String olderPath, String folderPath) { + public boolean copyPhoto(Context context, String olderPath, String folderPath) { + boolean success = false; try { File from = new File(olderPath); File to = new File(StringUtils.getPhotoPathMoved(olderPath, folderPath)); - - if (ContentHelper.copyFile(context, from, to)) { + if (success = ContentHelper.copyFile(context, from, to)) scanFile(context, new String[]{ to.getAbsolutePath() }); - } - /*InputStream in = new FileInputStream(from); - OutputStream out = new FileOutputStream(to); - - byte[] buf = new byte[1024]; - int len; - while ((len = in.read(buf)) > 0) - out.write(buf, 0, len); - - in.close(); - out.close();*/ - //scanFile(context, new String[]{to.getAbsolutePath()}); } catch (Exception e) { e.printStackTrace(); } + return success; } public void deleteCurrentMedia(Context context) { - deleteMedia(context, media.get(getCurrentMediaIndex())); + deleteMedia(context, getCurrentMedia()); media.remove(getCurrentMediaIndex()); setCount(media.size()); } - private void deleteMedia(Context context, Media media) { + private boolean deleteMedia(Context context, Media media) { + boolean success; File file = new File(media.getPath()); - if (ContentHelper.deleteFile(context, file)) + if (success = ContentHelper.deleteFile(context, file)) scanFile(context, new String[]{ file.getAbsolutePath() }); + return success; } public void deleteSelectedMedia(Context context) { @@ -393,7 +356,6 @@ public void renameAlbum(Context context, String newName) { for (int i = 0; i < media.size(); i++) { File from = new File(media.get(i).getPath()); File to = new File(StringUtils.getPhotoPathRenamedAlbumChange(media.get(i).getPath(), newName)); - //File to = new File(StringUtils.getPhotoPathRenamedAlbumChange(media.get(i).getPath(), newName)); if (ContentHelper.moveFile(context, from, to)) { MediaScannerConnection.scanFile(context, new String[]{from.getAbsolutePath(), to.getAbsolutePath()}, null, null); @@ -405,4 +367,7 @@ public void renameAlbum(Context context, String newName) { public void scanFile(Context context, String[] path) { MediaScannerConnection.scanFile(context, path, null, null); } + public boolean areFiltersActive() { + return filter != ImageFileFilter.FILTER_ALL; + } } diff --git a/app/src/main/java/com/horaapps/leafpic/MainActivity.java b/app/src/main/java/com/horaapps/leafpic/MainActivity.java index ffd02ae1b..ed50e0462 100644 --- a/app/src/main/java/com/horaapps/leafpic/MainActivity.java +++ b/app/src/main/java/com/horaapps/leafpic/MainActivity.java @@ -20,6 +20,7 @@ import android.os.Bundle; import android.os.Environment; import android.preference.PreferenceManager; +import android.provider.MediaStore; import android.support.design.widget.FloatingActionButton; import android.support.v4.content.ContextCompat; import android.support.v4.view.GravityCompat; @@ -91,6 +92,7 @@ public class MainActivity extends ThemedActivity { private static String TAG = "AlbumsAct"; + private int REQUEST_CODE_SD_CARD_PERMISSIONS = 42; private CustomAlbumsHandler customAlbumsHandler = new CustomAlbumsHandler(MainActivity.this); private SharedPreferences SP; @@ -112,16 +114,16 @@ public class MainActivity extends ThemedActivity { private SelectAlbumBottomSheet bottomSheetDialogFragment; private SwipeRefreshLayout swipeRefreshLayout; - private boolean hidden = false, pickmode = false, editmode = false, albumsMode = true, firstLaunch = true; + private boolean hidden = false, pickMode = false, editMode = false, albumsMode = true, firstLaunch = true; private View.OnLongClickListener photosOnLongClickListener = new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { int index = Integer.parseInt(v.findViewById(R.id.photo_path).getTag().toString()); - if (!editmode) { + if (!editMode) { // If it is the first long press mediaAdapter.notifyItemChanged(album.toggleSelectPhoto(index)); - editmode = true; + editMode = true; } else album.selectAllPhotosUpTo(index, mediaAdapter); @@ -134,8 +136,8 @@ public boolean onLongClick(View v) { @Override public void onClick(View v) { int index = Integer.parseInt(v.findViewById(R.id.photo_path).getTag().toString()); - if (!pickmode) { - if (editmode) { + if (!pickMode) { + if (editMode) { mediaAdapter.notifyItemChanged(album.toggleSelectPhoto(index)); invalidateOptionsMenu(); } else { @@ -157,7 +159,7 @@ public void onClick(View v) { public boolean onLongClick(View v) { int index = Integer.parseInt(v.findViewById(R.id.album_name).getTag().toString()); albumsAdapter.notifyItemChanged(albums.toggleSelectAlbum(index)); - editmode = true; + editMode = true; invalidateOptionsMenu(); return true; } @@ -167,7 +169,7 @@ public boolean onLongClick(View v) { @Override public void onClick(View v) { int index = Integer.parseInt(v.findViewById(R.id.album_name).getTag().toString()); - if (editmode) { + if (editMode) { albumsAdapter.notifyItemChanged(albums.toggleSelectAlbum(index)); invalidateOptionsMenu(); } else { @@ -187,7 +189,7 @@ public void onCreate(Bundle savedInstanceState) { albums = new HandlingAlbums(getApplicationContext()); album = new Album(); albumsMode = true; - editmode = false; + editMode = false; securityObj = new SecurityHelper(MainActivity.this); initUI(); @@ -233,7 +235,7 @@ public void onClick(View v) { displayAlbums(); } }); - albumsMode = editmode = false; + albumsMode = editMode = false; invalidateOptionsMenu(); } @@ -260,7 +262,7 @@ private void displayAlbums(boolean reload) { }); albumsMode = true; - editmode = false; + editMode = false; invalidateOptionsMenu(); mediaAdapter.updateDataSet(new ArrayList()); recyclerViewMedia.scrollToPosition(0); @@ -317,7 +319,7 @@ private void displayPreFetchedData(Bundle data){ if (content == SplashScreen.ALBUMS_PREFETCHED) { albums = ((MyApplication) getApplicationContext()).getAlbums(); displayAlbums(false); - pickmode = data.getBoolean(SplashScreen.PICK_MODE); + pickMode = data.getBoolean(SplashScreen.PICK_MODE); albumsAdapter.updateDataSet(albums.dispAlbums); toggleRecyclersVisibilty(true); } else if (content == SplashScreen.PHOTS_PREFETCHED) { @@ -393,8 +395,6 @@ public void onRefresh() { } }); - - /**** DRAWER ****/ mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerLayout.addDrawerListener(new ActionBarDrawerToggle(this, @@ -419,25 +419,31 @@ public void onDrawerOpened(View drawerView) { fabCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - /* if (!albumsMode && album.areFiltersActive()) { + if (!albumsMode && album.areFiltersActive()) { album.filterMedias(getApplicationContext(), ImageFileFilter.FILTER_ALL); mediaAdapter.updateDataSet(album.media); checkNothing(); toolbar.getMenu().findItem(R.id.all_media_filter).setChecked(true); fabCamera.setImageDrawable(new IconicsDrawable(MainActivity.this).icon(GoogleMaterial.Icon.gmd_camera_alt).color(Color.WHITE)); } else startActivity(new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA)); -*/ - //region TESTING - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { - startActivityForResult(new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE), 42); - } - - //newFolderDialog(); - //endregion } }); + //region TESTING + fabCamera.setOnLongClickListener(new View.OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + //newFolderDialog(); + // NOTE: this is used to acquire write permission on sd with api 21 + // TODO call this one when unable to write on sd + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { + startActivityForResult(new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE), REQUEST_CODE_SD_CARD_PERMISSIONS); + } + return false; + } + }); + //endregion int statusBarHeight = Measure.getStatusBarHeight(getResources()), @@ -536,6 +542,7 @@ public View getView(int position, View convertView, ViewGroup parent) { } } }); + /**** OK Dialog ****/ dialogExplorer.setPositiveButton(R.string.ok_action, new DialogInterface.OnClickListener() { @Override @@ -558,10 +565,7 @@ public void onClick(DialogInterface dialog, int which) { } }); /**** CANCEL Dialog ****/ - dialogExplorer.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){ - @Override - public void onClick(DialogInterface dialog, int which) {} - }); + dialogExplorer.setNegativeButton(R.string.cancel, null); /**** Set View ****/ dialogExplorer.setView(dialogExplorerLayout); @@ -598,11 +602,10 @@ public View getView(int position, View convertView, ViewGroup parent) { @TargetApi(Build.VERSION_CODES.LOLLIPOP) @Override public final void onActivityResult(final int requestCode, final int resultCode, final Intent resultData) { - if (requestCode == 42) { - Uri treeUri = null; - if (resultCode == Activity.RESULT_OK) { + if (resultCode == Activity.RESULT_OK) { + if (requestCode == REQUEST_CODE_SD_CARD_PERMISSIONS) { // Get Uri from Storage Access Framework. - treeUri = resultData.getData(); + Uri treeUri = resultData.getData(); // Persist URI in shared preference so that you can use it later. // Use your own framework here instead of PreferenceUtil. @@ -826,7 +829,7 @@ private void updateSelectedStuff() { toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - editmode = false; + editMode = false; albums.clearSelectedAlbums(); albumsAdapter.notifyDataSetChanged(); invalidateOptionsMenu(); @@ -892,7 +895,7 @@ public void onClick(View v) { } private void finishEditMode() { - editmode = false; + editMode = false; if (albumsMode) { albums.clearSelectedAlbums(); albumsAdapter.notifyDataSetChanged(); @@ -964,33 +967,33 @@ public boolean onCreateOptionsMenu(Menu menu) { @Override public boolean onPrepareOptionsMenu(final Menu menu) { if (albumsMode) { - editmode = albums.getSelectedCount() != 0; - menu.setGroupVisible(R.id.album_options_menu, editmode); + editMode = albums.getSelectedCount() != 0; + menu.setGroupVisible(R.id.album_options_menu, editMode); menu.setGroupVisible(R.id.photos_option_men, false); } else { - editmode = album.getSelectedCount() != 0; - menu.setGroupVisible(R.id.photos_option_men, editmode); - menu.setGroupVisible(R.id.album_options_menu, !editmode); + editMode = album.areMediaSelected(); + menu.setGroupVisible(R.id.photos_option_men, editMode); + menu.setGroupVisible(R.id.album_options_menu, !editMode); } togglePrimaryToolbarOptions(menu); updateSelectedStuff(); - menu.findItem(R.id.select_all).setVisible(editmode); - menu.findItem(R.id.installShortcut).setVisible(albumsMode && editmode); + menu.findItem(R.id.select_all).setVisible(editMode); + menu.findItem(R.id.installShortcut).setVisible(albumsMode && editMode); menu.findItem(R.id.type_sort_action).setVisible(!albumsMode); - menu.findItem(R.id.delete_action).setVisible(!albumsMode || editmode); + menu.findItem(R.id.delete_action).setVisible(!albumsMode || editMode); menu.findItem(R.id.setAsAlbumPreview).setVisible(!albumsMode && album.getSelectedCount() == 1); menu.findItem(R.id.clear_album_preview).setVisible(!albumsMode && album.hasCustomCover()); - menu.findItem(R.id.renameAlbum).setVisible((albumsMode && albums.getSelectedCount() == 1) || (!albumsMode && !editmode)); + menu.findItem(R.id.renameAlbum).setVisible((albumsMode && albums.getSelectedCount() == 1) || (!albumsMode && !editMode)); menu.findItem(R.id.affixPhoto).setVisible(!albumsMode && album.getSelectedCount() > 1); return super.onPrepareOptionsMenu(menu); } private void togglePrimaryToolbarOptions(final Menu menu) { - menu.setGroupVisible(R.id.general_action, !editmode); + menu.setGroupVisible(R.id.general_action, !editMode); - if (!editmode) { + if (!editMode) { menu.findItem(R.id.filter_menu).setVisible(!albumsMode); menu.findItem(R.id.search_action).setVisible(albumsMode); } @@ -1004,13 +1007,13 @@ public boolean onOptionsItemSelected(MenuItem item) { case R.id.select_all: if (albumsMode) { if (albums.getSelectedCount() == albumsAdapter.getItemCount()) { - editmode = false; + editMode = false; albums.clearSelectedAlbums(); } else albums.selectAllAlbums(); albumsAdapter.notifyDataSetChanged(); } else { if (album.getSelectedCount() == mediaAdapter.getItemCount()) { - editmode = false; + editMode = false; album.clearSelectedPhotos(); } else album.selectAllPhotos(); mediaAdapter.notifyDataSetChanged(); @@ -1080,7 +1083,7 @@ protected Void doInBackground(String... arg0) { if (albumsMode) albums.deleteSelectedAlbums(MainActivity.this); else { - if (editmode) + if (editMode) album.deleteSelectedMedia(getApplicationContext()); else { albums.deleteAlbum(album, getApplicationContext()); @@ -1111,7 +1114,7 @@ protected void onPostExecute(Void result) { } AlertDialog.Builder deleteDialog = new AlertDialog.Builder(MainActivity.this, getDialogStyle()); - AlertDialogsHelper.getTextDialog(this, deleteDialog, getString(R.string.delete), getString(albumsMode || (!albumsMode && !editmode) ? R.string.delete_album_message : R.string.delete_photos_message)); + AlertDialogsHelper.getTextDialog(this, deleteDialog, getString(R.string.delete), getString(albumsMode || (!albumsMode && !editMode) ? R.string.delete_album_message : R.string.delete_photos_message)); deleteDialog.setNegativeButton(this.getString(R.string.cancel), null); deleteDialog.setPositiveButton(this.getString(R.string.delete), new DialogInterface.OnClickListener() { @@ -1450,7 +1453,7 @@ protected Void doInBackground(String... arg0) { } @Override protected void onPostExecute(Void result) { - editmode = false; + editMode = false; album.clearSelectedPhotos(); dialog.dismiss(); invalidateOptionsMenu(); @@ -1588,7 +1591,7 @@ private void toggleRecyclersVisibilty(boolean albumsMode){ @Override public void onBackPressed() { - if (editmode) finishEditMode(); + if (editMode) finishEditMode(); else { if (albumsMode) { if (mDrawerLayout.isDrawerOpen(GravityCompat.START))