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

Commit 8600c8e9 authored by Bryce Lee's avatar Bryce Lee
Browse files

Reconnect to communal source.

This changelist adds reconnection logic when the
communal source package is reinstalled.

Bug: 194866716
Test: atest CommunalSourcePrimerTest
Change-Id: Icd9f17b494f9a1904b60504f006fac4ac8a15c81
parent f73d730f
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -691,4 +691,13 @@

    <!-- Timeout to idle mode duration in milliseconds. -->
    <integer name="config_idleModeTimeout">10000</integer>

    <!-- The maximum number of attempts to reconnect to the communal source target after failing
         to connect -->
    <integer name="config_communalSourceMaxReconnectAttempts">10</integer>

    <!-- The initial amount of time (in milliseconds) before attempting to reconnect to a communal
         source. This value is used as the base value in an exponential backoff in subsequent
         attempts. -->
    <integer name="config_communalSourceReconnectBaseDelay">1000</integer>
</resources>
+3 −0
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@ import android.os.Bundle;

import androidx.annotation.NonNull;

import com.android.internal.annotations.VisibleForTesting;

import java.io.FileDescriptor;
import java.io.PrintWriter;

@@ -48,6 +50,7 @@ public abstract class SystemUI implements Dumpable {
    public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) {
    }

    @VisibleForTesting
    protected void onBootCompleted() {
    }

+16 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.view.View;
import androidx.concurrent.futures.CallbackToFutureAdapter;

import com.android.systemui.communal.CommunalSource;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.shared.communal.ICommunalSource;
import com.android.systemui.shared.communal.ICommunalSurfaceCallback;

@@ -36,6 +37,8 @@ import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.concurrent.Executor;

import javax.inject.Inject;

