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

Commit 713029c7 authored by Joseph Pirozzo's avatar Joseph Pirozzo Committed by Android (Google) Code Review
Browse files

Merge changes I15a0b54b,I6b74307e into sc-dev

* changes:
  MapClient clean up at start
  MapClient Send MMS
parents f78537af 4ccface1
Loading
Loading
Loading
Loading
+43 −9
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.provider.Telephony;
import android.provider.Telephony.Mms;
import android.provider.Telephony.MmsSms;
import android.provider.Telephony.Sms;
import android.provider.Telephony.Threads;
import android.telephony.PhoneNumberUtils;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
@@ -124,12 +125,29 @@ class MapClientContent {
            }
        };

        clearMessages();
        clearMessages(mContext, mSubscriptionId);
        mResolver.registerContentObserver(Sms.CONTENT_URI, true, mContentObserver);
        mResolver.registerContentObserver(Mms.CONTENT_URI, true, mContentObserver);
        mResolver.registerContentObserver(MmsSms.CONTENT_URI, true, mContentObserver);
    }

    static void clearAllContent(Context context) {
        SubscriptionManager subscriptionManager = (SubscriptionManager) context
                .getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
        List<SubscriptionInfo> subscriptions = subscriptionManager.getActiveSubscriptionInfoList();
        for (SubscriptionInfo info : subscriptions) {
            if (info.getSubscriptionType() == SubscriptionManager.SUBSCRIPTION_TYPE_REMOTE_SIM) {
                clearMessages(context, info.getSubscriptionId());
                try {
                    subscriptionManager.removeSubscriptionInfoRecord(info.getIccId(),
                            SubscriptionManager.SUBSCRIPTION_TYPE_REMOTE_SIM);
                } catch (Exception e) {
                    Log.w(TAG, "cleanUp failed: " + e.toString());
                }
            }
        }
    }

    private static void logD(String message) {
        if (MapClientService.DBG) {
            Log.d(TAG, message);
@@ -161,6 +179,7 @@ class MapClientContent {
        } else {
            mPhoneNumber = PhoneNumberUtils.extractNetworkPortion(getOriginatorNumber(message));
        }

        logV("Found phone number: " + mPhoneNumber);
    }

@@ -320,6 +339,8 @@ class MapClientContent {
            values.put(Mms.MESSAGE_SIZE, mmsBmessage.getSize());

            Uri results = mResolver.insert(contentUri, values);
            mHandleToUriMap.put(handle, results);
            mUriToHandleMap.put(results, new MessageStatus(handle, read));

            logD("Map InsertedThread" + results);

@@ -333,7 +354,6 @@ class MapClientContent {

            values.put(Mms.Part.CONTENT_TYPE, "plain/text");
            values.put(Mms.SUBSCRIPTION_ID, mSubscriptionId);
            mUriToHandleMap.put(results, new MessageStatus(handle, read));
        } catch (Exception e) {
            Log.e(TAG, e.toString());
            throw e;
@@ -388,7 +408,8 @@ class MapClientContent {
     * clear the subscription info and content on shutdown
     */
    void cleanUp() {
        clearMessages();
        mResolver.unregisterContentObserver(mContentObserver);
        clearMessages(mContext, mSubscriptionId);
        try {
            mSubscriptionManager.removeSubscriptionInfoRecord(mDevice.getAddress(),
                    SubscriptionManager.SUBSCRIPTION_TYPE_REMOTE_SIM);
@@ -401,12 +422,24 @@ class MapClientContent {
     * clearMessages
     * clean up the content provider on startup
     */
    private void clearMessages() {
        mResolver.unregisterContentObserver(mContentObserver);
        mResolver.delete(Sms.CONTENT_URI, Sms.SUBSCRIPTION_ID + " =? ",
                new String[]{Integer.toString(mSubscriptionId)});
        mResolver.delete(Mms.CONTENT_URI, Mms.SUBSCRIPTION_ID + " =? ",
                new String[]{Integer.toString(mSubscriptionId)});
    private static void clearMessages(Context context, int subscriptionId) {
        ContentResolver resolver = context.getContentResolver();
        String threads = new String();

        Uri uri = Threads.CONTENT_URI.buildUpon().appendQueryParameter("simple", "true").build();
        Cursor threadCursor = resolver.query(uri, null, null, null, null);
        while (threadCursor.moveToNext()) {
            threads += threadCursor.getInt(threadCursor.getColumnIndex(Threads._ID)) + ", ";
        }

        resolver.delete(Sms.CONTENT_URI, Sms.SUBSCRIPTION_ID + " =? ",
                new String[]{Integer.toString(subscriptionId)});
        resolver.delete(Mms.CONTENT_URI, Mms.SUBSCRIPTION_ID + " =? ",
                new String[]{Integer.toString(subscriptionId)});
        if (threads.length() > 2) {
            threads = threads.substring(1, threads.length() - 2);
            resolver.delete(Threads.CONTENT_URI, Threads._ID + " IN (" + threads + ")", null);
        }
    }

    /**
@@ -480,6 +513,7 @@ class MapClientContent {
        String threadId = Uri.parse(thread).getLastPathSegment();

        logD("MATCHING THREAD" + threadId);
        logD(MmsSms.CONTENT_CONVERSATIONS_URI + threadId + "/recipients");

        Cursor cursor = mResolver
                .query(Uri.withAppendedPath(MmsSms.CONTENT_CONVERSATIONS_URI,
+3 −3
Original line number Diff line number Diff line
@@ -46,7 +46,6 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class MapClientService extends ProfileService {
@@ -291,7 +290,7 @@ public class MapClientService extends ProfileService {
    }

    @Override
    protected IProfileServiceBinder initBinder() {
    public IProfileServiceBinder initBinder() {
        return new Binder(this);
    }

@@ -318,6 +317,7 @@ public class MapClientService extends ProfileService {
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        registerReceiver(mMapReceiver, filter);
        removeUncleanAccounts();
        MapClientContent.clearAllContent(this);
        setMapClientService(this);
        return true;
    }
@@ -461,7 +461,7 @@ public class MapClientService extends ProfileService {

        @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
        private MapClientService getService(AttributionSource source) {
            if (!Utils.checkCallerIsSystemOrActiveUser(TAG)
            if (!(MapUtils.isSystemUser() || Utils.checkCallerIsSystemOrActiveUser(TAG))
                    || !Utils.checkServiceAvailable(mService, TAG)
                    || !Utils.checkConnectPermissionForDataDelivery(mService, source, TAG)) {
                return null;
+9 −8
Original line number Diff line number Diff line
@@ -132,7 +132,7 @@ class MceStateMachine extends StateMachine {
    private final BluetoothDevice mDevice;
    private MapClientService mService;
    private MasClient mMasClient;
    private final MapClientContent mDatabase;
    private MapClientContent mDatabase;
    private HashMap<String, Bmessage> mSentMessageLog = new HashMap<>(MAX_MESSAGES);
    private HashMap<Bmessage, PendingIntent> mSentReceiptRequested = new HashMap<>(MAX_MESSAGES);
    private HashMap<Bmessage, PendingIntent> mDeliveryReceiptRequested =
@@ -199,13 +199,6 @@ class MceStateMachine extends StateMachine {
        mDisconnecting = new Disconnecting();
        mConnected = new Connected();

        MapClientContent.Callbacks callbacks = new MapClientContent.Callbacks(){
            @Override
            public void onMessageStatusChanged(String handle, int status) {
                setMessageStatus(handle, status);
            }
        };
        mDatabase = new MapClientContent(mService, callbacks, mDevice);

        addState(mDisconnected);
        addState(mConnecting);
@@ -525,6 +518,14 @@ class MceStateMachine extends StateMachine {
            if (DBG) {
                Log.d(TAG, "Enter Connected: " + getCurrentMessage().what);
            }

            MapClientContent.Callbacks callbacks = new MapClientContent.Callbacks(){
                @Override
                public void onMessageStatusChanged(String handle, int status) {
                    setMessageStatus(handle, status);
                }
            };
            mDatabase = new MapClientContent(mService, callbacks, mDevice);
            onConnectionStateChanged(mPreviousState, BluetoothProfile.STATE_CONNECTED);
            if (Utils.isPtsTestMode()) return;

+30 −2
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@@ -30,9 +31,11 @@ import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Telephony.Mms;
import android.provider.Telephony.Sms;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.test.mock.MockContentProvider;
import android.test.mock.MockContentResolver;
@@ -58,6 +61,7 @@ import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@@ -104,6 +108,8 @@ public class MapClientContentTest {

    @Mock
    private SubscriptionManager mMockSubscriptionManager;
    @Mock
    private SubscriptionInfo mMockSubscription;

    @Before
    public void setUp() throws Exception {
@@ -126,6 +132,9 @@ public class MapClientContentTest {
        when(mMockContext.getContentResolver()).thenReturn(mMockContentResolver);
        when(mMockContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE))
                .thenReturn(mMockSubscriptionManager);

        when(mMockSubscriptionManager.getActiveSubscriptionInfoList())
                .thenReturn(Arrays.asList(mMockSubscription));
        createTestMessages();

    }
@@ -176,6 +185,7 @@ public class MapClientContentTest {

        mMapClientContent.cleanUp();
        Assert.assertEquals(0, mMockSmsContentProvider.mContentValues.size());
        Assert.assertEquals(0, mMockThreadContentProvider.mContentValues.size());
    }

    /**
@@ -337,6 +347,21 @@ public class MapClientContentTest {
        mMapClientContent.cleanUp();
    }

    /**
     * Test to validate old subscriptions are removed at startup.
     */
    @Test
    public void testCleanUpAtStartup() {
        MapClientContent.clearAllContent(mMockContext);
        verify(mMockSubscriptionManager, never()).removeSubscriptionInfoRecord(any(), anyInt());

        when(mMockSubscription.getSubscriptionType())
                .thenReturn(SubscriptionManager.SUBSCRIPTION_TYPE_REMOTE_SIM);
        MapClientContent.clearAllContent(mMockContext);
        verify(mMockSubscriptionManager).removeSubscriptionInfoRecord(any(),
                eq(SubscriptionManager.SUBSCRIPTION_TYPE_REMOTE_SIM));
    }

    void createTestMessages() {
        mOriginator = new VCardEntry();
        VCardProperty property = new VCardProperty();
@@ -402,7 +427,10 @@ public class MapClientContentTest {
            when(cursor.getInt(anyInt())).thenReturn(READ);
            return cursor;
        }
    }


        @Override
        public int update(Uri uri, ContentValues values, Bundle extras) {
            return 0;
        }
    }
}
+20 −0
Original line number Diff line number Diff line
@@ -21,7 +21,9 @@ import static org.mockito.Mockito.*;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.IBluetoothMapClient;
import android.content.Context;
import android.os.UserHandle;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.MediumTest;
@@ -30,6 +32,7 @@ import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.R;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.Utils;
import com.android.bluetooth.btservice.AdapterService;
import com.android.bluetooth.btservice.storage.DatabaseManager;

@@ -182,6 +185,23 @@ public class MapClientTest {
        Assert.assertFalse(mService.connect(last));
    }

    /**
     * Test calling connect via Binder
     */
    @Test
    public void testConnectViaBinder() {
        BluetoothDevice device = makeBluetoothDevice("11:11:11:11:11:11");
        mockDevicePriority(device, BluetoothProfile.CONNECTION_POLICY_ALLOWED);
        IBluetoothMapClient.Stub mapClientBinder = (IBluetoothMapClient.Stub) mService.initBinder();
        try {
            Assert.assertFalse(mapClientBinder.connect(device, mAdapter.getAttributionSource()));
            Utils.setForegroundUserId(UserHandle.getCallingUserId());
            Assert.assertTrue(mapClientBinder.connect(device, mAdapter.getAttributionSource()));
        } catch (Exception e) {
            Assert.fail(e.toString());
        }
    }

    private BluetoothDevice makeBluetoothDevice(String address) {
        return mAdapter.getRemoteDevice(address);
    }