EmailServiceProxy.java revision 37f3385803de19d4b6fb4ef1b1ac9a6196dec98c
1/*
2 * Copyright (C) 2009 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
17package com.android.emailcommon.service;
18
19import com.android.emailcommon.Api;
20import com.android.emailcommon.Device;
21import com.android.emailcommon.mail.MessagingException;
22import com.android.emailcommon.provider.EmailContent.HostAuth;
23
24import android.content.Context;
25import android.content.Intent;
26import android.os.Bundle;
27import android.os.IBinder;
28import android.os.RemoteException;
29import android.util.Log;
30
31import java.io.IOException;
32
33/**
34 * The EmailServiceProxy class provides a simple interface for the UI to call into the various
35 * EmailService classes (e.g. ExchangeService for EAS).  It wraps the service connect/disconnect
36 * process so that the caller need not be concerned with it.
37 *
38 * Use the class like this:
39 *   new EmailServiceClass(context, class).loadAttachment(attachmentId, callback)
40 *
41 * Methods without a return value return immediately (i.e. are asynchronous); methods with a
42 * return value wait for a result from the Service (i.e. they should not be called from the UI
43 * thread) with a default timeout of 30 seconds (settable)
44 *
45 * An EmailServiceProxy object cannot be reused (trying to do so generates a RemoteException)
46 */
47
48public class EmailServiceProxy extends ServiceProxy implements IEmailService {
49    private static final String TAG = "EmailServiceProxy";
50
51    // Private intent that will be used to connect to an independent Exchange service
52    public static final String EXCHANGE_INTENT = "com.android.email.EXCHANGE_INTENT";
53
54    public static final String AUTO_DISCOVER_BUNDLE_ERROR_CODE = "autodiscover_error_code";
55    public static final String AUTO_DISCOVER_BUNDLE_HOST_AUTH = "autodiscover_host_auth";
56
57    public static final String VALIDATE_BUNDLE_RESULT_CODE = "validate_result_code";
58    public static final String VALIDATE_BUNDLE_POLICY_SET = "validate_policy_set";
59    public static final String VALIDATE_BUNDLE_ERROR_MESSAGE = "validate_error_message";
60
61    private final IEmailServiceCallback mCallback;
62    private Object mReturn = null;
63    private IEmailService mService;
64
65    // Standard debugging
66    public static final int DEBUG_BIT = 1;
67    // Verbose (parser) logging
68    public static final int DEBUG_VERBOSE_BIT = 2;
69    // File (SD card) logging
70    public static final int DEBUG_FILE_BIT = 4;
71
72    // The first two constructors are used with local services that can be referenced by class
73    public EmailServiceProxy(Context _context, Class<?> _class) {
74        this(_context, _class, null);
75    }
76
77    public EmailServiceProxy(Context _context, Class<?> _class, IEmailServiceCallback _callback) {
78        super(_context, new Intent(_context, _class));
79        mCallback = _callback;
80    }
81
82    // The following two constructors are used with remote services that must be referenced by
83    // a known action or by a prebuilt intent
84    public EmailServiceProxy(Context _context, Intent _intent, IEmailServiceCallback _callback) {
85        super(_context, _intent);
86        try {
87            Device.getDeviceId(_context);
88        } catch (IOException e) {
89        }
90        mCallback = _callback;
91    }
92
93    public EmailServiceProxy(Context _context, String _action, IEmailServiceCallback _callback) {
94        super(_context, new Intent(_action));
95        try {
96            Device.getDeviceId(_context);
97        } catch (IOException e) {
98        }
99        mCallback = _callback;
100    }
101
102    @Override
103    public void onConnected(IBinder binder) {
104        mService = IEmailService.Stub.asInterface(binder);
105    }
106
107    @Override
108    public int getApiLevel() {
109        return Api.LEVEL;
110    }
111
112    public void loadAttachment(final long attachmentId, final boolean background)
113            throws RemoteException {
114        setTask(new ProxyTask() {
115            public void run() throws RemoteException {
116                try {
117                    if (mCallback != null) mService.setCallback(mCallback);
118                    mService.loadAttachment(attachmentId, background);
119                } catch (RemoteException e) {
120                    try {
121                        // Try to send a callback (if set)
122                        if (mCallback != null) {
123                            mCallback.loadAttachmentStatus(-1, attachmentId,
124                                    EmailServiceStatus.REMOTE_EXCEPTION, 0);
125                        }
126                    } catch (RemoteException e1) {
127                    }
128                }
129            }
130        }, "loadAttachment");
131    }
132
133    public void startSync(final long mailboxId, final boolean userRequest) throws RemoteException {
134        setTask(new ProxyTask() {
135            public void run() throws RemoteException {
136                if (mCallback != null) mService.setCallback(mCallback);
137                mService.startSync(mailboxId, userRequest);
138            }
139        }, "startSync");
140    }
141
142    public void stopSync(final long mailboxId) throws RemoteException {
143        setTask(new ProxyTask() {
144            public void run() throws RemoteException {
145                if (mCallback != null) mService.setCallback(mCallback);
146                mService.stopSync(mailboxId);
147            }
148        }, "stopSync");
149    }
150
151    public Bundle validate(final String protocol, final String host, final String userName,
152            final String password, final int port, final boolean ssl,
153            final boolean trustCertificates) throws RemoteException {
154        setTask(new ProxyTask() {
155            public void run() throws RemoteException{
156                if (mCallback != null) mService.setCallback(mCallback);
157                mReturn = mService.validate(protocol, host, userName, password, port, ssl,
158                        trustCertificates);
159            }
160        }, "validate");
161        waitForCompletion();
162        if (mReturn == null) {
163            Bundle bundle = new Bundle();
164            bundle.putInt(VALIDATE_BUNDLE_RESULT_CODE, MessagingException.UNSPECIFIED_EXCEPTION);
165            return bundle;
166        } else {
167            Bundle bundle = (Bundle) mReturn;
168            bundle.setClassLoader(PolicySet.class.getClassLoader());
169            Log.v(TAG, "validate returns " + bundle.getInt(VALIDATE_BUNDLE_RESULT_CODE));
170            return bundle;
171        }
172    }
173
174    public Bundle autoDiscover(final String userName, final String password)
175            throws RemoteException {
176        setTask(new ProxyTask() {
177            public void run() throws RemoteException{
178                if (mCallback != null) mService.setCallback(mCallback);
179                mReturn = mService.autoDiscover(userName, password);
180            }
181        }, "autoDiscover");
182        waitForCompletion();
183        if (mReturn == null) {
184            return null;
185        } else {
186            Bundle bundle = (Bundle) mReturn;
187            bundle.setClassLoader(HostAuth.class.getClassLoader());
188            Log.v(TAG, "autoDiscover returns " + bundle.getInt(AUTO_DISCOVER_BUNDLE_ERROR_CODE));
189            return bundle;
190        }
191    }
192
193    public void updateFolderList(final long accountId) throws RemoteException {
194        setTask(new ProxyTask() {
195            public void run() throws RemoteException {
196                if (mCallback != null) mService.setCallback(mCallback);
197                mService.updateFolderList(accountId);
198            }
199        }, "updateFolderList");
200    }
201
202    public void setLogging(final int on) throws RemoteException {
203        setTask(new ProxyTask() {
204            public void run() throws RemoteException {
205                if (mCallback != null) mService.setCallback(mCallback);
206                mService.setLogging(on);
207            }
208        }, "setLogging");
209    }
210
211    public void setCallback(final IEmailServiceCallback cb) throws RemoteException {
212        setTask(new ProxyTask() {
213            public void run() throws RemoteException {
214                mService.setCallback(cb);
215            }
216        }, "setCallback");
217    }
218
219    public void hostChanged(final long accountId) throws RemoteException {
220        setTask(new ProxyTask() {
221            public void run() throws RemoteException {
222                mService.hostChanged(accountId);
223            }
224        }, "hostChanged");
225    }
226
227    public void sendMeetingResponse(final long messageId, final int response)
228            throws RemoteException {
229        setTask(new ProxyTask() {
230            public void run() throws RemoteException {
231                if (mCallback != null) mService.setCallback(mCallback);
232                mService.sendMeetingResponse(messageId, response);
233            }
234        }, "sendMeetingResponse");
235    }
236
237    public void loadMore(long messageId) throws RemoteException {
238        // TODO Auto-generated method stub
239    }
240
241    public boolean createFolder(long accountId, String name) throws RemoteException {
242        return false;
243    }
244
245    public boolean deleteFolder(long accountId, String name) throws RemoteException {
246        return false;
247    }
248
249    public boolean renameFolder(long accountId, String oldName, String newName)
250            throws RemoteException {
251        return false;
252    }
253
254    public void moveMessage(final long messageId, final long mailboxId) throws RemoteException {
255        setTask(new ProxyTask() {
256            public void run() throws RemoteException {
257                mService.moveMessage(messageId, mailboxId);
258            }
259        }, "moveMessage");
260    }
261
262    public void deleteAccountPIMData(final long accountId) throws RemoteException {
263        setTask(new ProxyTask() {
264            public void run() throws RemoteException {
265                mService.deleteAccountPIMData(accountId);
266            }
267        }, "deleteAccountPIMData");
268    }
269
270    public IBinder asBinder() {
271        return null;
272    }
273}
274