Home | History | Annotate | Download | only in mail
      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  */
     16 package com.android.mail;
     17 
     18 import android.app.IntentService;
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.net.Uri;
     22 
     23 import com.android.mail.analytics.Analytics;
     24 import com.android.mail.photo.ContactPhotoFetcher;
     25 import com.android.mail.providers.Account;
     26 import com.android.mail.providers.Folder;
     27 import com.android.mail.utils.FolderUri;
     28 import com.android.mail.utils.LogTag;
     29 import com.android.mail.utils.LogUtils;
     30 import com.android.mail.utils.NotificationUtils;
     31 import com.android.mail.utils.StorageLowState;
     32 import com.android.mail.utils.Utils;
     33 
     34 /**
     35  * A service to handle various intents asynchronously.
     36  */
     37 public class MailIntentService extends IntentService {
     38     private static final String LOG_TAG = LogTag.getLogTag();
     39 
     40     public static final String ACTION_RESEND_NOTIFICATIONS =
     41             "com.android.mail.action.RESEND_NOTIFICATIONS";
     42     public static final String ACTION_CLEAR_NEW_MAIL_NOTIFICATIONS =
     43             "com.android.mail.action.CLEAR_NEW_MAIL_NOTIFICATIONS";
     44 
     45     /**
     46      * After user replies an email from Wear, it marks the conversation as read and resend
     47      * notifications.
     48      */
     49     public static final String ACTION_RESEND_NOTIFICATIONS_WEAR =
     50             "com.android.mail.action.RESEND_NOTIFICATIONS_WEAR";
     51 
     52     public static final String ACTION_BACKUP_DATA_CHANGED =
     53             "com.android.mail.action.BACKUP_DATA_CHANGED";
     54     public static final String ACTION_SEND_SET_NEW_EMAIL_INDICATOR =
     55             "com.android.mail.action.SEND_SET_NEW_EMAIL_INDICATOR";
     56 
     57     public static final String CONVERSATION_EXTRA = "conversation";
     58 
     59     public MailIntentService() {
     60         super("MailIntentService");
     61     }
     62 
     63     protected MailIntentService(final String name) {
     64         super(name);
     65     }
     66 
     67     @Override
     68     protected void onHandleIntent(final Intent intent) {
     69         // UnifiedEmail does not handle all Intents
     70 
     71         LogUtils.v(LOG_TAG, "Handling intent %s", intent);
     72 
     73         final String action = intent.getAction();
     74 
     75         if (Intent.ACTION_LOCALE_CHANGED.equals(action)) {
     76             NotificationUtils.cancelAndResendNotificationsOnLocaleChange(
     77                     this, getContactPhotoFetcher());
     78         } else if (ACTION_CLEAR_NEW_MAIL_NOTIFICATIONS.equals(action)) {
     79             final Account account = intent.getParcelableExtra(Utils.EXTRA_ACCOUNT);
     80             final Folder folder = intent.getParcelableExtra(Utils.EXTRA_FOLDER);
     81 
     82             NotificationUtils.clearFolderNotification(this, account, folder, true /* markSeen */);
     83             Analytics.getInstance().sendEvent("notification_dismiss", folder.getTypeDescription(),
     84                     null, 0);
     85         } else if (ACTION_RESEND_NOTIFICATIONS.equals(action)) {
     86             final Uri accountUri = intent.getParcelableExtra(Utils.EXTRA_ACCOUNT_URI);
     87             final Uri folderUri = intent.getParcelableExtra(Utils.EXTRA_FOLDER_URI);
     88 
     89             NotificationUtils.resendNotifications(this, false, accountUri,
     90                     new FolderUri(folderUri), getContactPhotoFetcher());
     91         } else if (ACTION_RESEND_NOTIFICATIONS_WEAR.equals(action)) {
     92             final Account account = intent.getParcelableExtra(Utils.EXTRA_ACCOUNT);
     93             final Folder folder = intent.getParcelableExtra(Utils.EXTRA_FOLDER);
     94             final Uri conversationUri = intent.getParcelableExtra(Utils.EXTRA_CONVERSATION);
     95 
     96             // Mark the conversation as read and refresh the notifications.  This happens
     97             // when user replies to a conversation remotely from a Wear device.
     98             NotificationUtils.markConversationAsReadAndSeen(this, conversationUri);
     99             NotificationUtils.resendNotifications(this, false, account.uri,
    100                     folder.folderUri, getContactPhotoFetcher());
    101         } else if (ACTION_SEND_SET_NEW_EMAIL_INDICATOR.equals(action)) {
    102             final int unreadCount = intent.getIntExtra(NotificationUtils.EXTRA_UNREAD_COUNT, 0);
    103             final int unseenCount = intent.getIntExtra(NotificationUtils.EXTRA_UNSEEN_COUNT, 0);
    104             final Account account = intent.getParcelableExtra(Utils.EXTRA_ACCOUNT);
    105             final Folder folder = intent.getParcelableExtra(Utils.EXTRA_FOLDER);
    106             final boolean getAttention =
    107                     intent.getBooleanExtra(NotificationUtils.EXTRA_GET_ATTENTION, false);
    108 
    109             NotificationUtils.setNewEmailIndicator(this, unreadCount, unseenCount,
    110                     account, folder, getAttention, getContactPhotoFetcher());
    111         } else if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(action)) {
    112             // The storage_low state is recorded centrally even though
    113             // no handler might be present to change application state
    114             // based on state changes.
    115             StorageLowState.setIsStorageLow(true);
    116         } else if (Intent.ACTION_DEVICE_STORAGE_OK.equals(action)) {
    117             StorageLowState.setIsStorageLow(false);
    118         }
    119     }
    120 
    121     public static void broadcastBackupDataChanged(final Context context) {
    122         final Intent intent = new Intent(ACTION_BACKUP_DATA_CHANGED);
    123         context.startService(intent);
    124     }
    125 
    126     /**
    127      * Derived classes should override this method if they wish to provide their
    128      * own photo loading behavior separate from the ContactProvider-based default.
    129      * The default behavior of this method returns null.
    130      */
    131     public ContactPhotoFetcher getContactPhotoFetcher() {
    132         return null;
    133     }
    134 }
    135