From bb34329a6b7889b943e7b2bf73b02feabe69729b Mon Sep 17 00:00:00 2001 From: Bryce Lee Date: Thu, 22 Jul 2021 22:31:04 -0700 Subject: [PATCH] Introduce CommunalSourcePrimer. CommunalSourcePrimer connects to a pre-defined CommunalSource at boot if present. It automatically sets the returned binder as the current CommunalSource. Bug: 194354107 Test: manual Change-Id: I7d1e7abcba2b48d77c9eaaacddb45ba1b3c72f49 --- packages/SystemUI/res/values/config.xml | 3 + .../service/CommunalSourcePrimer.java | 105 ++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 packages/SystemUI/src/com/android/systemui/communal/service/CommunalSourcePrimer.java diff --git a/packages/SystemUI/res/values/config.xml b/packages/SystemUI/res/values/config.xml index e912962ad0a39..30537df111993 100644 --- a/packages/SystemUI/res/values/config.xml +++ b/packages/SystemUI/res/values/config.xml @@ -669,4 +669,7 @@ false + + + @null diff --git a/packages/SystemUI/src/com/android/systemui/communal/service/CommunalSourcePrimer.java b/packages/SystemUI/src/com/android/systemui/communal/service/CommunalSourcePrimer.java new file mode 100644 index 0000000000000..bb3fef34e4cb4 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/communal/service/CommunalSourcePrimer.java @@ -0,0 +1,105 @@ +/* + * 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 android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.IBinder; +import android.util.Log; + +import com.android.systemui.R; +import com.android.systemui.SystemUI; +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 javax.inject.Inject; + +/** + * The {@link CommunalSourcePrimer} is responsible for priming SystemUI with a pre-configured + * Communal source. The SystemUI service binds to the component to retrieve the + * {@link com.android.systemui.communal.CommunalSource}. {@link CommunalSourcePrimer} has no effect + * if there is no pre-defined value. + */ +@SysUISingleton +public class CommunalSourcePrimer extends SystemUI { + private static final String TAG = "CommunalSourcePrimer"; + private static final boolean DEBUG = false; + private final Context mContext; + private final Executor mMainExecutor; + private final CommunalSourceMonitor mMonitor; + private static final String ACTION_COMMUNAL_SOURCE = "android.intent.action.COMMUNAL_SOURCE"; + + 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); + } + + mMonitor.setSource( + source != null ? new CommunalSourceImpl(mMainExecutor, source) : null); + + } + + @Override + public void onServiceDisconnected(ComponentName className) { + } + }; + + @Inject + public CommunalSourcePrimer(Context context, @Main Executor mainExecutor, + CommunalSourceMonitor monitor) { + super(context); + mContext = context; + mMainExecutor = mainExecutor; + mMonitor = monitor; + } + + @Override + public void start() { + if (DEBUG) { + Log.d(TAG, "start"); + } + } + + @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); + } + + if (serviceComponent == null || serviceComponent.isEmpty()) { + return; + } + + final Intent intent = new Intent(); + intent.setAction(ACTION_COMMUNAL_SOURCE); + intent.setComponent(ComponentName.unflattenFromString(serviceComponent)); + + mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREATE); + } +}