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

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

MAP: Add MAPEmail support in SDP only when email account available.

Register primary email account configuration or deletion to update
MAP Email support dynamically from MAP Server SDP only when primary
email is configured or available.

CRs-fixed: 727015

Change-Id: I7328e210d51e7a15f1493e00846f22688ff03148
parent 6710ad8d
Loading
Loading
Loading
Loading
+113 −1
Original line number Diff line number Diff line
@@ -47,6 +47,13 @@ import android.util.Log;
import android.provider.Settings;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.net.Uri;
import android.content.ContentResolver;
import android.database.ContentObserver;
import com.android.bluetooth.map.BluetoothMapUtils.*;
import com.android.bluetooth.map.BluetoothMapUtils.TYPE;
import android.database.sqlite.SQLiteException;


import com.android.bluetooth.R;
import com.android.bluetooth.Utils;
@@ -113,6 +120,8 @@ public class BluetoothMapService extends ProfileService {

    private static final int DISCONNECT_MAP = 3;

    private static final int MSG_INTERNAL_REGISTER_EMAIL = 4;

    private static final int RELEASE_WAKE_LOCK_DELAY = 10000;

    private PowerManager.WakeLock mWakeLock = null;
@@ -163,12 +172,50 @@ public class BluetoothMapService extends ProfileService {
    public static final int MAX_INSTANCES = 2;
    BluetoothMapObexConnectionManager mConnectionManager = null;
    public static final int MAS_INS_INFO[] = {MESSAGE_TYPE_SMS_MMS, MESSAGE_TYPE_EMAIL};
    private ContentObserver mEmailAccountObserver;
    public static final String AUTHORITY = "com.android.email.provider";
    public static final Uri EMAIL_URI = Uri.parse("content://" + AUTHORITY);
    public static final Uri EMAIL_ACCOUNT_URI = Uri.withAppendedPath(EMAIL_URI, "account");
    public static boolean isMapEmailRequestON = false;

    public BluetoothMapService() {
        mState = BluetoothMap.STATE_DISCONNECTED;
        mConnectionManager = new BluetoothMapObexConnectionManager();
        if (VERBOSE)
           Log.v(TAG, "BluetoothMapService: mIsEmailEnabled: " + mIsEmailEnabled);
        mEmailAccountObserver = new ContentObserver(null) {
            @Override
            public void onChange(boolean selfChange) {
                long isEmailConfigured = -1;
                Context context = mConnectionManager.getEmailContext();
                //Handle only email configuration changes
                if (context != null )
                    isEmailConfigured = BluetoothMapUtils.getEmailAccountId(context) ;
                boolean isMapEmailStarted = mConnectionManager.isMapEmailON();

                if (VERBOSE) {
                     Log.v(TAG,"onChange mEmailAccountObserver Configured Account: "+ isEmailConfigured
                     + "IsMapEmailRequstInprogress " + isMapEmailRequestON + "isMAP AlreadyStarted "+ isMapEmailStarted);
                }
                if( mSessionStatusHandler.hasMessages(MSG_INTERNAL_REGISTER_EMAIL)) {
                        // Remove any pending requests in queue
                        mSessionStatusHandler.removeMessages(MSG_INTERNAL_REGISTER_EMAIL);
                }
                // Donot send request if Inprogress or already ON
                if( isEmailConfigured != -1 && !isMapEmailRequestON && !isMapEmailStarted) {
                    if ( mAdapter != null) {
                        int state = mAdapter.getState();
                        if (state == BluetoothAdapter.STATE_ON ) {
                            mSessionStatusHandler.sendEmptyMessage(MSG_INTERNAL_REGISTER_EMAIL);
                        } else if (VERBOSE) {
                                Log.v(TAG, "BT is not ON, no start");
                        }
                    }
                } else if(isEmailConfigured == -1) {
                    mConnectionManager.closeMapEmail();
                }
            }
        };
    }
    private final void closeService() {
       if (VERBOSE) Log.v(TAG, "closeService");
@@ -186,6 +233,14 @@ public class BluetoothMapService extends ProfileService {
                        mConnectionManager.startAll();
                    }
                    break;

                case MSG_INTERNAL_REGISTER_EMAIL:
                    Log.d(TAG,"received MSG_INTERNAL_REGISTER_EMAIL");
                    if ((mAdapter != null) && mAdapter.isEnabled()) {
                        mConnectionManager.startMapEmail();
                    }
                    break;

                case USER_TIMEOUT:
                    Intent intent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_CANCEL);
                    intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mRemoteDevice);
@@ -381,6 +436,14 @@ public class BluetoothMapService extends ProfileService {
        mSessionStatusHandler.sendMessage(mSessionStatusHandler
                .obtainMessage(START_LISTENER));
        }
        // Register EmailAccountObserver for dynamic SDP update
        try {
            if (DEBUG) Log.d(TAG,"Registering observer");
            getContentResolver().registerContentObserver(
               EMAIL_ACCOUNT_URI, false, mEmailAccountObserver);
        } catch (SQLiteException e) {
            Log.e(TAG, "SQLite exception: " + e);
        }
        return true;
    }

