package com.sailrobotics.posterize; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.StrictMode; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import twitter4j.StatusUpdate; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.TwitterFactory; import twitter4j.User; import twitter4j.auth.AccessToken; import twitter4j.auth.RequestToken; import twitter4j.conf.Configuration; import twitter4j.conf.ConfigurationBuilder; public class TwitterActivity extends Activity implements View.OnClickListener { /* Shared preference keys */ private static final String PREF_NAME = "sample_twitter_pref"; private static final String PREF_KEY_OAUTH_TOKEN = "oauth_token"; private static final String PREF_KEY_OAUTH_SECRET = "oauth_token_secret"; private static final String PREF_KEY_TWITTER_LOGIN = "is_twitter_loggedin"; private static final String PREF_USER_NAME = "twitter_user_name"; /* Any number for uniquely distinguishing the request */ public static final int WEBVIEW_REQUEST_CODE = 100; private ProgressDialog pDialog; private static Twitter twitter; private static RequestToken requestToken; private static SharedPreferences mSharedPreferences; private EditText mShareEditText; private TextView userName; private TextView tweetCharCount; private View loginLayout; private View shareLayout; private String consumerKey = null; private String consumerSecret = null; private String callbackUrl = null; private String oAuthVerifier = null; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* initializing twitter parameters from string.xml */ initTwitterConfigs(); /* Enabling strict mode */ StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); /* Setting activity layout file */ setContentView(R.layout.activity_twitter); /* new AlertDialog.Builder(this) .setTitle("Request Twitter Access") .setMessage("HW4 would like to access your twitter account") .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // continue with delete } }) .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }) .setIcon(android.R.drawable.ic_dialog_alert) .show();*/ Bundle bundle = getIntent().getExtras(); String deviceInfo = bundle.getString("postMessage"); loginLayout = (RelativeLayout) findViewById(R.id.login_layout); shareLayout = (RelativeLayout) findViewById(R.id.share_layout); mShareEditText = (EditText) findViewById(R.id.share_text); userName = (TextView) findViewById(R.id.user_name); //setting tweet content mShareEditText.setText(deviceInfo); mShareEditText.setKeyListener(null); //tweetCharCount.setText(mShareEditText.getText().length()); /* register button click listeners */ findViewById(R.id.btn_login).setOnClickListener(this); findViewById(R.id.btn_share).setOnClickListener(this); /* Check if required twitter keys are set */ if (TextUtils.isEmpty(consumerKey) || TextUtils.isEmpty(consumerSecret)) { Toast.makeText(this, "Twitter key and secret not configured", Toast.LENGTH_SHORT).show(); return; } /* Initialize application preferences */ mSharedPreferences = getSharedPreferences(PREF_NAME, 0); boolean isLoggedIn = mSharedPreferences.getBoolean(PREF_KEY_TWITTER_LOGIN, false); String username = mSharedPreferences.getString(PREF_USER_NAME, ""); userName.setText(getResources ().getString(R.string.hello) + username); /* if already logged in, then hide login layout and show share layout */ if (isLoggedIn) { loginLayout.setVisibility(View.GONE); shareLayout.setVisibility(View.VISIBLE); } else { loginLayout.setVisibility(View.VISIBLE); shareLayout.setVisibility(View.GONE); Uri uri = getIntent().getData(); if (uri != null && uri.toString().startsWith(callbackUrl)) { String verifier = uri.getQueryParameter(oAuthVerifier); try { /* Getting oAuth authentication token */ AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier); /* Getting user id form access token */ long userID = accessToken.getUserId(); final User user = twitter.showUser(userID); /* save updated token */ saveTwitterInfo(accessToken); loginLayout.setVisibility(View.GONE); shareLayout.setVisibility(View.VISIBLE); userName.setText(getString(R.string.hello) + user.getName()); } catch (Exception e) { Log.e("Failed to login Twitter", e.getMessage()); } } } } /** * Saving user information, after user is authenticated for the first time. * You don't need to show user to login, until user has a valid access toen */ private void saveTwitterInfo(AccessToken accessToken) { long userID = accessToken.getUserId(); User user; try { user = twitter.showUser(userID); String username = user.getName(); String id = user.getScreenName(); /* Storing oAuth tokens to shared preferences */ SharedPreferences.Editor e = mSharedPreferences.edit(); e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken()); e.putString(PREF_KEY_OAUTH_SECRET, accessToken.getTokenSecret()); e.putBoolean(PREF_KEY_TWITTER_LOGIN, true); e.putString(PREF_USER_NAME, username); e.putString("id", id); e.commit(); } catch (TwitterException e1) { e1.printStackTrace(); } } /* Reading twitter essential configuration parameters from strings.xml */ private void initTwitterConfigs() { consumerKey = getString(R.string.twitter_consumer_key); consumerSecret = getString(R.string.twitter_consumer_secret); callbackUrl = getString(R.string.twitter_callback); oAuthVerifier = getString(R.string.twitter_oauth_verifier); } private void loginToTwitter() { boolean isLoggedIn = mSharedPreferences.getBoolean(PREF_KEY_TWITTER_LOGIN, false); if (!isLoggedIn) { final ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setOAuthConsumerKey(consumerKey); builder.setOAuthConsumerSecret(consumerSecret); final Configuration configuration = builder.build(); final TwitterFactory factory = new TwitterFactory(configuration); twitter = factory.getInstance(); try { requestToken = twitter.getOAuthRequestToken(callbackUrl); /** * Loading twitter login page on webview for authorization * Once authorized, results are received at onActivityResult * */ final Intent intent = new Intent(this, TwitterWebViewActivity.class); intent.putExtra(TwitterWebViewActivity.EXTRA_URL, requestToken.getAuthenticationURL()); startActivityForResult(intent, WEBVIEW_REQUEST_CODE); } catch (TwitterException e) { e.printStackTrace(); } } else { loginLayout.setVisibility(View.GONE); shareLayout.setVisibility(View.VISIBLE); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { String verifier = data.getExtras().getString(oAuthVerifier); try { AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier); long userID = accessToken.getUserId(); final User user = twitter.showUser(userID); String username = user.getName(); saveTwitterInfo(accessToken); loginLayout.setVisibility(View.GONE); shareLayout.setVisibility(View.VISIBLE); userName.setText(TwitterActivity.this.getResources().getString( R.string.hello) + username); } catch (Exception e) { Log.e("Twitter Login Failed", e.getMessage()); } } super.onActivityResult(requestCode, resultCode, data); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_login: loginToTwitter(); break; case R.id.btn_share: final String status = mShareEditText.getText().toString(); if (new ConnectionDetector(getBaseContext()).isConnectedToInternet() == true) { if (status.length() > 118 + 22) { //22 - size of image URL attached with the tweet Toast.makeText(this, "Tweet is more than 140 characters!", Toast.LENGTH_SHORT).show(); } else if (status.trim().length() > 0) { new updateTwitterStatus().execute(status); } else { Toast.makeText(this, "Tweet is empty!!", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "No internet connection!", Toast.LENGTH_SHORT).show(); } break; } } class updateTwitterStatus extends AsyncTask<String, String, Void> { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(TwitterActivity.this); pDialog.setMessage("Posting to twitter..."); pDialog.setIndeterminate(false); pDialog.setCancelable(false); pDialog.show(); } protected Void doInBackground(String... args) { String status = args[0]; try { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setOAuthConsumerKey(consumerKey); builder.setOAuthConsumerSecret(consumerSecret); // Access Token String access_token = mSharedPreferences.getString(PREF_KEY_OAUTH_TOKEN, ""); // Access Token Secret String access_token_secret = mSharedPreferences.getString(PREF_KEY_OAUTH_SECRET, ""); AccessToken accessToken = new AccessToken(access_token, access_token_secret); Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken); // Update status StatusUpdate statusUpdate = new StatusUpdate(status); //InputStream is = getResources().openRawResource(+ R.drawable.lakeside_view); twitter4j.Status response = twitter.updateStatus(statusUpdate); Log.e("Status", response.getText()); } catch (TwitterException e) { Log.e("Failed to post!", e.getMessage()); } return null; } @Override protected void onPostExecute(Void result) { /* Dismiss the progress dialog after sharing */ pDialog.dismiss(); Toast.makeText(TwitterActivity.this, "Posted to Twitter!", Toast.LENGTH_SHORT).show(); // Clearing EditText field mShareEditText.setText(""); finish(); } } }