Home | History | Annotate | Download | only in providers
      1 /**
      2  * Copyright (c) 2012, Google Inc.
      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 
     17 package com.android.mail.providers;
     18 
     19 import android.content.ContentValues;
     20 import android.text.TextUtils;
     21 
     22 import com.android.mail.providers.UIProvider.DraftType;
     23 import com.android.mail.providers.UIProvider.MessageColumns;
     24 
     25 import java.util.List;
     26 
     27 /**
     28  * A helper class for creating or updating messages. Use the putXxx methods to
     29  * provide initial or new values for the message. Then save or send the message.
     30  * To save or send an existing message without making other changes to it simply
     31  * provide an empty ContentValues.
     32  */
     33 public class MessageModification {
     34     /**
     35      * Sets the message's subject. Only valid for drafts.
     36      * @param values the ContentValues that will be used to create or update the
     37      *            message
     38      * @param subject the new subject
     39      */
     40     public static void putSubject(ContentValues values, String subject) {
     41         values.put(MessageColumns.SUBJECT, subject);
     42     }
     43 
     44     /**
     45      * Sets the message's to address. Only valid for drafts.
     46      * @param values the ContentValues that will be used to create or update the
     47      *            message
     48      * @param toAddresses the new to addresses
     49      */
     50     public static void putToAddresses(ContentValues values, String[] toAddresses) {
     51         values.put(MessageColumns.TO, TextUtils.join(UIProvider.EMAIL_SEPARATOR, toAddresses));
     52     }
     53 
     54     /**
     55      * Sets the message's cc address. Only valid for drafts.
     56      * @param values the ContentValues that will be used to create or update the
     57      *            message
     58      * @param ccAddresses the new cc addresses
     59      */
     60     public static void putCcAddresses(ContentValues values, String[] ccAddresses) {
     61         values.put(MessageColumns.CC, TextUtils.join(UIProvider.EMAIL_SEPARATOR, ccAddresses));
     62     }
     63 
     64     /**
     65      * Sets the message's bcc address. Only valid for drafts.
     66      * @param values the ContentValues that will be used to create or update the
     67      *            message
     68      * @param bccAddresses the new bcc addresses
     69      */
     70     public static void putBccAddresses(ContentValues values, String[] bccAddresses) {
     71         values.put(MessageColumns.BCC, TextUtils.join(UIProvider.EMAIL_SEPARATOR, bccAddresses));
     72     }
     73 
     74     /**
     75      * Sets the custom from address for a message, we only set this if its different than the
     76      * default adress for the account.
     77      *
     78      * @param values the ContentValues that will be used to create or update the message
     79      * @param customFromAddress from address
     80      */
     81      public static void putCustomFromAddress(ContentValues values, String customFromAddress) {
     82         values.put(MessageColumns.CUSTOM_FROM_ADDRESS, customFromAddress);
     83      }
     84 
     85     /**
     86      * Saves a flag indicating the message is forwarded. Only valid for drafts
     87      * not yet sent to / retrieved from server.
     88      * @param values the ContentValues that will be used to create or update the
     89      *            message
     90      * @param forward true if the message is forwarded
     91      */
     92     public static void putForward(ContentValues values, boolean forward) {
     93         values.put(MessageColumns.DRAFT_TYPE, DraftType.FORWARD);
     94     }
     95 
     96     /**
     97      * Saves an include quoted text flag. Only valid for drafts not yet sent to
     98      * / retrieved from server.
     99      * @param values the ContentValues that will be used to create or update the
    100      *            message
    101      * @param includeQuotedText the include quoted text flag
    102      */
    103     public static void putAppendRefMessageContent(ContentValues values, boolean includeQuotedText) {
    104         values.put(MessageColumns.APPEND_REF_MESSAGE_CONTENT, includeQuotedText ? 1 : 0);
    105     }
    106 
    107     /**
    108      * Saves a new body for the message. Only valid for drafts.
    109      * @param values the ContentValues that will be used to create or update the
    110      *            message
    111      * @param body the new body of the message
    112      */
    113     public static void putBody(ContentValues values, String body) {
    114         values.put(MessageColumns.BODY_TEXT, body);
    115     }
    116 
    117     /**
    118      * Saves a new body for the message. Only valid for drafts.
    119      * @param values the ContentValues that will be used to create or update the
    120      *            message
    121      * @param body the new body of the message
    122      */
    123     public static void putBodyHtml(ContentValues values, String body) {
    124         values.put(MessageColumns.BODY_HTML, body);
    125     }
    126 
    127     /**
    128      * Saves the type of the conversation.
    129      *
    130      * @param values the ContentValues that will be used to create or update the message
    131      * @param draftType
    132      */
    133     public static void putDraftType(ContentValues values, int draftType) {
    134         values.put(MessageColumns.DRAFT_TYPE, draftType);
    135     }
    136 
    137     /**
    138      * Saves the ref message id for the conversation. It will be a uri.
    139      *
    140      * @param values the ContentValues that will be used to create or update the message
    141      * @param uri of the reference message
    142      */
    143     public static void putRefMessageId(ContentValues values, String uri) {
    144         values.put(MessageColumns.REF_MESSAGE_ID, uri);
    145     }
    146 
    147     /**
    148      * Saves a quoted text starting position. Only valid for drafts not yet sent to /
    149      * retrieved from server.
    150      *
    151      * @param values the ContentValues that will be used to create or update the message
    152      * @param quoteStartPos the starting position for quoted text
    153      */
    154     public static void putQuoteStartPos(ContentValues values, int quoteStartPos) {
    155         values.put(MessageColumns.QUOTE_START_POS, quoteStartPos);
    156     }
    157 
    158     public static void putAttachments(ContentValues values, List<Attachment> attachments) {
    159         values.put(MessageColumns.ATTACHMENTS,  Attachment.toJSONArray(attachments));
    160     }
    161 }
    162