1ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu/* 2ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu * Copyright (C) 2013 The Android Open Source Project 3ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu * 4ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu * Licensed under the Apache License, Version 2.0 (the "License"); 5ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu * you may not use this file except in compliance with the License. 6ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu * You may obtain a copy of the License at 7ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu * 8ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu * http://www.apache.org/licenses/LICENSE-2.0 9ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu * 10ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu * Unless required by applicable law or agreed to in writing, software 11ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu * distributed under the License is distributed on an "AS IS" BASIS, 12ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu * See the License for the specific language governing permissions and 14ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu * limitations under the License. 15ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu */ 16ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu 17ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hupackage com.android.exchange.service; 18ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu 19ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Huimport android.content.Context; 20ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Huimport android.os.AsyncTask; 21ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu 22ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Huimport com.android.emailcommon.provider.Account; 23edbddd0bb01510cd8dde432636be00b6a8ed1aa6Alon Albertimport com.android.exchange.Eas; 24ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Huimport com.android.exchange.adapter.PingParser; 25c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Huimport com.android.exchange.eas.EasOperation; 26ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Huimport com.android.exchange.eas.EasPing; 27c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Huimport com.android.mail.utils.LogUtils; 28ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu 29ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu/** 30ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu * Thread management class for Ping operations. 31ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu */ 32ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hupublic class PingTask extends AsyncTask<Void, Void, Void> { 33ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu private final EasPing mOperation; 346c4254903d2f42836c5b74a934e54441ccee6dbeYu Ping Hu private final PingSyncSynchronizer mPingSyncSynchronizer; 35ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu 36edbddd0bb01510cd8dde432636be00b6a8ed1aa6Alon Albert private static final String TAG = Eas.LOG_TAG; 37edbddd0bb01510cd8dde432636be00b6a8ed1aa6Alon Albert 386c4254903d2f42836c5b74a934e54441ccee6dbeYu Ping Hu 396c4254903d2f42836c5b74a934e54441ccee6dbeYu Ping Hu public PingTask(final Context context, final Account account, 406c4254903d2f42836c5b74a934e54441ccee6dbeYu Ping Hu final android.accounts.Account amAccount, 416c4254903d2f42836c5b74a934e54441ccee6dbeYu Ping Hu final PingSyncSynchronizer pingSyncSynchronizer) { 426c4254903d2f42836c5b74a934e54441ccee6dbeYu Ping Hu assert pingSyncSynchronizer != null; 436c4254903d2f42836c5b74a934e54441ccee6dbeYu Ping Hu mOperation = new EasPing(context, account, amAccount); 446c4254903d2f42836c5b74a934e54441ccee6dbeYu Ping Hu mPingSyncSynchronizer = pingSyncSynchronizer; 45ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu } 46ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu 47ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu /** Start the ping loop. */ 48ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu public void start() { 49ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu executeOnExecutor(THREAD_POOL_EXECUTOR); 50ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu } 51ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu 52ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu /** Abort the ping loop (used when another operation interrupts the ping). */ 53ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu public void stop() { 54ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu mOperation.abort(); 55ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu } 56ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu 57ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu /** Restart the ping loop (used when a ping request happens during a ping). */ 58ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu public void restart() { 59ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu mOperation.restart(); 60ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu } 61ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu 62ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu @Override 63ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu protected Void doInBackground(Void... params) { 64c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu LogUtils.i(TAG, "Ping task starting for %d", mOperation.getAccountId()); 65ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu int pingStatus; 66c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu try { 67c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu do { 68c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu pingStatus = mOperation.doPing(); 69c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu } while (PingParser.shouldPingAgain(pingStatus)); 70c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu } catch (final Exception e) { 71c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu // TODO: This is hacky, try to be cleaner. 72c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu // If we get any sort of exception here, treat it like the ping returned a connection 73c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu // failure. 74c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu LogUtils.e(TAG, e, "Ping exception for account %d", mOperation.getAccountId()); 7561ebb38e67421fd122c81f046bf11778b61a2113Martin Hibdon pingStatus = EasOperation.RESULT_NETWORK_PROBLEM; 76c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu } 77c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu LogUtils.i(TAG, "Ping task ending with status: %d", pingStatus); 78ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu 7992c06e3394732f9de61b5f6619800f9ea1a75760Martin Hibdon mPingSyncSynchronizer.pingEnd(mOperation.getAccountId(), mOperation.getAmAccount()); 80ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu return null; 81ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu } 82c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu 83c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu @Override 84c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu protected void onCancelled (Void result) { 85c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu // TODO: This is also hacky, should have a separate result code at minimum. 86c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu // If the ping is cancelled, make sure it reports something to the sync adapter. 87c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu LogUtils.w(TAG, "Ping cancelled for %d", mOperation.getAccountId()); 8892c06e3394732f9de61b5f6619800f9ea1a75760Martin Hibdon mPingSyncSynchronizer.pingEnd(mOperation.getAccountId(), mOperation.getAmAccount()); 89c4bcd0a810b1a8c28f48fa8ef9e43a7d7ad252feYu Ping Hu } 90ff7e02603bc8196f411c0c491d74a42e747b7dc5Yu Ping Hu} 91