Android Open Source - ShareToIRC Share Text Activity






From Project

Back to project page ShareToIRC.

License

The source code is released under:

MIT License

If you think the Android project ShareToIRC listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.rrs_apps.android.share_to_irc;
// w  w  w . j av  a 2s  .  com
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.Toast;

import com.rrs_apps.android.share_to_irc.account.IrcAccountHandler;

import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.OnActivityResult;
import org.parceler.Parcels;

/**
 * ShareTextActivity receives text via an intent and shares it to a selected IRC account.
 */
@EActivity
public class ShareTextActivity extends Activity {
    private static final int REQ_CODE_PICK_ACCOUNT = 0;
    private final String TAG = getClass().getName();

    @Extra(Intent.EXTRA_SUBJECT)
    String subject;

    @Extra(Intent.EXTRA_TEXT)
    String text;

    @Override
    protected void onStart() {
        super.onStart();

        if (text == null || text.isEmpty()) {
            Toast.makeText(this, R.string.no_text_to_share, Toast.LENGTH_LONG).show();

            finish();
        } else {
            // Determine destination account
            IrcAccountHandler.launchAccountPicker(this, REQ_CODE_PICK_ACCOUNT, null, false,
                    new String[]{IrcAccountHandler.ACCOUNT_TYPE_SHARE_TO_IRC});
        }
    }

    @OnActivityResult(REQ_CODE_PICK_ACCOUNT)
    void sendText(Intent data) {
        if (data != null) {
            String acctName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
            String acctType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);

            Account acct = new Account(acctName, acctType);

            // Load account details
            String address = AccountManager.get(this).getUserData(acct,
                    IrcAccountHandler.ACCOUNT_KEY_HOST_ADDRESS);
            int port = Integer.parseInt(AccountManager.get(this).getUserData(acct,
                    IrcAccountHandler.ACCOUNT_KEY_HOST_PORT));
            String nick = AccountManager.get(this).getUserData(acct, IrcAccountHandler.ACCOUNT_KEY_NICK);
            boolean useSsl = Boolean.parseBoolean(AccountManager.get(this).getUserData(acct,
                    IrcAccountHandler.ACCOUNT_KEY_IS_SSL));
            String password = AccountManager.get(this).getUserData(acct,
                    IrcAccountHandler.ACCOUNT_KEY_SERVER_PASSWORD);
            String channelList = AccountManager.get(this).getUserData(acct,
                    IrcAccountHandler.ACCOUNT_KEY_CHANNEL_LIST);
            String[] channels = channelList.split(" ");

            // Notify user that sharing is occurring
            String serverName = AccountManager.get(this).getUserData(acct, IrcAccountHandler.ACCOUNT_KEY_SERVER_NAME);
            String sharingText = getResources().getText(R.string.sharing_to_x).toString();
            Toast.makeText(this, String.format(sharingText, serverName), Toast.LENGTH_SHORT).show();

            // Prepend subject if it was supplied
            if (subject != null && !subject.trim().isEmpty()) {
                // Some apps duplicate the subject in the actual text; attempt to tidy up the output by removing the duplicate text
                text = text.replace(subject, "").trim();

                text = subject + ": " + text;
            }

            queueMessageWithService(text, address, port, nick, useSsl, password, channels);
        } else {
            finish();
        }
    }

    private void queueMessageWithService(String text, String address, int port, String nick, boolean useSsl, String password, String[] channels) {
        Log.d(TAG, "Queueing text with service");

        Intent sendTextIntent = new Intent(this, ShareTextService_.class);

        IRCMessage message = new IRCMessage(text, address, port, nick, password, channels);
        message.setUsesSsl(true);

        sendTextIntent.putExtra(ShareTextService.EXTRA_IRC_MESSAGE, Parcels.wrap(message));

        startService(sendTextIntent);

        finish();
    }
}




Java Source Code List

com.rrs_apps.android.auth.AccountAuthenticatorFragmentActivity.java
com.rrs_apps.android.share_to_irc.IRCMessage.java
com.rrs_apps.android.share_to_irc.SendTextAndFinishListener.java
com.rrs_apps.android.share_to_irc.ShareTextActivity.java
com.rrs_apps.android.share_to_irc.ShareTextService.java
com.rrs_apps.android.share_to_irc.account.CreateIrcAccountActivity.java
com.rrs_apps.android.share_to_irc.account.EditIrcAccountActivity.java
com.rrs_apps.android.share_to_irc.account.IrcAccountEditorFragment.java
com.rrs_apps.android.share_to_irc.account.IrcAccountHandlerService.java
com.rrs_apps.android.share_to_irc.account.IrcAccountHandler.java
com.rrs_apps.android.share_to_irc.account.IrcAccountListFragment.java
com.rrs_apps.android.share_to_irc.account.IrcAccountPickerActivity.java
com.rrs_apps.android.share_to_irc.account.ListIrcAccountsActivity.java