PingTask.java revision 3eef378426c7c88608f53f5a268baed40259ccf6
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 android.accounts.Account amAccount,
39            final EmailSyncAdapterService.SyncHandlerSynchronizer syncHandlerMap) {
40        mOperation = new EasPing(context, account, amAccount);
41        mSyncHandlerMap = syncHandlerMap;
42    }
43
44    /** Start the ping loop. */
45    public void start() {
46        executeOnExecutor(THREAD_POOL_EXECUTOR);
47    }
48
49    /** Abort the ping loop (used when another operation interrupts the ping). */
50    public void stop() {
51        mOperation.abort();
52    }
53
54    /** Restart the ping loop (used when a ping request happens during a ping). */
55    public void restart() {
56        mOperation.restart();
57    }
58
59    @Override
60    protected Void doInBackground(Void... params) {
61        Log.i(TAG, "Ping task starting");
62        int pingStatus;
63        do {
64            pingStatus = mOperation.doPing();
65        } while (PingParser.shouldPingAgain(pingStatus));
66        Log.i(TAG, "Ping task ending with status: " + pingStatus);
67
68        mSyncHandlerMap.pingComplete(mOperation.getAmAccount(), mOperation.getAccountId(),
69                pingStatus);
70        return null;
71    }
72}
73