1 /*
2  * Copyright (C) 2008 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 package android.content;
18 
19 import android.os.RemoteException;
20 import android.os.SystemClock;
21 import android.os.IBinder;
22 
23 public class SyncContext {
24     private ISyncContext mSyncContext;
25     private long mLastHeartbeatSendTime;
26 
27     private static final long HEARTBEAT_SEND_INTERVAL_IN_MS = 1000;
28 
29     /**
30      * @hide
31      */
SyncContext(ISyncContext syncContextInterface)32     public SyncContext(ISyncContext syncContextInterface) {
33         mSyncContext = syncContextInterface;
34         mLastHeartbeatSendTime = 0;
35     }
36 
37     /**
38      * Call to update the status text for this sync. This internally invokes
39      * {@link #updateHeartbeat}, so it also takes the place of a call to that.
40      *
41      * @param message the current status message for this sync
42      *
43      * @hide
44      */
setStatusText(String message)45     public void setStatusText(String message) {
46         updateHeartbeat();
47     }
48 
49     /**
50      * Call to indicate that the SyncAdapter is making progress. E.g., if this SyncAdapter
51      * downloads or sends records to/from the server, this may be called after each record
52      * is downloaded or uploaded.
53      */
updateHeartbeat()54     private void updateHeartbeat() {
55         final long now = SystemClock.elapsedRealtime();
56         if (now < mLastHeartbeatSendTime + HEARTBEAT_SEND_INTERVAL_IN_MS) return;
57         try {
58             mLastHeartbeatSendTime = now;
59             if (mSyncContext != null) {
60                 mSyncContext.sendHeartbeat();
61             }
62         } catch (RemoteException e) {
63             // this should never happen
64         }
65     }
66 
onFinished(SyncResult result)67     public void onFinished(SyncResult result) {
68         try {
69             if (mSyncContext != null) {
70                 mSyncContext.onFinished(result);
71             }
72         } catch (RemoteException e) {
73             // this should never happen
74         }
75     }
76 
getSyncContextBinder()77     public IBinder getSyncContextBinder() {
78         return (mSyncContext == null) ? null : mSyncContext.asBinder();
79     }
80 }
81