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