TravelPost.java

create updateWidget() that is called from onDestroy that updates the widget to the current settings
This commit is contained in:
2011-02-11 15:37:02 +00:00
parent 26be6cea67
commit ff0e2a287e

View File

@@ -6,17 +6,22 @@
*/
package com.TwentyCodes.android.TravelPost;
import com.TwentyCodes.android.Facebook.FacebookAuth;
import com.TwentyCodes.android.Facebook.SessionNotValidException;
import twitter4j.TwitterException;
import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.widget.RemoteViews;
import com.TwentyCodes.android.Facebook.FacebookAuth;
import com.TwentyCodes.android.Facebook.SessionNotValidException;
import com.TwentyCodes.android.location.LocationService;
/**
* Main activity for the Travel Post widget.
@@ -35,53 +40,6 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL
public static final String USERS_POST = "users_post";
private FacebookAuth mFbAuth;
/**
* called when the activity is first created
* (non-Javadoc)
* @see android.preference.PreferenceActivity#onCreate(android.os.Bundle)
* @author ricky barrette
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set this preference activity to use the settings file we choose.
this.getPreferenceManager().setSharedPreferencesName(SETTINGS);
//start the exception handler
mExceptionReport.run();
Thread.setDefaultUncaughtExceptionHandler(mExceptionReport);
// load preferences xml
this.addPreferencesFromResource(R.xml.settings);
this.findPreference("twitter_sign_in").setOnPreferenceClickListener(this);
this.findPreference("facebook_sign_in").setOnPreferenceClickListener(this);
mFbAuth = new FacebookAuth(this, this);
mFbAuth.authorize();
}
@Override
public boolean onPreferenceClick(Preference preference) {
if (preference.getKey().equals("twitter_sign_in")) {
mTwitterServices = new TwitterServices(this);
try {
this.startActivityForResult(new Intent(this, WebAuth.class).putExtra(WebAuth.AUTH_URL, mTwitterServices.getAuthorizationURL()), TWITTER_AUTH_REQUEST_CODE);
} catch (TwitterException e) {
e.printStackTrace();
}
} else if (preference.getKey().equals("facebook_sign_in")) {
try {
mFbAuth.postToWall("Another test");
} catch (SessionNotValidException e) {
e.printStackTrace();
}
Log.i(TAG, "onPreferenceClick.posted to wall");
}
return false;
}
/**
* called when the web auth activity returns its result
* (non-Javadoc)
@@ -115,4 +73,89 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL
}
}
/**
* called when the activity is first created
* (non-Javadoc)
* @see android.preference.PreferenceActivity#onCreate(android.os.Bundle)
* @author ricky barrette
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate()");
//set this preference activity to use the settings file we choose.
this.getPreferenceManager().setSharedPreferencesName(SETTINGS);
//start the exception handler
mExceptionReport.run();
Thread.setDefaultUncaughtExceptionHandler(mExceptionReport);
// load preferences xml
this.addPreferencesFromResource(R.xml.settings);
this.findPreference("twitter_sign_in").setOnPreferenceClickListener(this);
this.findPreference("facebook_sign_in").setOnPreferenceClickListener(this);
// mFbAuth = new FacebookAuth(this, this);
// mFbAuth.authorize();
}
@Override
public void onDestroy(){
Log.v(TAG, "onDestroy()");
updateWidget();
super.onDestroy();
}
@Override
public boolean onPreferenceClick(Preference preference) {
if (preference.getKey().equals("twitter_sign_in")) {
mTwitterServices = new TwitterServices(this);
try {
this.startActivityForResult(new Intent(this, WebAuth.class).putExtra(WebAuth.AUTH_URL, mTwitterServices.getAuthorizationURL()), TWITTER_AUTH_REQUEST_CODE);
} catch (TwitterException e) {
e.printStackTrace();
}
} else if (preference.getKey().equals("facebook_sign_in")) {
try {
mFbAuth.postToWall("Another test");
} catch (SessionNotValidException e) {
e.printStackTrace();
}
Log.i(TAG, "onPreferenceClick.posted to wall");
}
return false;
}
/**
* updates the widget
* @author ricky barrette
*/
private void updateWidget(){
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
ComponentName thisWidget = new ComponentName(this, TravelPostWidget.class);
PendingIntent pendingIntent;
/*
* if the save a post option is enabled then go start the service,
* else get the users input
*/
if( this.getSharedPreferences(SETTINGS, 0).getBoolean(TravelPost.SAVE_A_POST, false)){
//Create a pending intent to start the location service
pendingIntent = PendingIntent.getService(this, 0, LocationService.getStartServiceIntent(this, LocationReceiver.ACTION_UPDATE), 0);
} else {
//create a pending intent to start the post activity
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, PostActivity.class), 0);
}
// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.travelpostwidget);
views.setOnClickPendingIntent(R.id.widgetbutton, pendingIntent);
//send the update to the widget
appWidgetManager.updateAppWidget(thisWidget, views);
}
}