Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add RoboSherlockAccountAuthenticatorActivity

AccountAuthenticatorActivity is a bit of a strange goose, doesn't do
much, but I'm sufficiently paranoid that I'm willing to jump through
this hoop.

wonder if I can do this better using Scala ;)
  • Loading branch information...
commit 1124b5808e425adf325afeb9985e8c5fcf0944f6 1 parent 8f60636
Roberto Tyley authored April 06, 2012
86  src/main/java/com/github/rtyley/android/sherlock/android/accounts/SherlockAccountAuthenticatorActivity.java
... ...
@@ -0,0 +1,86 @@
  1
+/*
  2
+ * Copyright (C) 2009 The Android Open Source Project
  3
+ *
  4
+ * Licensed under the Apache License, Version 2.0 (the "License");
  5
+ * you may not use this file except in compliance with the License.
  6
+ * You may obtain a copy of the License at
  7
+ *
  8
+ *      http://www.apache.org/licenses/LICENSE-2.0
  9
+ *
  10
+ * Unless required by applicable law or agreed to in writing, software
  11
+ * distributed under the License is distributed on an "AS IS" BASIS,
  12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13
+ * See the License for the specific language governing permissions and
  14
+ * limitations under the License.
  15
+ */
  16
+
  17
+package com.github.rtyley.android.sherlock.android.accounts;
  18
+
  19
+import android.accounts.AccountAuthenticatorResponse;
  20
+import android.accounts.AccountManager;
  21
+import android.app.Activity;
  22
+import android.content.Intent;
  23
+import android.os.Bundle;
  24
+import com.actionbarsherlock.app.SherlockActivity;
  25
+
  26
+/**
  27
+ * Base class for implementing an Activity that is used to help implement an
  28
+ * AbstractAccountAuthenticator. If the AbstractAccountAuthenticator needs to use an activity
  29
+ * to handle the request then it can have the activity extend SherlockAccountAuthenticatorActivity.
  30
+ * The AbstractAccountAuthenticator passes in the response to the intent using the following:
  31
+ * <pre>
  32
+ *      intent.putExtra({@link android.accounts.AccountManager#KEY_ACCOUNT_AUTHENTICATOR_RESPONSE}, response);
  33
+ * </pre>
  34
+ * The activity then sets the result that is to be handed to the response via
  35
+ * {@link #setAccountAuthenticatorResult(android.os.Bundle)}.
  36
+ * This result will be sent as the result of the request when the activity finishes. If this
  37
+ * is never set or if it is set to null then error {@link android.accounts.AccountManager#ERROR_CODE_CANCELED}
  38
+ * will be called on the response.
  39
+ */
  40
+public class SherlockAccountAuthenticatorActivity extends SherlockActivity {
  41
+    private AccountAuthenticatorResponse mAccountAuthenticatorResponse = null;
  42
+    private Bundle mResultBundle = null;
  43
+
  44
+    /**
  45
+     * Set the result that is to be sent as the result of the request that caused this
  46
+     * Activity to be launched. If result is null or this method is never called then
  47
+     * the request will be canceled.
  48
+     * @param result this is returned as the result of the AbstractAccountAuthenticator request
  49
+     */
  50
+    public final void setAccountAuthenticatorResult(Bundle result) {
  51
+        mResultBundle = result;
  52
+    }
  53
+
  54
+    /**
  55
+     * Retreives the AccountAuthenticatorResponse from either the intent of the icicle, if the
  56
+     * icicle is non-zero.
  57
+     * @param icicle the save instance data of this Activity, may be null
  58
+     */
  59
+    protected void onCreate(Bundle icicle) {
  60
+        super.onCreate(icicle);
  61
+
  62
+        mAccountAuthenticatorResponse =
  63
+                getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
  64
+
  65
+        if (mAccountAuthenticatorResponse != null) {
  66
+            mAccountAuthenticatorResponse.onRequestContinued();
  67
+        }
  68
+    }
  69
+
  70
+    /**
  71
+     * Sends the result or a Constants.ERROR_CODE_CANCELED error if a result isn't present.
  72
+     */
  73
+    public void finish() {
  74
+        if (mAccountAuthenticatorResponse != null) {
  75
+            // send the result bundle back if set, otherwise send an error.
  76
+            if (mResultBundle != null) {
  77
+                mAccountAuthenticatorResponse.onResult(mResultBundle);
  78
+            } else {
  79
+                mAccountAuthenticatorResponse.onError(AccountManager.ERROR_CODE_CANCELED,
  80
+                        "canceled");
  81
+            }
  82
+            mAccountAuthenticatorResponse = null;
  83
+        }
  84
+        super.finish();
  85
+    }
  86
+}
125  ...java/com/github/rtyley/android/sherlock/roboguice/activity/RoboSherlockAccountAuthenticatorActivity.java
... ...
@@ -0,0 +1,125 @@
  1
