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