diff --git a/app/src/androidTest/java/com/praharsh/vudit/ApplicationTest.java b/app/src/androidTest/java/com/praharsh/vudit/ApplicationTest.java index 9661184..abb748e 100644 --- a/app/src/androidTest/java/com/praharsh/vudit/ApplicationTest.java +++ b/app/src/androidTest/java/com/praharsh/vudit/ApplicationTest.java @@ -1,4 +1,4 @@ -package com.example.root.vudit; +package com.praharsh.vudit; import android.app.Application; import android.test.ApplicationTestCase; @@ -8,6 +8,6 @@ */ public class ApplicationTest extends ApplicationTestCase { public ApplicationTest() { - super(Application.class); + super(); } } \ No newline at end of file diff --git a/app/src/main/java/com/praharsh/vudit/FileViewer.java b/app/src/main/java/com/praharsh/vudit/FileViewer.java index 4b26065..488a637 100644 --- a/app/src/main/java/com/praharsh/vudit/FileViewer.java +++ b/app/src/main/java/com/praharsh/vudit/FileViewer.java @@ -101,6 +101,11 @@ public class FileViewer extends AppCompatActivity Manifest.permission.INTERNET, Manifest.permission.ACCESS_NETWORK_STATE, }; + + private enum MediaFileType { + Image, Audio, Video, Document, Archive, Text, APK, + } + private static boolean mBusy = false, recentsView = false, favouritesView = false, homeView = true; private static ViewHolder holder; private Toolbar toolbar; @@ -127,24 +132,16 @@ public class FileViewer extends AppCompatActivity return (res == 0 ? f1.getName().compareTo(f2.getName()) : res); }; private final Comparator byDate = (f1, f2) -> { - if (f1.lastModified() > f2.lastModified()) return 1; - else if (f1.lastModified() < f2.lastModified()) return -1; - else return 0; + return Long.compare(f1.lastModified(), f2.lastModified()); }; private final Comparator byDateDesc = (f1, f2) -> { - if (f1.lastModified() > f2.lastModified()) return -1; - else if (f1.lastModified() < f2.lastModified()) return 1; - else return 0; + return Long.compare(f2.lastModified(), f1.lastModified()); }; private final Comparator bySize = (f1, f2) -> { - if (f1.length() > f2.length()) return 1; - else if (f1.length() < f2.length()) return -1; - else return 0; + return Long.compare(f1.length(), f2.length()); }; private final Comparator bySizeDesc = (f1, f2) -> { - if (f1.length() > f2.length()) return -1; - else if (f1.length() < f2.length()) return 1; - else return 0; + return Long.compare(f2.length(), f1.length()); }; private static boolean deleteFiles(File f) { @@ -159,7 +156,8 @@ private static boolean deleteFiles(File f) { deleted &= deleteFiles(arr[i]); } return deleted && f.delete(); - } else return f.delete(); + } + return f.delete(); } private static String unpackZip(File zipFile, File targetDirectory) { @@ -285,13 +283,13 @@ protected void onCreate(Bundle savedInstanceState) { lv.setEmptyView(emptyListView); lv.setOnItemClickListener((adapterView, view, i, l) -> openFile(files[i])); homeViewLayout = findViewById(R.id.home_view); - homeViewLayout.findViewById(R.id.btn_image_files).setOnClickListener(view -> listMediaFiles(1)); - homeViewLayout.findViewById(R.id.btn_music_files).setOnClickListener(view -> listMediaFiles(2)); - homeViewLayout.findViewById(R.id.btn_video_files).setOnClickListener(view -> listMediaFiles(3)); - homeViewLayout.findViewById(R.id.btn_document_files).setOnClickListener(view -> listMediaFiles(4)); - homeViewLayout.findViewById(R.id.btn_archive_files).setOnClickListener(view -> listMediaFiles(5)); - homeViewLayout.findViewById(R.id.btn_text_files).setOnClickListener(view -> listMediaFiles(6)); - homeViewLayout.findViewById(R.id.btn_apps).setOnClickListener(view -> listMediaFiles(7)); + homeViewLayout.findViewById(R.id.btn_image_files).setOnClickListener(view -> listMediaFiles(MediaFileType.Image)); + homeViewLayout.findViewById(R.id.btn_music_files).setOnClickListener(view -> listMediaFiles(MediaFileType.Audio)); + homeViewLayout.findViewById(R.id.btn_video_files).setOnClickListener(view -> listMediaFiles(MediaFileType.Video)); + homeViewLayout.findViewById(R.id.btn_document_files).setOnClickListener(view -> listMediaFiles(MediaFileType.Document)); + homeViewLayout.findViewById(R.id.btn_archive_files).setOnClickListener(view -> listMediaFiles(MediaFileType.Archive)); + homeViewLayout.findViewById(R.id.btn_text_files).setOnClickListener(view -> listMediaFiles(MediaFileType.Text)); + homeViewLayout.findViewById(R.id.btn_apps).setOnClickListener(view -> listMediaFiles(MediaFileType.APK)); homeViewLayout.findViewById(R.id.btn_camera_folder).setOnClickListener(view -> { File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); if (f == null || "".equals(f.getPath()) || !f.exists()) @@ -784,7 +782,6 @@ private void openFile(File current_file) { if (isValid) { ImageView album_art, icon; final View player = getLayoutInflater().inflate(R.layout.music_player, null); - player.findViewById(R.id.icon); btn_play = player.findViewById(R.id.btn_play); btn_rev = player.findViewById(R.id.btn_rev); btn_forward = player.findViewById(R.id.btn_forward); @@ -919,13 +916,15 @@ private void showProperties(final File current_file) { int duration; AlertDialog.Builder properties_dialog = new AlertDialog.Builder(FileViewer.this); View properties_view = getLayoutInflater().inflate(R.layout.properties_view, null); - TextView name = properties_view.findViewById(R.id.name); - TextView type = properties_view.findViewById(R.id.type); - TextView time = properties_view.findViewById(R.id.time); + final TextView name = properties_view.findViewById(R.id.name); + final TextView type = properties_view.findViewById(R.id.type); + final TextView time = properties_view.findViewById(R.id.time); final TextView size = properties_view.findViewById(R.id.size); + final TextView location = properties_view.findViewById(R.id.location); final TextView details = properties_view.findViewById(R.id.details); //Fetch properties name.setText(current_file.getName()); + location.setText(current_file.getPath()); SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss a"); time.setText(format.format(current_file.lastModified())); properties_dialog.setTitle("Properties"); @@ -1157,60 +1156,64 @@ public void onScrollStateChanged(final AbsListView view, final int scrollState) switch (scrollState) { case AbsListView.OnScrollListener.SCROLL_STATE_IDLE: mBusy = false; - int first = view.getFirstVisiblePosition(); - int count = view.getChildCount(); - for (int i = 0; i < count; i++) { - final File current_file = files[first + i]; - if (!current_file.exists()) - continue; - holder.icon = view.getChildAt(i).findViewById(R.id.icon); - if (current_file.isDirectory()) { - holder.details.setText(""); - File[] temp = current_file.listFiles(); - int n = (temp != null ? temp.length : 0); - holder.details.setText(n + " items"); - holder.icon.setImageResource(n > 0 ? R.drawable.folder : R.drawable.folder_empty); - } else { - holder.details.setText(Util.displaySize(current_file.length())); - String ext = Util.extension(current_file.getName()); - if ("apk".equals(ext)) { - String path = current_file.getPath(); - PackageManager pm = getPackageManager(); - PackageInfo pi = pm.getPackageArchiveInfo(path, 0); - pi.applicationInfo.sourceDir = path; - pi.applicationInfo.publicSourceDir = path; - holder.icon.setImageDrawable(pi.applicationInfo.loadIcon(pm)); - } else if ("pdf".equals(ext)) { - holder.icon.setImageResource(R.drawable.file_pdf); - } else if ("svg".equals(ext)) { - holder.icon.setImageResource(R.drawable.file_svg); - } else if ("csv".equals(ext)) { - holder.icon.setImageResource(R.drawable.file_csv); - } else if ("sqlite".equals(ext)) { - holder.icon.setImageResource(R.drawable.file_sqlite); - } else if (Util.audio_ext.contains(ext)) { - holder.icon.setImageResource(R.drawable.file_music); - } else if (Util.image_ext.contains(ext) || Util.video_ext.contains(ext)) { - Glide.with(getApplicationContext()).load(Uri.fromFile(current_file)).placeholder(R.drawable.loading).into(holder.icon); - } else if (Util.archive_ext.contains(ext)) { - holder.icon.setImageResource(R.drawable.file_archive); - } else if (Util.doc_ext.contains(ext)) { - holder.icon.setImageResource(R.drawable.file_doc); - } else if (Util.xl_ext.contains(ext)) { - holder.icon.setImageResource(R.drawable.file_xl); - } else if (Util.ppt_ext.contains(ext)) { - holder.icon.setImageResource(R.drawable.file_ppt); - } else { - holder.icon.setImageResource(R.drawable.file_default); - } - } - } + refreshList(view); break; default: mBusy = true; } } + private void refreshList(final AbsListView view) { + int first = view.getFirstVisiblePosition(); + int count = view.getChildCount(); + for (int i = 0; i < count; i++) { + final File current_file = files[first + i]; + if (!current_file.exists()) + continue; + holder.icon = view.getChildAt(i).findViewById(R.id.icon); + if (current_file.isDirectory()) { + holder.details.setText(""); + File[] temp = current_file.listFiles(); + int n = (temp != null ? temp.length : 0); + holder.details.setText(n + " items"); + holder.icon.setImageResource(n > 0 ? R.drawable.folder : R.drawable.folder_empty); + } else { + holder.details.setText(Util.displaySize(current_file.length())); + String ext = Util.extension(current_file.getName()); + if ("apk".equals(ext)) { + String path = current_file.getPath(); + PackageManager pm = getPackageManager(); + PackageInfo pi = pm.getPackageArchiveInfo(path, 0); + pi.applicationInfo.sourceDir = path; + pi.applicationInfo.publicSourceDir = path; + holder.icon.setImageDrawable(pi.applicationInfo.loadIcon(pm)); + } else if ("pdf".equals(ext)) { + holder.icon.setImageResource(R.drawable.file_pdf); + } else if ("svg".equals(ext)) { + holder.icon.setImageResource(R.drawable.file_svg); + } else if ("csv".equals(ext)) { + holder.icon.setImageResource(R.drawable.file_csv); + } else if ("sqlite".equals(ext)) { + holder.icon.setImageResource(R.drawable.file_sqlite); + } else if (Util.audio_ext.contains(ext)) { + holder.icon.setImageResource(R.drawable.file_music); + } else if (Util.image_ext.contains(ext) || Util.video_ext.contains(ext)) { + Glide.with(getApplicationContext()).load(Uri.fromFile(current_file)).placeholder(R.drawable.loading).into(holder.icon); + } else if (Util.archive_ext.contains(ext)) { + holder.icon.setImageResource(R.drawable.file_archive); + } else if (Util.doc_ext.contains(ext)) { + holder.icon.setImageResource(R.drawable.file_doc); + } else if (Util.xl_ext.contains(ext)) { + holder.icon.setImageResource(R.drawable.file_xl); + } else if (Util.ppt_ext.contains(ext)) { + holder.icon.setImageResource(R.drawable.file_ppt); + } else { + holder.icon.setImageResource(R.drawable.file_default); + } + } + } + } + //Utility functions private void restoreData() { SharedPreferences prefs = getSharedPreferences("Vudit_Settings", MODE_PRIVATE); @@ -1316,9 +1319,7 @@ else if (sortDesc) { Arrays.sort(f, new Comparator() { @Override public int compare(File f1, File f2) { - if (f1.isDirectory() && !f2.isDirectory()) return -1; - else if (!f1.isDirectory() && f2.isDirectory()) return 1; - else return 0; + return Boolean.compare(!f1.isDirectory(), !f2.isDirectory()); } }); } catch (Exception e) { @@ -1380,26 +1381,30 @@ private boolean appInstalled(String uri) { } } - private void listMediaFiles(int type) { + private void listMediaFiles(MediaFileType type) { String toolbarTitle = ""; String selectionQuery = null; String[] selectionArgs = null; ArrayList fileList = null; - Uri uri = MediaStore.Files.getContentUri("external"); + Uri externalURI = MediaStore.Files.getContentUri("external"); + Uri internalURI = MediaStore.Files.getContentUri("internal"); switch (type) { - case 1: //images + case Image: toolbarTitle = "Pictures"; fileList = getFileListFromQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selectionQuery, selectionArgs); + fileList.addAll(getFileListFromQuery(MediaStore.Images.Media.INTERNAL_CONTENT_URI, selectionQuery, selectionArgs)); break; - case 2: //audio + case Audio: toolbarTitle = "Music"; fileList = getFileListFromQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, selectionQuery, selectionArgs); + fileList.addAll(getFileListFromQuery(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, selectionQuery, selectionArgs)); break; - case 3: //video + case Video: toolbarTitle = "Videos"; fileList = getFileListFromQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, selectionQuery, selectionArgs); + fileList.addAll(getFileListFromQuery(MediaStore.Video.Media.INTERNAL_CONTENT_URI, selectionQuery, selectionArgs)); break; - case 4: //documents + case Document: toolbarTitle = "Documents"; List extList = new ArrayList<>(Util.doc_ext); extList.addAll(Util.xl_ext); @@ -1408,34 +1413,29 @@ private void listMediaFiles(int type) { extList.add("pdf"); selectionArgs = Util.getMimeTypeQueryArgs(extList); selectionQuery = Util.getMimeTypeQuery(selectionArgs); - fileList = getFileListFromQuery(uri, selectionQuery, selectionArgs); - uri = MediaStore.Files.getContentUri("internal"); - fileList.addAll(getFileListFromQuery(uri, selectionQuery, selectionArgs)); + fileList = getFileListFromQuery(externalURI, selectionQuery, selectionArgs); + fileList.addAll(getFileListFromQuery(internalURI, selectionQuery, selectionArgs)); break; - case 5: //archives + case Archive: toolbarTitle = "Archives"; selectionArgs = Util.getMimeTypeQueryArgs(Util.archive_ext); selectionQuery = Util.getMimeTypeQuery(selectionArgs); - fileList = getFileListFromQuery(uri, selectionQuery, selectionArgs); - uri = MediaStore.Files.getContentUri("internal"); - fileList.addAll(getFileListFromQuery(uri, selectionQuery, selectionArgs)); + fileList = getFileListFromQuery(externalURI, selectionQuery, selectionArgs); + fileList.addAll(getFileListFromQuery(internalURI, selectionQuery, selectionArgs)); break; - case 6: //text files + case Text: toolbarTitle = "Text files"; selectionArgs = Util.getMimeTypeQueryArgs(Util.txt_ext); selectionQuery = Util.getMimeTypeQuery(selectionArgs); - fileList = getFileListFromQuery(uri, selectionQuery, selectionArgs); - uri = MediaStore.Files.getContentUri("internal"); - fileList.addAll(getFileListFromQuery(uri, selectionQuery, selectionArgs)); + fileList = getFileListFromQuery(externalURI, selectionQuery, selectionArgs); + fileList.addAll(getFileListFromQuery(internalURI, selectionQuery, selectionArgs)); break; - case 7: //apk files + case APK: toolbarTitle = "Apps"; selectionArgs = Util.getMimeTypeQueryArgs(Collections.singletonList("apk")); selectionQuery = Util.getMimeTypeQuery(selectionArgs); - uri = MediaStore.Files.getContentUri("external"); - fileList = getFileListFromQuery(uri, selectionQuery, selectionArgs); - uri = MediaStore.Files.getContentUri("internal"); - fileList.addAll(getFileListFromQuery(uri, selectionQuery, selectionArgs)); + fileList = getFileListFromQuery(externalURI, selectionQuery, selectionArgs); + fileList.addAll(getFileListFromQuery(internalURI, selectionQuery, selectionArgs)); } files = new File[fileList.size()]; files = fileList.toArray(files); diff --git a/app/src/main/java/com/praharsh/vudit/Util.java b/app/src/main/java/com/praharsh/vudit/Util.java index 6bfc004..8ecf239 100644 --- a/app/src/main/java/com/praharsh/vudit/Util.java +++ b/app/src/main/java/com/praharsh/vudit/Util.java @@ -13,7 +13,7 @@ public class Util { //supported extensions - static final List audio_ext = Arrays.asList("mp3", "oog", "wav", "mid", "m4a", "amr"); + static final List audio_ext = Arrays.asList("mp3", "ogg", "wav", "mid", "m4a", "amr"); static final List image_ext = Arrays.asList("png", "jpg", "gif", "bmp", "jpeg", "webp"); static final List video_ext = Arrays.asList("mp4", "3gp", "mkv", "webm", "flv", "m4v"); diff --git a/app/src/main/res/layout/properties_view.xml b/app/src/main/res/layout/properties_view.xml index 3134749..18f1db0 100644 --- a/app/src/main/res/layout/properties_view.xml +++ b/app/src/main/res/layout/properties_view.xml @@ -5,7 +5,7 @@ + android:layout_height="wrap_content"> + + + + @@ -59,7 +78,7 @@ android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_alignParentLeft="true" + android:layout_alignParentStart="true" android:layout_below="@+id/time_label" android:paddingLeft="10dp" android:text="Unknown" @@ -69,7 +88,7 @@ android:id="@+id/size_label" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_alignParentLeft="true" + android:layout_alignParentStart="true" android:layout_below="@+id/time" android:padding="10dp" android:text="Size" /> @@ -78,7 +97,7 @@ android:id="@+id/size" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_alignParentLeft="true" + android:layout_alignParentStart="true" android:layout_below="@+id/size_label" android:paddingLeft="10dp" android:text="Unknown" @@ -88,7 +107,7 @@ android:id="@+id/details_label" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_alignParentLeft="true" + android:layout_alignParentStart="true" android:layout_below="@+id/size" android:padding="10dp" android:text="Details" /> @@ -97,7 +116,7 @@ android:id="@+id/details" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_alignParentLeft="true" + android:layout_alignParentStart="true" android:layout_below="@+id/details_label" android:paddingLeft="10dp" android:text="Unknown" /> diff --git a/app/src/test/java/com/praharsh/vudit/ExampleUnitTest.java b/app/src/test/java/com/praharsh/vudit/ExampleUnitTest.java index ecc7698..0ade545 100644 --- a/app/src/test/java/com/praharsh/vudit/ExampleUnitTest.java +++ b/app/src/test/java/com/praharsh/vudit/ExampleUnitTest.java @@ -1,9 +1,9 @@ -package com.example.root.vudit; - -import org.junit.Test; +package com.praharsh.vudit; import static org.junit.Assert.assertEquals; +import org.junit.Test; + /** * To work on unit tests, switch the Test Artifact in the Build Variants view. */