Files
travel_post/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationReceiver.java
ricky barrette 3bf935095b I have got FB auth to work properly in the settings, also i was able to post to facebook with out user interaction.
I intergrated facebok posting into LocationReceiver.java and have successfully posted my location to my wall as travel post.

i am having issues with receiver crashing when trying to post to facebook, if facebook isnt authorized yet.

im thinking of using a shared_prefs to save whether or not if facebook has been authorized
2011-02-12 17:54:34 +00:00

129 lines
3.7 KiB
Java

/**
* LocationReceiver.java
* @date Jan 21, 2011
* @author ricky barrette
* @author Twenty Codes, LLC
*/
package com.TwentyCodes.android.TravelPost;
import com.TwentyCodes.android.Facebook.FacebookAuth;
import com.TwentyCodes.android.Facebook.SessionNotValidException;
import com.TwentyCodes.android.location.ReverseGeocoder;
import twitter4j.TwitterException;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
/**
* The broadcast receiver that works with LocationService.java
* @author ricky barrette
*/
public class LocationReceiver extends BroadcastReceiver{
public static final String ACTION_UPDATE = "TwentyCodes.TravelPost.intent.action.LocationUpdate";
public static final String LOCATION_PARCEL = "location_parcel";
private static final String TAG = "LocationReceiver";
private WakeLock mWakeLock;
private Context mContext;
/**
* acquires a wakelock to prevent the device's cpu from sleeping
* @param context
* @author ricky barrette
*/
private void acquireWakeLock(Context context) {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
mWakeLock.acquire();
}
/**
* called when this receiver receives a location update
* @param location
* @author ricky barrette
*/
private void onLocationUpdate(Location location) {
/*
* the following is used to load widget prefs from shared_prefs
*/
SharedPreferences settings = mContext.getSharedPreferences(TravelPost.SETTINGS, 0);
//check to see if save a post is enabled, if not use the post the user provide
if(settings.getBoolean(TravelPost.SAVE_A_POST, false))
postToSocialServices(settings.getString(TravelPost.SAVED_POST, null), location);
else {
postToSocialServices(settings.getString(TravelPost.USERS_POST, null), location);
settings.edit().remove(TravelPost.USERS_POST).commit();
}
removeWakeLock();
}
/**
* a convince method to post to social networks
* @param string
* @author ricky barrette
*/
private void postToSocialServices(String post, Location location) {
Log.d(TravelPost.TAG, "Posting: "+ post);
//generate the post
String thePost = ReverseGeocoder.getAddressFromLocation(location) +" : "+ post;
// TODO check post size, is it greater than 140 chars?
// TODO Add more social services
//post to twitter
try {
Log.d(TravelPost.TAG, TwitterServices.tweet(mContext, thePost).toString());
} catch (TwitterException e) {
e.printStackTrace();
}
//post to facebook
FacebookAuth fbAuth = new FacebookAuth(mContext, new Activity());
fbAuth.authorize();
if(fbAuth.isSessionValid())
try {
fbAuth.postToWall(thePost);
} catch (SessionNotValidException e) {
e.printStackTrace();
}
else
Log.d(TravelPost.TAG, "cant post to FB, not auth'd");
}
/**
* Called when there is a location update from the location service.
* @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
* @author ricky barrette
*/
@Override
public void onReceive(Context context, Intent intent) {
mContext = context;
acquireWakeLock(context);
if(intent.getParcelableExtra(LOCATION_PARCEL) != null){
Location location = intent.getParcelableExtra(LOCATION_PARCEL);
onLocationUpdate(location);
}
}
/**
* removes the wake lock if there is one held
* @author ricky barrette
*/
private void removeWakeLock() {
if(mWakeLock.isHeld())
mWakeLock.release();
}
}