summaryrefslogtreecommitdiff
path: root/src/com/android/server/telecom/voip/VoipCallTransaction.java
blob: a952eb136b50a90c8ae05605f986375dd72a5f6c (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
/*
 * Copyright (C) 2022 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.server.telecom.voip;

import android.os.Handler;
import android.os.HandlerThread;
import android.telecom.Log;

import com.android.server.telecom.LoggedHandlerExecutor;
import com.android.server.telecom.TelecomSystem;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;

public class VoipCallTransaction {
    //TODO: add log events
    protected static final long TIMEOUT_LIMIT = 5000L;
    protected final AtomicBoolean mCompleted = new AtomicBoolean(false);
    protected String mTransactionName = this.getClass().getSimpleName();
    private HandlerThread mHandlerThread;
    protected Handler mHandler;
    protected TransactionManager.TransactionCompleteListener mCompleteListener;
    protected List<VoipCallTransaction> mSubTransactions;
    protected TelecomSystem.SyncRoot mLock;

    public VoipCallTransaction(
            List<VoipCallTransaction> subTransactions, TelecomSystem.SyncRoot lock) {
        mSubTransactions = subTransactions;
        mHandlerThread = new HandlerThread(this.toString());
        mHandlerThread.start();
        mHandler = new Handler(mHandlerThread.getLooper());
        mLock = lock;
    }

    public VoipCallTransaction(TelecomSystem.SyncRoot lock) {
        this(null /** mSubTransactions */, lock);
    }

    public void start() {
        // post timeout work
        CompletableFuture<Void> future = new CompletableFuture<>();
        mHandler.postDelayed(() -> future.complete(null), TIMEOUT_LIMIT);
        future.thenApplyAsync((x) -> {
            if (mCompleted.getAndSet(true)) {
                return null;
            }
            if (mCompleteListener != null) {
                mCompleteListener.onTransactionTimeout(mTransactionName);
            }
            finish();
            return null;
        }, new LoggedHandlerExecutor(mHandler, mTransactionName + "@" + hashCode()
                + ".s", mLock));

        scheduleTransaction();
    }

    protected void scheduleTransaction() {
        LoggedHandlerExecutor executor = new LoggedHandlerExecutor(mHandler,
                mTransactionName + "@" + hashCode() + ".pT", mLock);
        CompletableFuture<Void> future = CompletableFuture.completedFuture(null);
        future.thenComposeAsync(this::processTransaction, executor)
                .thenApplyAsync((Function<VoipCallTransactionResult, Void>) result -> {
                    mCompleted.set(true);
                    if (mCompleteListener != null) {
                        mCompleteListener.onTransactionCompleted(result, mTransactionName);
                    }
                    finish();
                    return null;
                    }, executor)
                .exceptionallyAsync((throwable -> {
                    Log.e(this, throwable, "Error while executing transaction.");
                    return null;
                }), executor);
    }

    public CompletionStage<VoipCallTransactionResult> processTransaction(Void v) {
        return CompletableFuture.completedFuture(
                new VoipCallTransactionResult(VoipCallTransactionResult.RESULT_SUCCEED, null));
    }

    public void setCompleteListener(TransactionManager.TransactionCompleteListener listener) {
        mCompleteListener = listener;
    }

    public void finish() {
        // finish all sub transactions
        if (mSubTransactions != null && mSubTransactions.size() > 0) {
            mSubTransactions.forEach(VoipCallTransaction::finish);
        }
        mHandlerThread.quit();
    }
}