use of com.genonbeta.TrebleShot.object.TransferInstance in project TrebleShot by genonbeta.
the class CommunicationService method onStartCommand.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (intent != null)
Log.d(TAG, "onStart() : action = " + intent.getAction());
if (intent != null && AppUtils.checkRunningConditions(this)) {
if (ACTION_FILE_TRANSFER.equals(intent.getAction())) {
final int groupId = intent.getIntExtra(EXTRA_GROUP_ID, -1);
final int notificationId = intent.getIntExtra(NotificationUtils.EXTRA_NOTIFICATION_ID, -1);
final boolean isAccepted = intent.getBooleanExtra(EXTRA_IS_ACCEPTED, false);
getNotificationHelper().getUtils().cancel(notificationId);
try {
final TransferInstance transferInstance = new TransferInstance(getDatabase(), groupId);
CommunicationBridge.connect(getDatabase(), new CommunicationBridge.Client.ConnectionHandler() {
@Override
public void onConnect(CommunicationBridge.Client client) {
try {
CoolSocket.ActiveConnection activeConnection = client.communicate(transferInstance.getDevice(), transferInstance.getConnection());
activeConnection.reply(new JSONObject().put(Keyword.REQUEST, Keyword.REQUEST_RESPONSE).put(Keyword.TRANSFER_GROUP_ID, groupId).put(Keyword.TRANSFER_IS_ACCEPTED, isAccepted).toString());
} catch (Exception e) {
e.printStackTrace();
}
}
});
if (isAccepted)
startFileReceiving(groupId);
else
mDatabase.remove(transferInstance.getGroup());
} catch (Exception e) {
e.printStackTrace();
if (isAccepted)
getNotificationHelper().showToast(R.string.mesg_somethingWentWrong);
}
} else if (ACTION_IP.equals(intent.getAction())) {
String deviceId = intent.getStringExtra(EXTRA_DEVICE_ID);
boolean isAccepted = intent.getBooleanExtra(EXTRA_IS_ACCEPTED, false);
int notificationId = intent.getIntExtra(NotificationUtils.EXTRA_NOTIFICATION_ID, -1);
getNotificationHelper().getUtils().cancel(notificationId);
NetworkDevice device = new NetworkDevice(deviceId);
try {
mDatabase.reconstruct(device);
device.isRestricted = !isAccepted;
mDatabase.update(device);
} catch (Exception e) {
e.printStackTrace();
return START_NOT_STICKY;
}
} else if (ACTION_CANCEL_INDEXING.equals(intent.getAction())) {
int notificationId = intent.getIntExtra(NotificationUtils.EXTRA_NOTIFICATION_ID, -1);
int groupId = intent.getIntExtra(EXTRA_GROUP_ID, -1);
getNotificationHelper().getUtils().cancel(notificationId);
Interrupter interrupter = getOngoingIndexList().get(groupId);
if (interrupter != null)
interrupter.interrupt();
} else if (ACTION_CLIPBOARD.equals(intent.getAction()) && intent.hasExtra(EXTRA_CLIPBOARD_ACCEPTED)) {
int notificationId = intent.getIntExtra(NotificationUtils.EXTRA_NOTIFICATION_ID, -1);
int clipboardId = intent.getIntExtra(EXTRA_CLIPBOARD_ID, -1);
boolean isAccepted = intent.getBooleanExtra(EXTRA_CLIPBOARD_ACCEPTED, false);
TextStreamObject textStreamObject = new TextStreamObject(clipboardId);
getNotificationHelper().getUtils().cancel(notificationId);
try {
getDatabase().reconstruct(textStreamObject);
if (isAccepted) {
((ClipboardManager) getSystemService(CLIPBOARD_SERVICE)).setPrimaryClip(ClipData.newPlainText("receivedText", textStreamObject.text));
Toast.makeText(this, R.string.mesg_textCopiedToClipboard, Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (ACTION_END_SESSION.equals(intent.getAction())) {
stopSelf();
} else if (ACTION_SEAMLESS_RECEIVE.equals(intent.getAction()) && intent.hasExtra(EXTRA_GROUP_ID)) {
int groupId = intent.getIntExtra(EXTRA_GROUP_ID, -1);
try {
CoolTransfer.TransferHandler<ProcessHolder> process = findProcessById(groupId);
if (process == null)
startFileReceiving(groupId);
else
Toast.makeText(this, getString(R.string.mesg_groupOngoingNotice, process.getExtra().transactionObject.friendlyName), Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
} else if (ACTION_CANCEL_JOB.equals(intent.getAction()) || ACTION_CANCEL_KILL.equals(intent.getAction())) {
int groupId = intent.getIntExtra(EXTRA_GROUP_ID, -1);
int notificationId = intent.getIntExtra(NotificationUtils.EXTRA_NOTIFICATION_ID, -1);
CoolTransfer.TransferHandler<ProcessHolder> handler = findProcessById(groupId);
if (handler == null || ACTION_CANCEL_KILL.equals(intent.getAction()))
getNotificationHelper().getUtils().cancel(notificationId);
if (handler != null) {
if (ACTION_CANCEL_KILL.equals(intent.getAction())) {
try {
if (handler instanceof CoolTransfer.Receive.Handler) {
CoolTransfer.Receive.Handler receiveHandler = ((CoolTransfer.Receive.Handler) handler);
if (receiveHandler.getServerSocket() != null)
receiveHandler.getServerSocket().close();
}
if (handler.getExtra().activeConnection.getSocket() != null)
handler.getExtra().activeConnection.getSocket().close();
if (handler.getSocket() != null)
handler.getSocket().close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
handler.getExtra().notification = getNotificationHelper().notifyStuckThread(handler.getExtra().transactionObject);
handler.interrupt();
}
}
} else if (ACTION_TOGGLE_SEAMLESS_MODE.equals(intent.getAction())) {
updateServiceState(!mSeamlessMode);
} else if (ACTION_TOGGLE_HOTSPOT.equals(intent.getAction()) && (Build.VERSION.SDK_INT < 23 || Settings.System.canWrite(this))) {
setupHotspot();
} else if (ACTION_REQUEST_HOTSPOT_STATUS.equals(intent.getAction()))
sendHotspotStatus(getHotspotUtils().getConfiguration());
}
return START_STICKY;
}
Aggregations