Store.java revision 100867a2317c7c40d362ce9db982ee2875d3f448
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 17package com.android.email.mail; 18 19import com.android.email.Email; 20import com.android.email.R; 21 22import org.xmlpull.v1.XmlPullParserException; 23 24import android.content.Context; 25import android.content.res.XmlResourceParser; 26import android.util.Log; 27 28import java.io.IOException; 29 30/** 31 * Store is the access point for an email message store. It's location can be 32 * local or remote and no specific protocol is defined. Store is intended to 33 * loosely model in combination the JavaMail classes javax.mail.Store and 34 * javax.mail.Folder along with some additional functionality to improve 35 * performance on mobile devices. Implementations of this class should focus on 36 * making as few network connections as possible. 37 */ 38public abstract class Store { 39 40 /** 41 * String constants for known store schemes. 42 */ 43 public static final String STORE_SCHEME_IMAP = "imap"; 44 public static final String STORE_SCHEME_POP3 = "pop3"; 45 public static final String STORE_SCHEME_LOCAL = "local"; 46 47 /** 48 * A global suggestion to Store implementors on how much of the body 49 * should be returned on FetchProfile.Item.BODY_SANE requests. 50 */ 51 public static final int FETCH_BODY_SANE_SUGGESTED_SIZE = (50 * 1024); 52 53 private static java.util.HashMap<String, Store> mStores = 54 new java.util.HashMap<String, Store>(); 55 56 /** 57 * Static named constructor. It should be overrode by extending class. 58 * Because this method will be called through reflection, it can not be protected. 59 */ 60 public static Store newInstance(String uri, Context context, PersistentDataCallbacks callbacks) 61 throws MessagingException { 62 throw new MessagingException("Store.newInstance: Unknown scheme in " + uri); 63 } 64 65 private static Store instantiateStore(String className, String uri, Context context, 66 PersistentDataCallbacks callbacks) 67 throws MessagingException { 68 Object o = null; 69 try { 70 Class<?> c = Class.forName(className); 71 // and invoke "newInstance" class method and instantiate store object. 72 java.lang.reflect.Method m = 73 c.getMethod("newInstance", String.class, Context.class, 74 PersistentDataCallbacks.class); 75 o = m.invoke(null, uri, context, callbacks); 76 } catch (Exception e) { 77 Log.d(Email.LOG_TAG, String.format( 78 "exception %s invoking %s.newInstance.(String, Context) method for %s", 79 e.toString(), className, uri)); 80 throw new MessagingException("can not instantiate Store object for " + uri); 81 } 82 if (!(o instanceof Store)) { 83 throw new MessagingException( 84 uri + ": " + className + " create incompatible object"); 85 } 86 return (Store) o; 87 } 88 89 /** 90 * Look up descriptive information about a particular type of store. 91 */ 92 public static class StoreInfo { 93 public String mScheme; 94 public String mClassName; 95 public boolean mPushSupported = false; 96 public int mVisibleLimitDefault; 97 public int mVisibleLimitIncrement; 98 99 // TODO cache result for performance - silly to keep reading the XML 100 public static StoreInfo getStoreInfo(String scheme, Context context) { 101 StoreInfo result = getStoreInfo(R.xml.stores_product, scheme, context); 102 if (result == null) { 103 result = getStoreInfo(R.xml.stores, scheme, context); 104 } 105 return result; 106 } 107 108 public static StoreInfo getStoreInfo(int resourceId, String scheme, Context context) { 109 try { 110 XmlResourceParser xml = context.getResources().getXml(resourceId); 111 int xmlEventType; 112 // walk through stores.xml file. 113 while ((xmlEventType = xml.next()) != XmlResourceParser.END_DOCUMENT) { 114 if (xmlEventType == XmlResourceParser.START_TAG && 115 "store".equals(xml.getName())) { 116 String xmlScheme = xml.getAttributeValue(null, "scheme"); 117 if (scheme.startsWith(xmlScheme)) { 118 StoreInfo result = new StoreInfo(); 119 result.mScheme = scheme; 120 result.mClassName = xml.getAttributeValue(null, "class"); 121 result.mPushSupported = xml.getAttributeBooleanValue( 122 null, "push", false); 123 result.mVisibleLimitDefault = xml.getAttributeIntValue( 124 null, "visibleLimitDefault", Email.VISIBLE_LIMIT_DEFAULT); 125 result.mVisibleLimitIncrement = xml.getAttributeIntValue( 126 null, "visibleLimitIncrement", Email.VISIBLE_LIMIT_INCREMENT); 127 return result; 128 } 129 } 130 } 131 } catch (XmlPullParserException e) { 132 // ignore 133 } catch (IOException e) { 134 // ignore 135 } 136 return null; 137 } 138 } 139 140 /** 141 * Get an instance of a mail store. The URI is parsed as a standard URI and 142 * the scheme is used to determine which protocol will be used. 143 * 144 * Although the URI format is somewhat protocol-specific, we use the following 145 * guidelines wherever possible: 146 * 147 * scheme [+ security [+]] :// username : password @ host [ / resource ] 148 * 149 * Typical schemes include imap, pop3, local, eas. 150 * Typical security models include SSL or TLS. 151 * A + after the security identifier indicates "required". 152 * 153 * Username, password, and host are as expected. 154 * Resource is protocol specific. For example, IMAP uses it as the path prefix. EAS uses it 155 * as the domain. 156 * 157 * @param uri The URI of the store. 158 * @return an initialized store of the appropriate class 159 * @throws MessagingException 160 */ 161 public synchronized static Store getInstance(String uri, Context context, 162 PersistentDataCallbacks callbacks) 163 throws MessagingException { 164 Store store = mStores.get(uri); 165 if (store == null) { 166 StoreInfo info = StoreInfo.getStoreInfo(uri, context); 167 if (info != null) { 168 store = instantiateStore(info.mClassName, uri, context, callbacks); 169 } 170 171 if (store != null) { 172 mStores.put(uri, store); 173 } 174 } 175 176 if (store == null) { 177 throw new MessagingException("Unable to locate an applicable Store for " + uri); 178 } 179 180 return store; 181 } 182 183 /** 184 * Get class of SettingActivity for this Store class. 185 * @return Activity class that has class method actionEditIncomingSettings(). 186 */ 187 public Class<? extends android.app.Activity> getSettingActivityClass() { 188 // default SettingActivity class 189 return com.android.email.activity.setup.AccountSetupIncoming.class; 190 } 191 192 /** 193 * Get class of sync'er for this Store class 194 * @return Message Sync controller, or null to use default 195 */ 196 public StoreSynchronizer getMessageSynchronizer() { 197 return null; 198 } 199 200 public abstract Folder getFolder(String name) throws MessagingException; 201 202 public abstract Folder[] getPersonalNamespaces() throws MessagingException; 203 204 public abstract void checkSettings() throws MessagingException; 205 206 /** 207 * Enable or disable push mode delivery for a given Store. 208 * 209 * <p>For protocols that do not support push mode, be sure that push="true" is not 210 * set by the stores.xml definition file(s). This function need not be implemented. 211 * 212 * <p>For protocols that do support push mode, this will be called at startup (boot) time 213 * so that the Store can launch its own underlying connection service. It will also be called 214 * any time the user changes the settings for the account (because the user may switch back 215 * to polling mode (or disable checking completely). 216 * 217 * <p>This API will be called repeatedly, even after push mode has already been started or 218 * stopped. Stores that support push mode should return quickly if the configuration has not 219 * changed. 220 * 221 * @param enablePushMode start or stop push mode delivery 222 */ 223 public void enablePushModeDelivery(boolean enablePushMode) { 224 // does nothing for non-push protocols 225 } 226 227 /** 228 * Delete Store and its corresponding resources. 229 * @throws MessagingException 230 */ 231 public void delete() throws MessagingException { 232 } 233 234 /** 235 * Callback interface by which a Store can read and write persistent data. 236 * TODO This needs to be made more generic & flexible 237 */ 238 public interface PersistentDataCallbacks { 239 240 /** 241 * Provides a small place for Stores to store persistent data. 242 * @param storeData Data to persist. All data must be encoded into a string, 243 * so use base64 or some other encoding if necessary. 244 */ 245 public void setPersistentString(Context context, String storeData); 246 247 /** 248 * @return the data saved by the Store, or null if never set. 249 */ 250 public String getPersistentString(Context context); 251 } 252} 253