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

Commit b520ad21 authored by Pradeep Panigrahi's avatar Pradeep Panigrahi Committed by Linux Build Service Account
Browse files

Handle file send retry scenario inside thread to avoid ANR.

Use Case:
1. Turn on BT and pair with the other device via BT.
2. Launch Contacts ->press menu -> "Import/export" ->
   "Share visible contacts" -> "Bluetooth" -> "select
    paired device".
3. Turn off BT and let the transfer fail
4. Drag down the status bar and tap the bluetooth share notification
5. Wait for a while, tap "try again"

Failure:
ANR occurs and "Unfortunately,Bluetooth share has stopped" error comes.

Root Cause:
BluetoothOppSendFileInfo::generateFileInfo() takes long time to process
when there is huge contact list thereby causing ANR

Fix:
When tap the "try again" button on the send failed dialog, the
method "BluetoothOppSendFileInfo::generateFileInfo()" will be
call, if there are about 1500 contacts, then this method would
be very time consuming, therby causing ANR. Changed this method
into the child thread execution and through a Handler send a
message to the main thread after this method return, to retry failed
transfer.

Change-Id: Idde7e874e4e25caba59133e0981e812b06ba2ed1
parent 3a94fa0d
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -127,6 +127,7 @@
    <string name="bluetooth_map_settings_intro" msgid="6793938602201480648">"选中您要通过蓝牙共享的帐户。建立连接后,您仍需接受对这些帐户的所有访问请求。"</string>
    <string name="bluetooth_map_settings_intro" msgid="6793938602201480648">"选中您要通过蓝牙共享的帐户。建立连接后,您仍需接受对这些帐户的所有访问请求。"</string>
    <string name="bluetooth_map_settings_count" msgid="4557473074937024833">"剩余空档数:"</string>
    <string name="bluetooth_map_settings_count" msgid="4557473074937024833">"剩余空档数:"</string>
    <string name="bluetooth_map_settings_app_icon" msgid="7105805610929114707">"应用图标"</string>
    <string name="bluetooth_map_settings_app_icon" msgid="7105805610929114707">"应用图标"</string>
    <string name="upload_fail_waiting">正在进行数据准备,稍后自动重发</string>
    <string name="bluetooth_map_settings_title" msgid="7420332483392851321">"蓝牙消息共享设置"</string>
    <string name="bluetooth_map_settings_title" msgid="7420332483392851321">"蓝牙消息共享设置"</string>
    <string name="bluetooth_map_settings_no_account_slots_left" msgid="1796029082612965251">"无法选择帐户,目前没有任何空档"</string>
    <string name="bluetooth_map_settings_no_account_slots_left" msgid="1796029082612965251">"无法选择帐户,目前没有任何空档"</string>
</resources>
</resources>
+1 −0
Original line number Original line Diff line number Diff line
@@ -245,4 +245,5 @@
    <string name="bluetooth_map_settings_app_icon">Application Icon</string>
    <string name="bluetooth_map_settings_app_icon">Application Icon</string>
    <string name="bluetooth_map_settings_title">Bluetooth Message Sharing Settings</string>
    <string name="bluetooth_map_settings_title">Bluetooth Message Sharing Settings</string>
    <string name="bluetooth_map_settings_no_account_slots_left">Cannot select account. 0 slots left</string>
    <string name="bluetooth_map_settings_no_account_slots_left">Cannot select account. 0 slots left</string>
    <string name="upload_fail_waiting">Ongoing send file preparation, automatic retransmission later</string>
</resources>
</resources>
+55 −19
Original line number Original line Diff line number Diff line
@@ -41,6 +41,7 @@ import android.content.Intent;
import android.net.Uri;
import android.net.Uri;
import android.os.Bundle;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.Log;
import android.view.View;
import android.view.View;
import android.widget.TextView;
import android.widget.TextView;
@@ -69,6 +70,9 @@ public class BluetoothOppTransferActivity extends AlertActivity implements
    private static final boolean D = Constants.DEBUG;
    private static final boolean D = Constants.DEBUG;
    private static final boolean V = Constants.VERBOSE;
    private static final boolean V = Constants.VERBOSE;


    private UpdateHandler mHandler;
    private final int GENERATE_SEND_FILE_INFO_OK = 1;

    private Uri mUri;
    private Uri mUri;


    // ongoing transfer-0 complete transfer-1
    // ongoing transfer-0 complete transfer-1
@@ -163,6 +167,8 @@ public class BluetoothOppTransferActivity extends AlertActivity implements


        // Set up the "dialog"
        // Set up the "dialog"
        setUpDialog();
        setUpDialog();

        mHandler = new UpdateHandler();
    }
    }


    @Override
    @Override
