blob: cc914c0c4c44392883ac8bf6c9700f5ce3f100b5 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.content;
18
19import android.os.RemoteException;
20import android.os.SystemClock;
Fred Quintanaf0380042009-10-06 17:05:58 -070021import android.os.IBinder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023public class SyncContext {
24 private ISyncContext mSyncContext;
25 private long mLastHeartbeatSendTime;
26
27 private static final long HEARTBEAT_SEND_INTERVAL_IN_MS = 1000;
28
Fred Quintanaf0380042009-10-06 17:05:58 -070029 /**
30 * @hide
31 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032 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
Fred Quintanaf0380042009-10-06 17:05:58 -070042 *
43 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 */
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 */
Fred Quintanaf0380042009-10-06 17:05:58 -070054 private void updateHeartbeat() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 final long now = SystemClock.elapsedRealtime();
56 if (now < mLastHeartbeatSendTime + HEARTBEAT_SEND_INTERVAL_IN_MS) return;
57 try {
58 mLastHeartbeatSendTime = now;
Fred Quintanae7424ff2009-10-14 15:59:21 -070059 if (mSyncContext != null) {
60 mSyncContext.sendHeartbeat();
61 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 } catch (RemoteException e) {
63 // this should never happen
64 }
65 }
66
67 public void onFinished(SyncResult result) {
68 try {
Fred Quintanae7424ff2009-10-14 15:59:21 -070069 if (mSyncContext != null) {
70 mSyncContext.onFinished(result);
71 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 } catch (RemoteException e) {
73 // this should never happen
74 }
75 }
76
Fred Quintanaf0380042009-10-06 17:05:58 -070077 public IBinder getSyncContextBinder() {
Fred Quintanae7424ff2009-10-14 15:59:21 -070078 return (mSyncContext == null) ? null : mSyncContext.asBinder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 }
80}