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.content.ContentValues;
36 import android.content.Context;
37 import android.content.Intent;
38 import android.net.Uri;
39 import android.util.Log;
40 
41 import java.io.IOException;
42 import java.util.regex.Pattern;
43 
44 import javax.obex.HeaderSet;
45 
46 /**
47  * Bluetooth OPP internal constant definitions
48  */
49 public class Constants {
50     /** Tag used for debugging/logging */
51     public static final String TAG = "BluetoothOpp";
52 
53     /**
54      * The intent that gets sent when the service must wake up for a retry
55      * Note: Only retry Outbound transfers
56      */
57     static final String ACTION_RETRY = "android.btopp.intent.action.RETRY";
58 
59     /** the intent that gets sent when clicking a successful transfer */
60     static final String ACTION_OPEN = "android.btopp.intent.action.OPEN";
61 
62     /** the intent that gets sent when clicking outbound transfer notification */
63     static final String ACTION_OPEN_OUTBOUND_TRANSFER = "android.btopp.intent.action.OPEN_OUTBOUND";
64 
65     /** the intent that gets sent when clicking a inbound transfer notification */
66     static final String ACTION_OPEN_INBOUND_TRANSFER = "android.btopp.intent.action.OPEN_INBOUND";
67 
68     /** the intent that gets sent from the Settings app to show the received files */
69     static final String ACTION_OPEN_RECEIVED_FILES =
70             "android.btopp.intent.action.OPEN_RECEIVED_FILES";
71 
72     /** the intent that whitelists a remote bluetooth device for auto-receive confirmation (NFC) */
73     static final String ACTION_WHITELIST_DEVICE = "android.btopp.intent.action.WHITELIST_DEVICE";
74 
75     /** the intent that can be sent by handover requesters to stop a BTOPP transfer */
76     static final String ACTION_STOP_HANDOVER = "android.btopp.intent.action.STOP_HANDOVER_TRANSFER";
77 
78     /** the intent extra to show all received files in the transfer history */
79     static final String EXTRA_SHOW_ALL_FILES = "android.btopp.intent.extra.SHOW_ALL";
80 
81     /** the intent that gets sent when clicking an incomplete/failed transfer */
82     static final String ACTION_LIST = "android.btopp.intent.action.LIST";
83 
84     /** the intent that is used for initiating a handover transfer */
85     static final String ACTION_HANDOVER_SEND = "android.nfc.handover.intent.action.HANDOVER_SEND";
86 
87     /** the intent that is used for initiating a multi-uri handover transfer */
88     static final String ACTION_HANDOVER_SEND_MULTIPLE =
89             "android.nfc.handover.intent.action.HANDOVER_SEND_MULTIPLE";
90 
91     /** the intent that is used for indicating an incoming transfer*/
92     static final String ACTION_HANDOVER_STARTED =
93             "android.nfc.handover.intent.action.HANDOVER_STARTED";
94 
95     /** intent action used to indicate the progress of a handover transfer */
96     static final String ACTION_BT_OPP_TRANSFER_PROGRESS =
97             "android.nfc.handover.intent.action.TRANSFER_PROGRESS";
98 
99     /** intent action used to indicate the completion of a handover transfer */
100     static final String ACTION_BT_OPP_TRANSFER_DONE =
101             "android.nfc.handover.intent.action.TRANSFER_DONE";
102 
103     /** intent extra used to indicate the success of a handover transfer */
104     static final String EXTRA_BT_OPP_TRANSFER_STATUS =
105             "android.nfc.handover.intent.extra.TRANSFER_STATUS";
106 
107     /** intent extra used to indicate the address associated with the transfer */
108     static final String EXTRA_BT_OPP_ADDRESS = "android.nfc.handover.intent.extra.ADDRESS";
109 
110     static final String EXTRA_BT_OPP_OBJECT_COUNT =
111             "android.nfc.handover.intent.extra.OBJECT_COUNT";
112 
113     static final int COUNT_HEADER_UNAVAILABLE = -1;
114     static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0;
115 
116     static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1;
117 
118     /** intent extra used to indicate the direction of a handover transfer */
119     static final String EXTRA_BT_OPP_TRANSFER_DIRECTION =
120             "android.nfc.handover.intent.extra.TRANSFER_DIRECTION";
121 
122     static final int DIRECTION_BLUETOOTH_INCOMING = 0;
123 
124     static final int DIRECTION_BLUETOOTH_OUTGOING = 1;
125 
126     /** intent extra used to provide a unique ID for the transfer */
127     static final String EXTRA_BT_OPP_TRANSFER_ID = "android.nfc.handover.intent.extra.TRANSFER_ID";
128 
129     /** intent extra used to provide progress of the transfer */
130     static final String EXTRA_BT_OPP_TRANSFER_PROGRESS =
131             "android.nfc.handover.intent.extra.TRANSFER_PROGRESS";
132 
133     /** intent extra used to provide the Uri where the data was stored by the handover transfer */
134     static final String EXTRA_BT_OPP_TRANSFER_URI =
135             "android.nfc.handover.intent.extra.TRANSFER_URI";
136 
137     /** intent extra used to provide the mime-type of the data in the handover transfer */
138     static final String EXTRA_BT_OPP_TRANSFER_MIMETYPE =
139             "android.nfc.handover.intent.extra.TRANSFER_MIME_TYPE";
140 
141     /** permission needed to be able to receive handover status requests */
142     static final String HANDOVER_STATUS_PERMISSION = "android.permission.NFC_HANDOVER_STATUS";
143 
144     /** the intent that gets sent when deleting the incoming file confirmation notification */
145     static final String ACTION_HIDE = "android.btopp.intent.action.HIDE";
146 
147     /** the intent that gets sent when accepting the incoming file confirmation notification */
148     static final String ACTION_ACCEPT = "android.btopp.intent.action.ACCEPT";
149 
150     /** the intent that gets sent when declining the incoming file confirmation notification */
151     static final String ACTION_DECLINE = "android.btopp.intent.action.DECLINE";
152 
153     /**
154      * the intent that gets sent when deleting the notifications of outbound and
155      * inbound completed transfer
156      */
157     static final String ACTION_COMPLETE_HIDE = "android.btopp.intent.action.HIDE_COMPLETE";
158 
159     /** the intent that gets sent when clicking a incoming file confirm notification */
160     static final String ACTION_INCOMING_FILE_CONFIRM = "android.btopp.intent.action.CONFIRM";
161 
162     static final String THIS_PACKAGE_NAME = "com.android.bluetooth";
163 
164     /** The column that is used to remember whether the media scanner was invoked */
165     static final String MEDIA_SCANNED = "scanned";
166 
167     static final int MEDIA_SCANNED_NOT_SCANNED = 0;
168 
169     static final int MEDIA_SCANNED_SCANNED_OK = 1;
170 
171     static final int MEDIA_SCANNED_SCANNED_FAILED = 2;
172 
173     /**
174      * The MIME type(s) of we could accept from other device.
175      * This is in essence a "white list" of acceptable types.
176      * Today, restricted to images, audio, video and certain text types.
177      */
178     static final String[] ACCEPTABLE_SHARE_INBOUND_TYPES = new String[]{
179             "image/*",
180             "video/*",
181             "audio/*",
182             "text/x-vcard",
183             "text/x-vcalendar",
184             "text/calendar",
185             "text/plain",
186             "text/html",
187             "text/xml",
188             "application/zip",
189             "application/vnd.ms-excel",
190             "application/msword",
191             "application/vnd.ms-powerpoint",
192             "application/pdf",
193             "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
194             "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
195             "application/vnd.openxmlformats-officedocument.presentationml.presentation",
196             "application/x-hwp",
197     };
198 
199     /** Where we store received files */
200     static final String DEFAULT_STORE_SUBDIR = "/bluetooth";
201 
202     /** Notify NFC of the transfer progress periodically, or it will timeout after 20sec. */
203     static final int NFC_ALIVE_CHECK_MS = 10000;
204 
205     static final boolean DEBUG = true;
206 
207     static final boolean VERBOSE = false;
208 
209     static final int MAX_RECORDS_IN_DATABASE = 50;
210 
211     static final int BATCH_STATUS_PENDING = 0;
212 
213     static final int BATCH_STATUS_RUNNING = 1;
214 
215     static final int BATCH_STATUS_FINISHED = 2;
216 
217     static final int BATCH_STATUS_FAILED = 3;
218 
219     static final String BLUETOOTHOPP_NAME_PREFERENCE = "btopp_names";
220 
221     static final String BLUETOOTHOPP_CHANNEL_PREFERENCE = "btopp_channels";
222 
223     static final String FILENAME_SEQUENCE_SEPARATOR = "-";
224 
updateShareStatus(Context context, int id, int status)225     static void updateShareStatus(Context context, int id, int status) {
226         Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + id);
227         ContentValues updateValues = new ContentValues();
228         updateValues.put(BluetoothShare.STATUS, status);
229         context.getContentResolver().update(contentUri, updateValues, null, null);
230         Constants.sendIntentIfCompleted(context, contentUri, status);
231     }
232 
233     /** This function should be called whenever the transfer status changes to completed. */
sendIntentIfCompleted(Context context, Uri contentUri, int status)234     static void sendIntentIfCompleted(Context context, Uri contentUri, int status) {
235         if (BluetoothShare.isStatusCompleted(status)) {
236             Intent intent = new Intent(BluetoothShare.TRANSFER_COMPLETED_ACTION);
237             intent.setClassName(THIS_PACKAGE_NAME, BluetoothOppReceiver.class.getName());
238             intent.setDataAndNormalize(contentUri);
239             context.sendBroadcast(intent);
240         }
241     }
242 
mimeTypeMatches(String mimeType, String[] matchAgainst)243     static boolean mimeTypeMatches(String mimeType, String[] matchAgainst) {
244         for (String matchType : matchAgainst) {
245             if (mimeTypeMatches(mimeType, matchType)) {
246                 return true;
247             }
248         }
249         return false;
250     }
251 
mimeTypeMatches(String mimeType, String matchAgainst)252     private static boolean mimeTypeMatches(String mimeType, String matchAgainst) {
253         Pattern p =
254                 Pattern.compile(matchAgainst.replaceAll("\\*", "\\.\\*"), Pattern.CASE_INSENSITIVE);
255         return p.matcher(mimeType).matches();
256     }
257 
logHeader(HeaderSet hs)258     static void logHeader(HeaderSet hs) {
259         Log.v(TAG, "Dumping HeaderSet " + hs.toString());
260         try {
261             Log.v(TAG, "COUNT : " + hs.getHeader(HeaderSet.COUNT));
262             Log.v(TAG, "NAME : " + hs.getHeader(HeaderSet.NAME));
263             Log.v(TAG, "TYPE : " + hs.getHeader(HeaderSet.TYPE));
264             Log.v(TAG, "LENGTH : " + hs.getHeader(HeaderSet.LENGTH));
265             Log.v(TAG, "TIME_ISO_8601 : " + hs.getHeader(HeaderSet.TIME_ISO_8601));
266             Log.v(TAG, "TIME_4_BYTE : " + hs.getHeader(HeaderSet.TIME_4_BYTE));
267             Log.v(TAG, "DESCRIPTION : " + hs.getHeader(HeaderSet.DESCRIPTION));
268             Log.v(TAG, "TARGET : " + hs.getHeader(HeaderSet.TARGET));
269             Log.v(TAG, "HTTP : " + hs.getHeader(HeaderSet.HTTP));
270             Log.v(TAG, "WHO : " + hs.getHeader(HeaderSet.WHO));
271             Log.v(TAG, "OBJECT_CLASS : " + hs.getHeader(HeaderSet.OBJECT_CLASS));
272             Log.v(TAG, "APPLICATION_PARAMETER : " + hs.getHeader(HeaderSet.APPLICATION_PARAMETER));
273         } catch (IOException e) {
274             Log.e(TAG, "dump HeaderSet error " + e);
275         }
276     }
277 }
278