i migrated all strings to string.xml

i made PostActivity.java to get users input after a widget click

i added post options to the settings.xml

TravelPostWidget.java
i added an if block to check users settings for posting

LocationReceiver.java
i made a convince method for posting and added an if block that generates a post base on users settings
This commit is contained in:
2011-02-07 01:08:44 +00:00
parent 414f1a18bf
commit dafa2f284c
10 changed files with 279 additions and 28 deletions

View File

@@ -12,6 +12,7 @@ import twitter4j.TwitterException;
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;
@@ -46,18 +47,43 @@ public class LocationReceiver extends BroadcastReceiver{
* @author ricky barrette
*/
private void onLocationUpdate(Location location) {
//TODO something with the location i.e. report location to social services like twitter, ect...
/*
* the following is used to load widget prefs from shared_prefs
*/
SharedPreferences settings = mContext.getSharedPreferences(TravelPost.SETTINGS, 0);
try {
Log.d(TravelPost.TAG, TwitterServices.tweet(mContext, ReverseGeocoder.getAddressFromLocation(location)).toString());
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//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
try {
Log.d(TravelPost.TAG, TwitterServices.tweet(mContext, thePost).toString());
} catch (TwitterException e) {
e.printStackTrace();
}
}
/**
* Called when there is a location update from the location service.
* @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)

View File

@@ -0,0 +1,55 @@
/**
* PostActivity.java
* @date Feb 5, 2011
* @author ricky barrette
* @author Twenty Codes, LLC
*/
package com.TwentyCodes.android.TravelPost;
import com.TwentyCodes.android.location.LocationService;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
/**
* This is going to be the activity that is displayed when the travel post widget is clicked.
* This genreal feel of this activity is going to be similar to the google search widget activity
* @author ricky barrette
*/
public class PostActivity extends Activity implements OnClickListener {
private EditText mPostEditText;
/**
* called when the activity is first created
* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
* @author ricky barrette
*/
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.post);
findViewById(R.id.postOkButton).setOnClickListener(this);
mPostEditText = (EditText) findViewById(R.id.post);
}
/**
* called when the ok button is pressed
* (non-Javadoc)
* @see android.view.View.OnClickListener#onClick(android.view.View)
* @author ricky barrette
*/
@Override
public void onClick(View v) {
//store post information to shared_prefs
this.getSharedPreferences(TravelPost.SETTINGS, 0).edit().putString(TravelPost.USERS_POST, mPostEditText.getText().toString()).commit();
//start the service
this.startService(LocationService.getStartServiceIntent(this, LocationReceiver.ACTION_UPDATE));
//exit the activity
this.finish();
}
}

View File

@@ -0,0 +1,93 @@
/**
* @author Twenty Codes
* @author ricky barrette
*/
package com.TwentyCodes.android.TravelPost;
import android.content.Context;
import android.graphics.Typeface;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* this class will be a simple TextView to be used in a preference activity. you set the text using the set title tag
* @author ricky barrette
*/
public class TextViewPreference extends Preference {
/**
* creates a preference that is nothing but a text view
* @param context
*/
public TextViewPreference(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
/**
* creates a preference that is nothing but a text view
* @param context
* @param attrs
*/
public TextViewPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* creates a preference that is nothing but a text view
* @param context
* @param attrs
* @param defStyle
*/
public TextViewPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* creates a linear layout the contains only a textview.
* (non-Javadoc)
* @see android.preference.Preference#onCreateView(android.view.ViewGroup)
* @param parent
* @return
* @author ricky barrette
*/
@Override
protected View onCreateView(ViewGroup parent){
/*
* create a vertical linear layout that width and height that wraps content
*/
LinearLayout layout = new LinearLayout(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
layout.setPadding(15, 5, 10, 5);
layout.setOrientation(LinearLayout.VERTICAL);
layout.removeAllViews();
/*
* create a textview that will be used to display the title provided in xml
* and add it to the lay out
*/
TextView title = new TextView(getContext());
title.setText(getTitle());
title.setTextSize(16);
title.setTypeface(Typeface.SANS_SERIF);
title.setGravity(Gravity.LEFT);
title.setLayoutParams(params);
/*
* add the title and the time picker views to the layout
*/
layout.addView(title);
layout.setId(android.R.id.widget_frame);
return layout;
}
}

View File

@@ -30,6 +30,9 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL
public static final String SETTINGS = "settings";
private static final int TWITTER_AUTH_REQUEST_CODE = 0;
public static final String TAG = "TravelPost";
public static final String SAVED_POST = "saved_post";
public static final String SAVE_A_POST = "save_a_post";
public static final String USERS_POST = "users_post";
private FacebookAuth mFbAuth;
/**
@@ -41,6 +44,10 @@ public class TravelPost extends PreferenceActivity implements OnPreferenceClickL
@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);

View File

@@ -5,16 +5,17 @@
*/
package com.TwentyCodes.android.TravelPost;
import com.TwentyCodes.android.location.LocationService;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.RemoteViews;
import com.TwentyCodes.android.location.LocationService;
/**
* This class will display the widget on the android screen and handle user interaction
* @author warren
@@ -42,14 +43,30 @@ public class TravelPostWidget extends AppWidgetProvider {
if(DEBUG)
Log.v(TAG, "onUpdate()");
final int N = appWidgetIds.length;
/*
* the following is used to load widget prefs from shared_prefs
*/
SharedPreferences settings = context.getSharedPreferences(TravelPost.SETTINGS, 0);
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// Create a pending intent to start the location service;
PendingIntent pendingIntent = PendingIntent.getService(context, 0, LocationService.getStartServiceIntent(context, LocationReceiver.ACTION_UPDATE), 0);
PendingIntent pendingIntent;
/*
* if the save a post option is enabled then go start the service,
* else get the users input
*/
if( settings.getBoolean(TravelPost.SAVE_A_POST, false)){
//Create a pending intent to start the location service
pendingIntent = PendingIntent.getService(context, 0, LocationService.getStartServiceIntent(context, LocationReceiver.ACTION_UPDATE), 0);
} else {
//create a pending intent to start the post activity
pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, PostActivity.class), 0);
}
// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.travelpostwidget);
views.setOnClickPendingIntent(R.id.widgetbutton, pendingIntent);