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