Handle auth error on registration.

This commit is contained in:
burke.davey
2010-07-30 23:19:01 +00:00
parent 006a2e9bb7
commit 573d7e33a3
3 changed files with 36 additions and 23 deletions

View File

@@ -10,7 +10,8 @@
<string name="connecting_text">Connecting...</string> <string name="connecting_text">Connecting...</string>
<string name="disconnecting_text">Disconnecting...</string> <string name="disconnecting_text">Disconnecting...</string>
<string name="connected_text">Your phone is now connected to Chrome.</string> <string name="connected_text">Your phone is now connected to Chrome.</string>
<string name="connect_error_text">Error! Unable to establish connection.</string> <string name="connect_error_text">Error: Unable to connect.</string>
<string name="auth_error_text">Error: Google Apps accounts not supported.</string>
<string name="finish">Finish</string> <string name="finish">Finish</string>
<string name="select_link_action_text">Please set your link action preference:</string> <string name="select_link_action_text">Please set your link action preference:</string>
<string name="auto_launch_text">Automatically launch links</string> <string name="auto_launch_text">Automatically launch links</string>
@@ -18,7 +19,7 @@
<string name="connected_with_account_text">Your phone is connected to Chrome using this account:</string> <string name="connected_with_account_text">Your phone is connected to Chrome using this account:</string>
<string name="link_action_pref_text">Link action preference:</string> <string name="link_action_pref_text">Link action preference:</string>
<string name="disconnect">Disconnect phone</string> <string name="disconnect">Disconnect phone</string>
<string name="disconnect_error_text">Error! Unable to disconnect.</string> <string name="disconnect_error_text">Error: Unable to disconnect.</string>
<string name="help">Help</string> <string name="help">Help</string>
<string name="intro_text"> <string name="intro_text">
Chrome to Phone lets you easily share links, maps, and selected phone numbers and text Chrome to Phone lets you easily share links, maps, and selected phone numbers and text

View File

