ServiceStore.java revision 35b0e95ca795e17b6dc8dd98c7ab847d65d9aa0c
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
17package com.android.email.mail.store;
18
19import android.content.Context;
20import android.os.Bundle;
21import android.os.RemoteException;
22
23import com.android.email.mail.Store;
24import com.android.emailcommon.mail.MessagingException;
25import com.android.emailcommon.provider.Account;
26import com.android.emailcommon.provider.HostAuth;
27import com.android.emailcommon.service.EmailServiceProxy;
28import com.android.emailcommon.service.IEmailService;
29
30/**
31 * Base class for service-based stores
32 */
33public abstract class ServiceStore extends Store {
34    protected final HostAuth mHostAuth;
35
36    protected abstract IEmailService getService();
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    @Override
47    public Bundle checkSettings() throws MessagingException {
48        /**
49         * Here's where we check the settings
50         * @throws MessagingException if we can't authenticate the account
51         */
52        try {
53            IEmailService svc = getService();
54            // Use a longer timeout for the validate command.  Note that the instanceof check
55            // shouldn't be necessary; we'll do it anyway, just to be safe
56            if (svc instanceof EmailServiceProxy) {
57                ((EmailServiceProxy)svc).setTimeout(90);
58            }
59            return svc.validate(mHostAuth);
60        } catch (RemoteException e) {
61            throw new MessagingException("Call to validate generated an exception", e);
62        }
63    }
64
65    /**
66     * We handle AutoDiscover here, wrapping the EmailService call. The service call returns a
67     * HostAuth and we return null if there was a service issue
68     */
69    @Override
70    public Bundle autoDiscover(Context context, String username, String password) {
71        try {
72            return getService().autoDiscover(username, password);
73        } catch (RemoteException e) {
74            return null;
75        }
76    }
77}
78