Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jahhow committed Apr 11, 2020
1 parent 89e7195 commit 89998a3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import us.koller.cameraroll.util.StorageUtil;

public abstract class FileOperation extends IntentService implements Parcelable {
protected static final String TAG = Rename.class.getSimpleName();

private static final int NOTIFICATION_ID = 6;

Expand All @@ -69,7 +70,7 @@ public abstract class FileOperation extends IntentService implements Parcelable
public static final String REMOVABLE_STORAGE_TREE_URI = "REMOVABLE_STORAGE_TREE_URI";

private NotificationCompat.Builder notifBuilder;

private boolean sentFailedBroadcast;
private ArrayList<String> pathsToScan;

public FileOperation() {
Expand All @@ -80,6 +81,7 @@ public FileOperation() {

@Override
protected void onHandleIntent(Intent workIntent) {
sentFailedBroadcast = false;
notifBuilder = createNotificationBuilder();
notifBuilder.setProgress(1, 0, false);
Notification notification = notifBuilder.build();
Expand All @@ -102,7 +104,6 @@ protected void onHandleIntent(Intent workIntent) {
public void onAllPathsScanned() {
sendDoneBroadcast();
stopForeground(true);

}
});
} else {
Expand Down Expand Up @@ -176,10 +177,13 @@ public void sendDoneBroadcast(boolean showToastDone) {
ContentObserver.selfChange = false;
if (showToastDone) showToast(getString(R.string.done));
Intent intent = getDoneIntent();
if (sentFailedBroadcast)
intent.putExtra(FAILED, true);
sendLocalBroadcast(intent);
}

public void sendFailedBroadcast(Intent workIntent, String path) {
sentFailedBroadcast = true;
Intent intent = new Intent(FAILED);
intent.putExtra(FILES, path);
intent.putExtra(WORK_INTENT, workIntent);
Expand Down Expand Up @@ -483,7 +487,6 @@ public void run() {
manager.notify(NOTIFICATION_ID, notifBuilder.build());
}
}

}

if (manager != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.documentfile.provider.DocumentFile;

Expand All @@ -23,6 +24,7 @@
import us.koller.cameraroll.util.StorageUtil;

public class Rename extends FileOperation {
protected static final String TAG = Rename.class.getSimpleName();

public static final String NEW_FILE_PATH = "NEW_FILE_PATH";

Expand Down Expand Up @@ -59,13 +61,12 @@ public void execute(Intent workIntent) {
if (!result) {
sendFailedBroadcast(workIntent, file.getPath());
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), getString(R.string.successfully_renamed_file),
Toast.LENGTH_SHORT).show();
}
});
runOnUiThread(() ->
Toast.makeText(
getApplicationContext(), getString(R.string.successfully_renamed_file),
Toast.LENGTH_SHORT
).show()
);
}
}
}
Expand Down Expand Up @@ -139,7 +140,7 @@ public static class Util {

public static AlertDialog getRenameDialog(final BaseActivity activity,
final File_POJO file,
final BroadcastReceiver broadcastReceiver) {
@Nullable final BroadcastReceiver broadcastReceiver) {

@SuppressLint("InflateParams")
View dialogLayout = activity.getLayoutInflater()
Expand Down Expand Up @@ -167,7 +168,7 @@ public void onClick(DialogInterface dialogInterface, int i) {

final File_POJO[] files = new File_POJO[]{file};
Intent intent = FileOperation.getDefaultIntent(activity, FileOperation.RENAME, files)
.putExtra(FileOperation.NEW_FILE_NAME, newFileName);
.putExtra(FileOperation.NEW_FILE_NAME, newFileName);
activity.startService(intent);
}
})
Expand All @@ -178,4 +179,9 @@ public void onClick(DialogInterface dialogInterface, int i) {
return dialog;
}
}