@@ -395,6 +458,12 @@ public class BluetoothMapService extends ProfileService {

        setState(BluetoothMap.STATE_DISCONNECTED, BluetoothMap.RESULT_CANCELED);
        closeService();
        try {
            if (DEBUG) Log.d(TAG,"Unregistering Email account observer");
            getContentResolver().unregisterContentObserver(mEmailAccountObserver);
        } catch (SQLiteException e) {
            Log.e(TAG, "SQLite exception: " + e);
        }
        return true;
    }

@@ -532,6 +601,39 @@ public class BluetoothMapService extends ProfileService {
                connection.startRfcommSocketListener();
            }
        }
        public boolean isMapEmailON () {
                final BluetoothMapObexConnection connect = mConnections.get(1);
                if(connect != null && connect.mMasId == 1 && connect.mServerSocket != null
                    && connect.mAcceptThread != null )
                    return true;
                return false;
        }
        public Context getEmailContext () {
            final BluetoothMapObexConnection connect = mConnections.get(1);
            return connect.context;
        }
        public void startMapEmail() {
            final BluetoothMapObexConnection connect = mConnections.get(1);
            if (connect != null) {
                // SET Email Inprogress ON
                isMapEmailRequestON = true;
                // Start Listener and add email support in SDP
                connect.startRfcommSocketListener();
            }
            // SET Email Inprogress OFF
            isMapEmailRequestON = false;
        }

        public void closeMapEmail() {
            final BluetoothMapObexConnection connect = mConnections.get(1);
            if (connect != null) {
                // Handle common flag for MAP instances
                boolean isWaitingAuth = isWaitingAuthorization;
                // Stop Listener and remove email support in SDP
                connect.closeConnection();
                isWaitingAuthorization = isWaitingAuth;
            }
        }

        public void init() {
            for (BluetoothMapObexConnection connection: mConnections) {
@@ -584,6 +686,7 @@ public class BluetoothMapService extends ProfileService {
        private ServerSession mServerSession = null;
        private int mSupportedMessageTypes;
        private int mMasId;
        private Context context;
        boolean mWaitingForConfirmation = false;

        public BluetoothMapObexConnection(int supportedMessageTypes, int masId) {
@@ -599,6 +702,16 @@ public class BluetoothMapService extends ProfileService {
                Log.v(TAG, "Map Service startRfcommSocketListener");
                Log.v(TAG, "mMasId is "+mMasId);
            }

            context = getApplicationContext();
            // Promote Email Instance only if primary email account configured
            if(mMasId == 1) {
               if (BluetoothMapUtils.getEmailAccountId(context) == -1) {
                   if (VERBOSE) Log.v(TAG, "account is not configured");
                   return;
               }
            }

            if (mServerSocket == null) {
                if (!initSocket()) {
                    closeConnection();
@@ -728,7 +841,6 @@ public class BluetoothMapService extends ProfileService {
                  Log.d(TAG, "Map Service startObexServerSession");
                  Log.d(TAG, "mMasId is "+mMasId);
            }
            Context context = getApplicationContext();
            if(VERBOSE) Log.d(TAG, "after getting application context");
            if(mBluetoothMnsObexClient == null)
                mBluetoothMnsObexClient = new BluetoothMnsObexClient(context, mRemoteDevice);