use of com.genonbeta.TrebleShot.io.DocumentFile in project TrebleShot by genonbeta.
the class TransactionGroupInfoDialog method show.
@SuppressLint("SetTextI18n")
@Override
public AlertDialog show() {
@SuppressLint("InflateParams") View rootView = LayoutInflater.from(getContext()).inflate(R.layout.layout_transaction_group_info, null);
TextView incomingSize = rootView.findViewById(R.id.transaction_group_info_incoming_size);
TextView outgoingSize = rootView.findViewById(R.id.transaction_group_info_outgoing_size);
TextView availableDisk = rootView.findViewById(R.id.transaction_group_info_available_disk_space);
TextView savePath = rootView.findViewById(R.id.transaction_group_info_save_path);
TextView usedConnection = rootView.findViewById(R.id.transaction_group_info_connection);
DocumentFile storageFile = FileUtils.getSavePath(getContext(), mGroup);
Resources resources = getContext().getResources();
incomingSize.setText(getContext().getString(R.string.mode_itemCountedDetailed, resources.getQuantityString(R.plurals.text_files, getIndex().incomingCount, getIndex().incomingCount), FileUtils.sizeExpression(getIndex().incoming, false)));
outgoingSize.setText(getContext().getString(R.string.mode_itemCountedDetailed, resources.getQuantityString(R.plurals.text_files, getIndex().outgoingCount, getIndex().outgoingCount), FileUtils.sizeExpression(getIndex().outgoing, false)));
availableDisk.setText(storageFile instanceof LocalDocumentFile ? FileUtils.sizeExpression(((LocalDocumentFile) storageFile).getFile().getFreeSpace(), false) : getContext().getString(R.string.text_unknown));
savePath.setText(storageFile.getUri().toString());
NetworkDevice.Connection connection = new NetworkDevice.Connection(mGroup.deviceId, mGroup.connectionAdapter);
try {
mDatabase.reconstruct(connection);
usedConnection.setText(TextUtils.getAdapterName(getContext(), connection));
} catch (Exception e) {
e.printStackTrace();
}
setTitle(R.string.text_transactionGroupDetails);
setView(rootView);
setPositiveButton(R.string.butn_close, null);
return super.show();
}
use of com.genonbeta.TrebleShot.io.DocumentFile in project TrebleShot by genonbeta.
the class FileExplorerFragment method onBackPressed.
@Override
public boolean onBackPressed() {
DocumentFile path = getFileListFragment().getAdapter().getPath();
if (getFileListFragment() == null || path == null)
return false;
DocumentFile parentFile = getReadableFolder(path);
if (parentFile == null || File.separator.equals(parentFile.getName()))
requestPath(null);
else
requestPath(parentFile);
return true;
}
use of com.genonbeta.TrebleShot.io.DocumentFile in project TrebleShot by genonbeta.
the class UpdateUtils method receiveUpdate.
public static DocumentFile receiveUpdate(Context context, NetworkDevice device, Interrupter interrupter, OnConnectionReadyListener readyListener) throws IOException, PackageManager.NameNotFoundException {
ServerSocket serverSocket = null;
Socket socket = null;
DocumentFile updateFile = null;
try {
serverSocket = new ServerSocket(AppConfig.SERVER_PORT_UPDATE_CHANNEL);
updateFile = FileUtils.getApplicationDirectory(context).createFile(null, device.versionName + "_" + System.currentTimeMillis() + ".apk");
final ServerSocket finalServer = serverSocket;
interrupter.addCloser(new Interrupter.Closer() {
@Override
public void onClose(boolean userAction) {
try {
if (!finalServer.isClosed())
finalServer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
if (readyListener != null)
readyListener.onConnectionReady(serverSocket);
serverSocket.setSoTimeout(AppConfig.DEFAULT_SOCKET_TIMEOUT);
socket = serverSocket.accept();
socket.setSoTimeout(AppConfig.DEFAULT_SOCKET_TIMEOUT);
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = context.getContentResolver().openOutputStream(updateFile.getUri());
byte[] buffer = new byte[AppConfig.BUFFER_LENGTH_DEFAULT];
int len = 0;
long lastRead = System.currentTimeMillis();
while (len != -1) {
if ((len = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, len);
outputStream.flush();
lastRead = System.currentTimeMillis();
}
if ((System.currentTimeMillis() - lastRead) > AppConfig.DEFAULT_SOCKET_TIMEOUT || interrupter.interrupted())
throw new Exception("Timed out or interrupted. Exiting!");
}
outputStream.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
if (updateFile != null && updateFile.isFile())
updateFile.delete();
return null;
} finally {
if (socket != null && !socket.isClosed())
socket.close();
if (serverSocket != null && !serverSocket.isClosed())
serverSocket.close();
}
return updateFile;
}
use of com.genonbeta.TrebleShot.io.DocumentFile in project TrebleShot by genonbeta.
the class FileListAdapter method onLoad.
@Override
public ArrayList<GenericFileHolder> onLoad() {
DocumentFile path = getPath();
ArrayList<GenericFileHolder> list = new ArrayList<>();
ArrayList<GenericFileHolder> folders = new ArrayList<>();
ArrayList<GenericFileHolder> files = new ArrayList<>();
if (path != null) {
DocumentFile[] fileIndex = path.listFiles();
if (fileIndex != null && fileIndex.length > 0) {
for (DocumentFile file : fileIndex) {
if ((mFileMatch != null && !file.getName().matches(mFileMatch)))
continue;
if (file.isDirectory() && mShowDirectories)
folders.add(new DirectoryHolder(file, mContext.getString(R.string.text_folder), R.drawable.ic_folder_white_24dp));
else if (file.isFile() && mShowFiles)
files.add(new FileHolder(getContext(), file));
}
Collections.sort(folders, getDefaultComparator());
Collections.sort(files, getDefaultComparator());
}
} else {
ArrayList<File> referencedDirectoryList = new ArrayList<>();
DocumentFile defaultFolder = FileUtils.getApplicationDirectory(getContext());
folders.add(new DirectoryHolder(defaultFolder, getContext().getString(R.string.text_receivedFiles), R.drawable.ic_whatshot_white_24dp));
folders.add(new DirectoryHolder(DocumentFile.fromFile(new File(".")), mContext.getString(R.string.text_fileRoot), mContext.getString(R.string.text_folder), R.drawable.ic_folder_white_24dp));
if (Build.VERSION.SDK_INT >= 21)
referencedDirectoryList.addAll(Arrays.asList(getContext().getExternalMediaDirs()));
else if (Build.VERSION.SDK_INT >= 19)
referencedDirectoryList.addAll(Arrays.asList(getContext().getExternalFilesDirs(null)));
else
referencedDirectoryList.add(Environment.getExternalStorageDirectory());
for (File mediaDir : referencedDirectoryList) {
if (mediaDir == null || !mediaDir.canWrite())
continue;
DirectoryHolder fileHolder = new DirectoryHolder(DocumentFile.fromFile(mediaDir), getContext().getString(R.string.text_storage), R.drawable.ic_save_white_24dp);
String[] splitPath = mediaDir.getAbsolutePath().split(File.separator);
if (splitPath.length >= 2 && splitPath[1].equals("storage")) {
if (splitPath.length >= 4 && splitPath[2].equals("emulated")) {
File file = new File(buildPath(splitPath, 4));
if (file.canWrite()) {
fileHolder.file = DocumentFile.fromFile(file);
fileHolder.friendlyName = "0".equals(splitPath[3]) ? getContext().getString(R.string.text_internalStorage) : getContext().getString(R.string.text_emulatedMediaDirectory, splitPath[3]);
}
} else if (splitPath.length >= 3) {
File file = new File(buildPath(splitPath, 3));
if (!file.canWrite())
continue;
fileHolder.friendlyName = splitPath[2];
fileHolder.file = DocumentFile.fromFile(file);
}
}
folders.add(fileHolder);
}
ArrayList<WritablePathObject> objectList = getDatabase().castQuery(new SQLQuery.Select(AccessDatabase.TABLE_WRITABLEPATH), WritablePathObject.class);
if (Build.VERSION.SDK_INT >= 21) {
for (WritablePathObject pathObject : objectList) try {
folders.add(new WritablePathHolder(DocumentFile.fromUri(getContext(), pathObject.path, true), pathObject, getContext().getString(R.string.text_storage)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
list.addAll(folders);
list.addAll(files);
return list;
}
use of com.genonbeta.TrebleShot.io.DocumentFile in project TrebleShot by genonbeta.
the class TransactionActivity method onActivityResult.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null) {
if (resultCode == Activity.RESULT_OK) {
switch(requestCode) {
case REQUEST_CHOOSE_FOLDER:
if (data.hasExtra(FilePickerActivity.EXTRA_CHOSEN_PATH)) {
final Uri selectedPath = data.getParcelableExtra(FilePickerActivity.EXTRA_CHOSEN_PATH);
if (selectedPath.toString().equals(mGroup.savePath)) {
createSnackbar(R.string.mesg_pathSameError).show();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(TransactionActivity.this);
builder.setTitle(R.string.ques_checkOldFiles);
builder.setMessage(R.string.text_checkOldFiles);
builder.setNeutralButton(R.string.butn_cancel, null);
builder.setNegativeButton(R.string.butn_reject, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
updateSavePath(selectedPath.toString());
}
});
builder.setPositiveButton(R.string.butn_accept, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
WorkerService.run(TransactionActivity.this, new WorkerService.NotifiableRunningTask(TAG, JOB_FILE_FIXICATION) {
@Override
public void onUpdateNotification(DynamicNotification dynamicNotification, UpdateType updateType) {
switch(updateType) {
case Started:
dynamicNotification.setSmallIcon(R.drawable.ic_compare_arrows_white_24dp).setContentText(getString(R.string.mesg_organizingFiles));
break;
case Done:
dynamicNotification.setContentText(getString(R.string.text_movedCacheFiles));
break;
}
}
@Override
public void onRun() {
ArrayList<TransactionObject> checkList = mDatabase.castQuery(new SQLQuery.Select(AccessDatabase.TABLE_TRANSFER).setWhere(AccessDatabase.FIELD_TRANSFER_GROUPID + "=? AND " + AccessDatabase.FIELD_TRANSFER_TYPE + "=? AND " + AccessDatabase.FIELD_TRANSFER_FLAG + " != ?", String.valueOf(mGroup.groupId), TransactionObject.Type.INCOMING.toString(), TransactionObject.Flag.PENDING.toString()), TransactionObject.class);
TransactionObject.Group pseudoGroup = new TransactionObject.Group(mGroup.groupId);
try {
// Illustrate new change to build the structure accordingly
mDatabase.reconstruct(pseudoGroup);
pseudoGroup.savePath = selectedPath.toString();
for (TransactionObject transactionObject : checkList) {
if (getInterrupter().interrupted())
break;
try {
DocumentFile file = FileUtils.getIncomingPseudoFile(getApplicationContext(), transactionObject, mGroup, false);
DocumentFile pseudoFile = FileUtils.getIncomingPseudoFile(getApplicationContext(), transactionObject, pseudoGroup, true);
if (file.canRead())
FileUtils.move(TransactionActivity.this, file, pseudoFile, getInterrupter());
file.delete();
} catch (IOException e) {
e.printStackTrace();
}
}
updateSavePath(selectedPath.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
builder.show();
}
}
break;
}
}
}
}
Aggregations