Home | History | Annotate | Download | only in service
      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 EmailSyncAdapterService.SyncHandlerSynchronizer mSyncHandlerMap;
     35 
     36     private static final String TAG = Eas.LOG_TAG;
     37 
     38     public PingTask(final Context context, final Account account,
     39             final android.accounts.Account amAccount,
     40             final EmailSyncAdapterService.SyncHandlerSynchronizer syncHandlerMap) {
     41         mOperation = new EasPing(context, account, amAccount);
     42         mSyncHandlerMap = syncHandlerMap;
     43     }
     44 
     45     /** Start the ping loop. */
     46     public void start() {
     47         executeOnExecutor(THREAD_POOL_EXECUTOR);
     48     }
     49 
     50     /** Abort the ping loop (used when another operation interrupts the ping). */
     51     public void stop() {
     52         mOperation.abort();
     53     }
     54 
     55     /** Restart the ping loop (used when a ping request happens during a ping). */
     56     public void restart() {
     57         mOperation.restart();
     58     }
     59 
     60     @Override
     61     protected Void doInBackground(Void... params) {
     62         LogUtils.i(TAG, "Ping task starting for %d", mOperation.getAccountId());
     63         int pingStatus;
     64         try {
     65             do {
     66                 pingStatus = mOperation.doPing();
     67             } while (PingParser.shouldPingAgain(pingStatus));
     68         } catch (final Exception e) {
     69             // TODO: This is hacky, try to be cleaner.
     70             // If we get any sort of exception here, treat it like the ping returned a connection
     71             // failure.
     72             LogUtils.e(TAG, e, "Ping exception for account %d", mOperation.getAccountId());
     73             pingStatus = EasOperation.RESULT_REQUEST_FAILURE;
     74         }
     75         LogUtils.i(TAG, "Ping task ending with status: %d", pingStatus);
     76 
     77         mSyncHandlerMap.pingComplete(mOperation.getAmAccount(), mOperation.getAccountId(),
     78                 pingStatus);
     79         return null;
     80     }
     81 
     82     @Override
     83     protected void onCancelled (Void result) {
     84         // TODO: This is also hacky, should have a separate result code at minimum.
     85         // If the ping is cancelled, make sure it reports something to the sync adapter.
     86         LogUtils.w(TAG, "Ping cancelled for %d", mOperation.getAccountId());
     87         mSyncHandlerMap.pingComplete(mOperation.getAmAccount(), mOperation.getAccountId(),
     88                 EasOperation.RESULT_REQUEST_FAILURE);
     89     }
     90 }
     91