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 17 package com.android.exchange.service; 18 19 import android.content.Context; 20 import android.os.AsyncTask; 21 22 import com.android.emailcommon.provider.Account; 23 import com.android.exchange.Eas; 24 import com.android.exchange.adapter.PingParser; 25 import com.android.exchange.eas.EasOperation; 26 import com.android.exchange.eas.EasPing; 27 import com.android.mail.utils.LogUtils; 28 29 /** 30 * Thread management class for Ping operations. 31 */ 32 public class PingTask extends AsyncTask<Void, Void, Void> { 33 private final EasPing mOperation; 34 private final PingSyncSynchronizer mPingSyncSynchronizer; 35 36 private static final String TAG = Eas.LOG_TAG; 37 38 PingTask(final Context context, final Account account, final android.accounts.Account amAccount, final PingSyncSynchronizer pingSyncSynchronizer)39 public PingTask(final Context context, final Account account, 40 final android.accounts.Account amAccount, 41 final PingSyncSynchronizer pingSyncSynchronizer) { 42 assert pingSyncSynchronizer != null; 43 mOperation = new EasPing(context, account, amAccount); 44 mPingSyncSynchronizer = pingSyncSynchronizer; 45 } 46 47 /** Start the ping loop. */ start()48 public void start() { 49 executeOnExecutor(THREAD_POOL_EXECUTOR); 50 } 51 52 /** Abort the ping loop (used when another operation interrupts the ping). */ stop()53 public void stop() { 54 mOperation.abort(); 55 } 56 57 /** Restart the ping loop (used when a ping request happens during a ping). */ restart()58 public void restart() { 59 mOperation.restart(); 60 } 61 62 @Override doInBackground(Void... params)63 protected Void doInBackground(Void... params) { 64 LogUtils.i(TAG, "Ping task starting for %d", mOperation.getAccountId()); 65 int pingStatus; 66 try { 67 do { 68 pingStatus = mOperation.doPing(); 69 } while (PingParser.shouldPingAgain(pingStatus)); 70 } catch (final Exception e) { 71 // TODO: This is hacky, try to be cleaner. 72 // If we get any sort of exception here, treat it like the ping returned a connection 73 // failure. 74 LogUtils.e(TAG, e, "Ping exception for account %d", mOperation.getAccountId()); 75 pingStatus = EasOperation.RESULT_NETWORK_PROBLEM; 76 } 77 LogUtils.i(TAG, "Ping task ending with status: %d", pingStatus); 78 79 mPingSyncSynchronizer.pingEnd(mOperation.getAccountId(), mOperation.getAmAccount()); 80 return null; 81 } 82 83 @Override onCancelled(Void result)84 protected void onCancelled (Void result) { 85 // TODO: This is also hacky, should have a separate result code at minimum. 86 // If the ping is cancelled, make sure it reports something to the sync adapter. 87 LogUtils.w(TAG, "Ping cancelled for %d", mOperation.getAccountId()); 88 mPingSyncSynchronizer.pingEnd(mOperation.getAccountId(), mOperation.getAmAccount()); 89 } 90 } 91