From dafa2f284c8053a26cbdefed1a4204762e3a980c Mon Sep 17 00:00:00 2001 From: ricky barrette Date: Mon, 7 Feb 2011 01:08:44 +0000 Subject: [PATCH] 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 --- TravelPost/AndroidManifest.xml | 3 +- .../com/TwentyCodes/android/TravelPost/R.java | 31 +++++-- TravelPost/res/layout/post.xml | 26 ++++++ TravelPost/res/values/strings.xml | 8 ++ TravelPost/res/xml/settings.xml | 19 ++-- .../android/TravelPost/LocationReceiver.java | 38 ++++++-- .../android/TravelPost/PostActivity.java | 55 +++++++++++ .../TravelPost/TextViewPreference.java | 93 +++++++++++++++++++ .../android/TravelPost/TravelPost.java | 7 ++ .../android/TravelPost/TravelPostWidget.java | 27 +++++- 10 files changed, 279 insertions(+), 28 deletions(-) create mode 100644 TravelPost/res/layout/post.xml create mode 100644 TravelPost/src/com/TwentyCodes/android/TravelPost/PostActivity.java create mode 100644 TravelPost/src/com/TwentyCodes/android/TravelPost/TextViewPreference.java diff --git a/TravelPost/AndroidManifest.xml b/TravelPost/AndroidManifest.xml index 4981fce..4e9a079 100644 --- a/TravelPost/AndroidManifest.xml +++ b/TravelPost/AndroidManifest.xml @@ -29,8 +29,9 @@ - + + diff --git a/TravelPost/gen/com/TwentyCodes/android/TravelPost/R.java b/TravelPost/gen/com/TwentyCodes/android/TravelPost/R.java index 0c7329c..ac7939f 100644 --- a/TravelPost/gen/com/TwentyCodes/android/TravelPost/R.java +++ b/TravelPost/gen/com/TwentyCodes/android/TravelPost/R.java @@ -23,25 +23,36 @@ public final class R { public static final int user=0x7f020009; } public static final class id { - public static final int authCode=0x7f060005; - public static final int authOkButton=0x7f060004; - public static final int authWebView=0x7f060006; - public static final int skyhook_img=0x7f060000; - public static final int tc_img=0x7f060002; - public static final int text=0x7f060001; - public static final int widgetbutton=0x7f060003; + public static final int authCode=0x7f060007; + public static final int authOkButton=0x7f060006; + public static final int authWebView=0x7f060008; + public static final int post=0x7f060001; + public static final int postOkButton=0x7f060000; + public static final int skyhook_img=0x7f060002; + public static final int tc_img=0x7f060004; + public static final int text=0x7f060003; + public static final int widgetbutton=0x7f060005; } public static final class layout { public static final int main=0x7f030000; - public static final int powered_by_skyhook=0x7f030001; - public static final int travelpostwidget=0x7f030002; - public static final int webauth=0x7f030003; + public static final int post=0x7f030001; + public static final int powered_by_skyhook=0x7f030002; + public static final int travelpostwidget=0x7f030003; + public static final int webauth=0x7f030004; } public static final class string { + public static final int about=0x7f050007; public static final int app_name=0x7f050000; + public static final int fb_signin=0x7f050005; public static final int gps_fix=0x7f050001; public static final int hello=0x7f050003; + public static final int location_settings=0x7f050006; + public static final int post_hint=0x7f05000b; + public static final int post_settings=0x7f050008; + public static final int save_a_post=0x7f050009; + public static final int services=0x7f05000a; public static final int sorry_theres_trouble=0x7f050002; + public static final int twitter_signin=0x7f050004; } public static final class xml { public static final int settings=0x7f040000; diff --git a/TravelPost/res/layout/post.xml b/TravelPost/res/layout/post.xml new file mode 100644 index 0000000..e6ff568 --- /dev/null +++ b/TravelPost/res/layout/post.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + diff --git a/TravelPost/res/values/strings.xml b/TravelPost/res/values/strings.xml index d7bf46a..17e6ded 100644 --- a/TravelPost/res/values/strings.xml +++ b/TravelPost/res/values/strings.xml @@ -2,4 +2,12 @@ Hello World! Travel Post + Twitter Sign In + Facebook Sign In + Location Settings + About + Post Settings + Save a post + Social Services + Whats up? diff --git a/TravelPost/res/xml/settings.xml b/TravelPost/res/xml/settings.xml index 4e1de67..d2b1415 100644 --- a/TravelPost/res/xml/settings.xml +++ b/TravelPost/res/xml/settings.xml @@ -1,17 +1,24 @@ - + android:title="@string/app_name"> + + + + + android:title="@string/location_settings"> + + + + diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationReceiver.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationReceiver.java index c198afb..2ef6076 100644 --- a/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationReceiver.java +++ b/TravelPost/src/com/TwentyCodes/android/TravelPost/LocationReceiver.java @@ -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) diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/PostActivity.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/PostActivity.java new file mode 100644 index 0000000..ee28a1e --- /dev/null +++ b/TravelPost/src/com/TwentyCodes/android/TravelPost/PostActivity.java @@ -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(); + } +} \ No newline at end of file diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/TextViewPreference.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/TextViewPreference.java new file mode 100644 index 0000000..be84a80 --- /dev/null +++ b/TravelPost/src/com/TwentyCodes/android/TravelPost/TextViewPreference.java @@ -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; + } +} diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPost.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPost.java index f99a4c1..9af005c 100644 --- a/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPost.java +++ b/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPost.java @@ -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); diff --git a/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPostWidget.java b/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPostWidget.java index cced080..1bae9f6 100644 --- a/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPostWidget.java +++ b/TravelPost/src/com/TwentyCodes/android/TravelPost/TravelPostWidget.java @@ -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