Home | History | Annotate | Download | only in provider
      1 /*
      2  * Copyright (C) 2011 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 
     17 
     18 package com.android.emailcommon.provider;
     19 
     20 import com.android.emailcommon.provider.EmailContent;
     21 import com.android.emailcommon.provider.EmailContent.QuickResponseColumns;
     22 import com.google.common.base.Objects;
     23 
     24 import android.content.ContentUris;
     25 import android.content.ContentValues;
     26 import android.content.Context;
     27 import android.database.Cursor;
     28 import android.net.Uri;
     29 import android.os.Parcel;
     30 import android.os.Parcelable;
     31 
     32 /**
     33  * A user-modifiable message that may be quickly inserted into the body while user is composing
     34  * a message. Tied to a specific account.
     35  */
     36 public final class QuickResponse extends EmailContent
     37         implements QuickResponseColumns, Parcelable {
     38     public static final String TABLE_NAME = "QuickResponse";
     39     @SuppressWarnings("hiding")
     40     public static final Uri CONTENT_URI = Uri.parse(EmailContent.CONTENT_URI
     41             + "/quickresponse");
     42     public static final Uri ACCOUNT_ID_URI = Uri.parse(
     43             EmailContent.CONTENT_URI + "/quickresponse/account");
     44 
     45     private String mText;
     46     private long mAccountKey;
     47 
     48     private static final int CONTENT_ID_COLUMN = 0;
     49     private static final int CONTENT_QUICK_RESPONSE_COLUMN = 1;
     50     private static final int CONTENT_ACCOUNT_KEY_COLUMN = 2;
     51     public static final String[] CONTENT_PROJECTION = new String[] {
     52             RECORD_ID,
     53             QuickResponseColumns.TEXT,
     54             QuickResponseColumns.ACCOUNT_KEY
     55     };
     56 
     57     /**
     58      * Creates an empty QuickResponse. Restore should be called after.
     59      */
     60     private QuickResponse() {
     61         // empty
     62     }
     63 
     64     /**
     65      * Constructor used by CREATOR for parceling.
     66      */
     67     private QuickResponse(Parcel in) {
     68         mBaseUri = CONTENT_URI;
     69         mId = in.readLong();
     70         mText = in.readString();
     71         mAccountKey = in.readLong();
     72     }
     73 
     74     /**
     75      * Creates QuickResponse associated with a particular account using the given string.
     76      */
     77     public QuickResponse(long accountKey, String quickResponse) {
     78         mBaseUri = CONTENT_URI;
     79         mAccountKey = accountKey;
     80         mText = quickResponse;
     81     }
     82 
     83     /**
     84      * @see com.android.emailcommon.provider.EmailContent#restore(android.database.Cursor)
     85      */
     86     @Override
     87     public void restore(Cursor cursor) {
     88         mBaseUri = CONTENT_URI;
     89         mId = cursor.getLong(CONTENT_ID_COLUMN);
     90         mText = cursor.getString(CONTENT_QUICK_RESPONSE_COLUMN);
     91         mAccountKey = cursor.getLong(CONTENT_ACCOUNT_KEY_COLUMN);
     92     }
     93 
     94     /**
     95      * @see com.android.emailcommon.provider.EmailContent#toContentValues()
     96      */
     97     @Override
     98     public ContentValues toContentValues() {
     99         ContentValues values = new ContentValues();
    100 
    101         values.put(QuickResponseColumns.TEXT, mText);
    102         values.put(QuickResponseColumns.ACCOUNT_KEY, mAccountKey);
    103 
    104         return values;
    105     }
    106 
    107     @Override
    108     public String toString() {
    109         return mText;
    110     }
    111 
    112     /**
    113      * Given an array of QuickResponses, returns the an array of the String values
    114      * corresponding to each QuickResponse.
    115      */
    116     public static String[] getQuickResponseStrings(QuickResponse[] quickResponses) {
    117         int count = quickResponses.length;
    118         String[] quickResponseStrings = new String[count];
    119         for (int i = 0; i < count; i++) {
    120             quickResponseStrings[i] = quickResponses[i].toString();
    121         }
    122 
    123         return quickResponseStrings;
    124     }
    125 
    126     /**
    127      * @param context
    128      * @param accountId
    129      * @return array of QuickResponses for the account with id accountId
    130      */
    131     public static QuickResponse[] restoreQuickResponsesWithAccountId(Context context,
    132             long accountId) {
    133         Uri uri = ContentUris.withAppendedId(ACCOUNT_ID_URI, accountId);
    134         Cursor c = context.getContentResolver().query(uri, CONTENT_PROJECTION,
    135                 null, null, null);
    136 
    137         try {
    138             int count = c.getCount();
    139             QuickResponse[] quickResponses = new QuickResponse[count];
    140             for (int i = 0; i < count; ++i) {
    141                 c.moveToNext();
    142                 QuickResponse quickResponse = new QuickResponse();
    143                 quickResponse.restore(c);
    144                 quickResponses[i] = quickResponse;
    145             }
    146             return quickResponses;
    147         } finally {
    148             c.close();
    149         }
    150     }
    151 
    152     /**
    153      * Returns the base URI for this QuickResponse
    154      */
    155     public Uri getBaseUri() {
    156         return mBaseUri;
    157     }
    158 
    159     /**
    160      * Returns the unique id for this QuickResponse
    161      */
    162     public long getId() {
    163         return mId;
    164     }
    165 
    166     @Override
    167     public boolean equals(Object objectThat) {
    168         if (this == objectThat) return true;
    169         if (!(objectThat instanceof QuickResponse)) return false;
    170 
    171         QuickResponse that = (QuickResponse) objectThat;
    172         return
    173             mText.equals(that.mText) &&
    174             mId == that.mId &&
    175             mAccountKey == that.mAccountKey;
    176     }
    177 
    178     @Override
    179     public int hashCode() {
    180         return Objects.hashCode(mId, mText, mAccountKey);
    181     }
    182 
    183     /**
    184      * Implements Parcelable. Not used.
    185      */
    186     @Override
    187     public int describeContents() {
    188         return 0;
    189     }
    190 
    191     /**
    192      * Implements Parcelable.
    193      */
    194     public void writeToParcel(Parcel dest, int flags) {
    195         // mBaseUri is not parceled
    196         dest.writeLong(mId);
    197         dest.writeString(mText);
    198         dest.writeLong(mAccountKey);
    199     }
    200 
    201     /**
    202      * Implements Parcelable
    203      */
    204     public static final Parcelable.Creator<QuickResponse> CREATOR
    205             = new Parcelable.Creator<QuickResponse>() {
    206         @Override
    207         public QuickResponse createFromParcel(Parcel in) {
    208             return new QuickResponse(in);
    209         }
    210 
    211         @Override
    212         public QuickResponse[] newArray(int size) {
    213             return new QuickResponse[size];
    214         }
    215     };
    216 
    217 }