package info.justaway.task;

import android.os.AsyncTask;

import info.justaway.JustawayApplication;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.auth.AccessToken;

public class SendDirectMessageTask extends AsyncTask<String, Void, TwitterException> {

    private JustawayApplication mApplication;
    private AccessToken mAccessToken;

    public SendDirectMessageTask(AccessToken accessToken) {
        mAccessToken = accessToken;
        mApplication = JustawayApplication.getApplication();
    }

    @Override
    protected TwitterException doInBackground(String... params) {
        try {
            String[] s = params[0].split(" ");
            if (mAccessToken == null) {
                mApplication.getTwitter().sendDirectMessage(s[1], s[2]);
            } else {
                // ツイート画面から来たとき
                Twitter twitter = mApplication.getTwitterInstance();
                twitter.setOAuthAccessToken(mAccessToken);
                twitter.sendDirectMessage(s[1], s[2]);
            }
        } catch (TwitterException e) {
            return e;
        }
        return null;
    }
}
@@ -666,19 +666,22 @@
 plit(%22 %22
+, 3
 );%0A
-
         
@@ -768,34 +768,58 @@
 rectMessage(
-s%5B1%5D, s%5B2%5D
+getOrEmpty(s, 1), getOrEmpty(s, 2)
 );%0A         
@@ -1036,18 +1036,42 @@
 age(
-s%5B1%5D, s%5B2%5D
+getOrEmpty(s, 1), getOrEmpty(s, 2)
 );%0A 
@@ -1146,32 +1146,32 @@
 rn e;%0A        %7D%0A
-
         return n
@@ -1180,10 +1180,196 @@
 l;%0A    %7D
+%0A%0A    private static String getOrEmpty(String%5B%5D array, int index) %7B%0A        if (index %3C= 0 %7C%7C array.length %3C= index) %7B%0A            return %22%22;%0A        %7D%0A        return array%5Bindex%5D;%0A    %7D
 %0A%7D

fix a bug that characters after space character in DM are dropped. And fix crash bug when tweeting 'D ' or 'D screenname'