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 18package com.android.emailcommon.service; 19 20import android.content.ContentResolver; 21import android.net.Uri; 22import 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 */ 33public 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 // Data is invalid on the client side, sync cannot proceed. 55 public static final int HARD_DATA_ERROR = 0x22; 56 57 // Sync failed due to some type of IO error. 58 public static final int IO_ERROR = 0x23; 59 60 // The sync call encountered a protocol error. 61 public static final int PROTOCOL_ERROR = 0x24; 62 63 // The sync call encountered too many redirects. 64 public static final int TOO_MANY_REDIRECTS = 0x25; 65 66 // The sync call encountered a provisioning error. 67 public static final int PROVISIONING_ERROR = 0x26; 68 69 // We have encountered some sort of unexpected illegal state. 70 public static final int INTERNAL_ERROR = 0x27; 71 72 // Keys for the sync extras Bundle that specify the callback. 73 public static final String SYNC_EXTRAS_CALLBACK_URI = "callback_uri"; 74 public static final String SYNC_EXTRAS_CALLBACK_METHOD = "callback_method"; 75 public static final String SYNC_EXTRAS_CALLBACK_ARG = "callback_arg"; 76 77 // Keys for the status Bundle sent to the callback. These keys are used in every status type. 78 public static final String SYNC_STATUS_TYPE = "type"; 79 public static final String SYNC_STATUS_ID = "id"; 80 public static final String SYNC_STATUS_CODE = "status_code"; 81 public static final String SYNC_RESULT = "result"; 82 public static final String SYNC_STATUS_PROGRESS = "progress"; 83 84 // Values for the SYNC_STATUS_TYPE to specify what kind of sync status we're returning. 85 public static final int SYNC_STATUS_TYPE_MAILBOX = 0; 86 87 /** 88 * Some status updates need to provide values in addition to the core id, code, and progress. 89 * Those updates will provide an appropriate StatusWriter to fill in those extras. 90 */ 91 private static interface StatusWriter { 92 public void addToStatus(final Bundle statusExtras); 93 } 94 95 /** 96 * Generic function to check if the callback is necessary and, if so, perform it. 97 * The function is the common parts for the following functions, which are for use by the 98 * {@link android.content.AbstractThreadedSyncAdapter} to communicate the status of a sync 99 * action to the caller. 100 * @param cr A ContentResolver. 101 * @param syncExtras The extras provided to the sync request. 102 * @param statusType The type of sync status update to send. 103 * @param id The id of the thing whose status is being updated (type depends on statusType). 104 * @param statusCode The status code for this sync operation. 105 * @param progress The progress of this sync operation. 106 * @param writer If not null, an object which will write additional status fields. 107 */ 108 private static void syncStatus(final ContentResolver cr, final Bundle syncExtras, 109 final int statusType, final long id, final int statusCode, final int progress, 110 int syncResult, 111 final StatusWriter writer) { 112 final String callbackUri = syncExtras.getString(SYNC_EXTRAS_CALLBACK_URI); 113 final String callbackMethod = syncExtras.getString(SYNC_EXTRAS_CALLBACK_METHOD); 114 if (callbackUri != null && callbackMethod != null) { 115 final String callbackArg = syncExtras.getString(SYNC_EXTRAS_CALLBACK_ARG, ""); 116 final Bundle statusExtras = new Bundle(4); 117 statusExtras.putInt(SYNC_STATUS_TYPE, statusType); 118 statusExtras.putLong(SYNC_STATUS_ID, id); 119 statusExtras.putInt(SYNC_STATUS_CODE, statusCode); 120 if (statusCode != IN_PROGRESS) { 121 statusExtras.putInt(SYNC_RESULT, syncResult); 122 } 123 statusExtras.putInt(SYNC_STATUS_PROGRESS, progress); 124 if (writer != null) { 125 writer.addToStatus(statusExtras); 126 } 127 cr.call(Uri.parse(callbackUri), callbackMethod, callbackArg, statusExtras); 128 } 129 } 130 131 /** 132 * If the sync extras specify a callback, then notify the sync requester of the mailbox's 133 * sync status. This function is for use by the 134 * {@link android.content.AbstractThreadedSyncAdapter}. 135 * @param cr A ContentResolver. 136 * @param syncExtras The extras provided to the sync request. 137 * @param mailboxId The mailbox whose status is changing. 138 * @param statusCode The status code for this sync operation. 139 * @param progress The progress of this sync operation. 140 */ 141 public static void syncMailboxStatus(final ContentResolver cr, final Bundle syncExtras, 142 final long mailboxId, final int statusCode, final int progress, int syncResult) { 143 syncStatus(cr, syncExtras, SYNC_STATUS_TYPE_MAILBOX, mailboxId, statusCode, progress, 144 syncResult, null); 145 } 146 147 148} 149