Home | History | Annotate | Download | only in service
      1 /*
      2  *  Copyright (C) 2008-2009 Marc Blank
      3  * Licensed to The Android Open Source Project.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.emailcommon.service;
     19 
     20 import android.content.ContentResolver;
     21 import android.net.Uri;
     22 import android.os.Bundle;
     23 
     24 /**
     25  * Definitions of service status codes returned to IEmailServiceCallback's status method.
     26  *
     27  * Now that all sync requests are sent through the system SyncManager, there's no way to specify the
     28  * {@link IEmailServiceCallback} to {@link ContentResolver#requestSync} since all we have is a
     29  * {@link Bundle}. Instead, the caller requesting the sync specifies values with which to call
     30  * {@link ContentResolver#call} in order to receive a callback, and the
     31  * {@link android.content.ContentProvider} must handle this call.
     32  */
     33 public abstract class EmailServiceStatus {
     34     public static final int SUCCESS = 0;
     35     public static final int IN_PROGRESS = 1;
     36 
     37     public static final int MESSAGE_NOT_FOUND = 0x10;
     38     public static final int ATTACHMENT_NOT_FOUND = 0x11;
     39     public static final int FOLDER_NOT_DELETED = 0x12;
     40     public static final int FOLDER_NOT_RENAMED = 0x13;
     41     public static final int FOLDER_NOT_CREATED = 0x14;
     42     public static final int REMOTE_EXCEPTION = 0x15;
     43     public static final int LOGIN_FAILED = 0x16;
     44     public static final int SECURITY_FAILURE = 0x17;
     45     public static final int ACCOUNT_UNINITIALIZED = 0x18;
     46     public static final int ACCESS_DENIED = 0x19;
     47 
     48     // Maybe we should automatically retry these?
     49     public static final int CONNECTION_ERROR = 0x20;
     50 
     51     // Client certificates used to authenticate cannot be retrieved from the system.
     52     public static final int CLIENT_CERTIFICATE_ERROR = 0x21;
     53 
     54     // Keys for the sync extras Bundle that specify the callback.
     55     public static final String SYNC_EXTRAS_CALLBACK_URI = "callback_uri";
     56     public static final String SYNC_EXTRAS_CALLBACK_METHOD = "callback_method";
     57     public static final String SYNC_EXTRAS_CALLBACK_ARG = "callback_arg";
     58 
     59     // Keys for the status Bundle sent to the callback. These keys are used in every status type.
     60     public static final String SYNC_STATUS_TYPE = "type";
     61     public static final String SYNC_STATUS_ID = "id";
     62     public static final String SYNC_STATUS_CODE = "status_code";
     63     public static final String SYNC_RESULT = "result";
     64     public static final String SYNC_STATUS_PROGRESS = "progress";
     65 
     66     // Values for the SYNC_STATUS_TYPE to specify what kind of sync status we're returning.
     67     public static final int SYNC_STATUS_TYPE_MAILBOX = 0;
     68 
     69     /**
     70      * Some status updates need to provide values in addition to the core id, code, and progress.
     71      * Those updates will provide an appropriate StatusWriter to fill in those extras.
     72      */
     73     private static interface StatusWriter {
     74         public void addToStatus(final Bundle statusExtras);
     75     }
     76 
     77     /**
     78      * Generic function to check if the callback is necessary and, if so, perform it.
     79      * The function is the common parts for the following functions, which are for use by the
     80      * {@link android.content.AbstractThreadedSyncAdapter} to communicate the status of a sync
     81      * action to the caller.
     82      * @param cr A ContentResolver.
     83      * @param syncExtras The extras provided to the sync request.
     84      * @param statusType The type of sync status update to send.
     85      * @param id The id of the thing whose status is being updated (type depends on statusType).
     86      * @param statusCode The status code for this sync operation.
     87      * @param progress The progress of this sync operation.
     88      * @param writer If not null, an object which will write additional status fields.
     89      */
     90     private static void syncStatus(final ContentResolver cr, final Bundle syncExtras,
     91             final int statusType, final long id, final int statusCode, final int progress,
     92             int syncResult,
     93             final StatusWriter writer) {
     94         final String callbackUri = syncExtras.getString(SYNC_EXTRAS_CALLBACK_URI);
     95         final String callbackMethod = syncExtras.getString(SYNC_EXTRAS_CALLBACK_METHOD);
     96         if (callbackUri != null && callbackMethod != null) {
     97             final String callbackArg = syncExtras.getString(SYNC_EXTRAS_CALLBACK_ARG, "");
     98             final Bundle statusExtras = new Bundle(4);
     99             statusExtras.putInt(SYNC_STATUS_TYPE, statusType);
    100             statusExtras.putLong(SYNC_STATUS_ID, id);
    101             statusExtras.putInt(SYNC_STATUS_CODE, statusCode);
    102             if (statusCode != IN_PROGRESS) {
    103                 statusExtras.putInt(SYNC_RESULT, syncResult);
    104             }
    105             statusExtras.putInt(SYNC_STATUS_PROGRESS, progress);
    106             if (writer != null) {
    107                 writer.addToStatus(statusExtras);
    108             }
    109             cr.call(Uri.parse(callbackUri), callbackMethod, callbackArg, statusExtras);
    110         }
    111     }
    112 
    113     /**
    114      * If the sync extras specify a callback, then notify the sync requester of the mailbox's
    115      * sync status. This function is for use by the
    116      * {@link android.content.AbstractThreadedSyncAdapter}.
    117      * @param cr A ContentResolver.
    118      * @param syncExtras The extras provided to the sync request.
    119      * @param mailboxId The mailbox whose status is changing.
    120      * @param statusCode The status code for this sync operation.
    121      * @param progress The progress of this sync operation.
    122      */
    123     public static void syncMailboxStatus(final ContentResolver cr, final Bundle syncExtras,
    124             final long mailboxId, final int statusCode, final int progress, int syncResult) {
    125         syncStatus(cr, syncExtras, SYNC_STATUS_TYPE_MAILBOX, mailboxId, statusCode, progress,
    126                 syncResult, null);
    127     }
    128 
    129 }
    130