Convert HotspotService.java into Kotlin

This commit is contained in:
MohitMali 2022-10-17 19:23:14 +05:30 committed by Kelson
parent 19f2548214
commit 33ed3bbd7e
4 changed files with 156 additions and 160 deletions

View File

@ -28,6 +28,7 @@
<ID>PackageNaming:HotspotNotificationManager.kt$package org.kiwix.kiwixmobile.webserver.wifi_hotspot</ID>
<ID>PackageNaming:HotspotStateReceiver.kt$package org.kiwix.kiwixmobile.webserver.wifi_hotspot</ID>
<ID>PackageNaming:IpAddressCallbacks.kt$package org.kiwix.kiwixmobile.webserver.wifi_hotspot</ID>
<ID>PackageNaming:HotspotService.kt$package org.kiwix.kiwixmobile.webserver.wifi_hotspot</ID>
<ID>PackageNaming:LibraryAdapter.kt$package
org.kiwix.kiwixmobile.zimManager.libraryView.adapter</ID>
<ID>PackageNaming:LibraryDelegate.kt$package

View File

@ -60,9 +60,9 @@ import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDis
import org.kiwix.kiwixmobile.core.zim_manager.fileselect_view.adapter.BooksOnDiskListItem.BookOnDisk
import org.kiwix.kiwixmobile.main.KiwixMainActivity
import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService
import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService.ACTION_CHECK_IP_ADDRESS
import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService.ACTION_START_SERVER
import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService.ACTION_STOP_SERVER
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 javax.inject.Inject
class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
@ -135,7 +135,7 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
override fun onServiceConnected(className: ComponentName, service: IBinder) {
hotspotService = (service as HotspotService.HotspotBinder).service
hotspotService!!.registerCallBack(this@ZimHostFragment)
hotspotService?.registerCallBack(this@ZimHostFragment)
}
}
@ -417,7 +417,7 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
}
override fun onIpAddressValid() {
progressDialog!!.dismiss()
progressDialog?.dismiss()
requireActivity().startService(
createHotspotIntent(ACTION_START_SERVER).putStringArrayListExtra(
SELECTED_ZIM_PATHS_KEY, selectedBooksPath
@ -426,7 +426,7 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View {
}
override fun onIpAddressInvalid() {
progressDialog!!.dismiss()
progressDialog?.dismiss()
toast(R.string.server_failed_message, Toast.LENGTH_SHORT)
}

View File

@ -1,154 +0,0 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* 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 <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.webserver.wifi_hotspot;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import javax.inject.Inject;
import org.kiwix.kiwixmobile.KiwixApp;
import org.kiwix.kiwixmobile.core.R;
import org.kiwix.kiwixmobile.core.extensions.ContextExtensionsKt;
import org.kiwix.kiwixmobile.core.utils.ServerUtils;
import org.kiwix.kiwixmobile.webserver.WebServerHelper;
import org.kiwix.kiwixmobile.webserver.ZimHostCallbacks;
import static org.kiwix.kiwixmobile.webserver.ZimHostFragment.SELECTED_ZIM_PATHS_KEY;
import static org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotNotificationManager.HOTSPOT_NOTIFICATION_ID;
/**
* HotspotService is used to add a foreground service for the wifi hotspot.
* Created by Adeel Zafar on 07/01/2019.
*/
public class HotspotService extends Service
implements IpAddressCallbacks, HotspotStateReceiver.Callback {
public static final String ACTION_START_SERVER = "start_server";
public static final String ACTION_STOP_SERVER = "stop_server";
public static final String ACTION_CHECK_IP_ADDRESS = "check_ip_address";
private ZimHostCallbacks zimHostCallbacks;
private final IBinder serviceBinder = new HotspotBinder();
@Inject
WebServerHelper webServerHelper;
@Inject
HotspotNotificationManager hotspotNotificationManager;
@Inject
HotspotStateReceiver hotspotStateReceiver;
@Override public void onCreate() {
((KiwixApp) this.getApplicationContext()).getKiwixComponent()
.serviceComponent()
.service(this)
.build()
.inject(this);
super.onCreate();
ContextExtensionsKt.registerReceiver(this, hotspotStateReceiver);
}
@Override public void onDestroy() {
unregisterReceiver(hotspotStateReceiver);
super.onDestroy();
}
@Override public int onStartCommand(@NonNull Intent intent, int flags, int startId) {
switch (intent.getAction()) {
case ACTION_START_SERVER:
if (webServerHelper.startServerHelper(
intent.getStringArrayListExtra(SELECTED_ZIM_PATHS_KEY))) {
zimHostCallbacks.onServerStarted(ServerUtils.getSocketAddress());
startForegroundNotificationHelper();
Toast.makeText(this, R.string.server_started_successfully_toast_message,
Toast.LENGTH_SHORT).show();
} else {
zimHostCallbacks.onServerFailedToStart();
}
break;
case ACTION_STOP_SERVER:
Toast.makeText(this, R.string.server_stopped_successfully_toast_message,
Toast.LENGTH_SHORT).show();
stopHotspotAndDismissNotification();
break;
case ACTION_CHECK_IP_ADDRESS:
webServerHelper.pollForValidIpAddress();
break;
default:
break;
}
return START_NOT_STICKY;
}
@Nullable @Override public IBinder onBind(@Nullable Intent intent) {
return serviceBinder;
}
//Dismiss notification and turn off hotspot for devices>=O
private void stopHotspotAndDismissNotification() {
webServerHelper.stopAndroidWebServer();
if (zimHostCallbacks != null) {
zimHostCallbacks.onServerStopped();
}
stopForeground(true);
stopSelf();
hotspotNotificationManager.dismissNotification();
}
public void registerCallBack(@Nullable ZimHostCallbacks myCallback) {
zimHostCallbacks = myCallback;
}
private void startForegroundNotificationHelper() {
startForeground(HOTSPOT_NOTIFICATION_ID,
hotspotNotificationManager.buildForegroundNotification());
}
@Override public void onIpAddressValid() {
if (zimHostCallbacks != null) {
zimHostCallbacks.onIpAddressValid();
}
}
@Override public void onIpAddressInvalid() {
if (zimHostCallbacks != null) {
zimHostCallbacks.onIpAddressInvalid();
}
}
@Override public void onHotspotDisabled() {
stopHotspotAndDismissNotification();
}
public class HotspotBinder extends Binder {
@NonNull public HotspotService getService() {
return HotspotService.this;
}
}
}

View File

@ -0,0 +1,149 @@
/*
* Kiwix Android
* Copyright (c) 2019 Kiwix <android.kiwix.org>
* 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 <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.webserver.wifi_hotspot
import android.app.Service
import android.content.Intent
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.widget.Toast
import org.kiwix.kiwixmobile.KiwixApp
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 javax.inject.Inject
/**
* HotspotService is used to add a foreground service for the wifi hotspot.
* Created by Adeel Zafar on 07/01/2019.
*/
class HotspotService :
Service(),
IpAddressCallbacks,
HotspotStateReceiver.Callback {
@set:Inject
var webServerHelper: WebServerHelper? = null
@set:Inject
var hotspotNotificationManager: HotspotNotificationManager? = null
@set:Inject
var hotspotStateReceiver: HotspotStateReceiver? = null
private var zimHostCallbacks: ZimHostCallbacks? = null
private val serviceBinder: IBinder = HotspotBinder()
override fun onCreate() {
(this.application as KiwixApp).kiwixComponent
.serviceComponent()
.service(this)
.build()
.inject(this)
super.onCreate()
hotspotStateReceiver?.let(this::registerReceiver)
}
override fun onDestroy() {
hotspotStateReceiver?.let(this@HotspotService::unregisterReceiver)
super.onDestroy()
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
when (intent.action) {
ACTION_START_SERVER ->
intent.getStringArrayListExtra(ZimHostFragment.SELECTED_ZIM_PATHS_KEY)?.let {
if (webServerHelper?.startServerHelper(it) == true) {
zimHostCallbacks?.onServerStarted(getSocketAddress())
startForegroundNotificationHelper()
Toast.makeText(
this, R.string.server_started_successfully_toast_message,
Toast.LENGTH_SHORT
).show()
} else {
onServerFailedToStart()
}
} ?: kotlin.run(::onServerFailedToStart)
ACTION_STOP_SERVER -> {
Toast.makeText(
this, R.string.server_stopped_successfully_toast_message,
Toast.LENGTH_SHORT
).show()
stopHotspotAndDismissNotification()
}
ACTION_CHECK_IP_ADDRESS -> webServerHelper?.pollForValidIpAddress()
else -> {}
}
return START_NOT_STICKY
}
override fun onBind(intent: Intent?): IBinder = serviceBinder
// Dismiss notification and turn off hotspot for devices>=O
private fun stopHotspotAndDismissNotification() {
webServerHelper?.stopAndroidWebServer()
zimHostCallbacks?.onServerStopped()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
stopForeground(STOP_FOREGROUND_REMOVE)
}
stopSelf()
hotspotStateReceiver = null
hotspotNotificationManager?.dismissNotification()
}
private fun onServerFailedToStart() {
zimHostCallbacks?.onServerFailedToStart()
}
fun registerCallBack(myCallback: ZimHostCallbacks?) {
zimHostCallbacks = myCallback
}
private fun startForegroundNotificationHelper() {
startForeground(
HotspotNotificationManager.HOTSPOT_NOTIFICATION_ID,
hotspotNotificationManager?.buildForegroundNotification()
)
}
override fun onIpAddressValid() {
zimHostCallbacks?.onIpAddressValid()
}
override fun onIpAddressInvalid() {
zimHostCallbacks?.onIpAddressInvalid()
}
override fun onHotspotDisabled() {
stopHotspotAndDismissNotification()
}
inner class HotspotBinder : Binder() {
val service: HotspotService
get() = this@HotspotService
}
companion object {
const val ACTION_START_SERVER = "start_server"
const val ACTION_STOP_SERVER = "stop_server"
const val ACTION_CHECK_IP_ADDRESS = "check_ip_address"
}
}