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