Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2013 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.ui;
     19 
     20 import android.app.LoaderManager;
     21 import android.content.Context;
     22 import android.content.Loader;
     23 import android.database.DataSetObservable;
     24 import android.database.DataSetObserver;
     25 import android.os.Bundle;
     26 
     27 import com.android.mail.ContactInfo;
     28 import com.android.mail.ContactInfoSource;
     29 import com.android.mail.SenderInfoLoader;
     30 import com.google.common.collect.ImmutableMap;
     31 
     32 import java.util.Set;
     33 
     34 /**
     35  * Asynchronously loads contact data for all senders in the conversation,
     36  * and notifies observers when the data is ready.
     37  */
     38 public class ContactLoaderCallbacks implements ContactInfoSource,
     39         LoaderManager.LoaderCallbacks<ImmutableMap<String, ContactInfo>> {
     40 
     41     private Set<String> mSenders;
     42     private ImmutableMap<String, ContactInfo> mContactInfoMap;
     43     private DataSetObservable mObservable = new DataSetObservable();
     44 
     45     private Context mContext;
     46 
     47     public ContactLoaderCallbacks(Context context) {
     48         mContext = context;
     49     }
     50 
     51     public void setSenders(Set<String> emailAddresses) {
     52         mSenders = emailAddresses;
     53     }
     54 
     55     @Override
     56     public Loader<ImmutableMap<String, ContactInfo>> onCreateLoader(int id, Bundle args) {
     57         return new SenderInfoLoader(mContext, mSenders);
     58     }
     59 
     60     @Override
     61     public void onLoadFinished(Loader<ImmutableMap<String, ContactInfo>> loader,
     62             ImmutableMap<String, ContactInfo> data) {
     63         mContactInfoMap = data;
     64         mObservable.notifyChanged();
     65     }
     66 
     67     @Override
     68     public void onLoaderReset(Loader<ImmutableMap<String, ContactInfo>> loader) {
     69     }
     70 
     71     @Override
     72     public ContactInfo getContactInfo(String email) {
     73         if (mContactInfoMap == null) {
     74             return null;
     75         }
     76         return mContactInfoMap.get(email);
     77     }
     78 
     79     @Override
     80     public void registerObserver(DataSetObserver observer) {
     81         mObservable.registerObserver(observer);
     82     }
     83 
     84     @Override
     85     public void unregisterObserver(DataSetObserver observer) {
     86         mObservable.unregisterObserver(observer);
     87     }
     88 
     89     protected Context getContext() {
     90         return mContext;
     91     }
     92 
     93     protected Set<String> getSenders() {
     94         return mSenders;
     95     }
     96 }
     97