@@ -49,6 +49,12 @@ import android.util.Log;
* Will pass the registration id and user, authenticating with app engine. * Will pass the registration id and user, authenticating with app engine.
*/ */
public class DeviceRegistrar { public class DeviceRegistrar {
public static final String STATUS_EXTRA = "Status";
public static final int REGISTERED_STATUS = 1;
public static final int AUTH_ERROR_STATUS = 2;
public static final int UNREGISTERED_STATUS = 3;
public static final int ERROR_STATUS = 4;
private static final String TAG = "DeviceRegistrar"; private static final String TAG = "DeviceRegistrar";
static final String SENDER_ID = "stp.chrome@gmail.com"; static final String SENDER_ID = "stp.chrome@gmail.com";
static final String BASE_URL = "https://chrometophone.appspot.com"; static final String BASE_URL = "https://chrometophone.appspot.com";
@@ -64,6 +70,7 @@ public class DeviceRegistrar {
final String deviceRegistrationID) { final String deviceRegistrationID) {
new Thread(new Runnable() { new Thread(new Runnable() {
public void run() { public void run() {
Intent updateUIIntent = new Intent("com.google.ctp.UPDATE_UI");
try { try {
HttpResponse res = makeRequest(context, deviceRegistrationID, REGISTER_URL); HttpResponse res = makeRequest(context, deviceRegistrationID, REGISTER_URL);
if (res.getStatusLine().getStatusCode() == 200) { if (res.getStatusLine().getStatusCode() == 200) {
@@ -71,16 +78,21 @@ public class DeviceRegistrar {
SharedPreferences.Editor editor = settings.edit(); SharedPreferences.Editor editor = settings.edit();
editor.putString("deviceRegistrationID", deviceRegistrationID); editor.putString("deviceRegistrationID", deviceRegistrationID);
editor.commit(); editor.commit();
updateUIIntent.putExtra(STATUS_EXTRA, REGISTERED_STATUS);
} else if (res.getStatusLine().getStatusCode() == 400) {
updateUIIntent.putExtra(STATUS_EXTRA, AUTH_ERROR_STATUS);
} else { } else {
Log.w(TAG, "Registration error " + Log.w(TAG, "Registration error " +
String.valueOf(res.getStatusLine().getStatusCode())); String.valueOf(res.getStatusLine().getStatusCode()));
updateUIIntent.putExtra(STATUS_EXTRA, ERROR_STATUS);
} }
context.sendBroadcast(new Intent("com.google.ctp.UPDATE_UI")); context.sendBroadcast(updateUIIntent);
} catch (PendingAuthException e) { } catch (PendingAuthException e) {
// Ignore - we'll reregister later // Ignore - we'll reregister later
} catch (Exception e) { } catch (Exception e) {
Log.w(TAG, "Registration error " + e.getMessage()); Log.w(TAG, "Registration error " + e.getMessage());
context.sendBroadcast(new Intent("com.google.ctp.UPDATE_UI")); updateUIIntent.putExtra(STATUS_EXTRA, ERROR_STATUS);
context.sendBroadcast(updateUIIntent);
} }
} }
}).start(); }).start();
@@ -88,6 +100,7 @@ public class DeviceRegistrar {
public static void unregisterWithServer(final Context context, public static void unregisterWithServer(final Context context,
final String deviceRegistrationID) { final String deviceRegistrationID) {
Intent updateUIIntent = new Intent("com.google.ctp.UPDATE_UI");
try { try {
HttpResponse res = makeRequest(context, deviceRegistrationID, UNREGISTER_URL); HttpResponse res = makeRequest(context, deviceRegistrationID, UNREGISTER_URL);
if (res.getStatusLine().getStatusCode() == 200) { if (res.getStatusLine().getStatusCode() == 200) {
@@ -95,16 +108,19 @@ public class DeviceRegistrar {
SharedPreferences.Editor editor = settings.edit(); SharedPreferences.Editor editor = settings.edit();
editor.remove("deviceRegistrationID"); editor.remove("deviceRegistrationID");
editor.commit(); editor.commit();
updateUIIntent.putExtra(STATUS_EXTRA, UNREGISTERED_STATUS);
} else { } else {
Log.w(TAG, "Unregistration error " + Log.w(TAG, "Unregistration error " +
String.valueOf(res.getStatusLine().getStatusCode())); String.valueOf(res.getStatusLine().getStatusCode()));
updateUIIntent.putExtra(STATUS_EXTRA, ERROR_STATUS);
} }
} catch (Exception e) { } catch (Exception e) {
updateUIIntent.putExtra(STATUS_EXTRA, ERROR_STATUS);
Log.w(TAG, "Unegistration error " + e.getMessage()); Log.w(TAG, "Unegistration error " + e.getMessage());
} }
// Update dialog activity // Update dialog activity
context.sendBroadcast(new Intent("com.google.ctp.UPDATE_UI")); context.sendBroadcast(updateUIIntent);
} }
private static HttpResponse makeRequest(Context context, String deviceRegistrationID, private static HttpResponse makeRequest(Context context, String deviceRegistrationID,

View File

@@ -289,34 +289,28 @@ public class MainActivity extends Activity {
return result; return result;
} }
private void handleConnectingUpdate() { private void handleConnectingUpdate(int status) {
SharedPreferences prefs = Prefs.get(this); if (status == DeviceRegistrar.REGISTERED_STATUS) {
String deviceRegistrationID = prefs.getString("deviceRegistrationID", null); setScreenContent(R.layout.select_launch_mode);
} else {
if (deviceRegistrationID == null) { // registration error
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar); ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
progressBar.setVisibility(ProgressBar.INVISIBLE); progressBar.setVisibility(ProgressBar.INVISIBLE);
TextView textView = (TextView) findViewById(R.id.connecting_text); TextView textView = (TextView) findViewById(R.id.connecting_text);
textView.setText(R.string.connect_error_text); textView.setText(status == DeviceRegistrar.AUTH_ERROR_STATUS ? R.string.auth_error_text :
R.string.connect_error_text);
Button prevButton = (Button) findViewById(R.id.prev); Button prevButton = (Button) findViewById(R.id.prev);
prevButton.setEnabled(true); prevButton.setEnabled(true);
Button nextButton = (Button) findViewById(R.id.prev); Button nextButton = (Button) findViewById(R.id.next);
nextButton.setEnabled(true); nextButton.setEnabled(true);
} else { // registered successfully
setScreenContent(R.layout.select_launch_mode);
} }
} }
private void handleDisconnectingUpdate() { private void handleDisconnectingUpdate(int status) {
SharedPreferences prefs = Prefs.get(this); if (status == DeviceRegistrar.UNREGISTERED_STATUS) {
String deviceRegistrationID = prefs.getString("deviceRegistrationID", null);
if (deviceRegistrationID == null) { // unregistered successfully
setScreenContent(R.layout.intro); setScreenContent(R.layout.intro);
} else { // unregistration error } else {
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar); ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
progressBar.setVisibility(ProgressBar.INVISIBLE); progressBar.setVisibility(ProgressBar.INVISIBLE);
@@ -332,9 +326,11 @@ public class MainActivity extends Activity {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (mScreenId == R.layout.select_account) { if (mScreenId == R.layout.select_account) {
handleConnectingUpdate(); handleConnectingUpdate(intent.getIntExtra(
DeviceRegistrar.STATUS_EXTRA, DeviceRegistrar.ERROR_STATUS));
} else if (mScreenId == R.layout.connected) { } else if (mScreenId == R.layout.connected) {
handleDisconnectingUpdate(); handleDisconnectingUpdate(intent.getIntExtra(
DeviceRegistrar.STATUS_EXTRA, DeviceRegistrar.ERROR_STATUS));
} }
} }
}; };