Home | History | Annotate | Download | only in mail
      1 /*
      2  * Copyright (C) 2008 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.android.email.mail;
     18 
     19 import android.content.Context;
     20 import android.os.Bundle;
     21 import android.util.Log;
     22 
     23 import com.android.email.Email;
     24 import com.android.email.mail.store.ExchangeStore;
     25 import com.android.email.mail.store.ImapStore;
     26 import com.android.email.mail.store.Pop3Store;
     27 import com.android.emailcommon.Logging;
     28 import com.android.emailcommon.mail.Folder;
     29 import com.android.emailcommon.mail.MessagingException;
     30 import com.android.emailcommon.provider.Account;
     31 import com.android.emailcommon.provider.EmailContent;
     32 import com.android.emailcommon.provider.HostAuth;
     33 import com.android.emailcommon.provider.Mailbox;
     34 import com.google.common.annotations.VisibleForTesting;
     35 
     36 import java.lang.reflect.Method;
     37 import java.util.HashMap;
     38 
     39 /**
     40  * Store is the legacy equivalent of the Account class
     41  */
     42 public abstract class Store {
     43     /**
     44      * A global suggestion to Store implementors on how much of the body
     45      * should be returned on FetchProfile.Item.BODY_SANE requests.
     46      */
     47     public static final int FETCH_BODY_SANE_SUGGESTED_SIZE = (50 * 1024);
     48 
     49     @VisibleForTesting
     50     static final HashMap<HostAuth, Store> sStores = new HashMap<HostAuth, Store>();
     51     protected Context mContext;
     52     protected Account mAccount;
     53     protected Transport mTransport;
     54     protected String mUsername;
     55     protected String mPassword;
     56 
     57     static final HashMap<String, Class<? extends Store>> sStoreClasses =
     58         new HashMap<String, Class<? extends Store>>();
     59 
     60     static {
     61         sStoreClasses.put(HostAuth.SCHEME_EAS, ExchangeStore.class);
     62         sStoreClasses.put(HostAuth.SCHEME_IMAP, ImapStore.class);
     63         sStoreClasses.put(HostAuth.SCHEME_POP3, Pop3Store.class);
     64     }
     65 
     66     /**
     67      * Static named constructor.  It should be overrode by extending class.
     68      * Because this method will be called through reflection, it can not be protected.
     69      */
     70     static Store newInstance(Account account, Context context) throws MessagingException {
     71         throw new MessagingException("Store#newInstance: Unknown scheme in "
     72                 + account.mDisplayName);
     73     }
     74 
     75     /**
     76      * Get an instance of a mail store for the given account. The account must be valid (i.e. has
     77      * at least an incoming server name).
     78      *
     79      * NOTE: The internal algorithm used to find a cached store depends upon the account's
     80      * HostAuth row. If this ever changes (e.g. such as the user updating the
     81      * host name or port), we will leak entries. This should not be typical, so, it is not
     82      * a critical problem. However, it is something we should consider fixing.
     83      *
     84      * @param account The account of the store.
     85      * @return an initialized store of the appropriate class
     86      * @throws MessagingException If the store cannot be obtained or if the account is invalid.
     87      */
     88     public synchronized static Store getInstance(Account account, Context context)
     89             throws MessagingException {
     90         HostAuth hostAuth = account.getOrCreateHostAuthRecv(context);
     91         // An existing account might have been deleted
     92         if (hostAuth == null) return null;
     93         Store store = sStores.get(hostAuth);
     94         if (store == null) {
     95             Context appContext = context.getApplicationContext();
     96             Class<? extends Store> klass = sStoreClasses.get(hostAuth.mProtocol);
     97             try {
     98                 // invoke "newInstance" class method
     99                 Method m = klass.getMethod("newInstance", Account.class, Context.class);
    100                 store = (Store)m.invoke(null, account, appContext);
    101             } catch (Exception e) {
    102                 Log.d(Logging.LOG_TAG, String.format(
    103                         "exception %s invoking method %s#newInstance(Account, Context) for %s",
    104                         e.toString(), klass.getName(), account.mDisplayName));
    105                 throw new MessagingException("Can't instantiate Store for " + account.mDisplayName);
    106             }
    107             // Don't cache this unless it's we've got a saved HostAuth
    108             if (hostAuth.mId != EmailContent.NOT_SAVED) {
    109                 sStores.put(hostAuth, store);
    110             }
    111         }
    112         return store;
    113     }
    114 
    115     /**
    116      * Delete the mail store associated with the given account. The account must be valid (i.e. has
    117      * at least an incoming server name).
    118      *
    119      * The store should have been notified already by calling delete(), and the caller should
    120      * also take responsibility for deleting the matching LocalStore, etc.
    121      *
    122      * @throws MessagingException If the store cannot be removed or if the account is invalid.
    123      */
    124     public synchronized static Store removeInstance(Account account, Context context)
    125             throws MessagingException {
    126         return sStores.remove(HostAuth.restoreHostAuthWithId(context, account.mHostAuthKeyRecv));
    127     }
    128 
    129     /**
    130      * Get class of SettingActivity for this Store class.
    131      * @return Activity class that has class method actionEditIncomingSettings().
    132      */
    133     public Class<? extends android.app.Activity> getSettingActivityClass() {
    134         // default SettingActivity class
    135         return com.android.email.activity.setup.AccountSetupIncoming.class;
    136     }
    137 
    138     /**
    139      * Some protocols require that a sent message be copied (uploaded) into the Sent folder
    140      * while others can take care of it automatically (ideally, on the server).  This function
    141      * allows a given store to indicate which mode(s) it supports.
    142      * @return true if the store requires an upload into "sent", false if this happens automatically
    143      * for any sent message.
    144      */
    145     public boolean requireCopyMessageToSentFolder() {
    146         return true;
    147     }
    148 
    149     public Folder getFolder(String name) throws MessagingException {
    150         return null;
    151     }
    152 
    153     /**
    154      * Updates the local list of mailboxes according to what is located on the remote server.
    155      * <em>Note: This does not perform folder synchronization and it will not remove mailboxes
    156      * that are stored locally but not remotely.</em>
    157      * @return The set of remote folders
    158      * @throws MessagingException If there was a problem connecting to the remote server
    159      */
    160     public Folder[] updateFolders() throws MessagingException {
    161         return null;
    162     }
    163 
    164     public abstract Bundle checkSettings() throws MessagingException;
    165 
    166     /**
    167      * Handle discovery of account settings using only the user's email address and password
    168      * @param context the context of the caller
    169      * @param emailAddress the email address of the exchange user
    170      * @param password the password of the exchange user
    171      * @return a Bundle containing an error code and a HostAuth (if successful)
    172      * @throws MessagingException
    173      */
    174     public Bundle autoDiscover(Context context, String emailAddress, String password)
    175             throws MessagingException {
    176         return null;
    177     }
    178 
    179     /**
    180      * Updates the fields within the given mailbox. Only the fields that are important to
    181      * non-EAS accounts are modified.
    182      */
    183     protected static void updateMailbox(Mailbox mailbox, long accountId, String mailboxPath,
    184             char delimiter, boolean selectable, int type) {
    185         mailbox.mAccountKey = accountId;
    186         mailbox.mDelimiter = delimiter;
    187         String displayPath = mailboxPath;
    188         int pathIndex = mailboxPath.lastIndexOf(delimiter);
    189         if (pathIndex > 0) {
    190             displayPath = mailboxPath.substring(pathIndex + 1);
    191         }
    192         mailbox.mDisplayName = displayPath;
    193         if (selectable) {
    194             mailbox.mFlags = Mailbox.FLAG_HOLDS_MAIL | Mailbox.FLAG_ACCEPTS_MOVED_MAIL;
    195         }
    196         mailbox.mFlagVisible = true;
    197         //mailbox.mParentKey;
    198         //mailbox.mParentServerId;
    199         mailbox.mServerId = mailboxPath;
    200         //mailbox.mServerId;
    201         //mailbox.mSyncFrequency;
    202         //mailbox.mSyncKey;
    203         //mailbox.mSyncLookback;
    204         //mailbox.mSyncTime;
    205         mailbox.mType = type;
    206         //box.mUnreadCount;
    207         mailbox.mVisibleLimit = Email.VISIBLE_LIMIT_DEFAULT;
    208     }
    209 }
    210