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

Commit 5f0f252f authored by Chris Göllner's avatar Chris Göllner Committed by Android (Google) Code Review
Browse files

Merge "Shortcut Helper - Implement lifecycle of new version" into main

parents 8a86b0a7 f546072b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1110,7 +1110,7 @@
                android:resource="@xml/home_controls_dream_metadata" />
        </service>

        <activity android:name="com.android.systemui.keyboard.shortcut.ShortcutHelperActivity"
        <activity android:name="com.android.systemui.keyboard.shortcut.ui.view.ShortcutHelperActivity"
            android:exported="false"
            android:theme="@style/ShortcutHelperTheme"
            android:excludeFromRecents="true"
+2 −1
Original line number Diff line number Diff line
@@ -19,12 +19,13 @@ package com.android.systemui.keyboard

import com.android.systemui.keyboard.data.repository.KeyboardRepository
import com.android.systemui.keyboard.data.repository.KeyboardRepositoryImpl
import com.android.systemui.keyboard.shortcut.ShortcutHelperModule
import com.android.systemui.keyboard.stickykeys.data.repository.StickyKeysRepository
import com.android.systemui.keyboard.stickykeys.data.repository.StickyKeysRepositoryImpl
import dagger.Binds
import dagger.Module

@Module
@Module(includes = [ShortcutHelperModule::class])
abstract class KeyboardModule {

    @Binds
+69 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.keyboard.shortcut

import android.app.Activity
import com.android.systemui.CoreStartable
import com.android.systemui.Flags.keyboardShortcutHelperRewrite
import com.android.systemui.keyboard.shortcut.data.repository.ShortcutHelperRepository
import com.android.systemui.keyboard.shortcut.ui.ShortcutHelperActivityStarter
import com.android.systemui.keyboard.shortcut.ui.view.ShortcutHelperActivity
import dagger.Binds
import dagger.Lazy
import dagger.Module
import dagger.Provides
import dagger.multibindings.ClassKey
import dagger.multibindings.IntoMap

@Module
interface ShortcutHelperModule {

    @Binds
    @IntoMap
    @ClassKey(ShortcutHelperActivity::class)
    fun activity(impl: ShortcutHelperActivity): Activity

    companion object {
        @Provides
        @IntoMap
        @ClassKey(ShortcutHelperActivityStarter::class)
        fun starter(implLazy: Lazy<ShortcutHelperActivityStarter>): CoreStartable {
            return if (keyboardShortcutHelperRewrite()) {
                implLazy.get()
            } else {
                // No-op implementation when the flag is disabled.
                NoOpStartable
            }
        }

        @Provides
        @IntoMap
        @ClassKey(ShortcutHelperRepository::class)
        fun repo(implLazy: Lazy<ShortcutHelperRepository>): CoreStartable {
            return if (keyboardShortcutHelperRewrite()) {
                implLazy.get()
            } else {
                // No-op implementation when the flag is disabled.
                NoOpStartable
            }
        }
    }
}

private object NoOpStartable : CoreStartable {
    override fun start() {}
}
+86 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.keyboard.shortcut.data.repository

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import com.android.systemui.CoreStartable
import com.android.systemui.broadcast.BroadcastDispatcher
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutHelperState
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutHelperState.Active
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutHelperState.Inactive
import com.android.systemui.statusbar.CommandQueue
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow

@SysUISingleton
class ShortcutHelperRepository
@Inject
constructor(
    private val commandQueue: CommandQueue,
    private val broadcastDispatcher: BroadcastDispatcher,
) : CoreStartable {

    val state = MutableStateFlow<ShortcutHelperState>(Inactive)

    override fun start() {
        registerBroadcastReceiver(
            action = Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS,
            onReceive = { state.value = Active() }
        )
        registerBroadcastReceiver(
            action = Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS,
            onReceive = { state.value = Inactive }
        )
        commandQueue.addCallback(
            object : CommandQueue.Callbacks {
                override fun dismissKeyboardShortcutsMenu() {
                    state.value = Inactive
                }

                override fun toggleKeyboardShortcutsMenu(deviceId: Int) {
                    state.value =
                        if (state.value is Inactive) {
                            Active(deviceId)
                        } else {
                            Inactive
                        }
                }
            }
        )
    }

    fun hide() {
        state.value = Inactive
    }

    private fun registerBroadcastReceiver(action: String, onReceive: () -> Unit) {
        broadcastDispatcher.registerReceiver(
            receiver =
                object : BroadcastReceiver() {
                    override fun onReceive(context: Context, intent: Intent) {
                        onReceive()
                    }
                },
            filter = IntentFilter(action),
            flags = Context.RECEIVER_EXPORTED or Context.RECEIVER_VISIBLE_TO_INSTANT_APPS
        )
    }
}
+35 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.keyboard.shortcut.domain.interactor

import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.keyboard.shortcut.data.repository.ShortcutHelperRepository
import com.android.systemui.keyboard.shortcut.shared.model.ShortcutHelperState
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow

@SysUISingleton
class ShortcutHelperInteractor
@Inject
constructor(private val repository: ShortcutHelperRepository) {

    val state: Flow<ShortcutHelperState> = repository.state

    fun onUserLeave() {
        repository.hide()
    }
}
Loading