mirror of
https://github.com/fergalmoran/chrometophone.git
synced 2025-12-22 09:41:51 +00:00
Handle auth error on registration.
This commit is contained in:
@@ -10,7 +10,8 @@
|
||||
<string name="connecting_text">Connecting...</string>
|
||||
<string name="disconnecting_text">Disconnecting...</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="select_link_action_text">Please set your link action preference:</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="link_action_pref_text">Link action preference:</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="intro_text">
|
||||
Chrome to Phone lets you easily share links, maps, and selected phone numbers and text
|
||||
|
||||
@@ -49,6 +49,12 @@ import android.util.Log;
|
||||
* Will pass the registration id and user, authenticating with app engine.
|
||||
*/
|
||||
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";
|
||||
static final String SENDER_ID = "stp.chrome@gmail.com";
|
||||
static final String BASE_URL = "https://chrometophone.appspot.com";
|
||||
@@ -64,6 +70,7 @@ public class DeviceRegistrar {
|
||||
final String deviceRegistrationID) {
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
Intent updateUIIntent = new Intent("com.google.ctp.UPDATE_UI");
|
||||
try {
|
||||
HttpResponse res = makeRequest(context, deviceRegistrationID, REGISTER_URL);
|
||||
if (res.getStatusLine().getStatusCode() == 200) {
|
||||
@@ -71,16 +78,21 @@ public class DeviceRegistrar {
|
||||
SharedPreferences.Editor editor = settings.edit();
|
||||
editor.putString("deviceRegistrationID", deviceRegistrationID);
|
||||
editor.commit();
|
||||
updateUIIntent.putExtra(STATUS_EXTRA, REGISTERED_STATUS);
|
||||
} else if (res.getStatusLine().getStatusCode() == 400) {
|
||||
updateUIIntent.putExtra(STATUS_EXTRA, AUTH_ERROR_STATUS);
|
||||
} else {
|
||||
Log.w(TAG, "Registration error " +
|
||||
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) {
|
||||
// Ignore - we'll reregister later
|
||||
} catch (Exception e) {
|
||||
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();
|
||||
@@ -88,6 +100,7 @@ public class DeviceRegistrar {
|
||||
|
||||
public static void unregisterWithServer(final Context context,
|
||||
final String deviceRegistrationID) {
|
||||
Intent updateUIIntent = new Intent("com.google.ctp.UPDATE_UI");
|
||||
try {
|
||||
HttpResponse res = makeRequest(context, deviceRegistrationID, UNREGISTER_URL);
|
||||
if (res.getStatusLine().getStatusCode() == 200) {
|
||||
@@ -95,16 +108,19 @@ public class DeviceRegistrar {
|
||||
SharedPreferences.Editor editor = settings.edit();
|
||||
editor.remove("deviceRegistrationID");
|
||||
editor.commit();
|
||||
updateUIIntent.putExtra(STATUS_EXTRA, UNREGISTERED_STATUS);
|
||||
} else {
|
||||
Log.w(TAG, "Unregistration error " +
|
||||
String.valueOf(res.getStatusLine().getStatusCode()));
|
||||
updateUIIntent.putExtra(STATUS_EXTRA, ERROR_STATUS);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
updateUIIntent.putExtra(STATUS_EXTRA, ERROR_STATUS);
|
||||
Log.w(TAG, "Unegistration error " + e.getMessage());
|
||||
}
|
||||
|
||||
// Update dialog activity
|
||||
context.sendBroadcast(new Intent("com.google.ctp.UPDATE_UI"));
|
||||
context.sendBroadcast(updateUIIntent);
|
||||
}
|
||||
|
||||
private static HttpResponse makeRequest(Context context, String deviceRegistrationID,
|
||||
|
||||
@@ -289,34 +289,28 @@ public class MainActivity extends Activity {
|
||||
return result;
|
||||
}
|
||||
|
||||
private void handleConnectingUpdate() {
|
||||
SharedPreferences prefs = Prefs.get(this);
|
||||
String deviceRegistrationID = prefs.getString("deviceRegistrationID", null);
|
||||
|
||||
if (deviceRegistrationID == null) { // registration error
|
||||
private void handleConnectingUpdate(int status) {
|
||||
if (status == DeviceRegistrar.REGISTERED_STATUS) {
|
||||
setScreenContent(R.layout.select_launch_mode);
|
||||
} else {
|
||||
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
|
||||
progressBar.setVisibility(ProgressBar.INVISIBLE);
|
||||
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);
|
||||
prevButton.setEnabled(true);
|
||||
|
||||
Button nextButton = (Button) findViewById(R.id.prev);
|
||||
Button nextButton = (Button) findViewById(R.id.next);
|
||||
nextButton.setEnabled(true);
|
||||
|
||||
} else { // registered successfully
|
||||
setScreenContent(R.layout.select_launch_mode);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleDisconnectingUpdate() {
|
||||
SharedPreferences prefs = Prefs.get(this);
|
||||
String deviceRegistrationID = prefs.getString("deviceRegistrationID", null);
|
||||
|
||||
if (deviceRegistrationID == null) { // unregistered successfully
|
||||
private void handleDisconnectingUpdate(int status) {
|
||||
if (status == DeviceRegistrar.UNREGISTERED_STATUS) {
|
||||
setScreenContent(R.layout.intro);
|
||||
} else { // unregistration error
|
||||
} else {
|
||||
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
|
||||
progressBar.setVisibility(ProgressBar.INVISIBLE);
|
||||
|
||||
@@ -332,9 +326,11 @@ public class MainActivity extends Activity {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (mScreenId == R.layout.select_account) {
|
||||
handleConnectingUpdate();
|
||||
handleConnectingUpdate(intent.getIntExtra(
|
||||
DeviceRegistrar.STATUS_EXTRA, DeviceRegistrar.ERROR_STATUS));
|
||||
} else if (mScreenId == R.layout.connected) {
|
||||
handleDisconnectingUpdate();
|
||||
handleDisconnectingUpdate(intent.getIntExtra(
|
||||
DeviceRegistrar.STATUS_EXTRA, DeviceRegistrar.ERROR_STATUS));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user