diff --git a/app/src/main/java/org/kiwix/kiwixmobile/local_file_transfer/KiwixWifiP2pBroadcastReceiver.java b/app/src/main/java/org/kiwix/kiwixmobile/local_file_transfer/KiwixWifiP2pBroadcastReceiver.java deleted file mode 100644 index de19db033..000000000 --- a/app/src/main/java/org/kiwix/kiwixmobile/local_file_transfer/KiwixWifiP2pBroadcastReceiver.java +++ /dev/null @@ -1,86 +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.local_file_transfer; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.net.NetworkInfo; -import android.net.wifi.p2p.WifiP2pDevice; -import android.net.wifi.p2p.WifiP2pManager; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -/** - * Helper class for the local file sharing module. - * - * Handles the broadcasts pertaining to the wifi p2p group formed in WiFi Direct. Works along with - * the wifi p2p manager in {@link WifiDirectManager}. - */ -public class KiwixWifiP2pBroadcastReceiver extends BroadcastReceiver { - - private P2pEventListener p2pEventListener; - - public KiwixWifiP2pBroadcastReceiver(@NonNull P2pEventListener p2pEventListener) { - this.p2pEventListener = p2pEventListener; - } - - @Override - public void onReceive(@NonNull Context context, @NonNull Intent intent) { - - switch (intent.getAction()) { - case WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION: { - int wifiP2pState = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1); - p2pEventListener.onWifiP2pStateChanged( - (wifiP2pState == WifiP2pManager.WIFI_P2P_STATE_ENABLED)); - break; - } - - case WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION: { - p2pEventListener.onPeersChanged(); - break; - } - - case WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION: { - NetworkInfo networkInfo = intent.getParcelableExtra(WifiP2pManager.EXTRA_NETWORK_INFO); - p2pEventListener.onConnectionChanged(networkInfo.isConnected()); - break; - } - - case WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION: { - WifiP2pDevice userDevice = intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE); - p2pEventListener.onDeviceChanged(userDevice); - break; - } - - default: - break; - } - } - - public interface P2pEventListener { - void onWifiP2pStateChanged(boolean isEnabled); - - void onPeersChanged(); - - void onConnectionChanged(boolean isConnected); - - void onDeviceChanged(@Nullable WifiP2pDevice userDevice); - } -} diff --git a/app/src/main/java/org/kiwix/kiwixmobile/local_file_transfer/KiwixWifiP2pBroadcastReceiver.kt b/app/src/main/java/org/kiwix/kiwixmobile/local_file_transfer/KiwixWifiP2pBroadcastReceiver.kt new file mode 100644 index 000000000..a45ae0647 --- /dev/null +++ b/app/src/main/java/org/kiwix/kiwixmobile/local_file_transfer/KiwixWifiP2pBroadcastReceiver.kt @@ -0,0 +1,72 @@ +/* + * 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 . + * + */ + +@file:Suppress("PackageNaming") + +package org.kiwix.kiwixmobile.local_file_transfer + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.net.NetworkInfo +import android.net.wifi.p2p.WifiP2pDevice +import android.net.wifi.p2p.WifiP2pManager.EXTRA_NETWORK_INFO +import android.net.wifi.p2p.WifiP2pManager.EXTRA_WIFI_P2P_DEVICE +import android.net.wifi.p2p.WifiP2pManager.EXTRA_WIFI_STATE +import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION +import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION +import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION +import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_STATE_ENABLED +import android.net.wifi.p2p.WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION + +/** + * Helper class for the local file sharing module. + * + * Handles the broadcasts pertaining to the wifi p2p group formed in WiFi Direct. Works along with + * the wifi p2p manager in [WifiDirectManager]. + */ + +class KiwixWifiP2pBroadcastReceiver(private val p2pEventListener: P2pEventListener) : + BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + when (intent.action) { + WIFI_P2P_STATE_CHANGED_ACTION -> { + val wifiP2pState = intent.getIntExtra(EXTRA_WIFI_STATE, -1) + p2pEventListener.onWifiP2pStateChanged(wifiP2pState == WIFI_P2P_STATE_ENABLED) + } + WIFI_P2P_PEERS_CHANGED_ACTION -> p2pEventListener.onPeersChanged() + WIFI_P2P_CONNECTION_CHANGED_ACTION -> { + val networkInfo = + intent.getParcelableExtra(EXTRA_NETWORK_INFO) + p2pEventListener.onConnectionChanged(networkInfo.isConnected) + } + WIFI_P2P_THIS_DEVICE_CHANGED_ACTION -> { + val userDevice = + intent.getParcelableExtra(EXTRA_WIFI_P2P_DEVICE) + p2pEventListener.onDeviceChanged(userDevice) + } + } + } + + interface P2pEventListener { + fun onWifiP2pStateChanged(isEnabled: Boolean) + fun onPeersChanged() + fun onConnectionChanged(isConnected: Boolean) + fun onDeviceChanged(userDevice: WifiP2pDevice?) + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksActivity.kt index 5fdf8abc4..92c0d1708 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/bookmark/BookmarksActivity.kt @@ -83,10 +83,10 @@ class BookmarksActivity : OnItemClickListener, BaseActivity() { recycler_view.adapter = bookmarksAdapter recycler_view.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false) + bookmarks_switch.isChecked = sharedPreferenceUtil.showBookmarksAllBooks bookmarks_switch.setOnCheckedChangeListener { _, isChecked -> bookmarkViewModel.actions.offer(Action.UserClickedShowAllToggle(isChecked)) } - bookmarks_switch.isChecked = sharedPreferenceUtil.showBookmarksAllBooks compositeDisposable.add(bookmarkViewModel.effects.subscribe { it.invokeWith(this) }) } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryActivity.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryActivity.kt index d309b70b0..404e45937 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryActivity.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/page/history/HistoryActivity.kt @@ -88,10 +88,10 @@ class HistoryActivity : OnItemClickListener, BaseActivity() { recycler_view.adapter = historyAdapter compositeDisposable.add(historyViewModel.effects.subscribe { it.invokeWith(this) }) + history_switch.isChecked = sharedPreferenceUtil.showHistoryAllBooks history_switch.setOnCheckedChangeListener { _, isChecked -> historyViewModel.actions.offer(Action.UserClickedShowAllToggle(isChecked)) } - history_switch.isChecked = sharedPreferenceUtil.showHistoryAllBooks } override fun onCreateOptionsMenu(menu: Menu): Boolean {