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

Commit 66ad1353 authored by Will Leshner's avatar Will Leshner
Browse files

Use Launcher's widget picker to add widgets to the hub.

Test: atest CommunalInteractorTest
Bug: 307306823
Flag: ACONFIG com.android.systemui.communal_hub DEVELOPMENT

Change-Id: I7f932d6466c317a97ec6ff9b4d0d5fed8a4d3469
parent a338097c
Loading
Loading
Loading
Loading
+3 −9
Original line number Original line Diff line number Diff line
@@ -256,6 +256,9 @@
    <!-- launcher apps -->
    <!-- launcher apps -->
    <uses-permission android:name="android.permission.ACCESS_SHORTCUTS" />
    <uses-permission android:name="android.permission.ACCESS_SHORTCUTS" />


    <!-- Permission to start Launcher's widget picker activity. -->
    <uses-permission android:name="android.permission.START_WIDGET_PICKER_ACTIVITY" />

    <uses-permission android:name="android.permission.MODIFY_THEME_OVERLAY" />
    <uses-permission android:name="android.permission.MODIFY_THEME_OVERLAY" />


    <!-- accessibility -->
    <!-- accessibility -->
@@ -974,15 +977,6 @@
            android:excludeFromRecents="true"
            android:excludeFromRecents="true"
            android:visibleToInstantApps="true"/>
            android:visibleToInstantApps="true"/>


        <activity android:name="com.android.systemui.communal.widgets.WidgetPickerActivity"
            android:theme="@style/Theme.EditWidgetsActivity"
            android:excludeFromRecents="true"
            android:autoRemoveFromRecents="true"
            android:showOnLockScreen="true"
            android:launchMode="singleTop"
            android:exported="false">
        </activity>

        <activity android:name="com.android.systemui.communal.widgets.EditWidgetsActivity"
        <activity android:name="com.android.systemui.communal.widgets.EditWidgetsActivity"
            android:theme="@style/Theme.EditWidgetsActivity"
            android:theme="@style/Theme.EditWidgetsActivity"
            android:excludeFromRecents="true"
            android:excludeFromRecents="true"
+0 −30
Original line number Original line Diff line number Diff line
<!--
  ~ Copyright (C) 2023 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.
  -->

<HorizontalScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/widgets_container"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="horizontal">
    </LinearLayout>

</HorizontalScrollView>
+31 −15
Original line number Original line Diff line number Diff line
@@ -16,8 +16,9 @@


package com.android.systemui.communal.widgets
package com.android.systemui.communal.widgets


