• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.RequiresPermission;
36 import android.annotation.SdkConstant;
37 import android.annotation.SdkConstant.SdkConstantType;
38 import android.content.ContentValues;
39 import android.content.Context;
40 import android.content.Intent;
41 import android.net.Uri;
42 import android.util.Log;
43 
44 import com.android.bluetooth.BluetoothMethodProxy;
45 import com.android.obex.HeaderSet;
46 
47 import java.util.regex.Pattern;
48 
49 /** Bluetooth OPP internal constant definitions */
50 // Next tag value for ContentProfileErrorReportUtils.report(): 1
51 public class Constants {
52     /** Tag used for debugging/logging */
53     public static final String TAG_PREFIX_BLUETOOTH_OPP = "BluetoothOpp";
54 
55     static final String TAG = TAG_PREFIX_BLUETOOTH_OPP + Constants.class.getSimpleName();
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     /** permission needed to be able to receive handover status requests */
99     static final String HANDOVER_STATUS_PERMISSION = "android.permission.NFC_HANDOVER_STATUS";
100 
101     /** the intent that is used for indicating an incoming transfer */
102     @RequiresPermission(HANDOVER_STATUS_PERMISSION)
103     @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
104     static final String ACTION_HANDOVER_STARTED =
105             "android.nfc.handover.intent.action.HANDOVER_STARTED";
106 
107     /** intent action used to indicate the progress of a handover transfer */
108     @RequiresPermission(HANDOVER_STATUS_PERMISSION)
109     @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
110     static final String ACTION_BT_OPP_TRANSFER_PROGRESS =
111             "android.nfc.handover.intent.action.TRANSFER_PROGRESS";
112 
113     /** intent action used to indicate the completion of a handover transfer */
114     @RequiresPermission(HANDOVER_STATUS_PERMISSION)
115     @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
116     static final String ACTION_BT_OPP_TRANSFER_DONE =
117             "android.nfc.handover.intent.action.TRANSFER_DONE";
118 
119     /** intent extra used to indicate the success of a handover transfer */
120     static final String EXTRA_BT_OPP_TRANSFER_STATUS =
121             "android.nfc.handover.intent.extra.TRANSFER_STATUS";
122 
123     /** intent extra used to indicate the address associated with the transfer */
124     static final String EXTRA_BT_OPP_ADDRESS = "android.nfc.handover.intent.extra.ADDRESS";
125 
126     static final String EXTRA_BT_OPP_OBJECT_COUNT =
127             "android.nfc.handover.intent.extra.OBJECT_COUNT";
128 
129     static final int COUNT_HEADER_UNAVAILABLE = -1;
130     static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0;
131 
132     static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1;
133 
134     /** intent extra used to indicate the direction of a handover transfer */
135     static final String EXTRA_BT_OPP_TRANSFER_DIRECTION =
136             "android.nfc.handover.intent.extra.TRANSFER_DIRECTION";
137 
138     static final int DIRECTION_BLUETOOTH_INCOMING = 0;
139 
140     static final int DIRECTION_BLUETOOTH_OUTGOING = 1;
141 
142     /** intent extra used to provide a unique ID for the transfer */
143     static final String EXTRA_BT_OPP_TRANSFER_ID = "android.nfc.handover.intent.extra.TRANSFER_ID";
144 
145     /** intent extra used to provide progress of the transfer */
146     static final String EXTRA_BT_OPP_TRANSFER_PROGRESS =
147             "android.nfc.handover.intent.extra.TRANSFER_PROGRESS";
148 
149     /** intent extra used to provide the Uri where the data was stored by the handover transfer */
150     static final String EXTRA_BT_OPP_TRANSFER_URI =
151             "android.nfc.handover.intent.extra.TRANSFER_URI";
152 
153     /** intent extra used to provide the mime-type of the data in the handover transfer */
154     static final String EXTRA_BT_OPP_TRANSFER_MIMETYPE =
155             "android.nfc.handover.intent.extra.TRANSFER_MIME_TYPE";
156 
157     /** the intent that gets sent when deleting the incoming file confirmation notification */
158     static final String ACTION_HIDE = "android.btopp.intent.action.HIDE";
159 
160     /** the intent that gets sent when accepting the incoming file confirmation notification */
161     static final String ACTION_ACCEPT = "android.btopp.intent.action.ACCEPT";
162 
163     /** the intent that gets sent when declining the incoming file confirmation notification */
164     static final String ACTION_DECLINE = "android.btopp.intent.action.DECLINE";
165 
166     /** The intent that gets sent when deleting the notifications of completed inbound transfer. */
167     static final String ACTION_HIDE_COMPLETED_INBOUND_TRANSFER =
168             "android.btopp.intent.action.HIDE_COMPLETED_INBOUND_TRANSFER";
169 
170     /** The intent that gets sent when deleting the notifications of completed outbound transfer. */
171     static final String ACTION_HIDE_COMPLETED_OUTBOUND_TRANSFER =
172             "android.btopp.intent.action.HIDE_COMPLETED_OUTBOUND_TRANSFER";
173 
174     /** the intent that gets sent when clicking a incoming file confirm notification */
175     static final String ACTION_INCOMING_FILE_CONFIRM = "android.btopp.intent.action.CONFIRM";
176 
177     /** The column that is used to remember whether the media scanner was invoked */
178     static final String MEDIA_SCANNED = "scanned";
179 
180     static final int MEDIA_SCANNED_NOT_SCANNED = 0;
181 
182     static final int MEDIA_SCANNED_SCANNED_OK = 1;
183 
184     static final int MEDIA_SCANNED_SCANNED_FAILED = 2;
185 
186     /**
187      * The MIME type(s) of we could accept from other device. This is in essence a "acceptlist" of
188      * acceptable types. Today, restricted to images, audio, video and certain text types.
189      */
190     static final String[] ACCEPTABLE_SHARE_INBOUND_TYPES =
191             new String[] {
192                 "image/*",
193                 "video/*",
194                 "audio/*",
195                 "text/x-vcard",
196                 "text/x-vcalendar",
197                 "text/calendar",
198                 "text/plain",
199                 "text/html",
200                 "text/xml",
201                 "application/epub+zip",
202                 "application/zip",
203                 "application/vnd.ms-excel",
204                 "application/msword",
205                 "application/vnd.ms-powerpoint",
206                 "application/pdf",
207                 "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
208                 "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
209                 "application/vnd.openxmlformats-officedocument.presentationml.presentation",
210                 "application/x-hwp",
211             };
212 
213     /** Where we store received files */
214     static final String DEFAULT_STORE_SUBDIR = "/bluetooth";
215 
216     /** Notify NFC of the transfer progress periodically, or it will timeout after 20sec. */
217     static final int NFC_ALIVE_CHECK_MS = 10000;
218 
219     /**
220      * To log debug/verbose in OPP, use the command "setprop log.tag.BluetoothOpp DEBUG" or "setprop
221      * log.tag.BluetoothOpp VERBOSE" and then "adb root" + "adb shell "stop; start""
222      */
223     static final int MAX_RECORDS_IN_DATABASE = 50;
224 
225     static final int BATCH_STATUS_PENDING = 0;
226 
227     static final int BATCH_STATUS_RUNNING = 1;
228 
229     static final int BATCH_STATUS_FINISHED = 2;
230 
231     static final int BATCH_STATUS_FAILED = 3;
232 
233     static final String BLUETOOTHOPP_NAME_PREFERENCE = "btopp_names";
234 
235     static final String BLUETOOTHOPP_CHANNEL_PREFERENCE = "btopp_channels";
236 
237     static final String FILENAME_SEQUENCE_SEPARATOR = "-";
238 
updateShareStatus(Context context, int id, int status)239     static void updateShareStatus(Context context, int id, int status) {
240         Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + id);
241         ContentValues updateValues = new ContentValues();
242         updateValues.put(BluetoothShare.STATUS, status);
243         BluetoothMethodProxy.getInstance()
244                 .contentResolverUpdate(
245                         context.getContentResolver(), contentUri, updateValues, null, null);
246         Constants.sendIntentIfCompleted(context, contentUri, status);
247     }
248 
249     /** This function should be called whenever the transfer status changes to completed. */
sendIntentIfCompleted(Context context, Uri contentUri, int status)250     static void sendIntentIfCompleted(Context context, Uri contentUri, int status) {
251         if (BluetoothShare.isStatusCompleted(status)) {
252             Intent intent = new Intent(BluetoothShare.TRANSFER_COMPLETED_ACTION);
253             intent.setClassName(context, BluetoothOppReceiver.class.getName());
254             intent.setData(contentUri.normalizeScheme());
255             context.sendBroadcast(intent);
256         }
257     }
258 
mimeTypeMatches(String mimeType, String[] matchAgainst)259     static boolean mimeTypeMatches(String mimeType, String[] matchAgainst) {
260         for (String matchType : matchAgainst) {
261             if (mimeTypeMatches(mimeType, matchType)) {
262                 return true;
263             }
264         }
265         return false;
266     }
267 
mimeTypeMatches(String mimeType, String matchAgainst)268     private static boolean mimeTypeMatches(String mimeType, String matchAgainst) {
269         String matchRegex = matchAgainst.replaceAll("\\+", "\\\\+").replaceAll("\\*", ".*");
270         Pattern p = Pattern.compile(matchRegex, Pattern.CASE_INSENSITIVE);
271         return p.matcher(mimeType).matches();
272     }
273 
logHeader(HeaderSet hs)274     static void logHeader(HeaderSet hs) {
275         Log.v(TAG, "Dumping HeaderSet " + hs.toString());
276         Log.v(TAG, "COUNT : " + hs.getHeader(HeaderSet.COUNT));
277         Log.v(TAG, "NAME : " + hs.getHeader(HeaderSet.NAME));
278         Log.v(TAG, "TYPE : " + hs.getHeader(HeaderSet.TYPE));
279         Log.v(TAG, "LENGTH : " + hs.getHeader(HeaderSet.LENGTH));
280         Log.v(TAG, "TIME_ISO_8601 : " + hs.getHeader(HeaderSet.TIME_ISO_8601));
281         Log.v(TAG, "TIME_4_BYTE : " + hs.getHeader(HeaderSet.TIME_4_BYTE));
282         Log.v(TAG, "DESCRIPTION : " + hs.getHeader(HeaderSet.DESCRIPTION));
283         Log.v(TAG, "TARGET : " + hs.getHeader(HeaderSet.TARGET));
284         Log.v(TAG, "HTTP : " + hs.getHeader(HeaderSet.HTTP));
285         Log.v(TAG, "WHO : " + hs.getHeader(HeaderSet.WHO));
286         Log.v(TAG, "OBJECT_CLASS : " + hs.getHeader(HeaderSet.OBJECT_CLASS));
287         Log.v(TAG, "APPLICATION_PARAMETER : " + hs.getHeader(HeaderSet.APPLICATION_PARAMETER));
288     }
289 }
290