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