Skip to content

Instantly share code, notes, and snippets.

@loganj
Created May 11, 2010 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loganj/397719 to your computer and use it in GitHub Desktop.
Save loganj/397719 to your computer and use it in GitHub Desktop.
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..fe09c91
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+.hg
+main/res/values/credentials.xml
+main/src/com/joelapenna/foursquared/FoursquaredSettings.java
diff --git a/main/AndroidManifest.xml b/main/AndroidManifest.xml
index f4d409e..963fb7a 100644
--- a/main/AndroidManifest.xml
+++ b/main/AndroidManifest.xml
@@ -15,10 +15,17 @@
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
- android:name="android.permission.READ_CONTACTS" />
+ android:name="android.permission.READ_CONTACTS" />
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+ <uses-permission android:name="android.permission.WRITE_CONTACTS" />
+ <uses-permission android:name="android.permission.GET_ACCOUNTS" />
+ <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
+ <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
+ <uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
+ <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-sdk
android:minSdkVersion="3"
- android:targetSdkVersion="4" />
+ android:targetSdkVersion="5" />
<supports-screens
largeScreens="true"
normalScreens="true"
@@ -129,7 +136,8 @@
<activity
android:name=".LoginActivity"
- android:launchMode="singleInstance">
+ android:launchMode="singleInstance"
+ android:exported="true">
</activity>
@@ -382,6 +390,15 @@
<service
android:name=".app.FoursquaredService">
</service>
+
+ <service android:name=".AuthenticatorService"
+ android:exported="true" android:process=":auth">
+ <intent-filter>
+ <action android:name="android.accounts.AccountAuthenticator" />
+ </intent-filter>
+ <meta-data android:name="android.accounts.AccountAuthenticator"
+ android:resource="@xml/authenticator" />
+ </service>
</application>
</manifest>
diff --git a/main/build.xml b/main/build.xml
index 62442f0..511b12a 100644
--- a/main/build.xml
+++ b/main/build.xml
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
-<project name="Foursquare" default="help">
+<project name="Foursquare">
<!-- The local.properties file is created and updated by the 'android' tool.
It contain the path to the SDK. It should *NOT* be checked in in Version
diff --git a/main/default.properties b/main/default.properties
index 02bcff3..0386c5b 100644
--- a/main/default.properties
+++ b/main/default.properties
@@ -7,8 +7,8 @@
# "build.properties", and override values to adapt the script to your
# project structure.
-apk-configurations=
-# Project target.
-target=Google Inc.:Google APIs:4
# Indicates whether an apk should be generated for each density.
split.density=false
+# Project target.
+target=Google Inc.:Google APIs:5
+apk-configurations=
diff --git a/main/res/values/strings.xml b/main/res/values/strings.xml
index 839a3d3..cfcbd5e 100644
--- a/main/res/values/strings.xml
+++ b/main/res/values/strings.xml
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
+ <string name="account_type">org.joelapenna.foursquared.account</string>
<string name="add_a_tip">Add a Tip</string>
<string name="add_friends_activity_label">Foursquare Add Friends</string>
<string name="add_venue_activity_label">Add a New Venue</string>
@@ -22,7 +23,7 @@
<string name="checkins_label">Friends</string>
<string name="city">City</string>
<string name="cross_street">Cross Street</string>
- <string name="feedback_error">Couldn't find configured email client.</string>
+ <string name="feedback_error">Couldn\'t find configured email client.</string>
<string name="feedback_label">Send Feedback</string>
<string name="feedback_more">If you are reporting problem please answer the questions below:</string>
<string name="feedback_question_additional_information">Please provide any additional information below.</string>
@@ -88,11 +89,11 @@
<string name="preferences_logout_title">Logout</string>
<string name="preferences_other_settings">Other Settings</string>
<string name="preferences_share_checkin_title">Tell my friends</string>
- <string name="preferences_share_summary_off">By default, don't tell my friends when I check-in.</string>
+ <string name="preferences_share_summary_off">By default, don\'t tell my friends when I check-in.</string>
<string name="preferences_share_summary_on">By default, tell my friends when I check-in.</string>
<string name="preferences_startup_tab">Startup Tab</string>
<string name="preferences_startup_tab_summary">Select which tab to display at startup.</string>
- <string name="preferences_twitter_checkin_summary_off">By default, don't tweet my checkins.</string>
+ <string name="preferences_twitter_checkin_summary_off">By default, don\'t tweet my checkins.</string>
<string name="preferences_twitter_checkin_summary_on">By default, tweet my checkins.</string>
<string name="preferences_twitter_checkin_title">Tell Twitter</string>
<string name="preferences_unable_to_find_city_toast">Unable to update your city.</string>
diff --git a/main/res/xml/account_preferences.xml b/main/res/xml/account_preferences.xml
new file mode 100644
index 0000000..7c72e4f
--- /dev/null
+++ b/main/res/xml/account_preferences.xml
@@ -0,0 +1,4 @@
+<PreferenceScreen
+ xmlns:android="http://schemas.android.com/apk/res/android">
+
+</PreferenceScreen>
\ No newline at end of file
diff --git a/main/res/xml/authenticator.xml b/main/res/xml/authenticator.xml
new file mode 100644
index 0000000..b53ddcf
--- /dev/null
+++ b/main/res/xml/authenticator.xml
@@ -0,0 +1,5 @@
+<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
+ android:accountType="org.joelapenna.foursquared.account"
+ android:icon="@drawable/icon"
+ android:smallIcon="@drawable/icon"
+ android:label="@string/app_name"/>
\ No newline at end of file
diff --git a/main/src/com/joelapenna/foursquared/Authenticator.java b/main/src/com/joelapenna/foursquared/Authenticator.java
new file mode 100644
index 0000000..11fc06b
--- /dev/null
+++ b/main/src/com/joelapenna/foursquared/Authenticator.java
@@ -0,0 +1,77 @@
+/**
+ *
+ */
+package com.joelapenna.foursquared;
+
+import static com.joelapenna.foursquared.FoursquaredSettings.DEBUG;
+
+import android.accounts.AbstractAccountAuthenticator;
+import android.accounts.Account;
+import android.accounts.AccountAuthenticatorResponse;
+import android.accounts.AccountManager;
+import android.accounts.NetworkErrorException;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
+
+final class Authenticator extends AbstractAccountAuthenticator {
+ private static final String TAG = "AuthenticatorImpl";
+ final private Context mContext;
+
+ Authenticator(Context context) {
+ super(context);
+ mContext = context;
+ }
+
+ @Override
+ public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType,
+ String[] requiredFeatures, Bundle options) throws NetworkErrorException {
+ if ( DEBUG ) Log.d(TAG, "addAccount()");
+ final Intent intent = new Intent(mContext, LoginActivity.class);
+ intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
+ final Bundle reply = new Bundle();
+ reply.putParcelable(AccountManager.KEY_INTENT, intent);
+ return reply;
+ }
+
+ @Override
+ public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account,
+ Bundle options) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType,
+ Bundle options) throws NetworkErrorException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public String getAuthTokenLabel(String authTokenType) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Bundle hasFeatures(AccountAuthenticatorResponse arg0, Account arg1, String[] arg2)
+ throws NetworkErrorException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Bundle updateCredentials(AccountAuthenticatorResponse arg0, Account arg1,
+ String arg2, Bundle arg3) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+}
\ No newline at end of file
diff --git a/main/src/com/joelapenna/foursquared/AuthenticatorService.java b/main/src/com/joelapenna/foursquared/AuthenticatorService.java
new file mode 100644
index 0000000..cdd397f
--- /dev/null
+++ b/main/src/com/joelapenna/foursquared/AuthenticatorService.java
@@ -0,0 +1,33 @@
+package com.joelapenna.foursquared;
+
+import android.app.Service;
+import android.content.Intent;
+import android.os.IBinder;
+import android.util.Log;
+
+import static com.joelapenna.foursquared.FoursquaredSettings.DEBUG;
+
+
+public final class AuthenticatorService extends Service {
+
+ private static final String TAG = "AuthenticatorService";
+
+ private Authenticator mAuthenticator = null;
+
+ public IBinder onBind(Intent intent) {
+ if (DEBUG) Log.d(TAG, "onBind()");
+ if (intent.getAction().equals(android.accounts.AccountManager.ACTION_AUTHENTICATOR_INTENT)) {
+ IBinder binder = mAuthenticator.getIBinder();
+ if (DEBUG) Log.d(TAG, "returning binder " + binder);
+ return binder;
+ } else {
+ return null;
+ }
+ }
+
+ public void onCreate() {
+ if (DEBUG) Log.d(TAG, "onCreate()");
+ mAuthenticator = new Authenticator(this);
+ }
+
+}
diff --git a/main/src/com/joelapenna/foursquared/LoginActivity.java b/main/src/com/joelapenna/foursquared/LoginActivity.java
index 648e59d..6ddeb00 100644
--- a/main/src/com/joelapenna/foursquared/LoginActivity.java
+++ b/main/src/com/joelapenna/foursquared/LoginActivity.java
@@ -10,8 +10,13 @@ import com.joelapenna.foursquared.location.LocationUtils;
import com.joelapenna.foursquared.preferences.Preferences;
import com.joelapenna.foursquared.util.NotificationsUtil;
+import android.accounts.Account;
+import android.accounts.AccountAuthenticatorActivity;
+import android.accounts.AccountAuthenticatorResponse;
+import android.accounts.AccountManager;
import android.app.Activity;
import android.app.ProgressDialog;
+import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
@@ -34,8 +39,9 @@ import android.widget.Toast;
/**
* @author Joe LaPenna (joe@joelapenna.com)
*/
-public class LoginActivity extends Activity {
+public class LoginActivity extends AccountAuthenticatorActivity {
public static final String TAG = "LoginActivity";
+ public static final String LAUNCH_MAIN_WHEN_FINISHED = "LAUNCH_MAIN_WHEN_FINISHED";
public static final boolean DEBUG = FoursquaredSettings.DEBUG;
private AsyncTask<Void, Void, Boolean> mLoginTask;
@@ -64,7 +70,7 @@ public class LoginActivity extends Activity {
mLoginTask = (LoginTask) getLastNonConfigurationInstance();
if (mLoginTask != null && mLoginTask.isCancelled()) {
if (DEBUG) Log.d(TAG, "LoginTask previously cancelled, trying again.");
- mLoginTask = new LoginTask().execute();
+ mLoginTask = new LoginTask(this).execute();
}
}
@@ -115,7 +121,7 @@ public class LoginActivity extends Activity {
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
- mLoginTask = new LoginTask().execute();
+ mLoginTask = new LoginTask(LoginActivity.this).execute();
}
});
@@ -166,7 +172,12 @@ public class LoginActivity extends Activity {
private static final String TAG = "LoginTask";
private static final boolean DEBUG = FoursquaredSettings.DEBUG;
+ final private Context mContext;
private Exception mReason;
+
+ LoginTask(Context context) {
+ mContext = context;
+ }
@Override
protected void onPreExecute() {
@@ -199,6 +210,17 @@ public class LoginActivity extends Activity {
if (DEBUG) Log.d(TAG, "Preference store calls failed");
throw new FoursquareException(getResources().getString(
R.string.login_failed_login_toast));
+ } else {
+ Account account = new Account(userId, mContext.getString(R.string.account_type));
+ AccountManager am = AccountManager.get(mContext);
+ boolean accountCreated = am.addAccountExplicitly(account, password, null);
+ Bundle extras = getIntent().getExtras();
+ if ( extras != null && accountCreated ) {
+ Bundle result = new Bundle();
+ result.putString(AccountManager.KEY_ACCOUNT_NAME, phoneNumber);
+ result.putString(AccountManager.KEY_ACCOUNT_TYPE, getString(R.string.account_type));
+ setAccountAuthenticatorResult(result);
+ }
}
return loggedIn;
@@ -224,10 +246,12 @@ public class LoginActivity extends Activity {
// Launch the service to update any widgets, etc.
foursquared.requestStartService();
+ if ( getIntent().getBooleanExtra(LAUNCH_MAIN_WHEN_FINISHED, false) ) {
// Launch the main activity to let the user do anything.
- Intent intent = new Intent(LoginActivity.this, MainActivity.class);
- intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- startActivity(intent);
+ Intent intent = new Intent(LoginActivity.this, MainActivity.class);
+ intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ startActivity(intent);
+ }
// Be done with the activity.
finish();
diff --git a/main/src/com/joelapenna/foursquared/MainActivity.java b/main/src/com/joelapenna/foursquared/MainActivity.java
index 4e5aea8..b9b1709 100644
--- a/main/src/com/joelapenna/foursquared/MainActivity.java
+++ b/main/src/com/joelapenna/foursquared/MainActivity.java
@@ -150,6 +150,7 @@ public class MainActivity extends TabActivity {
setVisible(false);
Intent intent = new Intent(this, LoginActivity.class);
intent.setAction(Intent.ACTION_MAIN);
+ intent.putExtra(LoginActivity.LAUNCH_MAIN_WHEN_FINISHED, true);
intent.setFlags(
Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment