1/*
2 * Copyright (C) 2012 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 */
16package com.android.mail;
17
18import android.net.Uri;
19
20import com.android.mail.utils.StorageLowState;
21
22import android.app.IntentService;
23import android.content.Context;
24import android.content.Intent;
25
26import com.android.mail.analytics.Analytics;
27import com.android.mail.analytics.AnalyticsUtils;
28import com.android.mail.providers.Account;
29import com.android.mail.providers.Folder;
30import com.android.mail.utils.FolderUri;
31import com.android.mail.utils.LogTag;
32import com.android.mail.utils.LogUtils;
33import com.android.mail.utils.NotificationUtils;
34import com.android.mail.utils.Utils;
35
36/**
37 * A service to handle various intents asynchronously.
38 */
39public class MailIntentService extends IntentService {
40    private static final String LOG_TAG = LogTag.getLogTag();
41
42    public static final String ACTION_RESEND_NOTIFICATIONS =
43            "com.android.mail.action.RESEND_NOTIFICATIONS";
44    public static final String ACTION_CLEAR_NEW_MAIL_NOTIFICATIONS =
45            "com.android.mail.action.CLEAR_NEW_MAIL_NOTIFICATIONS";
46    public static final String ACTION_BACKUP_DATA_CHANGED =
47            "com.android.mail.action.BACKUP_DATA_CHANGED";
48
49    public static final String CONVERSATION_EXTRA = "conversation";
50
51    public MailIntentService() {
52        super("MailIntentService");
53    }
54
55    protected MailIntentService(final String name) {
56        super(name);
57    }
58
59    @Override
60    protected void onHandleIntent(final Intent intent) {
61        // UnifiedEmail does not handle all Intents
62
63        LogUtils.v(LOG_TAG, "Handling intent %s", intent);
64
65        final String action = intent.getAction();
66
67        if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
68            NotificationUtils.cancelAndResendNotifications(this);
69        } else if (ACTION_CLEAR_NEW_MAIL_NOTIFICATIONS.equals(action)) {
70            final Account account = intent.getParcelableExtra(Utils.EXTRA_ACCOUNT);
71            final Folder folder = intent.getParcelableExtra(Utils.EXTRA_FOLDER);
72
73            NotificationUtils.clearFolderNotification(this, account, folder, true /* markSeen */);
74
75            Analytics.getInstance().sendEvent("notification_dismiss", folder.getTypeDescription(),
76                    null, 0);
77
78        } else if (ACTION_RESEND_NOTIFICATIONS.equals(action)) {
79            final Uri accountUri = intent.getParcelableExtra(Utils.EXTRA_ACCOUNT_URI);
80            final Uri folderUri = intent.getParcelableExtra(Utils.EXTRA_FOLDER_URI);
81
82            NotificationUtils.resendNotifications(this, false, accountUri,
83                    new FolderUri(folderUri));
84        } else if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(action)) {
85            // The storage_low state is recorded centrally even though
86            // no handler might be present to change application state
87            // based on state changes.
88            StorageLowState.setIsStorageLow(true);
89        } else if (Intent.ACTION_DEVICE_STORAGE_OK.equals(action)) {
90            StorageLowState.setIsStorageLow(false);
91        }
92    }
93
94    public static void broadcastBackupDataChanged(final Context context) {
95        final Intent intent = new Intent(ACTION_BACKUP_DATA_CHANGED);
96        context.startService(intent);
97    }
98}
99