use of com.amaze.filemanager.filesystem.HybridFileParcelable in project AmazeFileManager by TeamAmaze.
the class GeneralDialogCreation method deleteFilesDialog.
@SuppressWarnings("ConstantConditions")
public static void deleteFilesDialog(final Context c, final ArrayList<LayoutElementParcelable> layoutElements, final MainActivity mainActivity, final List<LayoutElementParcelable> positions, AppTheme appTheme) {
final ArrayList<HybridFileParcelable> itemsToDelete = new ArrayList<>();
int accentColor = mainActivity.getColorPreference().getColor(ColorUsage.ACCENT);
// Build dialog with custom view layout and accent color.
MaterialDialog dialog = new MaterialDialog.Builder(c).title(c.getString(R.string.dialog_delete_title)).customView(R.layout.dialog_delete, true).theme(appTheme.getMaterialDialogTheme()).negativeText(c.getString(R.string.cancel).toUpperCase()).positiveText(c.getString(R.string.delete).toUpperCase()).positiveColor(accentColor).negativeColor(accentColor).onPositive((dialog1, which) -> {
Toast.makeText(c, c.getString(R.string.deleting), Toast.LENGTH_SHORT).show();
mainActivity.mainActivityHelper.deleteFiles(itemsToDelete);
}).build();
// Get views from custom layout to set text values.
final TextView categoryDirectories = (TextView) dialog.getCustomView().findViewById(R.id.category_directories);
final TextView categoryFiles = (TextView) dialog.getCustomView().findViewById(R.id.category_files);
final TextView listDirectories = (TextView) dialog.getCustomView().findViewById(R.id.list_directories);
final TextView listFiles = (TextView) dialog.getCustomView().findViewById(R.id.list_files);
final TextView total = (TextView) dialog.getCustomView().findViewById(R.id.total);
// Parse items to delete.
new AsyncTask<Void, Object, Void>() {
long sizeTotal = 0;
StringBuilder files = new StringBuilder();
StringBuilder directories = new StringBuilder();
int counterDirectories = 0;
int counterFiles = 0;
@Override
protected void onPreExecute() {
super.onPreExecute();
listFiles.setText(c.getString(R.string.loading));
listDirectories.setText(c.getString(R.string.loading));
total.setText(c.getString(R.string.loading));
}
@Override
protected Void doInBackground(Void... params) {
for (int i = 0; i < positions.size(); i++) {
final LayoutElementParcelable layoutElement = positions.get(i);
itemsToDelete.add(layoutElement.generateBaseFile());
// Build list of directories to delete.
if (layoutElement.isDirectory) {
// Don't add newline between category and list.
if (counterDirectories != 0) {
directories.append("\n");
}
long sizeDirectory = layoutElement.generateBaseFile().folderSize(c);
directories.append(++counterDirectories).append(". ").append(layoutElement.title).append(" (").append(Formatter.formatFileSize(c, sizeDirectory)).append(")");
sizeTotal += sizeDirectory;
// Build list of files to delete.
} else {
// Don't add newline between category and list.
if (counterFiles != 0) {
files.append("\n");
}
files.append(++counterFiles).append(". ").append(layoutElement.title).append(" (").append(layoutElement.size).append(")");
sizeTotal += layoutElement.longSize;
}
publishProgress(sizeTotal, counterFiles, counterDirectories, files, directories);
}
return null;
}
@Override
protected void onProgressUpdate(Object... result) {
super.onProgressUpdate(result);
int tempCounterFiles = (int) result[1];
int tempCounterDirectories = (int) result[2];
long tempSizeTotal = (long) result[0];
StringBuilder tempFilesStringBuilder = (StringBuilder) result[3];
StringBuilder tempDirectoriesStringBuilder = (StringBuilder) result[4];
updateViews(tempSizeTotal, tempFilesStringBuilder, tempDirectoriesStringBuilder, tempCounterFiles, tempCounterDirectories);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
updateViews(sizeTotal, files, directories, counterFiles, counterDirectories);
}
private void updateViews(long tempSizeTotal, StringBuilder filesStringBuilder, StringBuilder directoriesStringBuilder, int... values) {
int tempCounterFiles = values[0];
int tempCounterDirectories = values[1];
// Hide category and list for directories when zero.
if (tempCounterDirectories == 0) {
if (tempCounterDirectories == 0) {
categoryDirectories.setVisibility(View.GONE);
listDirectories.setVisibility(View.GONE);
}
// Hide category and list for files when zero.
}
if (tempCounterFiles == 0) {
categoryFiles.setVisibility(View.GONE);
listFiles.setVisibility(View.GONE);
}
if (tempCounterDirectories != 0 || tempCounterFiles != 0) {
listDirectories.setText(directoriesStringBuilder);
if (listDirectories.getVisibility() != View.VISIBLE && tempCounterDirectories != 0)
listDirectories.setVisibility(View.VISIBLE);
listFiles.setText(filesStringBuilder);
if (listFiles.getVisibility() != View.VISIBLE && tempCounterFiles != 0)
listFiles.setVisibility(View.VISIBLE);
if (categoryDirectories.getVisibility() != View.VISIBLE && tempCounterDirectories != 0)
categoryDirectories.setVisibility(View.VISIBLE);
if (categoryFiles.getVisibility() != View.VISIBLE && tempCounterFiles != 0)
categoryFiles.setVisibility(View.VISIBLE);
}
// Show total size with at least one directory or file and size is not zero.
if (tempCounterFiles + tempCounterDirectories > 1 && tempSizeTotal > 0) {
StringBuilder builderTotal = new StringBuilder().append(c.getString(R.string.total)).append(" ").append(Formatter.formatFileSize(c, tempSizeTotal));
total.setText(builderTotal);
if (total.getVisibility() != View.VISIBLE)
total.setVisibility(View.VISIBLE);
} else {
total.setVisibility(View.GONE);
}
}
}.execute();
// Set category text color for Jelly Bean (API 16) and later.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
categoryDirectories.setTextColor(accentColor);
categoryFiles.setTextColor(accentColor);
}
// Show dialog on screen.
dialog.show();
}
use of com.amaze.filemanager.filesystem.HybridFileParcelable in project AmazeFileManager by TeamAmaze.
the class OTGUtil method getDocumentFiles.
/**
* Get the files at a specific path in OTG
*
* @param path the path to the directory tree, starts with prefix 'otg:/'
* Independent of URI (or mount point) for the OTG
* @param context context for loading
* @return an array of list of files at the path
*/
public static void getDocumentFiles(String path, Context context, OnFileFound fileFound) {
SharedPreferences manager = PreferenceManager.getDefaultSharedPreferences(context);
String rootUriString = manager.getString(MainActivity.KEY_PREF_OTG, null);
DocumentFile rootUri = DocumentFile.fromTreeUri(context, Uri.parse(rootUriString));
String[] parts = path.split("/");
for (String part : parts) {
// first omit 'otg:/' before iterating through DocumentFile
if (path.equals(OTGUtil.PREFIX_OTG + "/"))
break;
if (part.equals("otg:") || part.equals(""))
continue;
// iterating through the required path to find the end point
rootUri = rootUri.findFile(part);
}
// we have the end point DocumentFile, list the files inside it and return
for (DocumentFile file : rootUri.listFiles()) {
if (file.exists()) {
long size = 0;
if (!file.isDirectory())
size = file.length();
Log.d(context.getClass().getSimpleName(), "Found file: " + file.getName());
HybridFileParcelable baseFile = new HybridFileParcelable(path + "/" + file.getName(), RootHelper.parseDocumentFilePermission(file), file.lastModified(), size, file.isDirectory());
baseFile.setName(file.getName());
baseFile.setMode(OpenMode.OTG);
fileFound.onFileFound(baseFile);
}
}
}
use of com.amaze.filemanager.filesystem.HybridFileParcelable in project AmazeFileManager by TeamAmaze.
the class CloudUtil method getCloudFiles.
public static void getCloudFiles(String path, CloudStorage cloudStorage, OpenMode openMode, OnFileFound fileFoundCallback) throws CloudPluginException {
String strippedPath = stripPath(openMode, path);
try {
for (CloudMetaData cloudMetaData : cloudStorage.getChildren(strippedPath)) {
HybridFileParcelable baseFile = new HybridFileParcelable(path + "/" + cloudMetaData.getName(), "", (cloudMetaData.getModifiedAt() == null) ? 0l : cloudMetaData.getModifiedAt(), cloudMetaData.getSize(), cloudMetaData.getFolder());
baseFile.setName(cloudMetaData.getName());
baseFile.setMode(openMode);
fileFoundCallback.onFileFound(baseFile);
}
} catch (Exception e) {
e.printStackTrace();
throw new CloudPluginException();
}
}
use of com.amaze.filemanager.filesystem.HybridFileParcelable in project AmazeFileManager by TeamAmaze.
the class MainActivity method onOptionsItemSelected.
// called when the user exits the action mode
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// ActionBarDrawerToggle will take care of this.
if (drawer.onOptionsItemSelected(item))
return true;
// Handle action buttons
MainFragment ma = getCurrentMainFragment();
switch(item.getItemId()) {
case R.id.home:
if (ma != null)
ma.home();
break;
case R.id.history:
if (ma != null)
GeneralDialogCreation.showHistoryDialog(dataUtils, getPrefs(), ma, getAppTheme());
break;
case R.id.sethome:
if (ma == null)
return super.onOptionsItemSelected(item);
final MainFragment main = ma;
if (main.openMode != OpenMode.FILE && main.openMode != OpenMode.ROOT) {
Toast.makeText(mainActivity, R.string.not_allowed, Toast.LENGTH_SHORT).show();
break;
}
final MaterialDialog dialog = GeneralDialogCreation.showBasicDialog(mainActivity, new String[] { getResources().getString(R.string.questionset), getResources().getString(R.string.setashome), getResources().getString(R.string.yes), getResources().getString(R.string.no), null });
dialog.getActionButton(DialogAction.POSITIVE).setOnClickListener((v) -> {
main.home = main.getCurrentPath();
updatePaths(main.no);
dialog.dismiss();
});
dialog.show();
break;
case R.id.exit:
finish();
break;
case R.id.sort:
Fragment fragment = getFragmentAtFrame();
if (fragment instanceof AppsListFragment) {
GeneralDialogCreation.showSortDialog((AppsListFragment) fragment, getAppTheme());
}
break;
case R.id.sortby:
if (ma != null)
GeneralDialogCreation.showSortDialog(ma, getAppTheme(), getPrefs());
break;
case R.id.dsort:
if (ma == null)
return super.onOptionsItemSelected(item);
String[] sort = getResources().getStringArray(R.array.directorysortmode);
MaterialDialog.Builder builder = new MaterialDialog.Builder(mainActivity);
builder.theme(getAppTheme().getMaterialDialogTheme());
builder.title(R.string.directorysort);
int current = Integer.parseInt(getPrefs().getString(PreferencesConstants.PREFERENCE_DIRECTORY_SORT_MODE, "0"));
final MainFragment mainFrag = ma;
builder.items(sort).itemsCallbackSingleChoice(current, (dialog1, view, which, text) -> {
getPrefs().edit().putString(PreferencesConstants.PREFERENCE_DIRECTORY_SORT_MODE, "" + which).commit();
mainFrag.getSortModes();
mainFrag.updateList();
dialog1.dismiss();
return true;
});
builder.build().show();
break;
case R.id.hiddenitems:
GeneralDialogCreation.showHiddenDialog(dataUtils, getPrefs(), ma, getAppTheme());
break;
case R.id.view:
final MainFragment mainFragment = ma;
int pathLayout = dataUtils.getListOrGridForPath(ma.getCurrentPath(), DataUtils.LIST);
if (ma.IS_LIST) {
if (pathLayout == DataUtils.LIST) {
AppConfig.runInBackground(() -> {
utilsHandler.removeListViewPath(mainFragment.getCurrentPath());
});
}
AppConfig.runInBackground(() -> {
utilsHandler.addGridView(mainFragment.getCurrentPath());
});
dataUtils.setPathAsGridOrList(ma.getCurrentPath(), DataUtils.GRID);
} else {
if (pathLayout == DataUtils.GRID) {
AppConfig.runInBackground(() -> {
utilsHandler.removeGridViewPath(mainFragment.getCurrentPath());
});
}
AppConfig.runInBackground(() -> {
utilsHandler.addListView(mainFragment.getCurrentPath());
});
dataUtils.setPathAsGridOrList(ma.getCurrentPath(), DataUtils.LIST);
}
ma.switchView();
break;
case R.id.paste:
String path = ma.getCurrentPath();
ArrayList<HybridFileParcelable> arrayList = new ArrayList<>(Arrays.asList(pasteHelper.paths));
boolean move = pasteHelper.operation == PasteHelper.OPERATION_CUT;
new PrepareCopyTask(ma, path, move, mainActivity, isRootExplorer()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, arrayList);
pasteHelper = null;
invalidatePasteButton(item);
break;
case R.id.extract:
Fragment fragment1 = getFragmentAtFrame();
if (fragment1 instanceof CompressedExplorerFragment) {
mainActivityHelper.extractFile(((CompressedExplorerFragment) fragment1).compressedFile);
}
break;
case R.id.search:
getAppbar().getSearchView().revealSearchView();
break;
}
return super.onOptionsItemSelected(item);
}
use of com.amaze.filemanager.filesystem.HybridFileParcelable in project AmazeFileManager by TeamAmaze.
the class WriteFileAbstraction method doInBackground.
@Override
protected Integer doInBackground(Void... voids) {
try {
OutputStream outputStream;
switch(fileAbstraction.scheme) {
case EditableFileAbstraction.SCHEME_CONTENT:
if (fileAbstraction.uri == null)
throw new NullPointerException("Something went really wrong!");
try {
outputStream = contentResolver.openOutputStream(fileAbstraction.uri);
} catch (RuntimeException e) {
throw new StreamNotFoundException(e);
}
break;
case EditableFileAbstraction.SCHEME_FILE:
final HybridFileParcelable hybridFileParcelable = fileAbstraction.hybridFileParcelable;
if (hybridFileParcelable == null)
throw new NullPointerException("Something went really wrong!");
Context context = this.context.get();
if (context == null) {
cancel(true);
return null;
}
outputStream = FileUtil.getOutputStream(hybridFileParcelable.getFile(), context);
if (isRootExplorer && outputStream == null) {
// try loading stream associated using root
try {
if (cachedFile != null && cachedFile.exists()) {
outputStream = new FileOutputStream(cachedFile);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
outputStream = null;
}
}
break;
default:
throw new IllegalArgumentException("The scheme for '" + fileAbstraction.scheme + "' cannot be processed!");
}
if (outputStream == null)
throw new StreamNotFoundException();
outputStream.write(dataToSave.getBytes());
outputStream.close();
if (cachedFile != null && cachedFile.exists()) {
// cat cache content to original file and delete cache file
RootUtils.cat(cachedFile.getPath(), fileAbstraction.hybridFileParcelable.getPath());
cachedFile.delete();
}
} catch (IOException e) {
e.printStackTrace();
return EXCEPTION_IO;
} catch (StreamNotFoundException e) {
e.printStackTrace();
return EXCEPTION_STREAM_NOT_FOUND;
} catch (ShellNotRunningException e) {
e.printStackTrace();
return EXCEPTION_SHELL_NOT_RUNNING;
}
return NORMAL;
}
Aggregations