Skip to content

Commit

Permalink
Send tweets at the specified duration
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilal Akhtar committed Oct 22, 2011
1 parent dc7a328 commit 0938efe
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
14 changes: 10 additions & 4 deletions src/com/bilal/schedulemytweets/ScheduleMyTweetsActivity.java
Expand Up @@ -42,6 +42,11 @@ public void onCreate(Bundle savedInstanceState) {
//OAuthConsumer = new CommonsHttpOAuthConsumer(consumer_key, consumer_secret);
//OAuthConsumer.setTokenWithSecret(sp.getString("access_token", "beep"), sp.getString("access_token_secret", "beep"));
twitter_instance = new Twitter (sp.getString("access_token", "beep"), sp.getString("access_token_secret", "beep"));

// Start the service
Intent serviceIntent = new Intent(this, ScheduleMyTweetsService.class);
serviceIntent.putExtra("twitter_instance", twitter_instance);
startService(serviceIntent);
}

Spinner dropdown_duration = (Spinner) findViewById(R.id.dropdown_duration);
Expand All @@ -51,10 +56,6 @@ public void onCreate(Bundle savedInstanceState) {
dropdown_duration.setAdapter(adapter);
dropdown_duration.setOnItemSelectedListener(this);

// Start the service
Intent serviceIntent = new Intent(this, ScheduleMyTweetsService.class);
startService(serviceIntent);

fill_time_options();
}

Expand All @@ -68,6 +69,11 @@ protected void onNewIntent(Intent intent) {
Uri uri = intent.getData();

twitter_instance.store_access_token(settings, uri);
if (uri != null && uri.toString().startsWith("app://schedulemytweets")) {
Intent serviceIntent = new Intent(this, ScheduleMyTweetsService.class);
serviceIntent.putExtra("twitter_instance", twitter_instance);
startService(serviceIntent);
}
}

private void fill_time_options() {
Expand Down
8 changes: 7 additions & 1 deletion src/com/bilal/schedulemytweets/ScheduleMyTweetsService.java
Expand Up @@ -19,6 +19,8 @@ public class ScheduleMyTweetsService extends Service {
private SQLiteTweetDB tweet_db_helper;
private SQLiteDatabase tweet_db;

private Twitter twitter_instance;

private class timer_task extends TimerTask {

@Override
Expand All @@ -36,7 +38,9 @@ public void run() {
cursor_result.moveToFirst();

while (cursor_result.isAfterLast() == false) {
Log.d(TAG,"Cursor result: " + cursor_result.getString(0));
String tweet_text = cursor_result.getString(0);
Log.d(TAG,"Cursor result: " + tweet_text);
twitter_instance.tweet(tweet_text);
cursor_result.moveToNext();
}

Expand All @@ -63,6 +67,8 @@ public int onStartCommand (Intent intent, int flags, int startId)

timer.scheduleAtFixedRate(new timer_task(), 0, 60000);

twitter_instance = (Twitter)intent.getParcelableExtra("twitter_instance");

return START_STICKY;
}

Expand Down
66 changes: 53 additions & 13 deletions src/com/bilal/schedulemytweets/Twitter.java
@@ -1,7 +1,5 @@
package com.bilal.schedulemytweets;

import java.io.*;
import java.lang.*;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -12,7 +10,6 @@
import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.entity.*;
import org.apache.http.impl.*;
import org.apache.http.impl.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.message.*;
Expand All @@ -23,7 +20,7 @@
import android.content.*;
import android.os.*;

public class Twitter {
public class Twitter implements Parcelable {

private CommonsHttpOAuthConsumer OAuthConsumer;
private OAuthProvider OAuthProvider;
Expand All @@ -33,18 +30,33 @@ public class Twitter {

private static final String CALLBACKURL = "app://schedulemytweets";

private String access_token;
private String access_token_secret;

private static final String TAG="ScheduleMyTweets";

public Twitter(String access_token, String access_token_secret) {
if (access_token != null) {
public Twitter(String access_token_s, String access_token_secret_s) {
if (access_token_s != null) {
OAuthConsumer = new CommonsHttpOAuthConsumer(consumer_key, consumer_secret);
OAuthConsumer.setTokenWithSecret(access_token, access_token_secret);
OAuthConsumer.setTokenWithSecret(access_token_s, access_token_secret_s);
access_token = new String(access_token_s);
access_token_secret = new String(access_token_secret_s);
}

}

private Twitter (Parcel parcel) {

// You can always assume that the access tokens are already supplied
access_token = parcel.readString();
access_token_secret = parcel.readString();
OAuthConsumer = new CommonsHttpOAuthConsumer(consumer_key, consumer_secret);
OAuthConsumer.setTokenWithSecret(access_token, access_token_secret);

Log.d(TAG, "Unpacking parcel");
Log.d(TAG,"Access token: " + access_token);
Log.d(TAG,"Access token secret: " + access_token_secret);
Log.d(TAG,"Consumer Key: " + consumer_key);
Log.d(TAG,"Consumer Secret: " + consumer_secret);
}

public void store_access_token(SharedPreferences settings, Uri uri) {
Expand All @@ -62,17 +74,17 @@ public void store_access_token(SharedPreferences settings, Uri uri) {
// Retrieve the access tokens

OAuthProvider.retrieveAccessToken(OAuthConsumer, verifier);
String userKey = OAuthConsumer.getToken();
String userSecret = OAuthConsumer.getTokenSecret();
access_token = OAuthConsumer.getToken();
access_token_secret = OAuthConsumer.getTokenSecret();

// Save the access tokens in shared preferences

SharedPreferences.Editor editor = settings.edit();
editor.putString("access_token", userKey);
editor.putString("access_token_secret", userSecret);
editor.putString("access_token", access_token);
editor.putString("access_token_secret", access_token_secret);
editor.commit();

Log.d(TAG,"Access token: " + userKey + "\nAccess Token Secret: " + userSecret);
Log.d(TAG,"Access token: " + access_token + "\nAccess Token Secret: " + access_token_secret);

} catch(Exception e){
e.printStackTrace();
Expand Down Expand Up @@ -129,4 +141,32 @@ public void run(){
e.printStackTrace();
}
}

public int describeContents() {
// TODO Auto-generated method stub
// We don't give a shit about this
return 0;
}

public void writeToParcel(Parcel dest, int flags) {
dest.writeString(access_token);
dest.writeString(access_token_secret);

Log.d(TAG, "Packing parcel..");
Log.d(TAG,"Access token: " + access_token);
Log.d(TAG,"Access token secret: " + access_token_secret);
Log.d(TAG,"Consumer Key: " + consumer_key);
Log.d(TAG,"Consumer Secret: " + consumer_secret);
}

public static final Parcelable.Creator<Twitter> CREATOR
= new Parcelable.Creator<Twitter>() {
public Twitter createFromParcel(Parcel in) {
return new Twitter(in);
}

public Twitter[] newArray(int size) {
return new Twitter[size];
}
};
}

0 comments on commit 0938efe

Please sign in to comment.