summaryrefslogtreecommitdiffstats
path: root/src/com/android/email/service/EmailBroadcastReceiver.java
blob: 2564c46e1833cfb785ac8f5da56e9f70cdf6f4c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.email.service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import com.android.email.Preferences;
import com.android.email.R;
import com.android.email.SecurityPolicy;
import com.android.email.provider.AccountReconciler;
import com.android.email.provider.EmailProvider;
import com.android.emailcommon.Logging;
import com.android.emailcommon.VendorPolicyLoader;
import com.android.emailcommon.provider.Account;
import com.android.emailcommon.provider.EmailContent;
import com.android.emailcommon.provider.EmailContent.AccountColumns;
import com.android.emailcommon.provider.HostAuth;
import com.android.emailcommon.provider.Mailbox;
import com.android.mail.utils.LogUtils;
import android.util.Log;


/**
 * The broadcast receiver.  The actual job is done in EmailBroadcastProcessor on a worker thread.
 */
public class EmailBroadcastReceiver extends BroadcastReceiver {

    private static final String TAG = "EmailBroadcastReceiver";
    private static final String ACTION_CHECK_MAIL =
        "com.android.email.intent.action.MAIL_SERVICE_WAKEUP";
    private static final String EXTRA_ACCOUNT = "com.android.email.intent.extra.ACCOUNT";
    private static final String ACTION_DELETE_MESSAGE =
        "com.android.email.intent.action.MAIL_SERVICE_DELETE_MESSAGE";
    private static final String ACTION_MOVE_MESSAGE =
        "com.android.email.intent.action.MAIL_SERVICE_MOVE_MESSAGE";
    private static final String ACTION_MESSAGE_READ =
        "com.android.email.intent.action.MAIL_SERVICE_MESSAGE_READ";
    private static final String ACTION_SEND_PENDING_MAIL =
        "com.android.email.intent.action.MAIL_SERVICE_SEND_PENDING";
    private static final String EXTRA_MESSAGE_ID = "com.android.email.intent.extra.MESSAGE_ID";
    private static final String EXTRA_MESSAGE_INFO = "com.android.email.intent.extra.MESSAGE_INFO";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d(TAG,"Received " + action);
        if(ACTION_CHECK_MAIL.equals(action)) {
            Intent i;
            final long accountId = intent.getLongExtra(EXTRA_ACCOUNT, -1);
            Log.d(TAG,"accountId is " + accountId);
            final long inboxId = Mailbox.findMailboxOfType(context, accountId,
                Mailbox.TYPE_INBOX);
            Log.d(TAG,"inboxId is " + inboxId);
            Mailbox mailbox = Mailbox.restoreMailboxWithId(context, inboxId);
            if (mailbox == null) return;
            Account account = Account.restoreAccountWithId(context, mailbox.mAccountKey);

            String protocol = account.getProtocol(context);
            Log.d(TAG,"protocol is "+protocol);
            String legacyImapProtocol = context.getString(R.string.protocol_legacy_imap);
            if (protocol.equals(legacyImapProtocol)) {
               i = new Intent(context, ImapService.class);
            } else {
               i = new Intent(context, Pop3Service.class);
            }
            i.setAction(intent.getAction());
            i.putExtra(EXTRA_ACCOUNT,
               intent.getLongExtra(EXTRA_ACCOUNT, -1));
            context.startService(i);
        } else if (ACTION_DELETE_MESSAGE.equals(action)) {
            Intent i;
            final long messageId = intent.getLongExtra(EXTRA_MESSAGE_ID, -1);
            Log.d(TAG,"messageId is " + messageId);
            Account account = Account.getAccountForMessageId(context, messageId);
            if(account == null )
               return;
            String protocol = account.getProtocol(context);
            Log.d(TAG,"protocol is "+protocol + " ActId: " +account.getId());
            String legacyImapProtocol = context.getString(R.string.protocol_legacy_imap);
            if (protocol.equals(legacyImapProtocol)) {
               i = new Intent(context, ImapService.class);
               i.setAction(intent.getAction());
               i.putExtra(EXTRA_ACCOUNT,
                   intent.getLongExtra(EXTRA_ACCOUNT, -1));
               i.putExtra(EXTRA_MESSAGE_ID,
                   intent.getLongExtra(EXTRA_MESSAGE_ID, -1));
               context.startService(i);
            } else {
               Log.i(TAG, "DELETE MESSAGE POP3 NOT Implemented");
            }
        } else if (ACTION_MESSAGE_READ.equals(action)) {
            Intent i;
            final long messageId = intent.getLongExtra(EXTRA_MESSAGE_ID, -1);
            Log.d(TAG,"messageId is " + messageId);
            Account account = Account.getAccountForMessageId(context, messageId);
            if(account == null )
               return;
            String protocol = account.getProtocol(context);
            Log.d(TAG,"protocol is "+protocol + " ActId: " +account.getId());
            String legacyImapProtocol = context.getString(R.string.protocol_legacy_imap);
            if (protocol.equals(legacyImapProtocol)) {
               i = new Intent(context, ImapService.class);
               i.setAction(intent.getAction());
               i.putExtra(EXTRA_ACCOUNT,
                   intent.getLongExtra(EXTRA_ACCOUNT, -1));
               i.putExtra(EXTRA_MESSAGE_ID,
                   intent.getLongExtra(EXTRA_MESSAGE_ID, -1));
               i.putExtra(EXTRA_MESSAGE_INFO,
                   intent.getIntExtra(EXTRA_MESSAGE_INFO, 0));
               context.startService(i);
            } else {
               Log.i(TAG, "READ MESSAGE POP3 NOT Implemented");
            }
        } else if (ACTION_MOVE_MESSAGE.equals(action)) {
            Intent i;
            final long messageId = intent.getLongExtra(EXTRA_MESSAGE_ID, -1);
            Log.d(TAG,"messageId is " + messageId);
            Account account = Account.getAccountForMessageId(context, messageId);
            if(account == null )
               return;
            String protocol = account.getProtocol(context);
            Log.d(TAG,"protocol is "+protocol + " ActId: " +account.getId());
            String legacyImapProtocol = context.getString(R.string.protocol_legacy_imap);
            if (protocol.equals(legacyImapProtocol)) {
               i = new Intent(context, ImapService.class);
               i.setAction(intent.getAction());
               i.putExtra(EXTRA_ACCOUNT,
                   intent.getLongExtra(EXTRA_ACCOUNT, -1));
               i.putExtra(EXTRA_MESSAGE_ID,
                   intent.getLongExtra(EXTRA_MESSAGE_ID, -1));
               i.putExtra(EXTRA_MESSAGE_INFO,
                   intent.getIntExtra(EXTRA_MESSAGE_INFO, 0));
               context.startService(i);
            } else {
               Log.i(TAG, "READ MESSAGE POP3 NOT Implemented");
            }
        } else if (ACTION_SEND_PENDING_MAIL.equals(action)) {
            Intent i;
            final long accountId = intent.getLongExtra(EXTRA_ACCOUNT, -1);
            Log.d(TAG,"accountId is " + accountId);
            Account account = Account.restoreAccountWithId(context, accountId);
            if(account == null )
               return;
            String protocol = account.getProtocol(context);
            Log.d(TAG,"protocol is "+protocol);
            String legacyImapProtocol = context.getString(R.string.protocol_legacy_imap);
            if (protocol.equals(legacyImapProtocol)) {
               i = new Intent(context, ImapService.class);
               i.setAction(intent.getAction());
               i.putExtra(EXTRA_ACCOUNT,
               intent.getLongExtra(EXTRA_ACCOUNT, -1));
               context.startService(i);
            } else {
               Log.i(TAG, "SEND MESSAGE POP3 NOT Implemented");
            }
        } else {
            EmailBroadcastProcessorService.processBroadcastIntent(context, intent);
        }
    }
}