OSDN Git Service

17ae93e426578d6b8ec18f6a899fe66dad09048d
[android-x86/packages-apps-IM.git] / src / com / android / im / imps / AsyncTransaction.java
1 /*
2  * Copyright (C) 2007 Esmertec AG.
3  * Copyright (C) 2007 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.im.imps;
19
20 import com.android.im.engine.ImErrorInfo;
21
22 abstract class AsyncTransaction extends ImpsTransaction {
23
24     private final AsyncCompletion mCompletionCallback;
25     private boolean mCompletionNotified;
26     protected final ImpsTransactionManager mTransManager;
27
28     AsyncTransaction(ImpsTransactionManager manager) {
29         this(manager, null);
30     }
31
32     AsyncTransaction(ImpsTransactionManager manager, AsyncCompletion completion) {
33         mTransManager = manager;
34         mCompletionCallback = completion;
35         manager.beginClientTransaction(this);
36     }
37
38     /**
39      * Sends a request within this transaction.
40      *
41      * @param request the request to send.
42      */
43     public void sendRequest(Primitive request) {
44         sendPrimitive(request);
45     }
46
47     /**
48      * Notify that an error occurs in the transaction.
49      *
50      * @param error the error
51      */
52     final void notifyError(ImErrorInfo error) {
53         notifyErrorResponse(new ImpsErrorInfo(error.getCode(), error.getDescription(), null));
54     }
55
56     /**
57      * Notify that a response from the server has arrived.
58      *
59      * @param response the response.
60      */
61     final void notifyResponse(Primitive response) {
62         response.setTransaction(this);
63         ImpsErrorInfo error = ImpsUtils.checkResultError(response);
64         if (error != null) {
65             notifyErrorResponse(error);
66         } else {
67             notifySuccessResponse(response);
68         }
69     }
70
71     protected void notifyErrorResponse(ImpsErrorInfo error) {
72         onResponseError(error);
73         mTransManager.endClientTransaction(this);
74         notifyAsyncCompletionError(error);
75     }
76
77     protected void notifySuccessResponse(Primitive response) {
78         onResponseOk(response);
79         mTransManager.endClientTransaction(this);
80         notifyAsyncCompletionSuccess();
81     }
82
83     public abstract void onResponseError(ImpsErrorInfo error);
84     public abstract void onResponseOk(Primitive response);
85
86     protected void notifyAsyncCompletionError(ImErrorInfo error) {
87         if (!mCompletionNotified) {
88             mCompletionNotified = true;
89             if (mCompletionCallback != null)
90                 mCompletionCallback.onError(error);
91         }
92     }
93
94     protected void notifyAsyncCompletionSuccess() {
95         if (!mCompletionNotified) {
96             mCompletionNotified = true;
97             if (mCompletionCallback != null)
98                 mCompletionCallback.onComplete();
99         }
100     }
101 }