1/*
2 * Copyright (C) 2014 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.server.telecom;
18
19import android.content.ContentResolver;
20import android.provider.Settings;
21
22/**
23 * A helper class which serves only to make it easier to lookup timeout values. This class should
24 * never be instantiated, and only accessed through the {@link #get(String, long)} method.
25 *
26 * These methods are safe to call from any thread, including the UI thread.
27 */
28public final class Timeouts {
29    public static class Adapter {
30        public Adapter() { }
31
32        public long getCallScreeningTimeoutMillis(ContentResolver cr) {
33            return Timeouts.getCallScreeningTimeoutMillis(cr);
34        }
35
36        public long getCallRemoveUnbindInCallServicesDelay(ContentResolver cr) {
37            return Timeouts.getCallRemoveUnbindInCallServicesDelay(cr);
38        }
39    }
40
41    /** A prefix to use for all keys so to not clobber the global namespace. */
42    private static final String PREFIX = "telecom.";
43
44    private Timeouts() {}
45
46    /**
47     * Returns the timeout value from Settings or the default value if it hasn't been changed. This
48     * method is safe to call from any thread, including the UI thread.
49     *
50     * @param contentResolver The content resolved.
51     * @param key Settings key to retrieve.
52     * @param defaultValue Default value, in milliseconds.
53     * @return The timeout value from Settings or the default value if it hasn't been changed.
54     */
55    private static long get(ContentResolver contentResolver, String key, long defaultValue) {
56        return Settings.Secure.getLong(contentResolver, PREFIX + key, defaultValue);
57    }
58
59    /**
60     * Returns the amount of time to wait before disconnecting a call that was canceled via
61     * NEW_OUTGOING_CALL broadcast. This timeout allows apps which repost the call using a gateway
62     * to reuse the existing call, preventing the call from causing a start->end->start jank in the
63     * in-call UI.
64     */
65    public static long getNewOutgoingCallCancelMillis(ContentResolver contentResolver) {
66        return get(contentResolver, "new_outgoing_call_cancel_ms", 500L);
67    }
68
69    /**
70     * Returns the maximum amount of time to wait before disconnecting a call that was canceled via
71     * NEW_OUTGOING_CALL broadcast. This prevents malicious or poorly configured apps from
72     * forever tying up the Telecom stack.
73     */
74    public static long getMaxNewOutgoingCallCancelMillis(ContentResolver contentResolver) {
75        return get(contentResolver, "max_new_outgoing_call_cancel_ms", 10000L);
76    }
77
78    /**
79     * Returns the amount of time to play each DTMF tone after post dial continue.
80     * This timeout allows the current tone to play for a certain amount of time before either being
81     * interrupted by the next tone or terminated.
82     */
83    public static long getDelayBetweenDtmfTonesMillis(ContentResolver contentResolver) {
84        return get(contentResolver, "delay_between_dtmf_tones_ms", 300L);
85    }
86
87    /**
88     * Returns the amount of time to wait for an emergency call to be placed before routing to
89     * a different call service. A value of 0 or less means no timeout should be used.
90     */
91    public static long getEmergencyCallTimeoutMillis(ContentResolver contentResolver) {
92        return get(contentResolver, "emergency_call_timeout_millis", 25000L /* 25 seconds */);
93    }
94
95    /**
96     * Returns the amount of time to wait for an emergency call to be placed before routing to
97     * a different call service. This timeout is used only when the radio is powered off (for
98     * example in airplane mode). A value of 0 or less means no timeout should be used.
99     */
100    public static long getEmergencyCallTimeoutRadioOffMillis(ContentResolver contentResolver) {
101        return get(contentResolver, "emergency_call_timeout_radio_off_millis",
102                60000L /* 1 minute */);
103    }
104
105    /**
106     * Returns the amount of delay before unbinding the in-call services after all the calls
107     * are removed.
108     */
109    public static long getCallRemoveUnbindInCallServicesDelay(ContentResolver contentResolver) {
110        return get(contentResolver, "call_remove_unbind_in_call_services_delay",
111                2000L /* 2 seconds */);
112    }
113
114    /**
115     * Returns the amount of time for which bluetooth is considered connected after requesting
116     * connection. This compensates for the amount of time it takes for the audio route to
117     * actually change to bluetooth.
118     */
119    public static long getBluetoothPendingTimeoutMillis(ContentResolver contentResolver) {
120        return get(contentResolver, "bluetooth_pending_timeout_millis", 5000L);
121    }
122
123    /**
124     * Returns the amount of time to wait before retrying the connectAudio call. This is
125     * necessary to account for the HeadsetStateMachine sometimes not being ready when we want to
126     * connect to bluetooth audio immediately after a device connects.
127     */
128    public static long getRetryBluetoothConnectAudioBackoffMillis(ContentResolver contentResolver) {
129        return get(contentResolver, "retry_bluetooth_connect_audio_backoff_millis", 500L);
130    }
131
132    /**
133     * Returns the amount of time after a Logging session has been started that Telecom is set to
134     * perform a sweep to check and make sure that the session is still not incomplete (stale).
135     */
136    public static long getStaleSessionCleanupTimeoutMillis(ContentResolver contentResolver) {
137        return get(contentResolver, "stale_session_cleanup_timeout_millis",
138                Log.DEFAULT_SESSION_TIMEOUT_MS);
139    }
140
141    /**
142     * Returns the amount of time to wait for the call screening service to allow or disallow a
143     * call.
144     */
145    public static long getCallScreeningTimeoutMillis(ContentResolver contentResolver) {
146        return get(contentResolver, "call_screening_timeout", 5000L /* 5 seconds */);
147    }
148}
149