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

Commit a0bfa947 authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

feat:3185,3186,3189: import listen device configuration changes from NotificationsReceiver project

parent d06083f0
Loading
Loading
Loading
Loading
+38 −0
Original line number Diff line number Diff line
/*
 *  Copyright (c) 2025 E FOUNDATION
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.notificationsreceiver

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import foundation.e.notificationsreceiver.domain.utils.AppBackgroundScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import javax.inject.Singleton

@InstallIn(SingletonComponent::class)
@Module
object NotificationsReceiverModule {

    @Singleton
    @AppBackgroundScope
    @Provides
    fun providesAppBackgroundScope(): CoroutineScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
}
+25 −0
Original line number Diff line number Diff line
/*
 *  Copyright (c) 2025 E FOUNDATION
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.notificationsreceiver.domain.bridges.androidinterfaces

interface DeviceConfigurationAndroidInterface {
    val version: String?
    val device: String
    val androidVersion: String
    suspend fun getLang(): String
}
+59 −0
Original line number Diff line number Diff line
/*
 *  Copyright (c) 2025 E FOUNDATION
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.notificationsreceiver.bridges.androidinterfaces

import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import dagger.hilt.android.qualifiers.ApplicationContext
import foundation.e.notificationsreceiver.domain.bridges.androidinterfaces.DeviceConfigurationAndroidInterface
import foundation.e.notificationsreceiver.domain.utils.w
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class DeviceConfigurationAndroidInterfaceImpl @Inject constructor(
    @ApplicationContext val appContext: Context,
) : DeviceConfigurationAndroidInterface {
    override val device: String = Build.DEVICE
    override val androidVersion: String = Build.VERSION.RELEASE

    override val version: String? by lazy { getVersionThroughReflection() }

    override suspend fun getLang(): String {
        return runCatching { appContext.resources.configuration.locales.get(0).language }.getOrNull() ?: "en"
    }

    private fun getVersionThroughReflection(): String? {
        return getSystemPropertyReflection("ro.lineage.version")
            ?: getSystemPropertyReflection("ro.version")
    }

    @Suppress("TooGenericExceptionCaught")
    @SuppressLint("PrivateApi")
    private fun getSystemPropertyReflection(propertyName: String): String? {
        return try {
            val c = Class.forName("android.os.SystemProperties")
            val get = c.getMethod("get", String::class.java)
            get.invoke(c, propertyName) as? String
        } catch (e: Exception) {
            w("Can't get SystemProperty $propertyName through reflexion", e)
            null
        }
    }
}
+46 −0
Original line number Diff line number Diff line
/*
 *  Copyright (c) 2025 E FOUNDATION
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.notificationsreceiver.bridges.broadcastreceivers

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import dagger.hilt.android.AndroidEntryPoint
import foundation.e.notificationsreceiver.bridges.utils.goAsync
import foundation.e.notificationsreceiver.domain.procedures.RegisterToEOSBroadcastProcedure
import foundation.e.notificationsreceiver.domain.utils.AppBackgroundScope
import kotlinx.coroutines.CoroutineScope
import javax.inject.Inject

@AndroidEntryPoint
class DeviceConfigurationChangedReceiver : BroadcastReceiver() {
    @Inject lateinit var registerToEOSBroadcastProcedure: RegisterToEOSBroadcastProcedure

    @AppBackgroundScope
    @Inject lateinit var backgroundScope: CoroutineScope

    override fun onReceive(context: Context?, intent: Intent?) {
        if (intent?.action == Intent.ACTION_BOOT_COMPLETED ||
            intent?.action == Intent.ACTION_LOCALE_CHANGED
        ) {
            goAsync(backgroundScope) {
                registerToEOSBroadcastProcedure.register()
            }
        }
    }
}