summaryrefslogtreecommitdiff
path: root/src/com/android/exchange/service/PingTask.java
blob: a016556b898dcf79cd444747774304808142bff3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.exchange.service;

import android.content.Context;
import android.os.AsyncTask;

import com.android.emailcommon.provider.Account;
import com.android.exchange.Eas;
import com.android.exchange.adapter.PingParser;
import com.android.exchange.eas.EasOperation;
import com.android.exchange.eas.EasPing;
import com.android.mail.utils.LogUtils;

/**
 * Thread management class for Ping operations.
 */
public class PingTask extends AsyncTask<Void, Void, Void> {
    private final EasPing mOperation;
    // TODO: Transition away from mSyncHandlerMap -> mPingSyncSynchronizer.
    private final EmailSyncAdapterService.SyncHandlerSynchronizer mSyncHandlerMap;
    private final PingSyncSynchronizer mPingSyncSynchronizer;

    private static final String TAG = Eas.LOG_TAG;

    public PingTask(final Context context, final Account account,
            final android.accounts.Account amAccount,
            final EmailSyncAdapterService.SyncHandlerSynchronizer syncHandlerMap) {
        assert syncHandlerMap != null;
        mOperation = new EasPing(context, account, amAccount);
        mSyncHandlerMap = syncHandlerMap;
        mPingSyncSynchronizer = null;
    }

    public PingTask(final Context context, final Account account,
            final android.accounts.Account amAccount,
            final PingSyncSynchronizer pingSyncSynchronizer) {
        assert pingSyncSynchronizer != null;
        mOperation = new EasPing(context, account, amAccount);
        mSyncHandlerMap = null;
        mPingSyncSynchronizer = pingSyncSynchronizer;
    }

    /** Start the ping loop. */
    public void start() {
        executeOnExecutor(THREAD_POOL_EXECUTOR);
    }

    /** Abort the ping loop (used when another operation interrupts the ping). */
    public void stop() {
        mOperation.abort();
    }

    /** Restart the ping loop (used when a ping request happens during a ping). */
    public void restart() {
        mOperation.restart();
    }

    @Override
    protected Void doInBackground(Void... params) {
        LogUtils.i(TAG, "Ping task starting for %d", mOperation.getAccountId());
        int pingStatus;
        try {
            do {
                pingStatus = mOperation.doPing();
            } while (PingParser.shouldPingAgain(pingStatus));
        } catch (final Exception e) {
            // TODO: This is hacky, try to be cleaner.
            // If we get any sort of exception here, treat it like the ping returned a connection
            // failure.
            LogUtils.e(TAG, e, "Ping exception for account %d", mOperation.getAccountId());
            pingStatus = EasOperation.RESULT_REQUEST_FAILURE;
        }
        LogUtils.i(TAG, "Ping task ending with status: %d", pingStatus);

        if (mSyncHandlerMap != null) {
            mSyncHandlerMap.pingComplete(mOperation.getAmAccount(), mOperation.getAccountId(),
                    pingStatus);
        } else {
            // TODO: Fix the pushEnabled param.
            mPingSyncSynchronizer.pingEnd(mOperation.getAccountId(), false);
        }
        return null;
    }

    @Override
    protected void onCancelled (Void result) {
        // TODO: This is also hacky, should have a separate result code at minimum.
        // If the ping is cancelled, make sure it reports something to the sync adapter.
        LogUtils.w(TAG, "Ping cancelled for %d", mOperation.getAccountId());
        if (mSyncHandlerMap != null) {
            mSyncHandlerMap.pingComplete(mOperation.getAmAccount(), mOperation.getAccountId(),
                    EasOperation.RESULT_REQUEST_FAILURE);
        } else {
            // TODO: Fix the pushEnabled param.
            mPingSyncSynchronizer.pingEnd(mOperation.getAccountId(), false);
        }
    }
}