Skip to content
This repository has been archived by the owner on Sep 8, 2019. It is now read-only.

Commit

Permalink
- added ability to trigger the MediaScanner from the file explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
kollerlukas committed Sep 1, 2017
1 parent 8b63945 commit 47b8a56
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package us.koller.cameraroll.data.fileOperations;

import android.annotation.SuppressLint;
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
Expand All @@ -23,6 +24,7 @@
import android.widget.Toast;

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -359,7 +361,7 @@ public static boolean isOnRemovableStorage(String path) {
return false;
}

static ArrayList<String> getAllChildPaths(ArrayList<String> paths, String path) {
public static ArrayList<String> getAllChildPaths(ArrayList<String> paths, String path) {
File file = new File(path);
if (file.exists()) {
if (file.isDirectory()) {
Expand Down Expand Up @@ -400,5 +402,34 @@ public void run() {
}
});
}

public static void scanPathsWithToast(final Context context, final String[] paths) {
@SuppressLint("ShowToast") final WeakReference<Toast> toastWeakReference = new WeakReference<>(
Toast.makeText(context, R.string.scanning, Toast.LENGTH_SHORT));
MediaScannerConnection.scanFile(context.getApplicationContext(),
paths,
null,
new MediaScannerConnection.OnScanCompletedListener() {
int pathsScanned;

@Override
public void onScanCompleted(String path, Uri uri) {
Log.d("FileOperation", "onScanCompleted() called with: path = [" + path + "]");
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Toast toast = toastWeakReference.get();
if (toast != null) {
toastWeakReference.get().show();
}
}
});
pathsScanned++;
if (pathsScanned == paths.length) {
toastWeakReference.clear();
}
}
});
}
}
}
100 changes: 62 additions & 38 deletions app/src/main/java/us/koller/cameraroll/ui/FileExplorerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import us.koller.cameraroll.R;
import us.koller.cameraroll.themes.Theme;
import us.koller.cameraroll.adapter.fileExplorer.RecyclerViewAdapter;
Expand Down Expand Up @@ -430,27 +432,32 @@ public boolean onCreateOptionsMenu(Menu menu) {
public void manageMenuItems() {
if (menu != null) {
for (int i = 0; i < menu.size(); i++) {
menu.getItem(i).setVisible(false);

int id = menu.getItem(i).getItemId();
if (id == R.id.exclude) {
MenuItem item = menu.getItem(i);
if (currentDir != null) {
item.setVisible(!currentDir.getPath().equals(STORAGE_ROOTS));
if (Provider.isPathPermanentlyExcluded(currentDir.getPath())) {
item.setChecked(true);
item.setEnabled(false);
MenuItem item = menu.getItem(i);
switch (item.getItemId()) {
case R.id.exclude:
if (currentDir != null) {
item.setVisible(!currentDir.getPath().equals(STORAGE_ROOTS));
if (Provider.isPathPermanentlyExcluded(currentDir.getPath())) {
item.setChecked(true);
item.setEnabled(false);
} else {
item.setChecked(currentDir.excluded);
item.setEnabled(!currentDir.getPath().equals(STORAGE_ROOTS)
&& !Provider.isDirExcludedBecauseParentDirIsExcluded(
currentDir.getPath(), Provider.getExcludedPaths()));
}
} else {
item.setChecked(currentDir.excluded);
item.setEnabled(!currentDir.getPath().equals(STORAGE_ROOTS)
&& !Provider.isDirExcludedBecauseParentDirIsExcluded(
currentDir.getPath(), Provider.getExcludedPaths()));
item.setVisible(true);
item.setChecked(false);
item.setEnabled(false);
}
} else {
item.setVisible(true);
item.setChecked(false);
item.setEnabled(false);
}
break;
case R.id.scan:
item.setVisible(!currentDir.getPath().equals(STORAGE_ROOTS));
break;
default:
item.setVisible(false);
break;
}
}
}
Expand All @@ -477,6 +484,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
FilesProvider.removeExcludedPath(this, currentDir.getPath());
}
break;
case R.id.scan:
ArrayList<String> paths = FileOperation.Util
.getAllChildPaths(new ArrayList<String>(), currentDir.getPath());
String[] pathsArray = new String[paths.size()];
paths.toArray(pathsArray);
FileOperation.Util.scanPathsWithToast(this, pathsArray);
break;
case R.id.paste:
if (!currentDir.getPath().equals(STORAGE_ROOTS)) {
recyclerViewAdapter.cancelMode();
Expand All @@ -487,9 +501,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
fileOpIntent = null;
}
} else {
Toast.makeText(this, "You can't "
+ fileOpIntent.getAction()
+ " files here!", Toast.LENGTH_SHORT).show();
Toast.makeText(this, R.string.paste_error, Toast.LENGTH_SHORT).show();
}
break;
case R.id.copy:
Expand Down Expand Up @@ -713,11 +725,16 @@ public void run() {

//make menu items visible
for (int i = 0; i < menu.size(); i++) {
int id = menu.getItem(i).getItemId();
if (id == R.id.paste || id == R.id.exclude) {
menu.getItem(i).setVisible(false);
} else {
menu.getItem(i).setVisible(true);
MenuItem item = menu.getItem(i);
switch (item.getItemId()) {
case R.id.copy:
case R.id.move:
case R.id.delete:
item.setVisible(true);
break;
default:
item.setVisible(false);
break;
}
}
}
Expand Down Expand Up @@ -822,11 +839,14 @@ public void setTitle(Toolbar toolbar) {
public void run() {
//hide menu items
for (int i = 0; i < menu.size(); i++) {
int id = menu.getItem(i).getItemId();
if (id == R.id.paste) {
menu.getItem(i).setVisible(true);
} else {
menu.getItem(i).setVisible(false);
MenuItem item = menu.getItem(i);
switch (item.getItemId()) {
case R.id.paste:
item.setVisible(true);
break;
default:
item.setVisible(false);
break;
}
}
}
Expand Down Expand Up @@ -900,7 +920,7 @@ public void setTitle(Toolbar toolbar) {
});

//fade overflow menu icon
ColorFade.fadeDrawableColor(toolbar.getOverflowIcon(), accentTextColor, textColorPrimary);
ColorFade.fadeDrawableColor(toolbar.getOverflowIcon(), accentTextColor, textColorSecondary);

Drawable navIcon = toolbar.getNavigationIcon();
if (navIcon instanceof Animatable) {
Expand All @@ -926,11 +946,15 @@ public void run() {

//hide menu items
for (int i = 0; i < menu.size(); i++) {
int id = menu.getItem(i).getItemId();
if (id == R.id.exclude) {
menu.getItem(i).setVisible(true);
} else {
menu.getItem(i).setVisible(false);
MenuItem item = menu.getItem(i);
switch (item.getItemId()) {
case R.id.exclude:
case R.id.scan:
item.setVisible(true);
break;
default:
item.setVisible(false);
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
@Override
public void recreate() {
recreated = true;

super.recreate();
}

Expand All @@ -163,7 +162,6 @@ public void onBackPressed() {
if (recreated) {
setResult(RESULT_OK);
}

super.onBackPressed();
}

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/menu/file_explorer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
android:title="@string/exclude"
app:showAsAction="never" />

<item
android:id="@+id/scan"
android:title="@string/scan"
app:showAsAction="never" />

<item
android:id="@+id/paste"
android:icon="@drawable/ic_content_paste_white_24dp"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-de/strings_translatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@
<!--FileExplorerActivity-->
<string name="no_files">KEINE DATEIEN</string>
<string name="storage">Speicher</string>
<string name="scan">Scannen</string>
<string name="scanning">Scanning…</string>
<string name="copy_file">%1$d Datei kopieren</string>
<string name="copy_files">%1$d Dateien kopieren</string>
<string name="move_file">%1$d Datei bewegen</string>
<string name="move_files">%1$d Dateien bewegen</string>
<string name="delete_file">%1$d Datei löschen</string>
<string name="paste_error">Hierhin können keine Dateien eingefügt werden!</string>
<!--SettingsActivity-->
<string name="appearance">Erscheinungsbild</string>
<string name="theme">Theme</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings_translatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@
<!--FileExplorerActivity-->
<string name="no_files">NO FILES</string>
<string name="storage">Storage</string>
<string name="scan">Scan</string>
<string name="scanning">Scanning…</string>
<string name="copy_file">Copy %1$d file</string>
<string name="copy_files">Copy %1$d files</string>
<string name="move_file">Move %1$d file</string>
<string name="move_files">Move %1$d files</string>
<string name="delete_file">Delete %1$d file</string>
<string name="paste_error">You can\'t paste files here!</string>
<!--SettingsActivity-->
<string name="appearance">Appearance</string>
<string name="theme">Theme</string>
Expand Down

0 comments on commit 47b8a56

Please sign in to comment.