use of com.genonbeta.TrebleShot.object.TransactionObject in project TrebleShot by genonbeta.
the class TransactionListFragment method onActionMenuItemSelected.
@Override
public boolean onActionMenuItemSelected(Context context, PowerfulActionMode actionMode, MenuItem item) {
int id = item.getItemId();
final ArrayList<TransactionObject> selectionList = new ArrayList<>(getSelectionConnection().getSelectedItemList());
if (id == R.id.action_mode_transaction_delete) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.ques_removeQueue);
builder.setMessage(getResources().getQuantityString(R.plurals.text_removeQueueSummary, selectionList.size(), selectionList.size()));
builder.setNegativeButton(R.string.butn_close, null);
builder.setPositiveButton(R.string.butn_proceed, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
new Handler(Looper.myLooper()).post(new Runnable() {
@Override
public void run() {
for (TransactionObject transactionObject : selectionList) if (transactionObject instanceof TransactionListAdapter.TransactionFolder) {
mDatabase.delete(new SQLQuery.Select(AccessDatabase.TABLE_TRANSFER).setWhere(AccessDatabase.FIELD_TRANSFER_GROUPID + "=? AND (" + AccessDatabase.FIELD_TRANSFER_DIRECTORY + " LIKE ? OR " + AccessDatabase.FIELD_TRANSFER_DIRECTORY + " = ?)", String.valueOf(getAdapter().getGroupId()), transactionObject.directory + File.separator + "%", transactionObject.directory));
} else
mDatabase.remove(transactionObject);
}
});
}
});
builder.show();
} else
return super.onActionMenuItemSelected(context, actionMode, item);
return true;
}
use of com.genonbeta.TrebleShot.object.TransactionObject in project TrebleShot by genonbeta.
the class ShareActivity method doCommunicate.
protected void doCommunicate(final NetworkDevice device, final NetworkDevice.Connection connection) {
resetProgressItems();
getProgressDialog().setMessage(getString(R.string.mesg_communicating));
getProgressDialog().show();
runOnWorkerService(new WorkerService.RunningTask(TAG, WORKER_TASK_CONNECT_SERVER) {
@Override
public void onRun() {
CommunicationBridge.connect(mDatabase, true, new CommunicationBridge.Client.ConnectionHandler() {
@Override
public void onConnect(CommunicationBridge.Client client) {
try {
final JSONObject jsonRequest = new JSONObject();
final TransactionObject.Group groupInstance = new TransactionObject.Group(AppUtils.getUniqueNumber(), device.deviceId, connection.adapterName);
final ArrayList<TransactionObject> pendingRegistry = new ArrayList<>();
if (device instanceof NetworkDeviceListAdapter.HotspotNetwork && ((NetworkDeviceListAdapter.HotspotNetwork) device).qrConnection)
jsonRequest.put(Keyword.FLAG_TRANSFER_QR_CONNECTION, true);
if (mSharedText == null) {
jsonRequest.put(Keyword.REQUEST, Keyword.REQUEST_TRANSFER);
jsonRequest.put(Keyword.TRANSFER_GROUP_ID, groupInstance.groupId);
JSONArray filesArray = new JSONArray();
getProgressDialog().setMax(mFiles.size());
for (SelectableStream selectableStream : mFiles) {
if (getDefaultInterrupter().interrupted())
throw new InterruptedException("Interrupted by user");
if (!selectableStream.isSelectableSelected())
continue;
getProgressDialog().setSecondaryProgress(getProgressDialog().getSecondaryProgress() + 1);
int requestId = AppUtils.getUniqueNumber();
JSONObject thisJson = new JSONObject();
TransactionObject transactionObject = new TransactionObject(requestId, groupInstance.groupId, selectableStream.getSelectableFriendlyName(), selectableStream.getDocumentFile().getUri().toString(), selectableStream.getDocumentFile().getType(), selectableStream.getDocumentFile().length(), TransactionObject.Type.OUTGOING);
if (selectableStream.mDirectory != null)
transactionObject.directory = selectableStream.mDirectory;
pendingRegistry.add(transactionObject);
try {
thisJson.put(Keyword.INDEX_FILE_NAME, transactionObject.friendlyName);
thisJson.put(Keyword.INDEX_FILE_SIZE, transactionObject.fileSize);
thisJson.put(Keyword.TRANSFER_REQUEST_ID, requestId);
thisJson.put(Keyword.INDEX_FILE_MIME, transactionObject.fileMimeType);
if (selectableStream.mDirectory != null)
thisJson.put(Keyword.INDEX_DIRECTORY, selectableStream.mDirectory);
filesArray.put(thisJson);
} catch (Exception e) {
Log.e(TAG, "Sender error on fileUri: " + e.getClass().getName() + " : " + transactionObject.friendlyName);
}
}
jsonRequest.put(Keyword.FILES_INDEX, filesArray);
} else {
jsonRequest.put(Keyword.REQUEST, Keyword.REQUEST_CLIPBOARD);
jsonRequest.put(Keyword.TRANSFER_CLIPBOARD_TEXT, mSharedText);
}
final CoolSocket.ActiveConnection activeConnection = client.communicate(device, connection);
getDefaultInterrupter().addCloser(new Interrupter.Closer() {
@Override
public void onClose(boolean userAction) {
try {
activeConnection.getSocket().close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
activeConnection.reply(jsonRequest.toString());
CoolSocket.ActiveConnection.Response response = activeConnection.receive();
JSONObject clientResponse = new JSONObject(response.response);
if (clientResponse.has(Keyword.RESULT) && clientResponse.getBoolean(Keyword.RESULT)) {
if (pendingRegistry.size() > 0) {
mDatabase.insert(groupInstance);
getDefaultInterrupter().addCloser(new Interrupter.Closer() {
@Override
public void onClose(boolean userAction) {
mDatabase.remove(groupInstance);
}
});
for (TransactionObject transactionObject : pendingRegistry) {
if (getDefaultInterrupter().interrupted())
throw new InterruptedException("Interrupted by user");
getProgressDialog().setProgress(mProgressDialog.getProgress() + 1);
mDatabase.insert(transactionObject);
}
TransactionActivity.startInstance(getApplicationContext(), groupInstance.groupId);
}
} else {
if (clientResponse.has(Keyword.ERROR) && clientResponse.getString(Keyword.ERROR).equals(Keyword.ERROR_NOT_ALLOWED))
createSnackbar(R.string.mesg_notAllowed).setAction(R.string.ques_why, new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(ShareActivity.this);
builder.setMessage(getString(R.string.text_notAllowedHelp, device.nickname, AppUtils.getLocalDeviceName(ShareActivity.this)));
builder.setNegativeButton(R.string.butn_close, null);
builder.show();
}
}).show();
else
createSnackbar(R.string.mesg_somethingWentWrong).show();
}
} catch (Exception e) {
if (!(e instanceof InterruptedException)) {
e.printStackTrace();
createSnackbar(R.string.mesg_fileSendError, getString(R.string.text_connectionProblem)).show();
}
} finally {
getProgressDialog().dismiss();
}
}
});
}
});
}
use of com.genonbeta.TrebleShot.object.TransactionObject in project TrebleShot by genonbeta.
the class TransactionListAdapter method onBindViewHolder.
@Override
public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
final TransactionObject object = getItem(position);
final View parentView = holder.getView();
final View selector = parentView.findViewById(R.id.selector);
final View layoutImage = parentView.findViewById(R.id.layout_image);
ImageView image = parentView.findViewById(R.id.image);
TextView mainText = parentView.findViewById(R.id.text);
TextView statusText = parentView.findViewById(R.id.text2);
TextView sizeText = parentView.findViewById(R.id.text3);
if (getSelectionConnection() != null) {
selector.setSelected(object.isSelectableSelected());
layoutImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getSelectionConnection().setSelected(object);
selector.setSelected(object.isSelectableSelected());
}
});
}
if (object instanceof TransactionFolder) {
image.setImageResource(R.drawable.ic_folder_black_24dp);
mainText.setText(object.friendlyName);
statusText.setText(R.string.text_folder);
sizeText.setText(null);
} else {
boolean isIncoming = object.type.equals(TransactionObject.Type.INCOMING);
image.setImageResource(isIncoming ? R.drawable.ic_file_download_black_24dp : R.drawable.ic_file_upload_black_24dp);
mainText.setText(object.friendlyName);
statusText.setText(getContext().getString(TextUtils.getTransactionFlagString(object.flag)).toLowerCase());
sizeText.setText(FileUtils.sizeExpression(object.fileSize, false));
}
}
use of com.genonbeta.TrebleShot.object.TransactionObject in project TrebleShot by genonbeta.
the class TransactionListAdapter method onLoad.
@Override
public ArrayList<TransactionObject> onLoad() {
ArrayList<TransactionObject> mergedList = new ArrayList<>();
String currentPath = getPath();
for (TransactionObject transactionObject : mDatabase.castQuery(getSelect().setWhere(AccessDatabase.FIELD_TRANSFER_GROUPID + "=? AND " + AccessDatabase.FIELD_TRANSFER_DIRECTORY + " LIKE ?", String.valueOf(getGroupId()), currentPath != null ? currentPath + File.separator + "%" : "%").setGroupBy(AccessDatabase.FIELD_TRANSFER_DIRECTORY), TransactionObject.class)) {
String cleanedName = currentPath != null ? transactionObject.directory.substring(currentPath.length() + File.separator.length()) : transactionObject.directory;
int obtainSlash = cleanedName.indexOf(File.separator);
if (obtainSlash != -1)
cleanedName = cleanedName.substring(0, obtainSlash);
TransactionFolder transactionFolder = new TransactionFolder();
transactionFolder.friendlyName = cleanedName;
transactionFolder.directory = currentPath != null ? currentPath + File.separator + cleanedName : cleanedName;
boolean addThis = true;
for (TransactionObject testObject : mergedList) {
if (!(testObject instanceof TransactionFolder))
continue;
TransactionFolder testFolder = (TransactionFolder) testObject;
if (testFolder.friendlyName.equals(transactionFolder.friendlyName)) {
addThis = false;
break;
}
}
if (addThis)
mergedList.add(transactionFolder);
}
ArrayList<TransactionObject> mainItems = mDatabase.castQuery((currentPath == null ? getSelect().setWhere(AccessDatabase.FIELD_TRANSFER_GROUPID + "=? AND " + AccessDatabase.FIELD_TRANSFER_DIRECTORY + " IS NULL", String.valueOf(getGroupId())) : getSelect().setWhere(AccessDatabase.FIELD_TRANSFER_GROUPID + "=? AND " + AccessDatabase.FIELD_TRANSFER_DIRECTORY + "=?", String.valueOf(getGroupId()), currentPath)).setGroupBy(null), TransactionObject.class);
Collections.sort(mainItems, getDefaultComparator());
mergedList.addAll(mainItems);
return mergedList;
}
use of com.genonbeta.TrebleShot.object.TransactionObject 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