Search in sources :

Example 1 with TextStreamObject

use of com.genonbeta.TrebleShot.object.TextStreamObject 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;
}
Also used : Interrupter(com.genonbeta.TrebleShot.util.Interrupter) TextStreamObject(com.genonbeta.TrebleShot.object.TextStreamObject) CoolTransfer(com.genonbeta.CoolSocket.CoolTransfer) NetworkDevice(com.genonbeta.TrebleShot.object.NetworkDevice) CommunicationBridge(com.genonbeta.TrebleShot.util.CommunicationBridge) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) JSONException(org.json.JSONException) ConnectionNotFoundException(com.genonbeta.TrebleShot.exception.ConnectionNotFoundException) IOException(java.io.IOException) DeviceNotFoundException(com.genonbeta.TrebleShot.exception.DeviceNotFoundException) TransactionGroupNotFoundException(com.genonbeta.TrebleShot.exception.TransactionGroupNotFoundException) JSONObject(org.json.JSONObject) TransferInstance(com.genonbeta.TrebleShot.object.TransferInstance)

Example 2 with TextStreamObject

use of com.genonbeta.TrebleShot.object.TextStreamObject in project TrebleShot by genonbeta.

the class TextEditorActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getIntent() == null || !ACTION_EDIT_TEXT.equals(getIntent().getAction()))
        finish();
    else {
        setContentView(R.layout.layout_text_editor_activity);
        if (getSupportActionBar() != null)
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mDatabase = new AccessDatabase(this);
        mEditTextEditor = findViewById(R.id.layout_text_editor_activity_text_text_box);
        if (getIntent().hasExtra(EXTRA_CLIPBOARD_ID)) {
            mTextStreamObject = new TextStreamObject(getIntent().getIntExtra(EXTRA_CLIPBOARD_ID, -1));
            try {
                mDatabase.reconstruct(mTextStreamObject);
                mEditTextEditor.getText().append(mTextStreamObject.text);
            } catch (Exception e) {
                e.printStackTrace();
                mTextStreamObject = null;
            }
        } else if (getIntent().hasExtra(EXTRA_TEXT_INDEX))
            mEditTextEditor.getText().append(getIntent().getStringExtra(EXTRA_TEXT_INDEX));
    }
}
Also used : TextStreamObject(com.genonbeta.TrebleShot.object.TextStreamObject) AccessDatabase(com.genonbeta.TrebleShot.database.AccessDatabase)

Example 3 with TextStreamObject

use of com.genonbeta.TrebleShot.object.TextStreamObject in project TrebleShot by genonbeta.

the class TextStreamListAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    View parentView = holder.getView();
    TextStreamObject object = getItem(position);
    if (!holder.tryBinding(object)) {
        View selector = parentView.findViewById(R.id.selector);
        TextView text1 = parentView.findViewById(R.id.text);
        TextView text2 = parentView.findViewById(R.id.text2);
        if (getSelectionConnection() != null)
            selector.setSelected(object.isSelectableSelected());
        text1.setText(object.text);
        text2.setText(DateUtils.formatDateTime(getContext(), object.date, DateUtils.FORMAT_SHOW_TIME));
    }
}
Also used : TextStreamObject(com.genonbeta.TrebleShot.object.TextStreamObject) TextView(android.widget.TextView) TextView(android.widget.TextView) View(android.view.View)

Example 4 with TextStreamObject

use of com.genonbeta.TrebleShot.object.TextStreamObject in project TrebleShot by genonbeta.

the class TextStreamListFragment method onActionMenuItemSelected.

@Override
public boolean onActionMenuItemSelected(Context context, PowerfulActionMode actionMode, MenuItem item) {
    int id = item.getItemId();
    ArrayList<TextStreamObject> selectionList = getSelectionConnection().getSelectedItemList();
    if (id == R.id.action_mode_text_stream_delete) {
        for (TextStreamObject textStreamObject : selectionList) getAdapter().getDatabase().remove(textStreamObject);
    } else if (id == R.id.action_mode_share_all_apps || id == R.id.action_mode_share_trebleshot) {
        if (selectionList.size() == 1) {
            TextStreamObject streamObject = selectionList.get(0);
            Intent shareIntent = new Intent(item.getItemId() == R.id.action_mode_share_all_apps ? Intent.ACTION_SEND : ShareActivity.ACTION_SEND).putExtra(Intent.EXTRA_TEXT, streamObject.text).setType("text/*");
            startActivity((item.getItemId() == R.id.action_mode_share_all_apps) ? Intent.createChooser(shareIntent, getString(R.string.text_fileShareAppChoose)) : shareIntent);
        } else {
            Toast.makeText(context, R.string.mesg_textShareLimit, Toast.LENGTH_SHORT).show();
            return false;
        }
    } else
        return super.onActionMenuItemSelected(context, actionMode, item);
    return true;
}
Also used : TextStreamObject(com.genonbeta.TrebleShot.object.TextStreamObject) Intent(android.content.Intent)

Example 5 with TextStreamObject

use of com.genonbeta.TrebleShot.object.TextStreamObject in project TrebleShot by genonbeta.

the class TextStreamListFragment method onAdapter.

@Override
public TextStreamListAdapter onAdapter() {
    return new TextStreamListAdapter(getActivity()) {

        @Override
        public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
            super.onBindViewHolder(holder, position);
            holder.getView().setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (!holder.isRepresentative() && !setItemSelected(holder)) {
                        TextStreamObject textStreamObject = getAdapter().getItem(holder);
                        startActivity(new Intent(getContext(), TextEditorActivity.class).setAction(TextEditorActivity.ACTION_EDIT_TEXT).putExtra(TextEditorActivity.EXTRA_CLIPBOARD_ID, textStreamObject.id).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                    }
                }
            });
        }
    };
}
Also used : TextStreamObject(com.genonbeta.TrebleShot.object.TextStreamObject) TextEditorActivity(com.genonbeta.TrebleShot.activity.TextEditorActivity) NonNull(android.support.annotation.NonNull) TextStreamListAdapter(com.genonbeta.TrebleShot.adapter.TextStreamListAdapter) Intent(android.content.Intent) View(android.view.View)

Aggregations

TextStreamObject (com.genonbeta.TrebleShot.object.TextStreamObject)5 Intent (android.content.Intent)2 View (android.view.View)2 NonNull (android.support.annotation.NonNull)1 TextView (android.widget.TextView)1 CoolTransfer (com.genonbeta.CoolSocket.CoolTransfer)1 TextEditorActivity (com.genonbeta.TrebleShot.activity.TextEditorActivity)1 TextStreamListAdapter (com.genonbeta.TrebleShot.adapter.TextStreamListAdapter)1 AccessDatabase (com.genonbeta.TrebleShot.database.AccessDatabase)1 ConnectionNotFoundException (com.genonbeta.TrebleShot.exception.ConnectionNotFoundException)1 DeviceNotFoundException (com.genonbeta.TrebleShot.exception.DeviceNotFoundException)1 TransactionGroupNotFoundException (com.genonbeta.TrebleShot.exception.TransactionGroupNotFoundException)1 NetworkDevice (com.genonbeta.TrebleShot.object.NetworkDevice)1 TransferInstance (com.genonbeta.TrebleShot.object.TransferInstance)1 CommunicationBridge (com.genonbeta.TrebleShot.util.CommunicationBridge)1 Interrupter (com.genonbeta.TrebleShot.util.Interrupter)1 IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1