From 0485691ab007346c9a95f6d497c9870c6bc894ed Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Wed, 12 Feb 2025 15:46:27 +0530 Subject: [PATCH 1/2] Reintroduced the "Hotspot Feature" for custom apps. --- app/detekt_baseline.xml | 5 -- .../kiwixmobile/webserver/ZimHostRobot.kt | 17 +++-- app/src/main/AndroidManifest.xml | 3 - .../di/components/KiwixActivityComponent.kt | 4 +- .../di/components/KiwixComponent.kt | 2 - .../di/components/ServiceComponent.kt | 38 ---------- .../di/modules/KiwixActivityModule.kt | 3 +- .../kiwixmobile/di/modules/KiwixModule.kt | 6 -- .../kiwixmobile/di/modules/ServiceModule.kt | 71 ------------------- .../kiwixmobile/main/KiwixMainActivity.kt | 17 +---- .../main/res/navigation/kiwix_nav_graph.xml | 2 +- core/detekt_baseline.xml | 5 ++ core/src/main/AndroidManifest.xml | 3 + .../di/components/CoreActivityComponent.kt | 5 +- .../core/di/components/CoreComponent.kt | 2 + .../di/components/CoreServiceComponent.kt | 2 + .../core/di/modules/ApplicationModule.kt | 6 ++ .../core/di/modules/CoreServiceModule.kt | 42 +++++++++++ .../kiwixmobile/core/main/CoreMainActivity.kt | 8 +++ .../core}/webserver/KiwixServer.kt | 2 +- .../core}/webserver/WebServerHelper.kt | 6 +- .../core}/webserver/ZimHostCallbacks.kt | 2 +- .../core}/webserver/ZimHostContract.kt | 2 +- .../core}/webserver/ZimHostFragment.kt | 27 ++++--- .../core}/webserver/ZimHostModule.kt | 2 +- .../core}/webserver/ZimHostPresenter.kt | 6 +- .../HotspotNotificationManager.kt | 8 +-- .../webserver/wifi_hotspot/HotspotService.kt | 20 +++--- .../wifi_hotspot/HotspotStateReceiver.kt | 4 +- .../wifi_hotspot/IpAddressCallbacks.kt | 2 +- .../webserver/wifi_hotspot/ServerStatus.kt | 2 +- .../src/main/res/layout/activity_zim_host.xml | 0 .../custom/di/CustomActivityComponent.kt | 3 +- .../custom/main/CustomMainActivity.kt | 4 +- .../main/res/navigation/custom_nav_graph.xml | 4 ++ 35 files changed, 140 insertions(+), 195 deletions(-) delete mode 100644 app/src/main/java/org/kiwix/kiwixmobile/di/components/ServiceComponent.kt delete mode 100644 app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt rename {app/src/main/java/org/kiwix/kiwixmobile => core/src/main/java/org/kiwix/kiwixmobile/core}/webserver/KiwixServer.kt (98%) rename {app/src/main/java/org/kiwix/kiwixmobile => core/src/main/java/org/kiwix/kiwixmobile/core}/webserver/WebServerHelper.kt (95%) rename {app/src/main/java/org/kiwix/kiwixmobile => core/src/main/java/org/kiwix/kiwixmobile/core}/webserver/ZimHostCallbacks.kt (95%) rename {app/src/main/java/org/kiwix/kiwixmobile => core/src/main/java/org/kiwix/kiwixmobile/core}/webserver/ZimHostContract.kt (96%) rename {app/src/main/java/org/kiwix/kiwixmobile => core/src/main/java/org/kiwix/kiwixmobile/core}/webserver/ZimHostFragment.kt (94%) rename {app/src/main/java/org/kiwix/kiwixmobile => core/src/main/java/org/kiwix/kiwixmobile/core}/webserver/ZimHostModule.kt (95%) rename {app/src/main/java/org/kiwix/kiwixmobile => core/src/main/java/org/kiwix/kiwixmobile/core}/webserver/ZimHostPresenter.kt (92%) rename {app/src/main/java/org/kiwix/kiwixmobile => core/src/main/java/org/kiwix/kiwixmobile/core}/webserver/wifi_hotspot/HotspotNotificationManager.kt (94%) rename {app/src/main/java/org/kiwix/kiwixmobile => core/src/main/java/org/kiwix/kiwixmobile/core}/webserver/wifi_hotspot/HotspotService.kt (90%) rename {app/src/main/java/org/kiwix/kiwixmobile => core/src/main/java/org/kiwix/kiwixmobile/core}/webserver/wifi_hotspot/HotspotStateReceiver.kt (92%) rename {app/src/main/java/org/kiwix/kiwixmobile => core/src/main/java/org/kiwix/kiwixmobile/core}/webserver/wifi_hotspot/IpAddressCallbacks.kt (93%) rename {app/src/main/java/org/kiwix/kiwixmobile => core/src/main/java/org/kiwix/kiwixmobile/core}/webserver/wifi_hotspot/ServerStatus.kt (93%) rename {app => core}/src/main/res/layout/activity_zim_host.xml (100%) diff --git a/app/detekt_baseline.xml b/app/detekt_baseline.xml index afafc1a17c..2985f0f99e 100644 --- a/app/detekt_baseline.xml +++ b/app/detekt_baseline.xml @@ -43,11 +43,6 @@ PackageNaming:SimplePageChangeListener.kt$package org.kiwix.kiwixmobile.zimManager PackageNaming:StartMultiSelection.kt$package org.kiwix.kiwixmobile.zimManager.fileselectView.effects - PackageNaming:HotspotNotificationManager.kt$package org.kiwix.kiwixmobile.webserver.wifi_hotspot - PackageNaming:HotspotStateReceiver.kt$package org.kiwix.kiwixmobile.webserver.wifi_hotspot - PackageNaming:IpAddressCallbacks.kt$package org.kiwix.kiwixmobile.webserver.wifi_hotspot - PackageNaming:HotspotService.kt$package org.kiwix.kiwixmobile.webserver.wifi_hotspot - PackageNaming:ServerStatus.kt$package org.kiwix.kiwixmobile.webserver.wifi_hotspot PackageNaming:ZimManageViewModel.kt$package org.kiwix.kiwixmobile.zimManager ReturnCount:Fat32Checker.kt$Fat32Checker$private fun canCreate4GbFile(storage: String): Boolean TooGenericExceptionCaught:FileWritingFileSystemChecker.kt$FileWritingFileSystemChecker$e: Exception diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/webserver/ZimHostRobot.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/webserver/ZimHostRobot.kt index 57e1653608..38078b0efd 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/webserver/ZimHostRobot.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/webserver/ZimHostRobot.kt @@ -35,7 +35,6 @@ import org.kiwix.kiwixmobile.Findable.StringId.TextId import org.kiwix.kiwixmobile.Findable.Text import org.kiwix.kiwixmobile.Findable.ViewId import org.kiwix.kiwixmobile.core.R -import org.kiwix.kiwixmobile.R.id import org.kiwix.kiwixmobile.core.utils.files.Log import org.kiwix.kiwixmobile.testutils.TestUtils import org.kiwix.kiwixmobile.testutils.TestUtils.testFlakyView @@ -75,7 +74,7 @@ class ZimHostRobot : BaseRobot() { fun startServer() { // stop the server if it is already running. stopServerIfAlreadyStarted() - clickOn(ViewId(id.startServerButton)) + clickOn(ViewId(R.id.startServerButton)) assetWifiDialogDisplayed() testFlakyView({ onView(withText("PROCEED")).perform(click()) }) } @@ -116,7 +115,7 @@ class ZimHostRobot : BaseRobot() { } catch (assertionFailedError: AssertionFailedError) { try { val recyclerViewItemsCount = - RecyclerViewItemCount(id.recyclerViewZimHost).checkRecyclerViewCount() + RecyclerViewItemCount(R.id.recyclerViewZimHost).checkRecyclerViewCount() (0 until recyclerViewItemsCount) .asSequence() .filter { it != 0 } @@ -130,14 +129,14 @@ class ZimHostRobot : BaseRobot() { private fun selectZimFile(position: Int) { try { onView( - RecyclerViewMatcher(id.recyclerViewZimHost).atPositionOnView( + RecyclerViewMatcher(R.id.recyclerViewZimHost).atPositionOnView( position, R.id.itemBookCheckbox ) ).check(matches(ViewMatchers.isChecked())) } catch (assertionError: AssertionFailedError) { onView( - RecyclerViewMatcher(id.recyclerViewZimHost).atPositionOnView( + RecyclerViewMatcher(R.id.recyclerViewZimHost).atPositionOnView( position, R.id.itemBookCheckbox ) @@ -148,14 +147,14 @@ class ZimHostRobot : BaseRobot() { fun assertItemHostedOnServer(itemCount: Int) { val checkedCheckboxCount = RecyclerViewSelectedCheckBoxCountAssertion( - id.recyclerViewZimHost, + R.id.recyclerViewZimHost, R.id.itemBookCheckbox ).countCheckedCheckboxes() assertThat(checkedCheckboxCount, CoreMatchers.`is`(itemCount)) } fun stopServer() { - testFlakyView({ onView(withId(id.startServerButton)).perform(click()) }) + testFlakyView({ onView(withId(R.id.startServerButton)).perform(click()) }) } fun assertServerStopped() { @@ -164,11 +163,11 @@ class ZimHostRobot : BaseRobot() { } fun assertQrShown() { - isVisible(ViewId(id.serverQrCode)) + isVisible(ViewId(R.id.serverQrCode)) } fun assertQrNotShown() { - isNotVisible(ViewId(id.serverQrCode)) + isNotVisible(ViewId(R.id.serverQrCode)) } private fun pauseForBetterTestPerformance() { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 07fe617eaa..da3b3ca46a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -152,8 +152,5 @@ - diff --git a/app/src/main/java/org/kiwix/kiwixmobile/di/components/KiwixActivityComponent.kt b/app/src/main/java/org/kiwix/kiwixmobile/di/components/KiwixActivityComponent.kt index 91046d55d4..b0949c5852 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/di/components/KiwixActivityComponent.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/di/components/KiwixActivityComponent.kt @@ -22,8 +22,7 @@ import dagger.BindsInstance import dagger.Subcomponent import org.kiwix.kiwixmobile.core.di.ActivityScope import org.kiwix.kiwixmobile.core.di.components.CoreActivityComponent -import org.kiwix.kiwixmobile.webserver.ZimHostModule -import org.kiwix.kiwixmobile.webserver.ZimHostFragment +import org.kiwix.kiwixmobile.core.webserver.ZimHostModule import org.kiwix.kiwixmobile.di.modules.KiwixActivityModule import org.kiwix.kiwixmobile.intro.IntroFragment import org.kiwix.kiwixmobile.intro.IntroModule @@ -50,7 +49,6 @@ interface KiwixActivityComponent : CoreActivityComponent { fun inject(deleteFiles: DeleteFiles) fun inject(localFileTransferFragment: LocalFileTransferFragment) fun inject(languageFragment: LanguageFragment) - fun inject(zimHostFragment: ZimHostFragment) fun inject(kiwixSettingsFragment: KiwixSettingsFragment) fun inject(introActivity: IntroFragment) fun inject(kiwixMainActivity: KiwixMainActivity) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/di/components/KiwixComponent.kt b/app/src/main/java/org/kiwix/kiwixmobile/di/components/KiwixComponent.kt index 3b13af038e..240f09c8a4 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/di/components/KiwixComponent.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/di/components/KiwixComponent.kt @@ -21,7 +21,6 @@ package org.kiwix.kiwixmobile.di.components import dagger.Component import org.kiwix.kiwixmobile.core.di.components.CoreComponent import org.kiwix.kiwixmobile.di.KiwixScope -import org.kiwix.kiwixmobile.di.components.ServiceComponent.Builder import org.kiwix.kiwixmobile.di.modules.KiwixModule import org.kiwix.kiwixmobile.di.modules.KiwixViewModelModule @@ -32,5 +31,4 @@ import org.kiwix.kiwixmobile.di.modules.KiwixViewModelModule ) interface KiwixComponent { fun activityComponentBuilder(): KiwixActivityComponent.Builder - fun serviceComponent(): Builder } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/di/components/ServiceComponent.kt b/app/src/main/java/org/kiwix/kiwixmobile/di/components/ServiceComponent.kt deleted file mode 100644 index 8f587f3074..0000000000 --- a/app/src/main/java/org/kiwix/kiwixmobile/di/components/ServiceComponent.kt +++ /dev/null @@ -1,38 +0,0 @@ -/* -* Kiwix Android -* Copyright (c) 2019 Kiwix -* 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 . -* -*/ - -package org.kiwix.kiwixmobile.di.components - -import android.app.Service -import dagger.BindsInstance -import dagger.Subcomponent -import org.kiwix.kiwixmobile.di.ServiceScope -import org.kiwix.kiwixmobile.di.modules.ServiceModule -import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService - -@Subcomponent(modules = [ServiceModule::class]) -@ServiceScope -interface ServiceComponent { - fun inject(hotspotService: HotspotService) - - @Subcomponent.Builder - interface Builder { - @BindsInstance fun service(service: Service): Builder - fun build(): ServiceComponent - } -} diff --git a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/KiwixActivityModule.kt b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/KiwixActivityModule.kt index a881282d0b..5748e66884 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/KiwixActivityModule.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/KiwixActivityModule.kt @@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.di.modules import dagger.Module import org.kiwix.kiwixmobile.core.di.modules.ActivityModule +import org.kiwix.kiwixmobile.core.webserver.ZimHostModule -@Module(includes = [ActivityModule::class]) +@Module(includes = [ActivityModule::class, ZimHostModule::class]) class KiwixActivityModule diff --git a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/KiwixModule.kt b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/KiwixModule.kt index 7254aad47f..0efdf7c1b9 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/KiwixModule.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/KiwixModule.kt @@ -20,7 +20,6 @@ package org.kiwix.kiwixmobile.di.modules import android.content.Context import android.location.LocationManager -import android.net.wifi.WifiManager import android.net.wifi.p2p.WifiP2pManager import dagger.Module import dagger.Provides @@ -56,9 +55,4 @@ object KiwixModule { // See: https://github.com/kiwix/kiwix-android/issues/2488 fun providesWiFiP2pManager(context: Context): WifiP2pManager? = context.getSystemService(Context.WIFI_P2P_SERVICE) as WifiP2pManager? - - @Provides - @KiwixScope - fun provideWifiManager(context: Context): WifiManager = - context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt b/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt deleted file mode 100644 index a48aacfe19..0000000000 --- a/app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2024 Kiwix - * 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 . - * - */ - -package org.kiwix.kiwixmobile.di.modules - -import android.app.NotificationManager -import android.app.Service -import android.content.Context -import dagger.Module -import dagger.Provides -import org.kiwix.kiwixmobile.core.qr.GenerateQR -import org.kiwix.kiwixmobile.di.ServiceScope -import org.kiwix.kiwixmobile.webserver.KiwixServer -import org.kiwix.kiwixmobile.webserver.WebServerHelper -import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotNotificationManager -import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotStateReceiver -import org.kiwix.kiwixmobile.webserver.wifi_hotspot.IpAddressCallbacks - -@Module -class ServiceModule { - - @Provides - @ServiceScope - fun providesWebServerHelper( - kiwixServerFactory: KiwixServer.Factory, - ipAddressCallbacks: IpAddressCallbacks - ): WebServerHelper = WebServerHelper(kiwixServerFactory, ipAddressCallbacks) - - @Provides - @ServiceScope - fun providesIpAddressCallbacks(service: Service): IpAddressCallbacks = - service as IpAddressCallbacks - - @Provides - @ServiceScope - fun providesHotspotNotificationManager( - notificationManager: NotificationManager, - context: Context, - generateQR: GenerateQR, - ): HotspotNotificationManager = - HotspotNotificationManager(notificationManager, context, generateQR) - - @Provides - @ServiceScope - fun providesHotspotStateReceiver(callback: HotspotStateReceiver.Callback): HotspotStateReceiver = - HotspotStateReceiver(callback) - - @Provides - @ServiceScope - fun providesHotspotStateReceiverCallback(service: Service): HotspotStateReceiver.Callback = - service as HotspotStateReceiver.Callback - - @Provides - @ServiceScope - fun providesGenerateQr(): GenerateQR = GenerateQR() -} diff --git a/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt b/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt index e207c6f81c..3adf250025 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/main/KiwixMainActivity.kt @@ -23,7 +23,6 @@ import android.content.res.Configuration import android.os.Bundle import android.os.Handler import android.os.Looper -import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.view.ActionMode import androidx.core.content.pm.ShortcutInfoCompat @@ -46,7 +45,6 @@ import kotlinx.coroutines.launch import org.kiwix.kiwixmobile.BuildConfig import org.kiwix.kiwixmobile.R import org.kiwix.kiwixmobile.core.R.drawable -import org.kiwix.kiwixmobile.core.R.id import org.kiwix.kiwixmobile.core.R.mipmap import org.kiwix.kiwixmobile.core.R.string import org.kiwix.kiwixmobile.core.base.FragmentActivityExtensions @@ -107,6 +105,8 @@ class KiwixMainActivity : CoreMainActivity() { override val notesFragmentResId: Int = R.id.notesFragment override val readerFragmentResId: Int = R.id.readerFragment override val helpFragmentResId: Int = R.id.helpFragment + override val zimHostFragmentResId: Int = R.id.zimHostFragment + override val navGraphId: Int = R.navigation.kiwix_nav_graph override val topLevelDestinations = setOf(R.id.downloadsFragment, R.id.libraryFragment, R.id.readerFragment) @@ -310,19 +310,6 @@ class KiwixMainActivity : CoreMainActivity() { } } - override fun onNavigationItemSelected(item: MenuItem): Boolean { - when (item.itemId) { - id.menu_host_books -> openZimHostFragment() - else -> return super.onNavigationItemSelected(item) - } - return true - } - - private fun openZimHostFragment() { - disableDrawer() - navigate(R.id.zimHostFragment) - } - override fun getIconResId() = mipmap.ic_launcher override fun createApplicationShortcuts() { diff --git a/app/src/main/res/navigation/kiwix_nav_graph.xml b/app/src/main/res/navigation/kiwix_nav_graph.xml index 067bbf5e2a..92ab791f2d 100644 --- a/app/src/main/res/navigation/kiwix_nav_graph.xml +++ b/app/src/main/res/navigation/kiwix_nav_graph.xml @@ -125,7 +125,7 @@ tools:layout="@layout/activity_language" /> PackageNaming:MountPointProducer.kt$package org.kiwix.kiwixmobile.core.zim_manager PackageNaming:SelectionMode.kt$package org.kiwix.kiwixmobile.core.zim_manager.fileselect_view PackageNaming:TagsView.kt$package org.kiwix.kiwixmobile.core.zim_manager + PackageNaming:HotspotNotificationManager.kt$package org.kiwix.kiwixmobile.core.webserver.wifi_hotspot + PackageNaming:HotspotStateReceiver.kt$package org.kiwix.kiwixmobile.core.webserver.wifi_hotspot + PackageNaming:IpAddressCallbacks.kt$package org.kiwix.kiwixmobile.core.webserver.wifi_hotspot + PackageNaming:HotspotService.kt$package org.kiwix.kiwixmobile.core.webserver.wifi_hotspot + PackageNaming:ServerStatus.kt$package org.kiwix.kiwixmobile.core.webserver.wifi_hotspot PackageNaming:ConnectivityBroadcastReceiver.kt$package org.kiwix.kiwixmobile.core.zim_manager PackageNaming:NetworkState.kt$package org.kiwix.kiwixmobile.core.zim_manager ReturnCount:FileUtils.kt$FileUtils$@JvmStatic fun getAllZimParts(book: Book): List<File> diff --git a/core/src/main/AndroidManifest.xml b/core/src/main/AndroidManifest.xml index d8ddfacdf9..67235d9d6e 100644 --- a/core/src/main/AndroidManifest.xml +++ b/core/src/main/AndroidManifest.xml @@ -95,5 +95,8 @@ + diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreActivityComponent.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreActivityComponent.kt index 518e6613ab..7c20087af9 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreActivityComponent.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreActivityComponent.kt @@ -36,10 +36,12 @@ import org.kiwix.kiwixmobile.core.page.notes.viewmodel.effects.ShowOpenNoteDialo import org.kiwix.kiwixmobile.core.search.SearchFragment import org.kiwix.kiwixmobile.core.search.viewmodel.effects.ShowDeleteSearchDialog import org.kiwix.kiwixmobile.core.settings.CorePrefsFragment +import org.kiwix.kiwixmobile.core.webserver.ZimHostFragment +import org.kiwix.kiwixmobile.core.webserver.ZimHostModule @ActivityScope @Subcomponent( - modules = [ActivityModule::class] + modules = [ActivityModule::class, ZimHostModule::class], ) interface CoreActivityComponent { fun inject(searchFragment: SearchFragment) @@ -55,6 +57,7 @@ interface CoreActivityComponent { fun inject(helpFragment: HelpFragment) fun inject(notesFragment: NotesFragment) fun inject(navigationHistoryDialog: NavigationHistoryDialog) + fun inject(zimHostFragment: ZimHostFragment) @Subcomponent.Builder interface Builder { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt index 367f47be06..bec314d563 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreComponent.kt @@ -21,6 +21,7 @@ import android.app.Application import android.app.NotificationManager import android.content.Context import android.net.ConnectivityManager +import android.net.wifi.WifiManager import dagger.BindsInstance import dagger.Component import eu.mhutti1.utils.storage.StorageSelectDialog @@ -102,6 +103,7 @@ interface CoreComponent { fun downloadRoomDao(): DownloadRoomDao fun newBookmarksDao(): NewBookmarksDao fun connectivityManager(): ConnectivityManager + fun wifiManager(): WifiManager fun objectBoxToLibkiwixMigrator(): ObjectBoxToLibkiwixMigrator fun libkiwixBookmarks(): LibkiwixBookmarks fun recentSearchRoomDao(): RecentSearchRoomDao diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreServiceComponent.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreServiceComponent.kt index 27e559a844..6199a6ea15 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreServiceComponent.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/components/CoreServiceComponent.kt @@ -25,12 +25,14 @@ import org.kiwix.kiwixmobile.core.di.CoreServiceScope import org.kiwix.kiwixmobile.core.di.modules.CoreServiceModule import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadMonitorService import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudService +import org.kiwix.kiwixmobile.core.webserver.wifi_hotspot.HotspotService @Subcomponent(modules = [CoreServiceModule::class]) @CoreServiceScope interface CoreServiceComponent { fun inject(readAloudService: ReadAloudService) fun inject(downloadMonitorService: DownloadMonitorService) + fun inject(hotspotService: HotspotService) @Subcomponent.Builder interface Builder { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/ApplicationModule.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/ApplicationModule.kt index 0e9764bc36..e9d77cdcde 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/ApplicationModule.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/ApplicationModule.kt @@ -21,6 +21,7 @@ import android.app.Application import android.app.NotificationManager import android.content.Context import android.net.ConnectivityManager +import android.net.wifi.WifiManager import android.os.storage.StorageManager import dagger.Module import dagger.Provides @@ -101,4 +102,9 @@ class ApplicationModule { @Singleton fun provideConnectivityManager(context: Context): ConnectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + + @Provides + @Singleton + fun provideWifiManager(context: Context): WifiManager = + context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/CoreServiceModule.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/CoreServiceModule.kt index 037adf8b4b..dc2b6b6e4e 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/CoreServiceModule.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/di/modules/CoreServiceModule.kt @@ -19,11 +19,18 @@ package org.kiwix.kiwixmobile.core.di.modules import android.app.NotificationManager +import android.app.Service import android.content.Context import dagger.Module import dagger.Provides import org.kiwix.kiwixmobile.core.di.CoreServiceScope +import org.kiwix.kiwixmobile.core.qr.GenerateQR import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudNotificationManger +import org.kiwix.kiwixmobile.core.webserver.KiwixServer +import org.kiwix.kiwixmobile.core.webserver.WebServerHelper +import org.kiwix.kiwixmobile.core.webserver.wifi_hotspot.HotspotNotificationManager +import org.kiwix.kiwixmobile.core.webserver.wifi_hotspot.HotspotStateReceiver +import org.kiwix.kiwixmobile.core.webserver.wifi_hotspot.IpAddressCallbacks @Module class CoreServiceModule { @@ -33,4 +40,39 @@ class CoreServiceModule { notificationManager: NotificationManager, context: Context ): ReadAloudNotificationManger = ReadAloudNotificationManger(notificationManager, context) + + @Provides + @CoreServiceScope + fun providesWebServerHelper( + kiwixServerFactory: KiwixServer.Factory, + ipAddressCallbacks: IpAddressCallbacks + ): WebServerHelper = WebServerHelper(kiwixServerFactory, ipAddressCallbacks) + + @Provides + @CoreServiceScope + fun providesIpAddressCallbacks(service: Service): IpAddressCallbacks = + service as IpAddressCallbacks + + @Provides + @CoreServiceScope + fun providesHotspotNotificationManager( + notificationManager: NotificationManager, + context: Context, + generateQR: GenerateQR, + ): HotspotNotificationManager = + HotspotNotificationManager(notificationManager, context, generateQR) + + @Provides + @CoreServiceScope + fun providesHotspotStateReceiver(callback: HotspotStateReceiver.Callback): HotspotStateReceiver = + HotspotStateReceiver(callback) + + @Provides + @CoreServiceScope + fun providesHotspotStateReceiverCallback(service: Service): HotspotStateReceiver.Callback = + service as HotspotStateReceiver.Callback + + @Provides + @CoreServiceScope + fun providesGenerateQr(): GenerateQR = GenerateQR() } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt index 1d2ba9acc1..0bf531b4c6 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/CoreMainActivity.kt @@ -97,6 +97,8 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider { abstract val historyFragmentResId: Int abstract val notesFragmentResId: Int abstract val helpFragmentResId: Int + abstract val zimHostFragmentResId: Int + abstract val navGraphId: Int abstract val cachedComponent: CoreActivityComponent abstract val topLevelDestinations: Set abstract val navHostContainer: FragmentContainerView @@ -307,11 +309,17 @@ abstract class CoreMainActivity : BaseActivity(), WebViewProvider { R.id.menu_notes -> openNotes() R.id.menu_history -> openHistory() R.id.menu_bookmarks_list -> openBookmarks() + R.id.menu_host_books -> openZimHostFragment() else -> return false } return true } + private fun openZimHostFragment() { + navigate(zimHostFragmentResId) + handleDrawerOnNavigation() + } + private fun openHelpFragment() { navigate(helpFragmentResId) handleDrawerOnNavigation() diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/KiwixServer.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/KiwixServer.kt similarity index 98% rename from app/src/main/java/org/kiwix/kiwixmobile/webserver/KiwixServer.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/webserver/KiwixServer.kt index 94ebff4bdb..fd49f3cf92 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/KiwixServer.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/KiwixServer.kt @@ -16,7 +16,7 @@ * */ -package org.kiwix.kiwixmobile.webserver +package org.kiwix.kiwixmobile.core.webserver import android.content.Context import kotlinx.coroutines.Dispatchers diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/WebServerHelper.kt similarity index 95% rename from app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/webserver/WebServerHelper.kt index 862266d57f..f017274f36 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/WebServerHelper.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/WebServerHelper.kt @@ -15,7 +15,7 @@ * along with this program. If not, see . * */ -package org.kiwix.kiwixmobile.webserver +package org.kiwix.kiwixmobile.core.webserver import io.reactivex.Flowable import io.reactivex.android.schedulers.AndroidSchedulers @@ -27,8 +27,8 @@ import org.kiwix.kiwixmobile.core.utils.ServerUtils.INVALID_IP import org.kiwix.kiwixmobile.core.utils.ServerUtils.getIp import org.kiwix.kiwixmobile.core.utils.ServerUtils.getIpAddress import org.kiwix.kiwixmobile.core.utils.files.Log -import org.kiwix.kiwixmobile.webserver.wifi_hotspot.IpAddressCallbacks -import org.kiwix.kiwixmobile.webserver.wifi_hotspot.ServerStatus +import org.kiwix.kiwixmobile.core.webserver.wifi_hotspot.IpAddressCallbacks +import org.kiwix.kiwixmobile.core.webserver.wifi_hotspot.ServerStatus import java.util.concurrent.TimeUnit import javax.inject.Inject diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostCallbacks.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostCallbacks.kt similarity index 95% rename from app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostCallbacks.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostCallbacks.kt index f0c9f84254..0a257904ca 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostCallbacks.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostCallbacks.kt @@ -15,7 +15,7 @@ * along with this program. If not, see . * */ -package org.kiwix.kiwixmobile.webserver +package org.kiwix.kiwixmobile.core.webserver interface ZimHostCallbacks { fun onServerStarted(ip: String) diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostContract.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostContract.kt similarity index 96% rename from app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostContract.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostContract.kt index e298facf18..95db29e25b 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostContract.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostContract.kt @@ -15,7 +15,7 @@ * along with this program. If not, see . * */ -package org.kiwix.kiwixmobile.webserver +package org.kiwix.kiwixmobile.core.webserver import org.kiwix.kiwixmobile.core.base.BaseContract import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt similarity index 94% rename from app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostFragment.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt index dc99985615..164e9a61e9 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt @@ -16,7 +16,7 @@ * */ -package org.kiwix.kiwixmobile.webserver +package org.kiwix.kiwixmobile.core.webserver import android.Manifest import android.Manifest.permission.POST_NOTIFICATIONS @@ -42,7 +42,7 @@ import androidx.appcompat.widget.Toolbar import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import androidx.fragment.app.FragmentActivity -import org.kiwix.kiwixmobile.R.layout +import org.kiwix.kiwixmobile.core.CoreApp.Companion.coreComponent import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.base.BaseActivity import org.kiwix.kiwixmobile.core.base.BaseFragment @@ -67,12 +67,12 @@ import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BookOnDisk import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskAdapter import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk -import org.kiwix.kiwixmobile.databinding.ActivityZimHostBinding -import org.kiwix.kiwixmobile.main.KiwixMainActivity -import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService -import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService.Companion.ACTION_CHECK_IP_ADDRESS -import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService.Companion.ACTION_START_SERVER -import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService.Companion.ACTION_STOP_SERVER +import org.kiwix.kiwixmobile.core.databinding.ActivityZimHostBinding +import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.cachedComponent +import org.kiwix.kiwixmobile.core.webserver.wifi_hotspot.HotspotService +import org.kiwix.kiwixmobile.core.webserver.wifi_hotspot.HotspotService.Companion.ACTION_CHECK_IP_ADDRESS +import org.kiwix.kiwixmobile.core.webserver.wifi_hotspot.HotspotService.Companion.ACTION_START_SERVER +import org.kiwix.kiwixmobile.core.webserver.wifi_hotspot.HotspotService.Companion.ACTION_STOP_SERVER import javax.inject.Inject class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View { @@ -178,7 +178,16 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View { } override fun inject(baseActivity: BaseActivity) { - (baseActivity as KiwixMainActivity).cachedComponent.inject(this) + baseActivity.cachedComponent.inject(this) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + coreComponent + .activityComponentBuilder() + .activity(requireActivity()) + .build() + .inject(this) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostModule.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostModule.kt similarity index 95% rename from app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostModule.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostModule.kt index 7e5ab83922..8692c01066 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostModule.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostModule.kt @@ -15,7 +15,7 @@ * along with this program. If not, see . * */ -package org.kiwix.kiwixmobile.webserver +package org.kiwix.kiwixmobile.core.webserver import dagger.Binds import dagger.Module diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostPresenter.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostPresenter.kt similarity index 92% rename from app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostPresenter.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostPresenter.kt index cd8caf688d..1e65966bf8 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostPresenter.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostPresenter.kt @@ -15,7 +15,7 @@ * along with this program. If not, see . * */ -package org.kiwix.kiwixmobile.webserver +package org.kiwix.kiwixmobile.core.webserver import org.kiwix.kiwixmobile.core.utils.files.Log import io.reactivex.SingleObserver @@ -24,8 +24,8 @@ import org.kiwix.kiwixmobile.core.base.BasePresenter import org.kiwix.kiwixmobile.core.data.DataSource import org.kiwix.kiwixmobile.core.di.ActivityScope import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem -import org.kiwix.kiwixmobile.webserver.ZimHostContract.Presenter -import org.kiwix.kiwixmobile.webserver.ZimHostContract.View +import org.kiwix.kiwixmobile.core.webserver.ZimHostContract.Presenter +import org.kiwix.kiwixmobile.core.webserver.ZimHostContract.View import javax.inject.Inject @ActivityScope diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/HotspotNotificationManager.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/HotspotNotificationManager.kt similarity index 94% rename from app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/HotspotNotificationManager.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/HotspotNotificationManager.kt index 68ddd798c2..f26ff3554e 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/HotspotNotificationManager.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/HotspotNotificationManager.kt @@ -15,7 +15,7 @@ * along with this program. If not, see . * */ -package org.kiwix.kiwixmobile.webserver.wifi_hotspot +package org.kiwix.kiwixmobile.core.webserver.wifi_hotspot import android.annotation.SuppressLint import android.app.Notification @@ -29,8 +29,6 @@ import androidx.core.app.NotificationCompat import androidx.navigation.NavDeepLinkBuilder import org.kiwix.kiwixmobile.core.CoreApp import org.kiwix.kiwixmobile.core.R -import org.kiwix.kiwixmobile.R.id -import org.kiwix.kiwixmobile.R.navigation import org.kiwix.kiwixmobile.core.qr.GenerateQR import org.kiwix.kiwixmobile.core.utils.HOTSPOT_SERVICE_CHANNEL_ID import javax.inject.Inject @@ -62,8 +60,8 @@ class HotspotNotificationManager @Inject constructor( val contentIntent = NavDeepLinkBuilder(context).setComponentName( coreMainActivity.mainActivity::class.java ) - .setGraph(navigation.kiwix_nav_graph) - .setDestination(id.zimHostFragment) + .setGraph(coreMainActivity.navGraphId) + .setDestination(coreMainActivity.zimHostFragmentResId) .createPendingIntent() hotspotNotificationChannel() val stopIntent = Intent(context, HotspotService::class.java).setAction( diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/HotspotService.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/HotspotService.kt similarity index 90% rename from app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/HotspotService.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/HotspotService.kt index 49c34b8c77..8e16b1d468 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/HotspotService.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/HotspotService.kt @@ -15,7 +15,7 @@ * along with this program. If not, see . * */ -package org.kiwix.kiwixmobile.webserver.wifi_hotspot +package org.kiwix.kiwixmobile.core.webserver.wifi_hotspot import android.app.Service import android.content.Intent @@ -26,14 +26,14 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import org.kiwix.kiwixmobile.KiwixApp +import org.kiwix.kiwixmobile.core.CoreApp.Companion.coreComponent import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.extensions.registerReceiver import org.kiwix.kiwixmobile.core.utils.ServerUtils.getSocketAddress -import org.kiwix.kiwixmobile.webserver.WebServerHelper -import org.kiwix.kiwixmobile.webserver.ZimHostCallbacks -import org.kiwix.kiwixmobile.webserver.ZimHostFragment -import org.kiwix.kiwixmobile.webserver.ZimHostFragment.Companion.RESTART_SERVER +import org.kiwix.kiwixmobile.core.webserver.WebServerHelper +import org.kiwix.kiwixmobile.core.webserver.ZimHostCallbacks +import org.kiwix.kiwixmobile.core.webserver.ZimHostFragment +import org.kiwix.kiwixmobile.core.webserver.ZimHostFragment.Companion.RESTART_SERVER import java.lang.ref.WeakReference import javax.inject.Inject @@ -58,18 +58,18 @@ class HotspotService : private val serviceBinder: IBinder = HotspotBinder(this) override fun onCreate() { - (this.application as KiwixApp).kiwixComponent - .serviceComponent() + coreComponent + .coreServiceComponent() .service(this) .build() .inject(this) super.onCreate() - hotspotStateReceiver?.let(this::registerReceiver) + hotspotStateReceiver?.let(::registerReceiver) } override fun onDestroy() { webServerHelper?.dispose() - hotspotStateReceiver?.let(this@HotspotService::unregisterReceiver) + hotspotStateReceiver?.let(::unregisterReceiver) super.onDestroy() } diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/HotspotStateReceiver.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/HotspotStateReceiver.kt similarity index 92% rename from app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/HotspotStateReceiver.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/HotspotStateReceiver.kt index 2482dff388..476d70463d 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/HotspotStateReceiver.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/HotspotStateReceiver.kt @@ -15,12 +15,12 @@ * along with this program. If not, see . * */ -package org.kiwix.kiwixmobile.webserver.wifi_hotspot +package org.kiwix.kiwixmobile.core.webserver.wifi_hotspot import android.content.Context import android.content.Intent import org.kiwix.kiwixmobile.core.base.BaseBroadcastReceiver -import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotStateReceiver.HotspotState.DISABLED +import org.kiwix.kiwixmobile.core.webserver.wifi_hotspot.HotspotStateReceiver.HotspotState.DISABLED import javax.inject.Inject const val EXTRA_WIFI_AP_STATE = "wifi_state" diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/IpAddressCallbacks.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/IpAddressCallbacks.kt similarity index 93% rename from app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/IpAddressCallbacks.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/IpAddressCallbacks.kt index 9366182760..c96d9ccfde 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/IpAddressCallbacks.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/IpAddressCallbacks.kt @@ -15,7 +15,7 @@ * along with this program. If not, see . * */ -package org.kiwix.kiwixmobile.webserver.wifi_hotspot +package org.kiwix.kiwixmobile.core.webserver.wifi_hotspot interface IpAddressCallbacks { fun onIpAddressValid() diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/ServerStatus.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/ServerStatus.kt similarity index 93% rename from app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/ServerStatus.kt rename to core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/ServerStatus.kt index 194cb2f2a8..1b88aaf4a6 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/wifi_hotspot/ServerStatus.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/wifi_hotspot/ServerStatus.kt @@ -16,7 +16,7 @@ * */ -package org.kiwix.kiwixmobile.webserver.wifi_hotspot +package org.kiwix.kiwixmobile.core.webserver.wifi_hotspot data class ServerStatus( val isServerStarted: Boolean, diff --git a/app/src/main/res/layout/activity_zim_host.xml b/core/src/main/res/layout/activity_zim_host.xml similarity index 100% rename from app/src/main/res/layout/activity_zim_host.xml rename to core/src/main/res/layout/activity_zim_host.xml diff --git a/custom/src/main/java/org/kiwix/kiwixmobile/custom/di/CustomActivityComponent.kt b/custom/src/main/java/org/kiwix/kiwixmobile/custom/di/CustomActivityComponent.kt index 9456ac62d8..b5138a8282 100644 --- a/custom/src/main/java/org/kiwix/kiwixmobile/custom/di/CustomActivityComponent.kt +++ b/custom/src/main/java/org/kiwix/kiwixmobile/custom/di/CustomActivityComponent.kt @@ -22,6 +22,7 @@ import android.app.Activity import dagger.BindsInstance import dagger.Subcomponent import org.kiwix.kiwixmobile.core.di.ActivityScope +import org.kiwix.kiwixmobile.core.webserver.ZimHostModule import org.kiwix.kiwixmobile.core.di.components.CoreActivityComponent import org.kiwix.kiwixmobile.custom.download.CustomDownloadFragment import org.kiwix.kiwixmobile.custom.main.CustomMainActivity @@ -29,7 +30,7 @@ import org.kiwix.kiwixmobile.custom.main.CustomReaderFragment import org.kiwix.kiwixmobile.custom.settings.CustomSettingsFragment @ActivityScope -@Subcomponent(modules = [CustomActivityModule::class]) +@Subcomponent(modules = [CustomActivityModule::class, ZimHostModule::class]) interface CustomActivityComponent : CoreActivityComponent { fun inject(customMainActivity: CustomMainActivity) fun inject(customSettingsFragment: CustomSettingsFragment) diff --git a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt index 813bdd9977..875e8d9074 100644 --- a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt +++ b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomMainActivity.kt @@ -75,6 +75,8 @@ class CustomMainActivity : CoreMainActivity() { override val historyFragmentResId: Int = R.id.historyFragment override val notesFragmentResId: Int = R.id.notesFragment override val helpFragmentResId: Int = R.id.helpFragment + override val zimHostFragmentResId: Int = R.id.zimHostFragment + override val navGraphId: Int = R.navigation.custom_nav_graph override val cachedComponent by lazy { customActivityComponent } override val topLevelDestinations = setOf(R.id.customReaderFragment) @@ -114,7 +116,7 @@ class CustomMainActivity : CoreMainActivity() { * For more info see https://github.com/kiwix/kiwix-android/pull/3516, * https://github.com/kiwix/kiwix-android/issues/4026 */ - menu.findItem(org.kiwix.kiwixmobile.core.R.id.menu_host_books)?.isVisible = false + menu.findItem(org.kiwix.kiwixmobile.core.R.id.menu_host_books)?.isVisible = true /** * Hide the `HelpFragment` from custom apps. * We have not removed the relevant code for `HelpFragment` from custom apps. diff --git a/custom/src/main/res/navigation/custom_nav_graph.xml b/custom/src/main/res/navigation/custom_nav_graph.xml index 198bb8c4b7..6efe078080 100644 --- a/custom/src/main/res/navigation/custom_nav_graph.xml +++ b/custom/src/main/res/navigation/custom_nav_graph.xml @@ -55,6 +55,10 @@ android:id="@+id/bookmarksFragment" android:name="org.kiwix.kiwixmobile.core.page.bookmark.BookmarksFragment" android:label="BookmarksFragment" /> + Date: Wed, 12 Feb 2025 18:31:04 +0530 Subject: [PATCH 2/2] Refactored the code to not ask permission for custom apps while starting the hotspot since we do not required any permission for starting the server in custom apps. * Fixed: ZIM file was not showing in the Hotspot screen. * Removed the unnecessary code and creation of demo file for custom app. --- .../kiwix/kiwixmobile/core/dao/NewBookDao.kt | 7 ++- .../kiwixmobile/core/utils/files/FileUtils.kt | 8 +-- .../kiwixmobile/core/webserver/KiwixServer.kt | 50 ++++++++++++------- .../core/webserver/ZimHostFragment.kt | 6 ++- .../custom/main/CustomReaderFragment.kt | 26 ++++------ 5 files changed, 55 insertions(+), 42 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewBookDao.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewBookDao.kt index b05d691aee..b0e56eead2 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewBookDao.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/dao/NewBookDao.kt @@ -26,9 +26,11 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.rx3.rxSingle +import org.kiwix.kiwixmobile.core.CoreApp.Companion.instance import org.kiwix.kiwixmobile.core.dao.entities.BookOnDiskEntity import org.kiwix.kiwixmobile.core.dao.entities.BookOnDiskEntity_ import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book +import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.isCustomApp import org.kiwix.kiwixmobile.core.reader.ZimReaderSource import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk import javax.inject.Inject @@ -42,7 +44,6 @@ class NewBookDao @Inject constructor(private val box: Box) { .flatMapSingle { bookOnDiskEntity -> val file = bookOnDiskEntity.file val zimReaderSource = ZimReaderSource(file) - rxSingle { zimReaderSource.canOpenInLibkiwix() } .map { canOpen -> if (canOpen) { @@ -72,7 +73,8 @@ class NewBookDao @Inject constructor(private val box: Box) { } } .filter { (bookOnDiskEntity, exists) -> - exists && !isInTrashFolder(bookOnDiskEntity.zimReaderSource.toDatabase()) + (instance.getMainActivity().isCustomApp() || exists) && + !isInTrashFolder(bookOnDiskEntity.zimReaderSource.toDatabase()) } .map(Pair::first) .toList() @@ -152,6 +154,7 @@ class NewBookDao @Inject constructor(private val box: Box) { } private suspend fun removeBooksThatDoNotExist(books: MutableList) { + if (instance.getMainActivity().isCustomApp()) return delete(books.filterNot { it.zimReaderSource.exists() }) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt index d4f4373571..2b728d71bf 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/files/FileUtils.kt @@ -593,10 +593,6 @@ object FileUtils { } } - @JvmStatic - fun getDemoFilePathForCustomApp(context: Context) = - "${context.getExternalFilesDirs(null)[0]}/demo.zim" - @SuppressLint("Recycle") @JvmStatic fun getAssetFileDescriptorFromUri( @@ -633,6 +629,10 @@ object FileUtils { // via the given file path before passing it to libkiwix. // This precaution helps prevent runtime crashes. // For more details, refer to https://github.com/kiwix/kiwix-android/pull/3636. + Log.e( + "CAN_OPEN_IN_LIBKIWIX", + "isFileDescriptorCanOpenWithLibkiwix: ${FileInputStream("dev/fd/$fdNumber")}" + ) FileInputStream("dev/fd/$fdNumber") true } catch (ignore: Exception) { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/KiwixServer.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/KiwixServer.kt index fd49f3cf92..9c66925938 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/KiwixServer.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/KiwixServer.kt @@ -19,11 +19,15 @@ package org.kiwix.kiwixmobile.core.webserver import android.content.Context +import android.os.Looper +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import org.kiwix.kiwixmobile.core.utils.files.Log +import org.kiwix.kiwixmobile.core.CoreApp +import org.kiwix.kiwixmobile.core.extensions.ActivityExtensions.isCustomApp import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer -import org.kiwix.kiwixmobile.core.utils.files.FileUtils.getDemoFilePathForCustomApp +import org.kiwix.kiwixmobile.core.utils.files.Log import org.kiwix.libkiwix.Book import org.kiwix.libkiwix.Library import org.kiwix.libkiwix.Server @@ -44,29 +48,28 @@ class KiwixServer @Inject constructor( private val context: Context, private val zimReaderContainer: ZimReaderContainer ) { - @Suppress("NestedBlockDepth") + @Suppress("NestedBlockDepth", "MagicNumber") suspend fun createKiwixServer(selectedBooksPath: ArrayList): KiwixServer = withContext(Dispatchers.IO) { val kiwixLibrary = Library() selectedBooksPath.forEach { path -> try { val book = Book().apply { + Log.e( + TAG, + " FD is valid = ${zimReaderContainer.zimReaderSource?.exists()} \n" + + "FD can read via the dev/fd/fdNumber = " + + "${zimReaderContainer.zimReaderSource?.canOpenInLibkiwix()}" + ) // Determine whether to create an Archive from an asset or a file path - val archive = if (path == getDemoFilePathForCustomApp(context)) { - // For custom apps using a demo file, create an Archive with FileDescriptor - val assetFileDescriptor = - zimReaderContainer.zimReaderSource?.assetFileDescriptorList?.get(0) - val startOffset = assetFileDescriptor?.startOffset ?: 0L - val size = assetFileDescriptor?.length ?: 0L - Archive( - assetFileDescriptor?.parcelFileDescriptor?.fileDescriptor, - startOffset, - size - ) - } else { - // For regular files, create an Archive from the file path - Archive(path) - } + val archive = + if (path == "null" && (context as CoreApp).getMainActivity().isCustomApp()) { + // For custom apps using create an Archive with FileDescriptor + zimReaderContainer.zimReaderSource?.createArchive() + } else { + // For regular files, create an Archive from the file path + Archive(path) + } update(archive) } kiwixLibrary.addBook(book) @@ -79,6 +82,17 @@ class KiwixServer @Inject constructor( ) } } + android.os.Handler(Looper.getMainLooper()).postDelayed({ + CoroutineScope(Dispatchers.IO).launch { + Log.e( + TAG, + "After 5 second FD is valid = " + + "${zimReaderContainer.zimReaderSource?.exists()} \n" + + "FD can read via the dev/fd/fdNumber =" + + " ${zimReaderContainer.zimReaderSource?.canOpenInLibkiwix()}" + ) + } + }, 5000) return@withContext KiwixServer(kiwixLibrary, Server(kiwixLibrary)) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt index 164e9a61e9..6731146196 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/webserver/ZimHostFragment.kt @@ -230,8 +230,10 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View { } private fun handleStoragePermissionAndServer() { - // we does not require any permission for playStore variant. - if (sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove()) { + // we does not require any permission for playStore variant, and custom apps. + if (sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove() || + requireActivity().isCustomApp() + ) { startStopServer() return } diff --git a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomReaderFragment.kt b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomReaderFragment.kt index 9e3602498a..4b73b2855f 100644 --- a/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomReaderFragment.kt +++ b/custom/src/main/java/org/kiwix/kiwixmobile/custom/main/CustomReaderFragment.kt @@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.custom.main import android.app.Dialog import android.os.Bundle +import android.util.Log import android.view.Menu import android.view.MenuInflater import android.view.View @@ -36,7 +37,6 @@ import org.kiwix.kiwixmobile.core.R.dimen import org.kiwix.kiwixmobile.core.base.BaseActivity import org.kiwix.kiwixmobile.core.extensions.browserIntent import org.kiwix.kiwixmobile.core.extensions.getResizedDrawable -import org.kiwix.kiwixmobile.core.extensions.isFileExist import org.kiwix.kiwixmobile.core.main.CoreReaderFragment import org.kiwix.kiwixmobile.core.main.MainMenu import org.kiwix.kiwixmobile.core.main.RestoreOrigin @@ -44,12 +44,10 @@ import org.kiwix.kiwixmobile.core.page.history.adapter.WebViewHistoryItem import org.kiwix.kiwixmobile.core.reader.ZimReaderSource import org.kiwix.kiwixmobile.core.utils.LanguageUtils import org.kiwix.kiwixmobile.core.utils.dialog.DialogShower -import org.kiwix.kiwixmobile.core.utils.files.FileUtils.getDemoFilePathForCustomApp import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk import org.kiwix.kiwixmobile.custom.BuildConfig import org.kiwix.kiwixmobile.custom.R import org.kiwix.kiwixmobile.custom.customActivityComponent -import java.io.File import java.util.Locale import javax.inject.Inject @@ -220,19 +218,11 @@ class CustomReaderFragment : CoreReaderFragment() { true, shouldManageExternalLaunch ) - // Save book in the database to display it in `ZimHostFragment`. - zimReaderContainer?.zimFileReader?.let { zimFileReader -> - // Check if the file is not null. If the file is null, - // it means we have created zimFileReader with a fileDescriptor, - // so we create a demo file to save it in the database for display on the `ZimHostFragment`. - val file = it.file ?: createDemoFile() - val bookOnDisk = BookOnDisk(zimFileReader) - repositoryActions?.saveBook(bookOnDisk) - } if (shouldManageExternalLaunch) { // Open the previous loaded pages after ZIM file loads. manageExternalLaunchAndRestoringViewState() } + saveBookInDatabase() } is ValidationState.HasBothFiles -> { @@ -242,6 +232,7 @@ class CustomReaderFragment : CoreReaderFragment() { // Open the previous loaded pages after ZIM file loads. manageExternalLaunchAndRestoringViewState() } + saveBookInDatabase() } else -> {} @@ -256,16 +247,19 @@ class CustomReaderFragment : CoreReaderFragment() { ) } - private suspend fun createDemoFile() = - File(getDemoFilePathForCustomApp(requireActivity())).also { - if (!it.isFileExist()) it.createNewFile() + private fun saveBookInDatabase() { + // Save book in the database to display it in `ZimHostFragment`. + zimReaderContainer?.zimFileReader?.let { zimFileReader -> + Log.e("SELECTED_BOOKS", "saveBookInDatabase: Saving book to database") + repositoryActions?.saveBook(BookOnDisk(zimFileReader)) } + } @Suppress("DEPRECATION") override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { super.onCreateOptionsMenu(menu, inflater) menu.findItem(org.kiwix.kiwixmobile.core.R.id.menu_help)?.isVisible = false - menu.findItem(org.kiwix.kiwixmobile.core.R.id.menu_host_books)?.isVisible = false + menu.findItem(org.kiwix.kiwixmobile.core.R.id.menu_host_books)?.isVisible = true } private fun enforcedLanguage(): Boolean {