import android.appwidget.AppWidgetProviderInfo
import android.content.ComponentName
import android.content.Intent
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Bundle
import android.os.RemoteException
import android.os.RemoteException
import android.util.Log
import android.util.Log
@@ -39,10 +40,8 @@ constructor(
    private var windowManagerService: IWindowManager? = null,
    private var windowManagerService: IWindowManager? = null,
) : ComponentActivity() {
) : ComponentActivity() {
    companion object {
    companion object {
        /**
        private const val EXTRA_FILTER_STRATEGY = "filter_strategy"
         * Intent extra name for the {@link AppWidgetProviderInfo} of a widget to add to hub mode.
        private const val FILTER_STRATEGY_GLANCEABLE_HUB = 1
         */
        const val ADD_WIDGET_INFO = "add_widget_info"
        private const val TAG = "EditWidgetsActivity"
        private const val TAG = "EditWidgetsActivity"
    }
    }


@@ -51,13 +50,8 @@ constructor(
            when (result.resultCode) {
            when (result.resultCode) {
                RESULT_OK -> {
                RESULT_OK -> {
                    result.data
                    result.data
                        ?.let {
                        ?.getParcelableExtra(Intent.EXTRA_COMPONENT_NAME, ComponentName::class.java)
                            it.getParcelableExtra(
                        ?.let { communalInteractor.addWidget(it, 0) }
                                ADD_WIDGET_INFO,
                                AppWidgetProviderInfo::class.java
                            )
                        }
                        ?.let { communalInteractor.addWidget(it.provider, 0) }
                        ?: run { Log.w(TAG, "No AppWidgetProviderInfo found in result.") }
                        ?: run { Log.w(TAG, "No AppWidgetProviderInfo found in result.") }
                }
                }
                else ->
                else ->
@@ -71,13 +65,35 @@ constructor(
    override fun onCreate(savedInstanceState: Bundle?) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        super.onCreate(savedInstanceState)


        setShowWhenLocked(true)

        setCommunalEditWidgetActivityContent(
        setCommunalEditWidgetActivityContent(
            activity = this,
            activity = this,
            viewModel = communalViewModel,
            viewModel = communalViewModel,
            onOpenWidgetPicker = {
            onOpenWidgetPicker = {
                val localPackageManager: PackageManager = getPackageManager()
                val intent =
                    Intent(Intent.ACTION_MAIN).also { it.addCategory(Intent.CATEGORY_HOME) }
                localPackageManager
                    .resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY)
                    ?.activityInfo
                    ?.packageName
                    ?.let { packageName ->
                        try {
                            addWidgetActivityLauncher.launch(
                            addWidgetActivityLauncher.launch(
                    Intent(applicationContext, WidgetPickerActivity::class.java)
                                Intent(Intent.ACTION_PICK).also {
                                    it.setPackage(packageName)
                                    it.putExtra(
                                        EXTRA_FILTER_STRATEGY,
                                        FILTER_STRATEGY_GLANCEABLE_HUB
                                    )
                                    )
                                }
                            )
                        } catch (e: Exception) {
                            Log.e(TAG, "Failed to launch widget picker activity", e)
                        }
                    }
                    ?: run { Log.e(TAG, "Couldn't resolve launcher package name") }
            },
            },
            onEditDone = {
            onEditDone = {
                try {
                try {
+0 −104
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2023 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.widgets

import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProviderInfo
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.activity.ComponentActivity
import androidx.core.view.setMargins
import androidx.core.view.setPadding
import com.android.systemui.res.R
import javax.inject.Inject

/**
 * An Activity responsible for displaying a list of widgets to add to the hub mode grid. This is
 * essentially a placeholder until Launcher's widget picker can be used.
 */
class WidgetPickerActivity
@Inject
constructor(
    private val appWidgetManager: AppWidgetManager,
) : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.widget_picker)
        loadWidgets()
    }

    private fun loadWidgets() {
        val containerView: ViewGroup? = findViewById(R.id.widgets_container)
        containerView?.apply {
            try {
                appWidgetManager
                    .getInstalledProviders(AppWidgetProviderInfo.WIDGET_CATEGORY_KEYGUARD)
                    ?.stream()
                    ?.forEach { widgetInfo ->
                        val activity = this@WidgetPickerActivity
                        (widgetInfo.loadPreviewImage(activity, 0)
                                ?: widgetInfo.loadIcon(activity, 0))
                            ?.let {
                                addView(
                                    ImageView(activity).also { v ->
                                        v.setImageDrawable(it)
                                        v.setBackgroundColor(WIDGET_PREVIEW_BACKGROUND_COLOR)
                                        v.setPadding(WIDGET_PREVIEW_PADDING)
                                        v.layoutParams =
                                            LinearLayout.LayoutParams(
                                                    WIDGET_PREVIEW_SIZE,
                                                    WIDGET_PREVIEW_SIZE
                                                )
                                                .also { lp ->
                                                    lp.setMargins(WIDGET_PREVIEW_MARGINS)
                                                }
                                        v.setOnClickListener {
                                            setResult(
                                                RESULT_OK,
                                                Intent()
                                                    .putExtra(
                                                        EditWidgetsActivity.ADD_WIDGET_INFO,
                                                        widgetInfo
                                                    )
                                            )
                                            finish()
                                        }
                                    }
                                )
                            }
                    }
            } catch (e: RuntimeException) {
                Log.e(TAG, "Exception fetching widget providers", e)
            }
        }
    }

    companion object {
        private const val WIDGET_PREVIEW_SIZE = 600
        private const val WIDGET_PREVIEW_MARGINS = 32
        private const val WIDGET_PREVIEW_PADDING = 32
        private val WIDGET_PREVIEW_BACKGROUND_COLOR = Color.rgb(216, 225, 220)
        private const val TAG = "WidgetPickerActivity"
    }
}
+0 −7
Original line number Original line Diff line number Diff line
@@ -20,7 +20,6 @@ import android.app.Activity;


import com.android.systemui.ForegroundServicesDialog;
import com.android.systemui.ForegroundServicesDialog;
import com.android.systemui.communal.widgets.EditWidgetsActivity;
import com.android.systemui.communal.widgets.EditWidgetsActivity;
import com.android.systemui.communal.widgets.WidgetPickerActivity;
import com.android.systemui.contrast.ContrastDialogActivity;
import com.android.systemui.contrast.ContrastDialogActivity;
import com.android.systemui.keyguard.WorkLockActivity;
import com.android.systemui.keyguard.WorkLockActivity;
import com.android.systemui.people.PeopleSpaceActivity;
import com.android.systemui.people.PeopleSpaceActivity;
@@ -158,12 +157,6 @@ public abstract class DefaultActivityBinder {
    @ClassKey(EditWidgetsActivity.class)
    @ClassKey(EditWidgetsActivity.class)
    public abstract Activity bindEditWidgetsActivity(EditWidgetsActivity activity);
    public abstract Activity bindEditWidgetsActivity(EditWidgetsActivity activity);


    /** Inject into WidgetPickerActivity. */
    @Binds
    @IntoMap
    @ClassKey(WidgetPickerActivity.class)
    public abstract Activity bindWidgetPickerActivity(WidgetPickerActivity activity);

    /** Inject into SwitchToManagedProfileForCallActivity. */
    /** Inject into SwitchToManagedProfileForCallActivity. */
    @Binds
    @Binds
    @IntoMap
    @IntoMap