EmailServiceUtils.java revision 4f813fb12937de74d3ccec730b8de0c9de7a87e0
1/*
2 * Copyright (C) 2010 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.service;
18
19import android.content.Context;
20import android.content.Intent;
21import com.android.emailcommon.provider.Account;
22import com.android.emailcommon.provider.HostAuth;
23import com.android.emailcommon.service.EmailServiceProxy;
24import com.android.emailcommon.service.IEmailService;
25import com.android.emailcommon.service.IEmailServiceCallback;
26
27/**
28 * Utility functions for EmailService support.
29 */
30public class EmailServiceUtils {
31    /**
32     * Starts an EmailService by name
33     */
34    public static void startService(Context context, String intentAction) {
35        context.startService(new Intent(intentAction));
36    }
37
38    /**
39     * Returns an {@link IEmailService} for the service; otherwise returns an empty
40     * {@link IEmailService} implementation.
41     *
42     * @param context
43     * @param callback Object to get callback, or can be null
44     */
45    public static EmailServiceProxy getService(Context context, String intentAction,
46            IEmailServiceCallback callback) {
47        return new EmailServiceProxy(context, intentAction, callback);
48    }
49
50    /**
51     * Determine if the EmailService is available
52     */
53    public static boolean isServiceAvailable(Context context, String intentAction) {
54        return new EmailServiceProxy(context, intentAction, null).test();
55    }
56
57    public static void startExchangeService(Context context) {
58        startService(context, EmailServiceProxy.EXCHANGE_INTENT);
59    }
60
61    public static EmailServiceProxy getExchangeService(Context context,
62            IEmailServiceCallback callback) {
63        return getService(context, EmailServiceProxy.EXCHANGE_INTENT, callback);
64    }
65
66    public static EmailServiceProxy getImapService(Context context,
67            IEmailServiceCallback callback) {
68        return new EmailServiceProxy(context, ImapService.class, callback);
69    }
70
71    public static EmailServiceProxy getPop3Service(Context context,
72            IEmailServiceCallback callback) {
73        return new EmailServiceProxy(context, Pop3Service.class, callback);
74    }
75
76    public static boolean isExchangeAvailable(Context context) {
77        return isServiceAvailable(context, EmailServiceProxy.EXCHANGE_INTENT);
78    }
79
80    /**
81     * For a given account id, return a service proxy if applicable, or null.
82     *
83     * @param accountId the message of interest
84     * @result service proxy, or null if n/a
85     */
86    public static EmailServiceProxy getServiceForAccount(Context context,
87            IEmailServiceCallback callback, long accountId) {
88        String protocol = Account.getProtocol(context, accountId);
89        if (protocol.equals(HostAuth.SCHEME_IMAP)) {
90            return getImapService(context, callback);
91        } else if (protocol.equals(HostAuth.SCHEME_POP3)) {
92            return getPop3Service(context, callback);
93        } else if (protocol.equals(HostAuth.SCHEME_EAS)) {
94        return getExchangeService(context, callback);
95        } else {
96            throw new IllegalArgumentException("Account with unknown protocol: " + accountId);
97        }
98    }
99}
100