AccountAuthenticatorActivity.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. package com.owncloud.android.authentication;
  17. import android.accounts.AccountAuthenticatorResponse;
  18. import android.accounts.AccountManager;
  19. import android.os.Bundle;
  20. import android.support.v7.app.AppCompatActivity;
  21. /*
  22. * Base class for implementing an Activity that is used to help implement an AbstractAccountAuthenticator.
  23. * If the AbstractAccountAuthenticator needs to use an activity to handle the request then it can have the activity extend
  24. * AccountAuthenticatorActivity. The AbstractAccountAuthenticator passes in the response to the intent using the following:
  25. * intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
  26. *
  27. * The activity then sets the result that is to be handed to the response via setAccountAuthenticatorResult(android.os.Bundle).
  28. * This result will be sent as the result of the request when the activity finishes. If this is never set or if it is set to null
  29. * then error AccountManager.ERROR_CODE_CANCELED will be called on the response.
  30. */
  31. public class AccountAuthenticatorActivity extends AppCompatActivity {
  32. private AccountAuthenticatorResponse mAccountAuthenticatorResponse = null;
  33. private Bundle mResultBundle = null;
  34. /**
  35. * Set the result that is to be sent as the result of the request that caused this Activity to be launched.
  36. * If result is null or this method is never called then the request will be canceled.
  37. *
  38. * @param result this is returned as the result of the AbstractAccountAuthenticator request
  39. */
  40. public final void setAccountAuthenticatorResult(Bundle result) {
  41. mResultBundle = result;
  42. }
  43. /**
  44. * Retreives the AccountAuthenticatorResponse from either the intent of the icicle, if the
  45. * icicle is non-zero.
  46. * @param icicle the save instance data of this Activity, may be null
  47. */
  48. protected void onCreate(Bundle icicle) {
  49. super.onCreate(icicle);
  50. mAccountAuthenticatorResponse =
  51. getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
  52. if (mAccountAuthenticatorResponse != null) {
  53. mAccountAuthenticatorResponse.onRequestContinued();
  54. }
  55. }
  56. /**
  57. * Sends the result or a Constants.ERROR_CODE_CANCELED error if a result isn't present.
  58. */
  59. public void finish() {
  60. if (mAccountAuthenticatorResponse != null) {
  61. // send the result bundle back if set, otherwise send an error.
  62. if (mResultBundle != null) {
  63. mAccountAuthenticatorResponse.onResult(mResultBundle);
  64. } else {
  65. mAccountAuthenticatorResponse.onError(AccountManager.ERROR_CODE_CANCELED,
  66. "canceled");
  67. }
  68. mAccountAuthenticatorResponse = null;
  69. }
  70. super.finish();
  71. }
  72. }