PingTask.java revision edbddd0bb01510cd8dde432636be00b6a8ed1aa6
1/* 2 * Copyright (C) 2013 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.exchange.service; 18 19import android.content.Context; 20import android.os.AsyncTask; 21import android.util.Log; 22 23import com.android.emailcommon.provider.Account; 24import com.android.exchange.Eas; 25import com.android.exchange.adapter.PingParser; 26import com.android.exchange.eas.EasPing; 27 28/** 29 * Thread management class for Ping operations. 30 */ 31public class PingTask extends AsyncTask<Void, Void, Void> { 32 private final EasPing mOperation; 33 private final EmailSyncAdapterService.SyncHandlerSynchronizer mSyncHandlerMap; 34 35 private static final String TAG = Eas.LOG_TAG; 36 37 public PingTask(final Context context, final Account account, 38 final EmailSyncAdapterService.SyncHandlerSynchronizer syncHandlerMap) { 39 mOperation = new EasPing(context, account); 40 mSyncHandlerMap = syncHandlerMap; 41 } 42 43 /** Start the ping loop. */ 44 public void start() { 45 executeOnExecutor(THREAD_POOL_EXECUTOR); 46 } 47 48 /** Abort the ping loop (used when another operation interrupts the ping). */ 49 public void stop() { 50 mOperation.abort(); 51 } 52 53 /** Restart the ping loop (used when a ping request happens during a ping). */ 54 public void restart() { 55 mOperation.restart(); 56 } 57 58 @Override 59 protected Void doInBackground(Void... params) { 60 int pingStatus; 61 do { 62 pingStatus = mOperation.doPing(); 63 } while (PingParser.shouldPingAgain(pingStatus)); 64 Log.i(TAG, "Ping task ending with status: " + pingStatus); 65 66 mSyncHandlerMap.pingComplete(mOperation.getAmAccount(), mOperation.getAccountId(), 67 pingStatus); 68 return null; 69 } 70} 71