1page.title=Implementing Block Phone Numbers
2@jd:body
3
4<!--
5    Copyright 2016 The Android Open Source Project
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18-->
19
20<div id="qv-wrapper">
21  <div id="qv">
22    <h2>In this document</h2>
23    <ol id="auto-toc">
24    </ol>
25  </div>
26</div>
27
28<p>
29Because telephony is such an open communications channel - anyone may call or
30text any number at any time - Android users need the ability to easily block
31unwanted calls and texts.
32</p>
33
34<p>
35Before N, Android users had to rely on downloaded apps to restrict calls and
36texts from bothersome phone numbers. Many of those apps either do not work as
37desired or provide a less-than ideal experience because there are no proper APIs
38for blocking calls and messages.
39</p>
40
41<p>
42Some manufacturers might ship their own blocking solutions out-of-the-box, but
43if users switch devices, they may lose the blocked list completely due to lack
44of interoperability. Finally, even if users are employing dialing apps and
45messaging clients that provide such functionality, they likely still have to
46perform the block action in each app for the block to take effect for both
47calling and texting.
48</p>
49
50<h2 id="features">Features</h2>
51
52<p>
53The Android 7.0 release introduces a <code>BlockedNumberProvider</code> content
54provider that stores a list of phone numbers the user has specified should not
55be able to contact them via telephony communications (calls, SMS, MMS). The
56system will respect the numbers in the blocked list by restricting calls and
57texts from those numbers. Android 7.0 displays the list of blocked numbers and
58allows the user to add and remove numbers.
59</p>
60
61<p>
62Further, the number-blocking feature enables the system and the relevant apps on
63the platform to work together to help protect the user and to simplify the
64experience. The default dialer, default messaging client, UICC-privileged app,
65and apps with the same signature as the system can all directly read from and
66write to the blocked list. Because the blocked numbers are stored on the system,
67no matter what dialing or messaging apps the user employs, the numbers stay
68blocked. Finally, the blocked numbers list may be restored on any new device,
69regardless of the manufacturer.
70</p>
71
72<ul>
73<li>User will be guaranteed to have a blocking feature that works out-of-the-box
74and will not lose their block list when they switch apps or get a new phone. All
75relevant apps on the system can share the same list to provide the user with the
76most streamlined experience.
77<li>App developers do not need to develop their own way to manage a block list
78and the calls and messages that come in. They can simply use the
79platform-provided feature.
80<li>Dialer / messenger apps that are selected as the default by the user can
81read and write to the provider. Other apps can launch the block list management
82user interface by using <code>createManageBlockedNumbersIntent()</code>
83<li>OEMs can use platform provided feature to ship a blocking feature
84out-of-the-box. OEMs can rest assured that when users switch from another OEM’s
85device that they have a better onboarding experience because the block list will
86be transferred as well.
87<li>If carrier has their own dialer or messenger app, they can reuse platform
88feature for allowing the user to maintain a block list. They can rest assured
89that the user’s block list can stay with the users, even when they get a new
90device. Finally, all carrier-privileged apps can read the block list, so if the
91carrier wants to provide some additional more powerful blocking for the user
92based on the block list, that is now possible with this feature.</li></ul>
93
94<h2 id="data-flow">Data flow</h2>
95
96<img src="images/block-numbers-flow.png" alt="block numbers data flow" width="642" id="block-numbers-flow" />
97<p class="img-caption">
98  <strong>Figure 1.</strong> Block phone numbers data flow
99</p>
100
101<h2 id="examples-and-source">Examples and source</h2>
102
103<p>
104Here are example calls using the number-blocking new feature:
105</p>
106
107<h3 id="launch-from-app">Launch blocked number manager from app</h3>
108
109<pre>
110Context.startActivity(telecomManager.createManageBlockedNumbersIntent(), null);
111</pre>
112
113<h3 id="query-blocked-numbers">Query blocked numbers</h3>
114
115<pre>
116Cursor c = getContentResolver().query(BlockedNumbers.CONTENT_URI,
117         new String[]{BlockedNumbers.COLUMN_ID,
118         BlockedNumbers.COLUMN_ORIGINAL_NUMBER,
119         BlockedNumbers.COLUMN_E164_NUMBER}, null, null, null);
120</pre>
121
122<h3 id="put-blocked-number">Put blocked number</h3>
123
124<pre>
125ContentValues values = new ContentValues();
126values.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, "1234567890");
127Uri uri = getContentResolver().insert(BlockedNumbers.CONTENT_URI, values);
128</pre>
129
130<h3 id="delete-blocked-number">Delete blocked number</h3>
131
132<pre>
133ContentValues values = new ContentValues();
134values.put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, "1234567890");
135Uri uri = getContentResolver().insert(BlockedNumbers.CONTENT_URI, values);
136getContentResolver().delete(uri, null, null);
137</pre>
138
139<h2 id="implementation">Implementation</h2>
140
141<p>
142These are the high-level tasks that must be completed to put the number-blocking
143feature to use:
144</p>
145
146<ul>
147<li>OEMs implement call/message-restriction features on their devices by using
148<code>BlockedNumberProvider</code>
149<li>If carrier has dialer or messenger application, implement call/message
150restriction features by using <code>BlockedNumberProvider</code>
151<li>Third-party dialer and messenger app vendors use
152<code>BlockedNumberProvider</code> for their blocking features</li>
153</ul>
154
155<h3 id="recommendations-for-oems">Recommendations for OEMs</h3>
156
157<p>
158If the device had previously never shipped with any additional call/message
159restriction features, use the number-blocking feature in the Android Open Source
160Project (AOSP) on all such devices. It is recommended that reasonable entry
161points for blocking are supported, such as blocking a number right from the call
162log or within a message thread.
163</p>
164
165<p>
166If the device had previously shipped with call/message restriction features,
167adapt the features so all <em>strict-match phone numbers</em> that are blocked
168are stored in the <code>BlockedNumberProvider,</code> and that the behavior
169around the provider satisfy the requirements for this feature outlined in the
170Android Compatibility Definition Document (CDD).
171</p>
172
173<p>
174Any other advanced feature can be implemented via custom providers and custom UI
175/ controls, as long as the CDD requirements are satisfied with regards to
176blocking strict-match phone numbers. It is recommended that those other features
177be labeled as “advanced” features to avoid confusion with the basic
178number-blocking feature.
179</p>
180
181<h3 id="apis">APIs</h3>
182
183<p>
184Here are the APIs in use:
185</p>
186<ul>
187<li><code><a
188href="http://developer.android.com/reference/android/telecom/TelecomManager.html">TelecomManager</a>
189API</code>
190  <ul>
191 <li><code>Intent createManageBlockedNumbersIntent()</code>
192  </ul>
193</li>
194<li><code><a
195href="http://developer.android.com/reference/android/telephony/CarrierConfigManager.html">Carrier
196Config</a></code>
197  <ul>
198 <li><code>KEY_DURATION_BLOCKING_DISABLED_AFTER_EMERGENCY_INT</code>
199  </ul>
200</li>
201<li>Please refer to  <code>BlockedNumberContract</code>
202  <ul>
203 <li>APIs provided by <code><a
204 href="https://developer.android.com/reference/android/provider/BlockedNumberContract.html">BlockedNumberContract</a></code></li>
205 <li><code>boolean isBlocked(Context context, String phoneNumber)</code></li>
206 <li><code>int unblock(Context context, String phoneNumber)</code></li>
207 <li><code>boolean canCurrentUserBlockNumbers(Context context)</code></li>
208  </ul>
209 </li>
210</ul>
211
212<h3 id="user-interface">User interface</h3>
213<p>
214The BlockedNumbersActivity.java user interface provided in AOSP can be used as
215is. Device implementers may also implement their own version of the UI, as long as it
216satisfies related CDD requirements.
217</p>
218
219<p>
220Please note, the partner’s PC application for backup and restore may be needed
221to implement restoration of the block list by using
222<code>BlockedNumberProvider</code>. See the images below for the blocked
223numbers interface supplied in AOSP.
224</p>
225
226<img src="images/block-numbers-ui.png" alt="block numbers user interface" width="665" id="block-numbers-ui" />
227<p class="img-caption">
228  <strong>Figure 2.</strong> Block phone numbers user interface
229</p>
230
231<h2 id="validation">Validation</h2>
232
233<p>
234Implementers can ensure their version of the feature works as intended by
235running the following CTS tests:
236</p>
237
238<pre>
239android.provider.cts.BlockedNumberContractTest
240com.android.cts.numberblocking.hostside.NumberBlockingTest
241android.telecom.cts.ExtendedInCallServiceTest#testIncomingCallFromBlockedNumber_IsRejected
242android.telephony.cts.SmsManagerTest#testSmsBlocking
243</pre>
244
245<p>
246The <code>BlockedNumberProvider</code> can be manipulated using <code>adb</code> commands
247after running <code>$ adb root</code>. For example:
248</p>
249<pre>
250$ adb root
251$ adb shell content query --uri content://com.android.blockednumber/blocked
252$ adb shell content insert --uri / content://com.android.blockednumber/blocked --bind / original_number:s:'6501002000'
253$ adb shell content delete --uri / content://com.android.blockednumber/blocked/1
254</pre>
255