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         Store store = sStores.get(hostAuth);
     92         if (store == null) {
     93             Context appContext = context.getApplicationContext();
     94             Class<? extends Store> klass = sStoreClasses.get(hostAuth.mProtocol);
     95             try {
     96                 // invoke "newInstance" class method
     97                 Method m = klass.getMethod("newInstance", Account.class, Context.class);
     98                 store = (Store)m.invoke(null, account, appContext);
     99             } catch (Exception e) {
    100                 Log.d(Logging.LOG_TAG, String.format(
    101                         "exception %s invoking method %s#newInstance(Account, Context) for %s",
    102                         e.toString(), klass.getName(), account.mDisplayName));
    103                 throw new MessagingException("Can't instantiate Store for " + account.mDisplayName);
    104             }
    105             // Don't cache this unless it's we've got a saved HostAuth
    106             if (hostAuth.mId != EmailContent.NOT_SAVED) {
    107                 sStores.put(hostAuth, store);
    108             }
    109         }
    110         return store;
    111     }
    112 
    113     /**
    114      * Delete the mail store associated with the given account. The account must be valid (i.e. has
    115      * at least an incoming server name).
    116      *
    117      * The store should have been notified already by calling delete(), and the caller should
    118      * also take responsibility for deleting the matching LocalStore, etc.
    119      *
    120      * @throws MessagingException If the store cannot be removed or if the account is invalid.
    121      */
    122     public synchronized static Store removeInstance(Account account, Context context)
    123             throws MessagingException {
    124         return sStores.remove(HostAuth.restoreHostAuthWithId(context, account.mHostAuthKeyRecv));
    125     }
    126 
    127     /**
    128      * Get class of SettingActivity for this Store class.
    129      * @return Activity class that has class method actionEditIncomingSettings().
    130      */
    131     public Class<? extends android.app.Activity> getSettingActivityClass() {
    132         // default SettingActivity class
    133         return com.android.email.activity.setup.AccountSetupIncoming.class;
    134     }
    135 
    136     /**
    137      * Some protocols require that a sent message be copied (uploaded) into the Sent folder
    138      * while others can take care of it automatically (ideally, on the server).  This function
    139      * allows a given store to indicate which mode(s) it supports.
    140      * @return true if the store requires an upload into "sent", false if this happens automatically
    141      * for any sent message.
    142      */
    143     public boolean requireCopyMessageToSentFolder() {
    144         return true;
    145     }
    146 
    147     public Folder getFolder(String name) throws MessagingException {
    148         return null;
    149     }
    150 
    151     /**
    152      * Updates the local list of mailboxes according to what is located on the remote server.
    153      * <em>Note: This does not perform folder synchronization and it will not remove mailboxes
    154      * that are stored locally but not remotely.</em>
    155      * @return The set of remote folders
    156      * @throws MessagingException If there was a problem connecting to the remote server
    157      */
    158     public Folder[] updateFolders() throws MessagingException {
    159         return null;
    160     }
    161 
    162     public abstract Bundle checkSettings() throws MessagingException;
    163 
    164     /**
    165      * Handle discovery of account settings using only the user's email address and password
    166      * @param context the context of the caller
    167      * @param emailAddress the email address of the exchange user
    168      * @param password the password of the exchange user
    169      * @return a Bundle containing an error code and a HostAuth (if successful)
    170      * @throws MessagingException
    171      */
    172     public Bundle autoDiscover(Context context, String emailAddress, String password)
    173             throws MessagingException {
    174         return null;
    175     }
    176 
    177     /**
    178      * Updates the fields within the given mailbox. Only the fields that are important to
    179      * non-EAS accounts are modified.
    180      */
    181     protected static void updateMailbox(Mailbox mailbox, long accountId, String mailboxPath,
    182             char delimiter, boolean selectable, int type) {
    183         mailbox.mAccountKey = accountId;
    184         mailbox.mDelimiter = delimiter;
    185         String displayPath = mailboxPath;
    186         int pathIndex = mailboxPath.lastIndexOf(delimiter);
    187         if (pathIndex > 0) {
    188             displayPath = mailboxPath.substring(pathIndex + 1);
    189         }
    190         mailbox.mDisplayName = displayPath;
    191         if (selectable) {
    192             mailbox.mFlags = Mailbox.FLAG_HOLDS_MAIL | Mailbox.FLAG_ACCEPTS_MOVED_MAIL;
    193         }
    194         mailbox.mFlagVisible = true;
    195         //mailbox.mParentKey;
    196         //mailbox.mParentServerId;
    197         mailbox.mServerId = mailboxPath;
    198         //mailbox.mServerId;
    199         //mailbox.mSyncFrequency;
    200         //mailbox.mSyncKey;
    201         //mailbox.mSyncLookback;
    202         //mailbox.mSyncTime;
    203         mailbox.mType = type;
    204         //box.mUnreadCount;
    205         mailbox.mVisibleLimit = Email.VISIBLE_LIMIT_DEFAULT;
    206     }
    207 }
    208