@Override
public void sendDoneBroadcast() {
sendDoneBroadcast(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.pm.PackageManager;
import android.os.Build;

import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

Expand Down Expand Up @@ -38,7 +39,7 @@ public abstract static class OnMediaLoadedCallback implements Provider.Callback
}

public interface OnAlbumLoadedCallback {
void onAlbumLoaded(Album album);
void onAlbumLoaded(@Nullable Album album);
}

public MediaProvider(Context context) {
Expand Down
77 changes: 44 additions & 33 deletions app/src/main/java/us/koller/cameraroll/ui/AlbumActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowInsets;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
Expand Down Expand Up @@ -556,37 +557,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
Rename.Util.getRenameDialog(this, file, new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final Activity activity = AlbumActivity.this;

final String newFilePath = intent.getStringExtra(Rename.NEW_FILE_PATH);
getIntent().putExtra(ALBUM_PATH, newFilePath);

boolean hiddenFolders = Settings.getInstance(activity).getHiddenFolders();
new MediaProvider(activity).loadAlbums(activity, hiddenFolders,
new MediaProvider.OnMediaLoadedCallback() {
@Override
public void onMediaLoaded(ArrayList<Album> albums) {
//reload activity
MediaProvider.getAlbum(activity, newFilePath,
new MediaProvider.OnAlbumLoadedCallback() {
@Override
public void onAlbumLoaded(Album album) {
AlbumActivity.this.album = album;
AlbumActivity.this.onAlbumLoaded(null);
}
});
}

@Override
public void timeout() {
supportFinishAfterTransition();
}

@Override
public void needPermission() {
supportFinishAfterTransition();
}
});
}
}).show();
break;
Expand Down Expand Up @@ -1126,7 +1097,7 @@ public void onReceive(Context context, Intent intent) {
}
break;
}
case FileOperation.COPY:
case FileOperation.COPY: {
//todo animate
String albumPath = getIntent().getStringExtra(ALBUM_PATH);
MediaProvider.getAlbum(AlbumActivity.this, albumPath,
Expand All @@ -1153,14 +1124,53 @@ public void onAlbumLoaded(Album newAlbum) {
}
}, true);
break;
}
case FileOperation.RENAME: {
if (intent.getBooleanExtra(FileOperation.FAILED, false)) {
Toast.makeText(context, R.string.error, Toast.LENGTH_SHORT).show();
break;
}
final Activity activity = AlbumActivity.this;

final String newFilePath = intent.getStringExtra(Rename.NEW_FILE_PATH);
getIntent().putExtra(ALBUM_PATH, newFilePath);

boolean hiddenFolders = Settings.getInstance(activity).getHiddenFolders();
new MediaProvider(activity).loadAlbums(activity, hiddenFolders,
new MediaProvider.OnMediaLoadedCallback() {
@Override
public void onMediaLoaded(ArrayList<Album> albums) {
//reload activity
MediaProvider.getAlbum(activity, newFilePath,
new MediaProvider.OnAlbumLoadedCallback() {
@Override
public void onAlbumLoaded(@Nullable Album album) {
AlbumActivity.this.album = album;
AlbumActivity.this.onAlbumLoaded(null);
}
});
}

@Override
public void timeout() {
supportFinishAfterTransition();
}

@Override
public void needPermission() {
supportFinishAfterTransition();
}
});
}
}
break;
case ALBUM_ITEM_REMOVED:
case ALBUM_ITEM_REMOVED: {
String path = intent.getStringExtra(ItemActivity.ALBUM_ITEM_PATH);
removeAlbumItem(path);
break;
}
case ALBUM_ITEM_RENAMED:
case DATA_CHANGED:
case DATA_CHANGED: {
String albumPath = getIntent().getStringExtra(ALBUM_PATH);
MediaProvider.getAlbum(AlbumActivity.this, albumPath,
new MediaProvider.OnAlbumLoadedCallback() {
Expand All @@ -1171,6 +1181,7 @@ public void onAlbumLoaded(Album album) {
}
});
break;
}
default:
break;
}
Expand Down

0 comments on commit 89998a3

Please sign in to comment.