Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 15bb907b authored by Hamster Tian's avatar Hamster Tian Committed by Steve Kondik
Browse files

[2/3]Bluetooth: add "Accept all files" option for incoming files via Bluetooth

  This patch will let the OBEX server read "Accept all files" option
before it starts to check mime-type whitelist.
  If not enabled,the check process will go on as usual.
  If enabled,mime-type checking will be skipped.

Change-Id: I056f88246911da0f47da9043187ff3f959315dce
parent 83e048a2
Loading
Loading
Loading
Loading
+57 −36
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.provider.Settings;
import android.os.Process;
import android.util.Log;
import android.webkit.MimeTypeMap;
@@ -255,6 +256,8 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen
        }
        boolean isWhitelisted = BluetoothOppManager.getInstance(mContext).
                isWhitelisted(destination);
        boolean acceptAllFilesIsEnabled = Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.BLUETOOTH_ACCEPT_ALL_FILES, 0) == 1;

        try {
            boolean pre_reject = false;
@@ -277,26 +280,31 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen
                obexResponse = ResponseCodes.OBEX_HTTP_BAD_REQUEST;
            }

            if (!acceptAllFilesIsEnabled) {
                if (!pre_reject) {
                    /* first we look for Mimetype in Android map */
                    String extension, type;
                    int dotIndex = name.lastIndexOf(".");
                    if (dotIndex < 0 && mimeType == null) {
                    if (D) Log.w(TAG, "There is no file extension or mime type," +
                            "reject the transfer");
                        if (D)
                            Log.w(TAG, "There is no file extension or mime type," +
                                    "reject the transfer. File name:" + name);
                        pre_reject = true;
                        obexResponse = ResponseCodes.OBEX_HTTP_BAD_REQUEST;
                    } else {
                        extension = name.substring(dotIndex + 1).toLowerCase();
                        MimeTypeMap map = MimeTypeMap.getSingleton();
                        type = map.getMimeTypeFromExtension(extension);
                    if (V) Log.v(TAG, "Mimetype guessed from extension " + extension + " is " + type);
                        if (V)
                            Log.v(TAG, "Mimetype guessed from extension " + extension + " is "
                                    + type);
                        if (type != null) {
                            mimeType = type;

                        } else {
                            if (mimeType == null) {
                            if (D) Log.w(TAG, "Can't get mimetype, reject the transfer");
                                if (D)
                                    Log.w(TAG, "Can't get mimetype, reject the transfer");
                                pre_reject = true;
                                obexResponse = ResponseCodes.OBEX_HTTP_UNSUPPORTED_TYPE;
                            }
@@ -307,7 +315,8 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen
                    }
                }

            // Reject policy: anything outside the "white list" plus unspecified
                // Reject policy: anything outside the "white list" plus
                // unspecified
                // MIME Types. Also reject everything in the "black list".
                if (!pre_reject
                        && (mimeType == null
@@ -315,10 +324,22 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen
                                        Constants.ACCEPTABLE_SHARE_INBOUND_TYPES))
                                || Constants.mimeTypeMatches(mimeType,
                                Constants.UNACCEPTABLE_SHARE_INBOUND_TYPES))) {
                if (D) Log.w(TAG, "mimeType is null or in unacceptable list, reject the transfer");
                    if (D)
                        Log.w(TAG,
                                "mimeType is null or in unacceptable list, reject the transfer. mimeType is "
                                        + ((mimeType == null) ? "null" : mimeType));
                    pre_reject = true;
                    obexResponse = ResponseCodes.OBEX_HTTP_UNSUPPORTED_TYPE;
                }
            } else {
                if (D)
                    Log.i(TAG, "acceptAllFilesIsEnabled is true, skipped check of mime type");
                if (mimeType == null) {
                    mimeType = "*/*";
                    if (D)
                        Log.i(TAG, "mimeType is null. Fixed to */*");
                }
            }

            if (pre_reject && obexResponse != ResponseCodes.OBEX_HTTP_OK) {
                // some bad implemented client won't send disconnect