135b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank/*
235b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank * Copyright (C) 2011 The Android Open Source Project
335b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank *
435b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank * Licensed under the Apache License, Version 2.0 (the "License");
535b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank * you may not use this file except in compliance with the License.
635b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank * You may obtain a copy of the License at
735b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank *
835b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank *      http://www.apache.org/licenses/LICENSE-2.0
935b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank *
1035b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank * Unless required by applicable law or agreed to in writing, software
1135b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank * distributed under the License is distributed on an "AS IS" BASIS,
1235b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1335b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank * See the License for the specific language governing permissions and
1435b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank * limitations under the License.
1535b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank */
1635b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank
1735b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blankpackage com.android.email.mail.store;
1835b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank
1935b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blankimport android.content.Context;
2035b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blankimport android.os.Bundle;
2135b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blankimport android.os.RemoteException;
2235b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank
2335b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blankimport com.android.email.mail.Store;
24f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blankimport com.android.email.service.EmailServiceUtils;
2535b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blankimport com.android.emailcommon.mail.MessagingException;
2635b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blankimport com.android.emailcommon.provider.Account;
2735b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blankimport com.android.emailcommon.provider.HostAuth;
2835b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blankimport com.android.emailcommon.service.EmailServiceProxy;
291d8fd9c05449863ac6a151a6db87c267c4f55ac2Martin Hibdonimport com.android.emailcommon.service.HostAuthCompat;
3035b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blankimport com.android.emailcommon.service.IEmailService;
3135b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank
3235b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank/**
3335b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank * Base class for service-based stores
3435b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank */
35f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blankpublic class ServiceStore extends Store {
3635b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank    protected final HostAuth mHostAuth;
3735b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank
3835b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank    /**
3935b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank     * Creates a new store for the given account.
4035b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank     */
4135b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank    public ServiceStore(Account account, Context context) throws MessagingException {
4235b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank        mContext = context;
4335b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank        mHostAuth = account.getOrCreateHostAuthRecv(mContext);
4435b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank    }
4535b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank
46f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank    /**
47f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank     * Static named constructor.
48f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank     */
49f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank    public static Store newInstance(Account account, Context context) throws MessagingException {
50f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank        return new ServiceStore(account, context);
51f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank    }
52f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank
53f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank    private IEmailService getService() {
542075c97f608a853923980865b72147a5c8ef71f0Yu Ping Hu        return EmailServiceUtils.getService(mContext, mHostAuth.mProtocol);
55f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank    }
56f419287f22ae44f25e1ba1f757ec33c7941bbfa8Marc Blank
5735b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank    @Override
5835b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank    public Bundle checkSettings() throws MessagingException {
5935b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank        /**
6035b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank         * Here's where we check the settings
6135b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank         * @throws MessagingException if we can't authenticate the account
6235b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank         */
6335b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank        try {
6435b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank            IEmailService svc = getService();
6535b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank            // Use a longer timeout for the validate command.  Note that the instanceof check
6635b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank            // shouldn't be necessary; we'll do it anyway, just to be safe
6735b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank            if (svc instanceof EmailServiceProxy) {
6835b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank                ((EmailServiceProxy)svc).setTimeout(90);
6935b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank            }
701d8fd9c05449863ac6a151a6db87c267c4f55ac2Martin Hibdon            HostAuthCompat hostAuthCom = new HostAuthCompat(mHostAuth);
711d8fd9c05449863ac6a151a6db87c267c4f55ac2Martin Hibdon            return svc.validate(hostAuthCom);
7235b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank        } catch (RemoteException e) {
7335b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank            throw new MessagingException("Call to validate generated an exception", e);
7435b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank        }
7535b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank    }
7635b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank
7735b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank    /**
7835b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank     * We handle AutoDiscover here, wrapping the EmailService call. The service call returns a
7935b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank     * HostAuth and we return null if there was a service issue
8035b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank     */
8135b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank    @Override
8235b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank    public Bundle autoDiscover(Context context, String username, String password) {
8335b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank        try {
8435b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank            return getService().autoDiscover(username, password);
8535b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank        } catch (RemoteException e) {
8635b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank            return null;
8735b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank        }
8835b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank    }
8935b0e95ca795e17b6dc8dd98c7ab847d65d9aa0cMarc Blank}
90