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

Commit 21946df7 authored by Joseph Pirozzo's avatar Joseph Pirozzo
Browse files

MapClient clean up at start

Clean up stale data if an unexpeced shutdown occurs while devices are
connected.
Bug: 181696727
Bug: 189207206
Test: atest BluetoothInstrumentationTests
Tag: #stability

Change-Id: I15a0b54b963591e2df87657cc3a1dae0949374f0
Merged-In: I15a0b54b963591e2df87657cc3a1dae0949374f0
parent 6118900c
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;
@@ -123,12 +124,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);
@@ -160,6 +178,7 @@ class MapClientContent {
        } else {
            mPhoneNumber = PhoneNumberUtils.extractNetworkPortion(getOriginatorNumber(message));
        }

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

@@ -319,6 +338,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);

@@ -332,7 +353,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;
@@ -387,7 +407,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);
@@ -400,12 +421,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);
        }
    }

    /**
@@ -479,6 +512,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,
+1 −0
Original line number Diff line number Diff line
@@ -314,6 +314,7 @@ public class MapClientService extends ProfileService {
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        registerReceiver(mMapReceiver, filter);
        removeUncleanAccounts();
        MapClientContent.clearAllContent(this);
        setMapClientService(this);
        return true;
    }
+9 −8
Original line number Diff line number Diff line
@@ -129,7 +129,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 =
@@ -196,13 +196,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);
@@ -522,6 +515,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;

+29 −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 {
@@ -128,6 +134,8 @@ public class MapClientContentTest {
                .thenReturn(mMockSubscriptionManager);
        when(mMockContext.getSystemServiceName(SubscriptionManager.class))
                .thenReturn(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
        when(mMockSubscriptionManager.getActiveSubscriptionInfoList())
                .thenReturn(Arrays.asList(mMockSubscription));
        createTestMessages();

    }
@@ -178,6 +186,7 @@ public class MapClientContentTest {

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

    /**
@@ -339,6 +348,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();
@@ -404,7 +428,10 @@ public class MapClientContentTest {
            when(cursor.getInt(anyInt())).thenReturn(READ);
            return cursor;
        }
    }


        @Override
        public int update(Uri uri, ContentValues values, Bundle extras) {
            return 0;
        }
    }
}