summaryrefslogtreecommitdiff
path: root/java/com/android/voicemailomtp/sync/VvmNetworkRequestCallback.java
blob: 8481a9d16c56cd22279bf2011946b1e6d2971b95 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright (C) 2015 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.voicemailomtp.sync;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.CallSuper;
import android.telecom.PhoneAccountHandle;

import com.android.voicemailomtp.OmtpEvents;
import com.android.voicemailomtp.OmtpVvmCarrierConfigHelper;
import com.android.voicemailomtp.TelephonyManagerStub;
import com.android.voicemailomtp.VoicemailStatus;
import com.android.voicemailomtp.VvmLog;

/**
 * Base class for network request call backs for visual voicemail syncing with the Imap server. This
 * handles retries and network requests.
 */
public abstract class VvmNetworkRequestCallback extends ConnectivityManager.NetworkCallback {

    private static final String TAG = "VvmNetworkRequest";

    // Timeout used to call ConnectivityManager.requestNetwork
    private static final int NETWORK_REQUEST_TIMEOUT_MILLIS = 60 * 1000;

    public static final String NETWORK_REQUEST_FAILED_TIMEOUT = "timeout";
    public static final String NETWORK_REQUEST_FAILED_LOST = "lost";

    protected Context mContext;
    protected PhoneAccountHandle mPhoneAccount;
    protected NetworkRequest mNetworkRequest;
    private ConnectivityManager mConnectivityManager;
    private final OmtpVvmCarrierConfigHelper mCarrierConfigHelper;
    private final VoicemailStatus.Editor mStatus;
    private boolean mRequestSent = false;
    private boolean mResultReceived = false;

    public VvmNetworkRequestCallback(Context context, PhoneAccountHandle phoneAccount,
        VoicemailStatus.Editor status) {
        mContext = context;
        mPhoneAccount = phoneAccount;
        mStatus = status;
        mCarrierConfigHelper = new OmtpVvmCarrierConfigHelper(context, mPhoneAccount);
        mNetworkRequest = createNetworkRequest();
    }

    public VvmNetworkRequestCallback(OmtpVvmCarrierConfigHelper config,
        PhoneAccountHandle phoneAccount, VoicemailStatus.Editor status) {
        mContext = config.getContext();
        mPhoneAccount = phoneAccount;
        mStatus = status;
        mCarrierConfigHelper = config;
        mNetworkRequest = createNetworkRequest();
    }

    public VoicemailStatus.Editor getVoicemailStatusEditor() {
        return mStatus;
    }

    /**
     * @return NetworkRequest for a proper transport type. Use only cellular network if the carrier
     * requires it. Otherwise use whatever available.
     */
    private NetworkRequest createNetworkRequest() {

        NetworkRequest.Builder builder = new NetworkRequest.Builder()
                .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);

        if (mCarrierConfigHelper.isCellularDataRequired()) {
            VvmLog.d(TAG, "Transport type: CELLULAR");
            builder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
                    .setNetworkSpecifier(TelephonyManagerStub
                            .getNetworkSpecifierForPhoneAccountHandle(mContext, mPhoneAccount));
        } else {
            VvmLog.d(TAG, "Transport type: ANY");
        }
        return builder.build();
    }

    public NetworkRequest getNetworkRequest() {
        return mNetworkRequest;
    }

    @Override
    @CallSuper
    public void onLost(Network network) {
        VvmLog.d(TAG, "onLost");
        mResultReceived = true;
        onFailed(NETWORK_REQUEST_FAILED_LOST);
    }

    @Override
    @CallSuper
    public void onAvailable(Network network) {
        super.onAvailable(network);
        mResultReceived = true;
    }

    @CallSuper
    public void onUnavailable() {
        // TODO: b/32637799 this is hidden, do we really need this?
        mResultReceived = true;
        onFailed(NETWORK_REQUEST_FAILED_TIMEOUT);
    }

    public void requestNetwork() {
        if (mRequestSent == true) {
            VvmLog.e(TAG, "requestNetwork() called twice");
            return;
        }
        mRequestSent = true;
        getConnectivityManager().requestNetwork(getNetworkRequest(), this);
        /**
         * Somehow requestNetwork() with timeout doesn't work, and it's a hidden method.
         * Implement our own timeout mechanism instead.
         */
        Handler handler = new Handler(Looper.getMainLooper());
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mResultReceived == false) {
                    onFailed(NETWORK_REQUEST_FAILED_TIMEOUT);
                }
            }
        }, NETWORK_REQUEST_TIMEOUT_MILLIS);
    }

    public void releaseNetwork() {
        VvmLog.d(TAG, "releaseNetwork");
        getConnectivityManager().unregisterNetworkCallback(this);
    }

    public ConnectivityManager getConnectivityManager() {
        if (mConnectivityManager == null) {
            mConnectivityManager = (ConnectivityManager) mContext.getSystemService(
                    Context.CONNECTIVITY_SERVICE);
        }
        return mConnectivityManager;
    }

    @CallSuper
    public void onFailed(String reason) {
        VvmLog.d(TAG, "onFailed: " + reason);
        if (mCarrierConfigHelper.isCellularDataRequired()) {
            mCarrierConfigHelper
                .handleEvent(mStatus, OmtpEvents.DATA_NO_CONNECTION_CELLULAR_REQUIRED);
        } else {
            mCarrierConfigHelper.handleEvent(mStatus, OmtpEvents.DATA_NO_CONNECTION);
        }
        releaseNetwork();
    }
}