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