+/*
  2
+ * Copyright 2009 Michael Burton
  3
+ *
  4
+ * Licensed under the Apache License, Version 2.0 (the "License");
  5
+ * you may not use this file except in compliance with the License.
  6
+ * You may obtain a copy of the License at
  7
+ *
  8
+ *   http://www.apache.org/licenses/LICENSE-2.0
  9
+ *
  10
+ * Unless required by applicable law or agreed to in writing, software
  11
+ * distributed under the License is distributed on an "AS IS" BASIS,
  12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13
+ * See the License for the specific language governing permissions
  14
+ * and limitations under the License.
  15
+ */
  16
+// derived from RoboActivity.java
  17
+
  18
+package com.github.rtyley.android.sherlock.roboguice.activity;
  19
+
  20
+import com.github.rtyley.android.sherlock.android.accounts.SherlockAccountAuthenticatorActivity;
  21
+import roboguice.RoboGuice;
  22
+import roboguice.activity.event.*;
  23
+import roboguice.event.EventManager;
  24
+import roboguice.inject.ContentViewListener;
  25
+import roboguice.inject.RoboInjector;
  26
+
  27
+import android.accounts.AccountAuthenticatorActivity;
  28
+import android.content.Intent;
  29
+import android.content.res.Configuration;
  30
+import android.os.Bundle;
  31
+
  32
+import com.google.inject.Inject;
  33
+
  34
+/**
  35
+ * A subclass of {@link AccountAuthenticatorActivity} that provides dependency injection
  36
+ * with RoboGuice.
  37
+ *
  38
+ * @author Marcus Better
  39
+ */
  40
+public class RoboSherlockAccountAuthenticatorActivity extends SherlockAccountAuthenticatorActivity {
  41
+    protected EventManager eventManager;
  42
+
  43
+    @Inject ContentViewListener ignored; // BUG find a better place to put this
  44
+
  45
+    @Override
  46
+    protected void onCreate(Bundle savedInstanceState) {
  47
+        final RoboInjector injector = RoboGuice.getInjector(this);
  48
+        eventManager = injector.getInstance(EventManager.class);
  49
+        injector.injectMembersWithoutViews(this);
  50
+        super.onCreate(savedInstanceState);
  51
+        eventManager.fire(new OnCreateEvent(savedInstanceState));
  52
+    }
  53
+
  54
+    @Override
  55
+    protected void onRestart() {
  56
+        super.onRestart();
  57
+        eventManager.fire(new OnRestartEvent());
  58
+    }
  59
+
  60
+    @Override
  61
+    protected void onStart() {
  62
+        super.onStart();
  63
+        eventManager.fire(new OnStartEvent());
  64
+    }
  65
+
  66
+    @Override
  67
+    protected void onResume() {
  68
+        super.onResume();
  69
+        eventManager.fire(new OnResumeEvent());
  70
+    }
  71
+
  72
+    @Override
  73
+    protected void onPause() {
  74
+        super.onPause();
  75
+        eventManager.fire(new OnPauseEvent());
  76
+    }
  77
+
  78
+    @Override
  79
+    protected void onNewIntent( Intent intent ) {
  80
+        super.onNewIntent(intent);
  81
+        eventManager.fire(new OnNewIntentEvent());
  82
+    }
  83
+
  84
+    @Override
  85
+    protected void onStop() {
  86
+        try {
  87
+            eventManager.fire(new OnStopEvent());
  88
+        } finally {
  89
+            super.onStop();
  90
+        }
  91
+    }
  92
+
  93
+    @Override
  94
+    protected void onDestroy() {
  95
+        try {
  96
+            eventManager.fire(new OnDestroyEvent());
  97
+        } finally {
  98
+            try {
  99
+                RoboGuice.destroyInjector(this);
  100
+            } finally {
  101
+                super.onDestroy();
  102
+            }
  103
+        }
  104
+    }
  105
+
  106
+    @Override
  107
+    public void onConfigurationChanged(Configuration newConfig) {
  108
+        final Configuration currentConfig = getResources().getConfiguration();
  109
+        super.onConfigurationChanged(newConfig);
  110
+        eventManager.fire(new OnConfigurationChangedEvent(currentConfig, newConfig));
  111
+    }
  112
+
  113
+    @Override
  114
+    public void onContentChanged() {
  115
+        super.onContentChanged();
  116
+        RoboGuice.getInjector(this).injectViewMembers(this);
  117
+        eventManager.fire(new OnContentChangedEvent());
  118
+    }
  119
+
  120
+    @Override
  121
+    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  122
+        super.onActivityResult(requestCode, resultCode, data);
  123
+        eventManager.fire(new OnActivityResultEvent(requestCode, resultCode, data));
  124
+    }
  125
+}

0 notes on commit 1124b58

Please sign in to comment.
Something went wrong with that request. Please try again.