1 /* 2 * Copyright (C) 2012 Google Inc. 3 * Licensed to The Android Open Source Project. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.mail.providers; 19 20 import android.database.DataSetObserver; 21 22 import com.android.mail.ui.AccountController; 23 import com.android.mail.utils.LogTag; 24 import com.android.mail.utils.LogUtils; 25 26 /** 27 * A simple extension of {@link DataSetObserver} to provide the updated account in 28 * {@link #onChanged(Account)} when the account changes. Initializing the object registers with 29 * the observer with the {@link AccountController} provided. The object will then begin to 30 * receive {@link #onChanged(Account)} till {@link #unregisterAndDestroy()} is called. 31 * <p> 32 * To implement an {@link AccountObserver}, you need to implement the {@link #onChanged(Account)} 33 * method. 34 */ 35 public abstract class AccountObserver extends DataSetObserver { 36 /** 37 * The AccountController that the observer is registered with. 38 */ 39 private AccountController mController; 40 41 private static final String LOG_TAG = LogTag.getLogTag(); 42 43 /** 44 * The no-argument constructor leaves the object unusable till 45 * {@link #initialize(AccountController)} is called. 46 */ AccountObserver()47 public AccountObserver () { 48 } 49 50 /** 51 * Initializes an {@link AccountObserver} object that receives a call to 52 * {@link #onChanged(Account)} when the controller changes the account. 53 * 54 * @param controller 55 */ initialize(AccountController controller)56 public Account initialize(AccountController controller) { 57 if (controller == null) { 58 LogUtils.wtf(LOG_TAG, "AccountObserver initialized with null controller!"); 59 } 60 mController = controller; 61 mController.registerAccountObserver(this); 62 return mController.getAccount(); 63 } 64 65 @Override onChanged()66 public final void onChanged() { 67 if (mController == null) { 68 return; 69 } 70 onChanged(mController.getAccount()); 71 } 72 73 /** 74 * Callback invoked when the account object is changed. Since {@link Account} objects are 75 * immutable, updates can be received on changes to individual settings (sync on/off) 76 * in addition to changes of accounts: alice@example.com -> bob@example.com. 77 * The updated account is passed as the argument. 78 * @param newAccount 79 */ onChanged(Account newAccount)80 public abstract void onChanged(Account newAccount); 81 82 /** 83 * Return the most current account. 84 * @return 85 */ getAccount()86 public final Account getAccount() { 87 if (mController == null) { 88 return null; 89 } 90 return mController.getAccount(); 91 } 92 93 /** 94 * Unregisters for account changes and makes the object unusable. 95 */ unregisterAndDestroy()96 public void unregisterAndDestroy() { 97 if (mController == null) { 98 return; 99 } 100 mController.unregisterAccountObserver(this); 101 } 102 }