Home | History | Annotate | Download | only in store
      1 /*
      2  * Copyright (C) 2011 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.store;
     18 
     19 import android.content.Context;
     20 import android.os.Bundle;
     21 import android.os.RemoteException;
     22 
     23 import com.android.email.mail.Store;
     24 import com.android.email.service.EmailServiceUtils;
     25 import com.android.emailcommon.mail.MessagingException;
     26 import com.android.emailcommon.provider.Account;
     27 import com.android.emailcommon.provider.HostAuth;
     28 import com.android.emailcommon.service.EmailServiceProxy;
     29 import com.android.emailcommon.service.IEmailService;
     30 
     31 /**
     32  * Base class for service-based stores
     33  */
     34 public class ServiceStore extends Store {
     35     protected final HostAuth mHostAuth;
     36 
     37     /**
     38      * Creates a new store for the given account.
     39      */
     40     public ServiceStore(Account account, Context context) throws MessagingException {
     41         mContext = context;
     42         mHostAuth = account.getOrCreateHostAuthRecv(mContext);
     43     }
     44 
     45     /**
     46      * Static named constructor.
     47      */
     48     public static Store newInstance(Account account, Context context) throws MessagingException {
     49         return new ServiceStore(account, context);
     50     }
     51 
     52     private IEmailService getService() {
     53         return EmailServiceUtils.getService(mContext, mHostAuth.mProtocol);
     54     }
     55 
     56     @Override
     57     public Bundle checkSettings() throws MessagingException {
     58         /**
     59          * Here's where we check the settings
     60          * @throws MessagingException if we can't authenticate the account
     61          */
     62         try {
     63             IEmailService svc = getService();
     64             // Use a longer timeout for the validate command.  Note that the instanceof check
     65             // shouldn't be necessary; we'll do it anyway, just to be safe
     66             if (svc instanceof EmailServiceProxy) {
     67                 ((EmailServiceProxy)svc).setTimeout(90);
     68             }
     69             return svc.validate(mHostAuth);
     70         } catch (RemoteException e) {
     71             throw new MessagingException("Call to validate generated an exception", e);
     72         }
     73     }
     74 
     75     /**
     76      * We handle AutoDiscover here, wrapping the EmailService call. The service call returns a
     77      * HostAuth and we return null if there was a service issue
     78      */
     79     @Override
     80     public Bundle autoDiscover(Context context, String username, String password) {
     81         try {
     82             return getService().autoDiscover(username, password);
     83         } catch (RemoteException e) {
     84             return null;
     85         }
     86     }
     87 }
     88