Android Open Source - Antares Auto Reply






From Project

Back to project page Antares.

License

The source code is released under:

Apache License

If you think the Android project Antares 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

/*
 * ??????????//w  w  w  .  ja  va  2 s .  com
 * name  ????????????
 * math  ????????????????
 * tweet ?3???????????????????
 * rpn   ??????????????????????????????????
 *
 */

package com.takac_j30.antares;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Stack;

import twitter4j.Status;
import twitter4j.StatusUpdate;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterStream;
import twitter4j.TwitterStreamFactory;
import twitter4j.User;
import twitter4j.UserStreamAdapter;
import twitter4j.auth.AccessToken;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class AutoReply extends Service {
  private static Twitter tw;
  private static TwitterStream ts;

  User user;

  static HashMap<String, Integer> op = new HashMap<String, Integer>();
  @Override
  public void onCreate() {
    Log.e("AutoReply","onCreate");
    Toast.makeText(this, "??", Toast.LENGTH_SHORT).show();

    user = MainActivity.user;

    //?????????????????????
    op.put("(", 1);
    op.put(")", 1);
    op.put("+", 2);
    op.put("-", 2);
    op.put("*", 3);
    op.put("/", 3);

    tw = TwitterUtils.getTwitterInstance(getApplicationContext());

    AccessToken at = TwitterUtils.loadAccessToken(getApplicationContext());
    String token = at.getToken();
    String tokenSecret = at.getTokenSecret();

    ConfigurationBuilder confBuilder = new ConfigurationBuilder();
    confBuilder.setOAuthConsumerKey(getString(R.string.CONSUMER_KEY));
    confBuilder.setOAuthConsumerSecret(getString(R.string.CONSUMER_SECRET));
    confBuilder.setOAuthAccessToken(token);
    confBuilder.setOAuthAccessTokenSecret(tokenSecret);
    confBuilder.setUserStreamBaseURL("https://userstream.twitter.com/2/");
    Configuration conf = confBuilder.build();
    TwitterStreamFactory tsf = new TwitterStreamFactory(conf);
    ts = tsf.getInstance();

    Log.e("test","??????????????????????????????");
    ts.addListener(new StreamAdapter());
    ts.user();
  }

  class StreamAdapter extends UserStreamAdapter {
    // ???????????????
    @Override
    public void onStatus(final Status status) {
      super.onStatus(status);

      // ??????????????????????????????
      final String twUser = "@" + status.getUser().getScreenName();
      final String tweet = twUser + ":" + status.getText();

      /*
       * ?????????????
       */

      if(status.getText().toLowerCase().indexOf("@" + user.getScreenName().toLowerCase()) > -1) {
        AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
          @Override
          protected Boolean doInBackground(Void... param) {
            String text = "";
            try {
              //????????????????
              tw.createFavorite(status.getId());
              String[] element = status.getText().split(" ");
              if(element.length >= 3) {
                //??????
                String command = element[1];
                Log.e("ADA", element[1]);
                if(command.equals("name")) {
                  /*
                   * ??????????
                   */
                  text = element[2];
                  for(int i = 3; i < element.length; i++) text += " " + element[i];
                  text = text.trim();
                  if(text.length() <= 20) {
                    tw.updateProfile(text, user.getURL(), user.getLocation(), user.getDescription());
                    text = "????" + text + "????????????????????";
                  }
                  StatusUpdate replyTweet = new StatusUpdate(twUser + text).inReplyToStatusId(status.getId());
                  tw.updateStatus(replyTweet);
                } else if(command.equals("math")) {
                  /*
                   * ????(????)
                   * TODO ?????????
                   */
                  String result = calculate(element);
                  StatusUpdate replyTweet = null;
                  if(result != null) {
                    replyTweet = new StatusUpdate(twUser + " " + result).inReplyToStatusId(status.getId());
                  } else {
                    replyTweet = new StatusUpdate(twUser + " ???").inReplyToStatusId(status.getId());
                  }
                  tw.updateStatus(replyTweet);
                } else if(command.equals("tweet")) {
                  /*
                   * ????????
                   */
                  text = element[2];
                  for(int i = 3; i < element.length; i++) text += " " + element[i];
                  tw.updateStatus(text);
                }
              } else if(element.length == 2) {
                if(element[1].equals("help")) {
                  text = "name [????]:???????\nmath [???]:??(?????????????SPC)\ntweet [????]:????";
                  StatusUpdate replyTweet = new StatusUpdate(".@" + status.getUser().getScreenName() + "\n" +text).inReplyToStatusId(status.getId());
                  tw.updateStatus(replyTweet);
                }
              }
              return true;
            } catch (TwitterException e) {
              e.printStackTrace();
              return false;
            }
          }
        };
        task.execute();
      }
      //tweetText.setText(tweet + "\n" + tweetText.getText().toString());
    }
  }

  /*
   * ??????????????????
   */
  public String calculate(String[] element) {

    ArrayList<String> rpn = new ArrayList<String>();
    rpn = convertRPN(element);
    if(rpn != null) {
      try {
        String siki = "";
        for(String str : rpn) {
          siki += str;
        }
        Log.e("math", "---???????" + siki);

        Stack<Float> stack = new Stack<Float>();
        for(int n = 0; n < rpn.size(); n++) {
          String el = rpn.get(n);
          try {
            int num = Integer.parseInt(el);
            stack.push((float) num);
          } catch(Exception ex) {
            float i1 = stack.pop();
            float i2 = stack.pop();
            if(el.equals("+")) {      stack.push(i2 + i1);
            } else if(el.equals("-")) {    stack.push(i2 - i1);
            } else if(el.equals("*")) {    stack.push(i2 * i1);
            } else {            stack.push(i2 / i1);
            }
          }
        }
        Float result = (float)stack.pop();
        int i = Integer.parseInt(String.valueOf(result));
        if(result == i) {
          return String.valueOf(i);
        } else {
          return String.valueOf(result);
        }
      } catch(Exception e) {
        return null;
      }
    } else {
      return null;
    }
  }

  /*
   * ????????????????
   */
  public ArrayList<String> convertRPN(String[] elem) {
    try {
      ArrayList<String> buf = new ArrayList<String>();
      Stack<String> work = new Stack<String>();

      op.put("(", 1);
      op.put(")", 1);
      op.put("+", 2);
      op.put("-", 2);
      op.put("*", 3);
      op.put("/", 3);

      for(int i = 2; i < elem.length; i++) {
        String token = elem[i];
        Log.e("TOKEN", token);
        System.out.println(token);
        try {
          int num = Integer.parseInt(token);
          buf.add(String.valueOf(num));
        } catch(NumberFormatException e) {
          if(token.equals(")")) {
            String s = work.pop();
            while(!s.equals("(")) {
              buf.add(s);
              s = work.pop();
            }
          } else if(token.equals("(")) {
            work.push(token);
          } else {
            WHILE:while(!work.empty()) {
              if(op.get(token) < op.get(work.peek())) {
                buf.add(work.pop());
              } else {
                break WHILE;
              }
            }
          work.push(token);
          }
        }
      }
      while(!work.empty()) buf.add(work.pop());

      return buf;
    } catch(Exception e) {
      return null;
    }


  }
  public void tweet(final String text) {
    AsyncTask<Void, Void, Void> startTweet = new AsyncTask<Void, Void, Void>() {
      @Override
      protected Void doInBackground(Void... params) {
        try {
          tw.updateStatus(text);
        } catch (TwitterException e) {
          e.printStackTrace();
        }
        return null;
      }
      @Override
      public void onPostExecute(Void us) {

      }
    };
    startTweet.execute();
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("Service", "onStartCommand Received start id " + startId + ": " + intent);
    //????????????????????????????????????????
    return START_STICKY;
  }

  @Override
  public void onDestroy() {
    Toast.makeText(this, "?????????????????????????", Toast.LENGTH_SHORT).show();
    ts.shutdown();
  }

  @Override
  public IBinder onBind(Intent intent) {
    // TODO ????????????????????
    return null;
  }
}




Java Source Code List

com.takac_j30.antares.Account.java
com.takac_j30.antares.AppInformation.java
com.takac_j30.antares.AutoReply.java
com.takac_j30.antares.AutoTweetAdapter.java
com.takac_j30.antares.CustomPagerAdapter.java
com.takac_j30.antares.MainActivity.java
com.takac_j30.antares.OAuthActivity.java
com.takac_j30.antares.ReplySettings.java
com.takac_j30.antares.TweetSettings.java
com.takac_j30.antares.TwitterUtils.java