/**
 * {@link CommunalSourceImpl} provides a wrapper around {@link ICommunalSource} proxies as an
 * implementation of {@link CommunalSource}. Requests and responses for communal surfaces are
@@ -47,6 +50,19 @@ public class CommunalSourceImpl implements CommunalSource {
    private final ICommunalSource mSourceProxy;
    private final Executor mMainExecutor;

    static class Factory {
        private final Executor mExecutor;

        @Inject
        Factory(@Main Executor executor) {
            mExecutor = executor;
        }

        public CommunalSource create(ICommunalSource source) {
            return new CommunalSourceImpl(mExecutor, source);
        }
    }

    // mConnected is initialized to true as it is presumed instances are constructed with valid
    // proxies. The source can never be reconnected once the proxy has died. Once this value
    // becomes false, the source will always report disconnected to registering callbacks.
+146 −16
Original line number Diff line number Diff line
@@ -16,11 +16,15 @@

package com.android.systemui.communal.service;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.res.Resources;
import android.os.IBinder;
import android.os.PatternMatcher;
import android.util.Log;

import com.android.systemui.R;
@@ -29,8 +33,7 @@ import com.android.systemui.communal.CommunalSourceMonitor;
import com.android.systemui.dagger.SysUISingleton;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.shared.communal.ICommunalSource;

import java.util.concurrent.Executor;
import com.android.systemui.util.concurrency.DelayableExecutor;

import javax.inject.Inject;

@@ -43,63 +46,190 @@ import javax.inject.Inject;
@SysUISingleton
public class CommunalSourcePrimer extends SystemUI {
    private static final String TAG = "CommunalSourcePrimer";
    private static final boolean DEBUG = false;
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
    private static final String ACTION_COMMUNAL_SOURCE = "android.intent.action.COMMUNAL_SOURCE";

    private final Context mContext;
    private final Executor mMainExecutor;
    private final DelayableExecutor mMainExecutor;
    private final CommunalSourceMonitor mMonitor;
    private static final String ACTION_COMMUNAL_SOURCE = "android.intent.action.COMMUNAL_SOURCE";
    private final CommunalSourceImpl.Factory mSourceFactory;
    private final ComponentName mComponentName;
    private final int mBaseReconnectDelayMs;
    private final int mMaxReconnectAttempts;

    private int mReconnectAttempts = 0;
    private Runnable mCurrentReconnectCancelable;

    private final Runnable mConnectRunnable = new Runnable() {
        @Override
        public void run() {
            mCurrentReconnectCancelable = null;
            bindToService();
        }
    };


    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (DEBUG) {
                Log.d(TAG, "package added receiver - onReceive");
            }

            initiateConnectionAttempt();
        }
    };

    private final ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            final ICommunalSource source = ICommunalSource.Stub.asInterface(service);
            if (DEBUG) {
                Log.d(TAG, "onServiceConnected. source;" + source);
                Log.d(TAG, "onServiceConnected. source:" + source);
            }

            mMonitor.setSource(
                    source != null ? new CommunalSourceImpl(mMainExecutor, source) : null);
            if (source == null) {
                if (DEBUG) {
                    Log.d(TAG, "onServiceConnected. invalid source");
                    // Since the service could just repeatedly return null, the primer chooses
                    // to schedule rather than initiate a new connection attempt sequence.
                    scheduleConnectionAttempt();
                }
                return;
            }

            mMonitor.setSource(mSourceFactory.create(source));
        }

        @Override
        public void onBindingDied(ComponentName name) {
            if (DEBUG) {
                Log.d(TAG, "onBindingDied. lost communal source. initiating reconnect");
            }

            initiateConnectionAttempt();
        }

        @Override
        public void onServiceDisconnected(ComponentName className) {
            if (DEBUG) {
                Log.d(TAG,
                        "onServiceDisconnected. lost communal source. initiating reconnect");
            }

            initiateConnectionAttempt();
        }
    };

    @Inject
    public CommunalSourcePrimer(Context context, @Main Executor mainExecutor,
            CommunalSourceMonitor monitor) {
    public CommunalSourcePrimer(Context context, @Main Resources resources,
            DelayableExecutor mainExecutor,
            CommunalSourceMonitor monitor,
            CommunalSourceImpl.Factory sourceFactory) {
        super(context);
        mContext = context;
        mMainExecutor = mainExecutor;
        mMonitor = monitor;
        mSourceFactory = sourceFactory;
        mMaxReconnectAttempts = resources.getInteger(
                R.integer.config_communalSourceMaxReconnectAttempts);
        mBaseReconnectDelayMs = resources.getInteger(
                R.integer.config_communalSourceReconnectBaseDelay);

        final String component = resources.getString(R.string.config_communalSourceComponent);
        mComponentName = component != null && !component.isEmpty()
                ? ComponentName.unflattenFromString(component) : null;
    }

    @Override
    public void start() {
    }

    private void initiateConnectionAttempt() {
        // Reset attempts
        mReconnectAttempts = 0;
        mMonitor.setSource(null);

        // The first attempt is always a direct invocation rather than delayed.
        bindToService();
    }

    private void registerPackageListening() {
        if (mComponentName == null) {
            return;
        }

        final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
        filter.addDataScheme("package");
        filter.addDataSchemeSpecificPart(mComponentName.getPackageName(),
                PatternMatcher.PATTERN_LITERAL);
        // Note that we directly register the receiver here as data schemes are not supported by
        // BroadcastDispatcher.
        mContext.registerReceiver(mReceiver, filter);
    }

    private void scheduleConnectionAttempt() {
        // always clear cancelable if present.
        if (mCurrentReconnectCancelable != null) {
            mCurrentReconnectCancelable.run();
            mCurrentReconnectCancelable = null;
        }

        if (mReconnectAttempts >= mMaxReconnectAttempts) {
            if (DEBUG) {
                Log.d(TAG, "exceeded max connection attempts.");
            }
            return;
        }

        final long reconnectDelayMs =
                (long) Math.scalb(mBaseReconnectDelayMs, mReconnectAttempts);

        if (DEBUG) {
            Log.d(TAG, "start");
            Log.d(TAG,
                    "scheduling connection attempt in " + reconnectDelayMs + "milliseconds");
        }

        mCurrentReconnectCancelable = mMainExecutor.executeDelayed(mConnectRunnable,
                reconnectDelayMs);

        mReconnectAttempts++;
    }

    @Override
    protected void onBootCompleted() {
        super.onBootCompleted();
        final String serviceComponent = mContext.getString(R.string.config_communalSourceComponent);

        if (DEBUG) {
            Log.d(TAG, "onBootCompleted. communal source component:" + serviceComponent);
            Log.d(TAG, "onBootCompleted. communal source component:" + mComponentName);
        }

        registerPackageListening();
        initiateConnectionAttempt();
    }

        if (serviceComponent == null || serviceComponent.isEmpty()) {
    private void bindToService() {
        if (mComponentName == null) {
            return;
        }

        if (DEBUG) {
            Log.d(TAG, "attempting to bind to communal source");
        }

        final Intent intent = new Intent();
        intent.setAction(ACTION_COMMUNAL_SOURCE);
        intent.setComponent(ComponentName.unflattenFromString(serviceComponent));
        intent.setComponent(mComponentName);

        mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
        final boolean binding = mContext.bindService(intent, Context.BIND_AUTO_CREATE,
                mMainExecutor, mConnection);

        if (!binding) {
            if (DEBUG) {
                Log.d(TAG, "bindService failed, rescheduling");
            }

            scheduleConnectionAttempt();
        }
    }
}
+223 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.communal.service;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.res.Resources;
import android.os.IBinder;
import android.testing.AndroidTestingRunner;

import androidx.test.filters.SmallTest;

import com.android.systemui.R;
import com.android.systemui.SysuiTestCase;
import com.android.systemui.communal.CommunalSourceMonitor;
import com.android.systemui.shared.communal.ICommunalSource;
import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.time.FakeSystemClock;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.concurrent.Executor;

@SmallTest
@RunWith(AndroidTestingRunner.class)
public class CommunalSourcePrimerTest extends SysuiTestCase {
    private static final String TEST_COMPONENT_NAME = "com.google.tests/.CommualService";
    private static final ComponentName TEST_COMPONENT =
            ComponentName.unflattenFromString(TEST_COMPONENT_NAME);
    private static final int MAX_RETRIES = 5;
    private static final int RETRY_DELAY_MS = 1000;

    @Mock
    private Context mContext;

    @Mock
    private Resources mResources;

    private FakeExecutor mFakeExecutor = new FakeExecutor(new FakeSystemClock());

    @Mock
    private CommunalSourceMonitor mCommunalSourceMonitor;

    @Mock
    private CommunalSourceImpl.Factory mCommunalSourceFactory;

    @Mock
    private CommunalSourceImpl mCommunalSourceImpl;

    @Mock
    private IBinder mServiceProxy;

    private CommunalSourcePrimer mPrimer;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        when(mResources.getInteger(R.integer.config_communalSourceMaxReconnectAttempts))
                .thenReturn(MAX_RETRIES);
        when(mResources.getInteger(R.integer.config_communalSourceReconnectBaseDelay))
                .thenReturn(RETRY_DELAY_MS);
        when(mResources.getString(R.string.config_communalSourceComponent))
                .thenReturn(TEST_COMPONENT_NAME);
        when(mCommunalSourceFactory.create(any(ICommunalSource.class)))
                .thenReturn(mCommunalSourceImpl);

        mPrimer = new CommunalSourcePrimer(mContext, mResources, mFakeExecutor,
                mCommunalSourceMonitor, mCommunalSourceFactory);
    }

    @Test
    public void testNoConnectWithEmptyComponent() {
        when(mResources.getString(R.string.config_communalSourceComponent)).thenReturn(null);
        final CommunalSourcePrimer emptyComponentPrimer = new CommunalSourcePrimer(mContext,
                mResources, mFakeExecutor, mCommunalSourceMonitor, mCommunalSourceFactory);

        emptyComponentPrimer.onBootCompleted();
        mFakeExecutor.runAllReady();
        // When there is no component, we should not register any broadcast receives or bind to
        // any service
        verify(mContext, times(0))
                .registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
        verify(mContext, times(0)).bindService(any(Intent.class), anyInt(),
                any(Executor.class), any(ServiceConnection.class));
    }

    private ServiceConnection givenOnBootCompleted(boolean bindSucceed) {
        ArgumentCaptor<ServiceConnection> connectionCapture =
                ArgumentCaptor.forClass(ServiceConnection.class);

        when(mContext.bindService(any(Intent.class), anyInt(), any(Executor.class),
                any(ServiceConnection.class))).thenReturn(bindSucceed);

        mPrimer.onBootCompleted();
        mFakeExecutor.runAllReady();

        verify(mContext).bindService(any(Intent.class), anyInt(), any(Executor.class),
                connectionCapture.capture());

        // Simulate successful connection.
        return connectionCapture.getValue();
    }

    @Test
    public void testConnect() {
        final ServiceConnection connection = givenOnBootCompleted(true);

        // Simulate successful connection.
        connection.onServiceConnected(TEST_COMPONENT, mServiceProxy);

        // Verify source created and monitor informed.
        verify(mCommunalSourceFactory).create(any(ICommunalSource.class));
        verify(mCommunalSourceMonitor).setSource(mCommunalSourceImpl);
    }

    @Test
    public void testRetryOnBindFailure() {
        // Fail to bind on connection.
        givenOnBootCompleted(false);

        // Verify attempts happen. Note that we account for the retries plus initial attempt, which
        // is not scheduled.
        for (int attemptCount = 0; attemptCount < MAX_RETRIES + 1; attemptCount++) {
            verify(mContext, times(1)).bindService(any(Intent.class),
                    anyInt(), any(Executor.class), any(ServiceConnection.class));
            clearInvocations(mContext);
            mFakeExecutor.advanceClockToNext();
            mFakeExecutor.runAllReady();
        }

        // Verify no more attempts occur.
        verify(mContext, times(0)).bindService(any(Intent.class), anyInt(),
                any(Executor.class), any(ServiceConnection.class));

        // Verify source is not created and monitor is not informed.
        verify(mCommunalSourceFactory, times(0))
                .create(any(ICommunalSource.class));
        verify(mCommunalSourceMonitor, times(0))
                .setSource(any(CommunalSourceImpl.class));
    }

    @Test
    public void testAttemptOnPackageChange() {
        ArgumentCaptor<BroadcastReceiver> receiverCapture =
                ArgumentCaptor.forClass(BroadcastReceiver.class);

        // Fail to bind initially.
        givenOnBootCompleted(false);

        // Capture broadcast receiver.
        verify(mContext).registerReceiver(receiverCapture.capture(), any(IntentFilter.class));

        clearInvocations(mContext);

        // Inform package has been added.
        receiverCapture.getValue().onReceive(mContext, new Intent());

        // Verify bind has been attempted.
        verify(mContext, times(1)).bindService(any(Intent.class), anyInt(),
                any(Executor.class), any(ServiceConnection.class));
    }

    @Test
    public void testRetryOnServiceDisconnected() {
        verifyConnectionFailureReconnect(v -> v.onServiceDisconnected(TEST_COMPONENT));
    }

    @Test
    public void testRetryOnBindingDied() {
        verifyConnectionFailureReconnect(v -> v.onBindingDied(TEST_COMPONENT));
    }

    private void verifyConnectionFailureReconnect(ConnectionHandler connectionHandler) {
        // Fail to bind on connection.
        final ServiceConnection connection = givenOnBootCompleted(false);

        clearInvocations(mContext, mCommunalSourceMonitor);

        connectionHandler.onConnectionMade(connection);

        // Ensure source is cleared.
        verify(mCommunalSourceMonitor).setSource(null);

        // Ensure request made to bind. This is not a reattempt so it should happen in the same
        // execution loop.
        verify(mContext).bindService(any(Intent.class), anyInt(), any(Executor.class),
                any(ServiceConnection.class));
    }

    interface ConnectionHandler {
        void onConnectionMade(ServiceConnection connection);
    }
}