I am using below code to rename and delete video.
Need review for the following:
Code optimization
Performance
Overall code review
renameVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int index = filePath.lastIndexOf('.');
String selectedVideoTitleForRename;
if (index > 0) {
selectedVideoTitleForRename = filePath.substring(filePath.lastIndexOf("/") + 1, index);
} else {
selectedVideoTitleForRename = filePath.substring(filePath.lastIndexOf("/") + 1);
}
renameFile(PreviewActivity.this, selectedVideoTitleForRename, filePath, tvInstruction);
}
});
deleteVideo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteFile(PreviewActivity.this, filePath);
}
});
private void renameFile(final Context context, final String selectedVideoTitleForRename, final String selectedVideoRenamePath,
final TextView tvInstruction) {
AlertDialog.Builder alert = new AlertDialog.Builder(context);
LayoutInflater li = LayoutInflater.from(context);
View renameVideoView = li.inflate(R.layout.rename_video, null);
final EditText input = (EditText) renameVideoView.findViewById(R.id.rename_edit_text);
input.setText(selectedVideoTitleForRename);
alert.setView(renameVideoView);
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
File fileToRename = new File(selectedVideoRenamePath);
File fileNameNew = new File(selectedVideoRenamePath.replace(
selectedVideoTitleForRename, input.getText().toString()));
if (fileNameNew.exists()) {
Toast.makeText(context,
context.getResources().getString(R.string.same_title_exists), Toast.LENGTH_LONG).show();
} else {
boolean isRename = fileToRename.renameTo(fileNameNew);
if (isRename) {
String newFilePath = fileNameNew.toString();
tvInstruction.setText(context.getResources().getString(R.string.videoStoredPath, newFilePath));
try {
MediaScannerConnection.scanFile(context,
new String[]{fileToRename.toString(), newFilePath},
null,
null);
} catch (Exception e) {
FirebaseDatabase.getInstance().getReference("exception").push().setValue(e.toString());
Crashlytics.logException(e);
}
filePath = newFilePath;
Toast.makeText(context,
context.getResources().getString(R.string.rename_success), Toast.LENGTH_LONG).show();
}
}
}
});
alert.show();
}
private void deleteFile(final Context context, final String selectedVideoDelete) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Confirm Delete...");
alertDialog.setMessage("Are you sure you want to Delete:\n\n" + selectedVideoDelete);
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
File fileToDelete = new File(selectedVideoDelete);
boolean deletedSuccessfully = fileToDelete.delete();
if (deletedSuccessfully) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
MediaScannerConnection.scanFile(context,
new String[]{selectedVideoDelete}, null, null);
} else {
context.sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}
Toast.makeText(context,
context.getResources().getString(R.string.delete_success), Toast.LENGTH_LONG).show();
((Activity) context).finish();
}
}
});
alertDialog.show();
}