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

@@ -29,8 +29,9 @@
<meta-data android:name="android.appwidget.provider" android:resource="@xml/travelpostwidgetinfo"/>
</receiver>
<service android:process=":TravelPostLocationService" android:enabled="true" android:name="LocationService" android:exported="true"></service>
<service android:process=":TravelPostLocationService" android:enabled="true" android:exported="true" android:name="com.TwentyCodes.android.location.LocationService"></service>
<activity android:name="WebAuth" android:configChanges="orientation"></activity>
<activity android:name="PostActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar"></activity>
</application>
<uses-sdk android:minSdkVersion="7" />

View File

@@ -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;

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/ok"
android:id="@+id/postOkButton"
android:layout_alignBottom="@+id/post"
android:layout_alignTop="@id/post"
android:layout_alignParentRight="true"></Button>
<EditText android:layout_toLeftOf="@id/postOkButton"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:hint="@string/post_hint"
android:id="@id/post">
</EditText>
</RelativeLayout>
</RelativeLayout>

View File

@@ -2,4 +2,12 @@
<resources>
<string name="hello">Hello World!</string>
<string name="app_name">Travel Post</string>
<string name="twitter_signin">Twitter Sign In</string>
<string name="fb_signin">Facebook Sign In</string>
<string name="location_settings">Location Settings</string>
<string name="about">About</string>
<string name="post_settings">Post Settings</string>
<string name="save_a_post">Save a post</string>
<string name="services">Social Services</string>
<string name="post_hint">Whats up?</string>
</resources>

View File

@@ -1,17 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Travel Post">
<PreferenceCategory
android:title="Services">
android:title="@string/app_name">
<PreferenceCategory android:title="@string/post_settings">
<CheckBoxPreference android:title="@string/save_a_post" android:key="save_a_post"></CheckBoxPreference>
<EditTextPreference android:key="saved_post" android:title="Post" android:dependency="save_a_post"></EditTextPreference>
</PreferenceCategory><PreferenceCategory
android:title="@string/services">
<Preference
android:title="Twitter Sign In"
android:title="@string/twitter_signin"
android:key="twitter_sign_in"/>
<Preference
android:title="Facebook Sign In"
android:title="@string/fb_signin"
android:key="facebook_sign_in"/>
</PreferenceCategory>
<PreferenceCategory
android:title="Location Settings">
android:title="@string/location_settings">
</PreferenceCategory><PreferenceCategory android:title="@string/about">
<com.TwentyCodes.android.TravelPost.TextViewPreference android:title="@string/hello"/>
</PreferenceCategory>
</PreferenceScreen>

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);