@@ -346,6 +352,34 @@ public class BluetoothOppTransferActivity extends AlertActivity implements
        }
        }
    }
    }


    private class UpdateHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == GENERATE_SEND_FILE_INFO_OK) {
                if (D) Log.d(TAG, "GENERATE_SEND_FILE_INFO_OK");
                // make current transfer "hidden"
                BluetoothOppUtility.updateVisibilityToHidden(BluetoothOppTransferActivity.this,
                        mUri);

                // clear correspondent notification item
                ((NotificationManager)getSystemService(NOTIFICATION_SERVICE))
                        .cancel(mTransInfo.mID);

                // retry the failed transfer
                BluetoothOppUtility.retryTransfer(BluetoothOppTransferActivity.this, mTransInfo);

                BluetoothDevice remoteDevice = (BluetoothDevice)msg.obj;
                // Display toast message
                Toast.makeText(BluetoothOppTransferActivity.this,
                        BluetoothOppTransferActivity.this.getString(R.string.bt_toast_4,
                            BluetoothOppManager.getInstance(BluetoothOppTransferActivity.this)
                                .getDeviceName(remoteDevice)), Toast.LENGTH_SHORT).show();
                finish();
            }
       }
    };

    public void onClick(DialogInterface dialog, int which) {
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
        switch (which) {
            case DialogInterface.BUTTON_POSITIVE:
            case DialogInterface.BUTTON_POSITIVE:
@@ -363,25 +397,27 @@ public class BluetoothOppTransferActivity extends AlertActivity implements
                } else if (mWhichDialog == DIALOG_SEND_COMPLETE_FAIL) {
                } else if (mWhichDialog == DIALOG_SEND_COMPLETE_FAIL) {
                    // "try again"
                    // "try again"


                    // make current transfer "hidden"
                    new Thread("putSendFileInfo") {
                    BluetoothOppUtility.updateVisibilityToHidden(this, mUri);
                        Uri uri = BluetoothOppUtility.originalUri(Uri.parse(mTransInfo.mFileUri));

                        public void run() {
                    // clear correspondent notification item
                            BluetoothOppSendFileInfo sendFileInfo =
                    ((NotificationManager)getSystemService(NOTIFICATION_SERVICE))
                                BluetoothOppSendFileInfo.generateFileInfo(BluetoothOppTransferActivity.this,
                            .cancel(mTransInfo.mID);
                                uri, mTransInfo.mFileType);

                            uri = BluetoothOppUtility.generateUri(uri, sendFileInfo);
                    // retry the failed transfer
                            BluetoothOppUtility.putSendFileInfo(uri, sendFileInfo);
                    BluetoothOppUtility.retryTransfer(this, mTransInfo);
                            mTransInfo.mFileUri = uri.toString();


                    BluetoothDevice remoteDevice = mAdapter.getRemoteDevice(mTransInfo.mDestAddr);
                            BluetoothDevice remoteDevice =

                                mAdapter.getRemoteDevice(mTransInfo.mDestAddr);
                    // Display toast message
                            Message message = mHandler.obtainMessage();
                    Toast.makeText(
                            message.obj = remoteDevice;
                            this,
                            message.what = GENERATE_SEND_FILE_INFO_OK;
                            this.getString(R.string.bt_toast_4, BluetoothOppManager.getInstance(
                            message.sendToTarget();
                                    this).getDeviceName(remoteDevice)), Toast.LENGTH_SHORT)
                        }
                            .show();
                    }.start();

                    Toast.makeText(this, getString(R.string.upload_fail_waiting),
                            Toast.LENGTH_LONG).show();
                    return;
                } else if (mWhichDialog == DIALOG_SEND_COMPLETE_SUCCESS) {
                } else if (mWhichDialog == DIALOG_SEND_COMPLETE_SUCCESS) {
                    BluetoothOppUtility.updateVisibilityToHidden(this, mUri);
                    BluetoothOppUtility.updateVisibilityToHidden(this, mUri);
                    ((NotificationManager)getSystemService(NOTIFICATION_SERVICE))
                    ((NotificationManager)getSystemService(NOTIFICATION_SERVICE))
+20 −0
Original line number Original line Diff line number Diff line
@@ -324,6 +324,26 @@ public class BluetoothOppUtility {
                transInfo.mDeviceName);
                transInfo.mDeviceName);
    }
    }


    static Uri originalUri(Uri uri) {
        String mUri = uri.toString();
        int atIndex = mUri.lastIndexOf("@");
        if (atIndex != -1) {
            mUri = mUri.substring(0, atIndex);
            uri = Uri.parse(mUri);
        }
        if (V) Log.v(TAG, "originalUri: " + uri);
        return uri;
    }

    static Uri generateUri(Uri uri, BluetoothOppSendFileInfo sendFileInfo) {
        String fileInfo = sendFileInfo.toString();
        int atIndex = fileInfo.lastIndexOf("@");
        fileInfo = fileInfo.substring(atIndex);
        uri = Uri.parse(uri + fileInfo);
        if (V) Log.v(TAG, "generateUri: " + uri);
        return uri;
    }

    static void putSendFileInfo(Uri uri, BluetoothOppSendFileInfo sendFileInfo) {
    static void putSendFileInfo(Uri uri, BluetoothOppSendFileInfo sendFileInfo) {
        if (D) Log.d(TAG, "putSendFileInfo: uri=" + uri + " sendFileInfo=" + sendFileInfo);
        if (D) Log.d(TAG, "putSendFileInfo: uri=" + uri + " sendFileInfo=" + sendFileInfo);
        sSendFileMap.put(uri, sendFileInfo);
        sSendFileMap.put(uri, sendFileInfo);