1 /*
2  * Copyright (c) 2008-2009, Motorola, Inc.
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * - Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the Motorola, Inc. nor the names of its contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 package com.android.bluetooth.opp;
34 
35 import android.bluetooth.BluetoothProfile;
36 import android.bluetooth.BluetoothProtoEnums;
37 import android.content.ContentValues;
38 import android.content.Context;
39 import android.content.Intent;
40 import android.net.Uri;
41 import android.util.Log;
42 
43 import com.android.bluetooth.BluetoothMethodProxy;
44 import com.android.bluetooth.BluetoothStatsLog;
45 import com.android.bluetooth.content_profiles.ContentProfileErrorReportUtils;
46 import com.android.obex.HeaderSet;
47 
48 import java.io.IOException;
49 import java.util.regex.Pattern;
50 
51 /** Bluetooth OPP internal constant definitions */
52 // Next tag value for ContentProfileErrorReportUtils.report(): 1
53 public class Constants {
54     /** Tag used for debugging/logging */
55     public static final String TAG = "BluetoothOpp";
56 
57     /** the permission required for others to send us handover broadcasts */
58     static final String PERMISSION_ALLOWLIST_BLUETOOTH_DEVICE =
59             "com.android.permission.ALLOWLIST_BLUETOOTH_DEVICE";
60 
61     /**
62      * The intent that gets sent when the service must wake up for a retry Note: Only retry Outbound
63      * transfers
64      */
65     static final String ACTION_RETRY = "android.btopp.intent.action.RETRY";
66 
67     /** the intent that gets sent when clicking a successful transfer */
68     static final String ACTION_OPEN = "android.btopp.intent.action.OPEN";
69 
70     /** the intent that gets sent when clicking outbound transfer notification */
71     static final String ACTION_OPEN_OUTBOUND_TRANSFER = "android.btopp.intent.action.OPEN_OUTBOUND";
72 
73     /** the intent that gets sent when clicking a inbound transfer notification */
74     static final String ACTION_OPEN_INBOUND_TRANSFER = "android.btopp.intent.action.OPEN_INBOUND";
75 
76     /** the intent that acceptlists a remote bluetooth device for auto-receive confirmation (NFC) */
77     static final String ACTION_ACCEPTLIST_DEVICE = "android.btopp.intent.action.ACCEPTLIST_DEVICE";
78 
79     /** the intent that can be sent by handover requesters to stop a BTOPP transfer */
80     static final String ACTION_STOP_HANDOVER = "android.btopp.intent.action.STOP_HANDOVER_TRANSFER";
81 
82     /**
83      * the intent extra to show the direction of a transfer. Value should be one of {@link
84      * BluetoothShare#DIRECTION_INBOUND} or {@link BluetoothShare#DIRECTION_OUTBOUND}
85      */
86     static final String EXTRA_DIRECTION = "android.btopp.intent.extra.DIRECTION";
87 
88     /** the intent that gets sent when clicking an incomplete/failed transfer */
89     static final String ACTION_LIST = "android.btopp.intent.action.LIST";
90 
91     /** the intent that is used for initiating a handover transfer */
92     static final String ACTION_HANDOVER_SEND = "android.nfc.handover.intent.action.HANDOVER_SEND";
93 
94     /** the intent that is used for initiating a multi-uri handover transfer */
95     static final String ACTION_HANDOVER_SEND_MULTIPLE =
96             "android.nfc.handover.intent.action.HANDOVER_SEND_MULTIPLE";
97 
98     /** the intent that is used for indicating an incoming transfer */
99     static final String ACTION_HANDOVER_STARTED =
100             "android.nfc.handover.intent.action.HANDOVER_STARTED";
101 
102     /** intent action used to indicate the progress of a handover transfer */
103     static final String ACTION_BT_OPP_TRANSFER_PROGRESS =
104             "android.nfc.handover.intent.action.TRANSFER_PROGRESS";
105 
106     /** intent action used to indicate the completion of a handover transfer */
107     static final String ACTION_BT_OPP_TRANSFER_DONE =
108             "android.nfc.handover.intent.action.TRANSFER_DONE";
109 
110     /** intent extra used to indicate the success of a handover transfer */
111     static final String EXTRA_BT_OPP_TRANSFER_STATUS =
112             "android.nfc.handover.intent.extra.TRANSFER_STATUS";
113 
114     /** intent extra used to indicate the address associated with the transfer */
115     static final String EXTRA_BT_OPP_ADDRESS = "android.nfc.handover.intent.extra.ADDRESS";
116 
117     static final String EXTRA_BT_OPP_OBJECT_COUNT =
118             "android.nfc.handover.intent.extra.OBJECT_COUNT";
119 
120     static final int COUNT_HEADER_UNAVAILABLE = -1;
121     static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0;
122 
123     static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1;
124 
125     /** intent extra used to indicate the direction of a handover transfer */
126     static final String EXTRA_BT_OPP_TRANSFER_DIRECTION =
127             "android.nfc.handover.intent.extra.TRANSFER_DIRECTION";
128 
129     static final int DIRECTION_BLUETOOTH_INCOMING = 0;
130 
131     static final int DIRECTION_BLUETOOTH_OUTGOING = 1;
132 
133     /** intent extra used to provide a unique ID for the transfer */
134     static final String EXTRA_BT_OPP_TRANSFER_ID = "android.nfc.handover.intent.extra.TRANSFER_ID";
135 
136     /** intent extra used to provide progress of the transfer */
137     static final String EXTRA_BT_OPP_TRANSFER_PROGRESS =
138             "android.nfc.handover.intent.extra.TRANSFER_PROGRESS";
139 
140     /** intent extra used to provide the Uri where the data was stored by the handover transfer */
141     static final String EXTRA_BT_OPP_TRANSFER_URI =
142             "android.nfc.handover.intent.extra.TRANSFER_URI";
143 
144     /** intent extra used to provide the mime-type of the data in the handover transfer */
145     static final String EXTRA_BT_OPP_TRANSFER_MIMETYPE =
146             "android.nfc.handover.intent.extra.TRANSFER_MIME_TYPE";
147 
148     /** permission needed to be able to receive handover status requests */
149     static final String HANDOVER_STATUS_PERMISSION = "android.permission.NFC_HANDOVER_STATUS";
150 
151     /** the intent that gets sent when deleting the incoming file confirmation notification */
152     static final String ACTION_HIDE = "android.btopp.intent.action.HIDE";
153 
154     /** the intent that gets sent when accepting the incoming file confirmation notification */
155     static final String ACTION_ACCEPT = "android.btopp.intent.action.ACCEPT";
156 
157     /** the intent that gets sent when declining the incoming file confirmation notification */
158     static final String ACTION_DECLINE = "android.btopp.intent.action.DECLINE";
159 
160     /**
161      * The intent that gets sent when deleting the notifications of outbound and inbound completed
162      * transfer.
163      */
164     // TODO(b/323096132): Remove this variable when the flag
165     //                    opp_fix_multiple_notifications_issues is ramped up.
166     static final String ACTION_COMPLETE_HIDE = "android.btopp.intent.action.HIDE_COMPLETE";
167 
168     /** The intent that gets sent when deleting the notifications of completed inbound transfer. */
169     static final String ACTION_HIDE_COMPLETED_INBOUND_TRANSFER =
170             "android.btopp.intent.action.HIDE_COMPLETED_INBOUND_TRANSFER";
171 
172     /** The intent that gets sent when deleting the notifications of completed outbound transfer. */
173     static final String ACTION_HIDE_COMPLETED_OUTBOUND_TRANSFER =
174             "android.btopp.intent.action.HIDE_COMPLETED_OUTBOUND_TRANSFER";
175 
176     /** the intent that gets sent when clicking a incoming file confirm notification */
177     static final String ACTION_INCOMING_FILE_CONFIRM = "android.btopp.intent.action.CONFIRM";
178 
179     /** The column that is used to remember whether the media scanner was invoked */
180     static final String MEDIA_SCANNED = "scanned";
181 
182     static final int MEDIA_SCANNED_NOT_SCANNED = 0;
183 
184     static final int MEDIA_SCANNED_SCANNED_OK = 1;
185 
186     static final int MEDIA_SCANNED_SCANNED_FAILED = 2;
187 
188     /**
189      * The MIME type(s) of we could accept from other device. This is in essence a "acceptlist" of
190      * acceptable types. Today, restricted to images, audio, video and certain text types.
191      */
192     static final String[] ACCEPTABLE_SHARE_INBOUND_TYPES =
193             new String[] {
194                 "image/*",
195                 "video/*",
196                 "audio/*",
197                 "text/x-vcard",
198                 "text/x-vcalendar",
199                 "text/calendar",
200                 "text/plain",
201                 "text/html",
202                 "text/xml",
203                 "application/epub+zip",
204                 "application/zip",
205                 "application/vnd.ms-excel",
206                 "application/msword",
207                 "application/vnd.ms-powerpoint",
208                 "application/pdf",
209                 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
210                 "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
211                 "application/vnd.openxmlformats-officedocument.presentationml.presentation",
212                 "application/x-hwp",
213             };
214 
215     /** Where we store received files */
216     static final String DEFAULT_STORE_SUBDIR = "/bluetooth";
217 
218     /** Notify NFC of the transfer progress periodically, or it will timeout after 20sec. */
219     static final int NFC_ALIVE_CHECK_MS = 10000;
220 
221     /**
222      * To log debug/verbose in OPP, use the command "setprop log.tag.BluetoothOpp DEBUG" or "setprop
223      * log.tag.BluetoothOpp VERBOSE" and then "adb root" + "adb shell "stop; start""
224      */
225     static final int MAX_RECORDS_IN_DATABASE = 50;
226 
227     static final int BATCH_STATUS_PENDING = 0;
228 
229     static final int BATCH_STATUS_RUNNING = 1;
230 
231     static final int BATCH_STATUS_FINISHED = 2;
232 
233     static final int BATCH_STATUS_FAILED = 3;
234 
235     static final String BLUETOOTHOPP_NAME_PREFERENCE = "btopp_names";
236 
237     static final String BLUETOOTHOPP_CHANNEL_PREFERENCE = "btopp_channels";
238 
239     static final String FILENAME_SEQUENCE_SEPARATOR = "-";
240 
updateShareStatus(Context context, int id, int status)241     static void updateShareStatus(Context context, int id, int status) {
242         Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + id);
243         ContentValues updateValues = new ContentValues();
244         updateValues.put(BluetoothShare.STATUS, status);
245         BluetoothMethodProxy.getInstance()
246                 .contentResolverUpdate(
247                         context.getContentResolver(), contentUri, updateValues, null, null);
248         Constants.sendIntentIfCompleted(context, contentUri, status);
249     }
250 
251     /** This function should be called whenever the transfer status changes to completed. */
sendIntentIfCompleted(Context context, Uri contentUri, int status)252     static void sendIntentIfCompleted(Context context, Uri contentUri, int status) {
253         if (BluetoothShare.isStatusCompleted(status)) {
254             Intent intent = new Intent(BluetoothShare.TRANSFER_COMPLETED_ACTION);
255             intent.setClassName(context, BluetoothOppReceiver.class.getName());
256             intent.setDataAndNormalize(contentUri);
257             context.sendBroadcast(intent);
258         }
259     }
260 
mimeTypeMatches(String mimeType, String[] matchAgainst)261     static boolean mimeTypeMatches(String mimeType, String[] matchAgainst) {
262         for (String matchType : matchAgainst) {
263             if (mimeTypeMatches(mimeType, matchType)) {
264                 return true;
265             }
266         }
267         return false;
268     }
269 
mimeTypeMatches(String mimeType, String matchAgainst)270     private static boolean mimeTypeMatches(String mimeType, String matchAgainst) {
271         String matchRegex = matchAgainst.replaceAll("\\+", "\\\\+").replaceAll("\\*", ".*");
272         Pattern p = Pattern.compile(matchRegex, Pattern.CASE_INSENSITIVE);
273         return p.matcher(mimeType).matches();
274     }
275 
logHeader(HeaderSet hs)276     static void logHeader(HeaderSet hs) {
277         Log.v(TAG, "Dumping HeaderSet " + hs.toString());
278         try {
279             Log.v(TAG, "COUNT : " + hs.getHeader(HeaderSet.COUNT));
280             Log.v(TAG, "NAME : " + hs.getHeader(HeaderSet.NAME));
281             Log.v(TAG, "TYPE : " + hs.getHeader(HeaderSet.TYPE));
282             Log.v(TAG, "LENGTH : " + hs.getHeader(HeaderSet.LENGTH));
283             Log.v(TAG, "TIME_ISO_8601 : " + hs.getHeader(HeaderSet.TIME_ISO_8601));
284             Log.v(TAG, "TIME_4_BYTE : " + hs.getHeader(HeaderSet.TIME_4_BYTE));
285             Log.v(TAG, "DESCRIPTION : " + hs.getHeader(HeaderSet.DESCRIPTION));
286             Log.v(TAG, "TARGET : " + hs.getHeader(HeaderSet.TARGET));
287             Log.v(TAG, "HTTP : " + hs.getHeader(HeaderSet.HTTP));
288             Log.v(TAG, "WHO : " + hs.getHeader(HeaderSet.WHO));
289             Log.v(TAG, "OBJECT_CLASS : " + hs.getHeader(HeaderSet.OBJECT_CLASS));
290             Log.v(TAG, "APPLICATION_PARAMETER : " + hs.getHeader(HeaderSet.APPLICATION_PARAMETER));
291         } catch (IOException e) {
292             ContentProfileErrorReportUtils.report(
293                     BluetoothProfile.OPP,
294                     BluetoothProtoEnums.BLUETOOTH_OPP_CONSTANTS,
295                     BluetoothStatsLog.BLUETOOTH_CONTENT_PROFILE_ERROR_REPORTED__TYPE__EXCEPTION,
296                     0);
297             Log.e(TAG, "dump HeaderSet error " + e);
298         }
299     }
300 }
301