Home | History | Annotate | Download | only in opp
      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 java.io.IOException;
     36 import java.util.regex.Pattern;
     37 
     38 import javax.obex.HeaderSet;
     39 
     40 import android.content.ContentValues;
     41 import android.content.Context;
     42 import android.content.Intent;
     43 import android.net.Uri;
     44 import android.util.Log;
     45 
     46 /**
     47  * Bluetooth OPP internal constants definition
     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 Note:
     55      * only retry Outbound transfer
     56      */
     57     public static final String ACTION_RETRY = "android.btopp.intent.action.RETRY";
     58 
     59     /** the intent that gets sent when clicking a successful transfer */
     60     public static final String ACTION_OPEN = "android.btopp.intent.action.OPEN";
     61 
     62     /** the intent that gets sent when clicking outbound transfer notification */
     63     public 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     public 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     public static final String ACTION_OPEN_RECEIVED_FILES = "android.btopp.intent.action.OPEN_RECEIVED_FILES";
     70 
     71     /** the intent that whitelists a remote bluetooth device for auto-receive confirmation (NFC) */
     72     public static final String ACTION_WHITELIST_DEVICE = "android.btopp.intent.action.WHITELIST_DEVICE";
     73 
     74     /** the intent that can be sent by handover requesters to stop a BTOPP transfer */
     75     public static final String ACTION_STOP_HANDOVER = "android.btopp.intent.action.STOP_HANDOVER_TRANSFER";
     76 
     77     /** the intent extra to show all received files in the transfer history */
     78     public static final String EXTRA_SHOW_ALL_FILES = "android.btopp.intent.extra.SHOW_ALL";
     79 
     80     /** the intent that gets sent when clicking an incomplete/failed transfer */
     81     public static final String ACTION_LIST = "android.btopp.intent.action.LIST";
     82 
     83     /** the intent that is used for initiating a handover transfer */
     84     public static final String ACTION_HANDOVER_SEND =
     85             "android.btopp.intent.action.HANDOVER_SEND";
     86 
     87     /** the intent that is used for initiating a multi-uri handover transfer */
     88     public static final String ACTION_HANDOVER_SEND_MULTIPLE =
     89             "android.btopp.intent.action.HANDOVER_SEND_MULTIPLE";
     90 
     91     /** the intent that is used for indicating an incoming transfer*/
     92     public static final String ACTION_HANDOVER_STARTED =
     93             "android.btopp.intent.action.BT_OPP_HANDOVER_STARTED";
     94 
     95     /** intent action used to indicate the progress of a handover transfer */
     96     public static final String ACTION_BT_OPP_TRANSFER_PROGRESS =
     97             "android.btopp.intent.action.BT_OPP_TRANSFER_PROGRESS";
     98 
     99     /** intent action used to indicate the completion of a handover transfer */
    100     public static final String ACTION_BT_OPP_TRANSFER_DONE =
    101             "android.btopp.intent.action.BT_OPP_TRANSFER_DONE";
    102 
    103     /** intent extra used to indicate the success of a handover transfer */
    104     public static final String EXTRA_BT_OPP_TRANSFER_STATUS =
    105             "android.btopp.intent.extra.BT_OPP_TRANSFER_STATUS";
    106 
    107     /** intent extra used to indicate the address associated with the transfer */
    108     public static final String EXTRA_BT_OPP_ADDRESS =
    109             "android.btopp.intent.extra.BT_OPP_ADDRESS";
    110 
    111     public static final String EXTRA_BT_OPP_OBJECT_COUNT =
    112             "android.btopp.intent.extra.BT_OPP_OBJECT_COUNT";
    113 
    114     public static final int COUNT_HEADER_UNAVAILABLE = -1;
    115     public static final int HANDOVER_TRANSFER_STATUS_SUCCESS = 0;
    116 
    117     public static final int HANDOVER_TRANSFER_STATUS_FAILURE = 1;
    118 
    119     /** intent extra used to indicate the direction of a handover transfer */
    120     public static final String EXTRA_BT_OPP_TRANSFER_DIRECTION =
    121             "android.btopp.intent.extra.BT_OPP_TRANSFER_DIRECTION";
    122 
    123     public static final int DIRECTION_BLUETOOTH_INCOMING = 0;
    124 
    125     public static final int DIRECTION_BLUETOOTH_OUTGOING = 1;
    126 
    127     /** intent extra used to provide a unique ID for the transfer */
    128     public static final String EXTRA_BT_OPP_TRANSFER_ID =
    129             "android.btopp.intent.extra.BT_OPP_TRANSFER_ID";
    130 
    131     /** intent extra used to provide progress of the transfer */
    132     public static final String EXTRA_BT_OPP_TRANSFER_PROGRESS =
    133             "android.btopp.intent.extra.BT_OPP_TRANSFER_PROGRESS";
    134 
    135     /** intent extra used to provide the Uri where the data was stored
    136      * by the handover transfer */
    137     public static final String EXTRA_BT_OPP_TRANSFER_URI =
    138             "android.btopp.intent.extra.BT_OPP_TRANSFER_URI";
    139 
    140     /** intent extra used to provide the mime-type of the data in
    141      *  the handover transfer */
    142     public static final String EXTRA_BT_OPP_TRANSFER_MIMETYPE =
    143             "android.btopp.intent.extra.BT_OPP_TRANSFER_MIMETYPE";
    144 
    145     /** permission needed to be able to receive handover status requests */
    146     public static final String HANDOVER_STATUS_PERMISSION =
    147             "com.android.permission.HANDOVER_STATUS";
    148 
    149     /** intent extra that indicates this transfer is a handover from another
    150       * transport (NFC, WIFI)
    151       */
    152     public static final String EXTRA_CONNECTION_HANDOVER =
    153             "com.android.intent.extra.CONNECTION_HANDOVER";
    154 
    155     /**
    156      * the intent that gets sent when deleting the incoming file confirmation
    157      * notification
    158      */
    159     public static final String ACTION_HIDE = "android.btopp.intent.action.HIDE";
    160 
    161     /**
    162      * the intent that gets sent when deleting the notifications of outbound and
    163      * inbound completed transfer
    164      */
    165     public static final String ACTION_COMPLETE_HIDE = "android.btopp.intent.action.HIDE_COMPLETE";
    166 
    167     /**
    168      * the intent that gets sent when clicking a incoming file confirm
    169      * notification
    170      */
    171     public static final String ACTION_INCOMING_FILE_CONFIRM = "android.btopp.intent.action.CONFIRM";
    172 
    173     public static final String THIS_PACKAGE_NAME = "com.android.bluetooth";
    174 
    175     /**
    176      * The column that is used to remember whether the media scanner was invoked
    177      */
    178     public static final String MEDIA_SCANNED = "scanned";
    179 
    180     public static final int MEDIA_SCANNED_NOT_SCANNED = 0;
    181 
    182     public static final int MEDIA_SCANNED_SCANNED_OK = 1;
    183 
    184     public static final int MEDIA_SCANNED_SCANNED_FAILED = 2;
    185 
    186     /**
    187      * The MIME type(s) of we could share to other device.
    188      */
    189     /*
    190      * TODO: define correct type list
    191      */
    192     public static final String[] ACCEPTABLE_SHARE_OUTBOUND_TYPES = new String[] {
    193         "image/*", "text/x-vcard",
    194     };
    195 
    196     /**
    197      * The MIME type(s) of we could not share to other device. TODO: define
    198      * correct type list
    199      */
    200     public static final String[] UNACCEPTABLE_SHARE_OUTBOUND_TYPES = new String[] {
    201         "virus/*",
    202     };
    203 
    204     /**
    205      * The MIME type(s) of we could accept from other device.
    206      * This is in essence a "white list" of acceptable types.
    207      * Today, restricted to images, audio, video and certain text types.
    208      */
    209     public static final String[] ACCEPTABLE_SHARE_INBOUND_TYPES = new String[] {
    210         "image/*",
    211         "video/*",
    212         "audio/*",
    213         "text/x-vcard",
    214         "text/plain",
    215         "text/html",
    216         "application/zip",
    217         "application/vnd.ms-excel",
    218         "application/msword",
    219         "application/vnd.ms-powerpoint",
    220         "application/pdf",
    221         "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    222         "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    223         "application/vnd.openxmlformats-officedocument.presentationml.presentation",
    224     };
    225 
    226     /**
    227      * The MIME type(s) of we could not accept from other device. TODO: define
    228      * correct type list
    229      */
    230     public static final String[] UNACCEPTABLE_SHARE_INBOUND_TYPES = new String[] {
    231         "text/x-vcalendar",
    232     };
    233 
    234     /** Where we store Bluetooth received files on the external storage */
    235     public static final String DEFAULT_STORE_SUBDIR = "/bluetooth";
    236 
    237     /**
    238      * Debug level logging
    239      */
    240     public static final boolean DEBUG = true;
    241 
    242     /**
    243      * Verbose level logging
    244      */
    245     public static final boolean VERBOSE = false;
    246 
    247     /** use TCP socket instead of Rfcomm Socket to develop */
    248     public static final boolean USE_TCP_DEBUG = false;
    249 
    250     /** use simple TCP server started from TestActivity */
    251     public static final boolean USE_TCP_SIMPLE_SERVER = false;
    252 
    253     /** Test TCP socket port */
    254     public static final int TCP_DEBUG_PORT = 6500;
    255 
    256     /** use emulator to debug */
    257     public static final boolean USE_EMULATOR_DEBUG = false;
    258 
    259     public static final int MAX_RECORDS_IN_DATABASE = 1000;
    260 
    261     public static final int BATCH_STATUS_PENDING = 0;
    262 
    263     public static final int BATCH_STATUS_RUNNING = 1;
    264 
    265     public static final int BATCH_STATUS_FINISHED = 2;
    266 
    267     public static final int BATCH_STATUS_FAILED = 3;
    268 
    269     public static final String BLUETOOTHOPP_NAME_PREFERENCE = "btopp_names";
    270 
    271     public static final String BLUETOOTHOPP_CHANNEL_PREFERENCE = "btopp_channels";
    272 
    273     public static String filename_SEQUENCE_SEPARATOR = "-";
    274 
    275     public static void updateShareStatus(Context context, int id, int status) {
    276         Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + id);
    277         ContentValues updateValues = new ContentValues();
    278         updateValues.put(BluetoothShare.STATUS, status);
    279         context.getContentResolver().update(contentUri, updateValues, null, null);
    280         Constants.sendIntentIfCompleted(context, contentUri, status);
    281     }
    282 
    283     /*
    284      * This function should be called whenever transfer status change to
    285      * completed.
    286      */
    287     public static void sendIntentIfCompleted(Context context, Uri contentUri, int status) {
    288         if (BluetoothShare.isStatusCompleted(status)) {
    289             Intent intent = new Intent(BluetoothShare.TRANSFER_COMPLETED_ACTION);
    290             intent.setClassName(THIS_PACKAGE_NAME, BluetoothOppReceiver.class.getName());
    291             intent.setDataAndNormalize(contentUri);
    292             context.sendBroadcast(intent);
    293         }
    294     }
    295 
    296     public static boolean mimeTypeMatches(String mimeType, String[] matchAgainst) {
    297         for (String matchType : matchAgainst) {
    298             if (mimeTypeMatches(mimeType, matchType)) {
    299                 return true;
    300             }
    301         }
    302         return false;
    303     }
    304 
    305     public static boolean mimeTypeMatches(String mimeType, String matchAgainst) {
    306         Pattern p = Pattern.compile(matchAgainst.replaceAll("\\*", "\\.\\*"),
    307                 Pattern.CASE_INSENSITIVE);
    308         return p.matcher(mimeType).matches();
    309     }
    310 
    311     public static void logHeader(HeaderSet hs) {
    312         Log.v(TAG, "Dumping HeaderSet " + hs.toString());
    313         try {
    314 
    315             Log.v(TAG, "COUNT : " + hs.getHeader(HeaderSet.COUNT));
    316             Log.v(TAG, "NAME : " + hs.getHeader(HeaderSet.NAME));
    317             Log.v(TAG, "TYPE : " + hs.getHeader(HeaderSet.TYPE));
    318             Log.v(TAG, "LENGTH : " + hs.getHeader(HeaderSet.LENGTH));
    319             Log.v(TAG, "TIME_ISO_8601 : " + hs.getHeader(HeaderSet.TIME_ISO_8601));
    320             Log.v(TAG, "TIME_4_BYTE : " + hs.getHeader(HeaderSet.TIME_4_BYTE));
    321             Log.v(TAG, "DESCRIPTION : " + hs.getHeader(HeaderSet.DESCRIPTION));
    322             Log.v(TAG, "TARGET : " + hs.getHeader(HeaderSet.TARGET));
    323             Log.v(TAG, "HTTP : " + hs.getHeader(HeaderSet.HTTP));
    324             Log.v(TAG, "WHO : " + hs.getHeader(HeaderSet.WHO));
    325             Log.v(TAG, "OBJECT_CLASS : " + hs.getHeader(HeaderSet.OBJECT_CLASS));
    326             Log.v(TAG, "APPLICATION_PARAMETER : " + hs.getHeader(HeaderSet.APPLICATION_PARAMETER));
    327         } catch (IOException e) {
    328             Log.e(TAG, "dump HeaderSet error " + e);
    329         }
    330     }
    331 }
    332