#1466 null check nullable field and prevent leaks

This commit is contained in:
Sean Mac Gillicuddy 2019-09-13 12:58:39 +01:00
parent 4b118621d0
commit 12dd1a8e45
6 changed files with 41 additions and 18 deletions

View File

@ -0,0 +1,7 @@
package org.kiwix.kiwixmobile.di
import javax.inject.Scope
@Scope
@Retention
annotation class ActivityScope

View File

@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.di.components
import android.app.Activity
import dagger.BindsInstance
import dagger.Subcomponent
import org.kiwix.kiwixmobile.di.ActivityScope
import org.kiwix.kiwixmobile.di.modules.ActivityModule
import org.kiwix.kiwixmobile.downloader.DownloadFragment
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.ZimFileSelectFragment
@ -27,6 +28,7 @@ import org.kiwix.kiwixmobile.zim_manager.fileselect_view.effects.DeleteFiles
import org.kiwix.kiwixmobile.zim_manager.library_view.LibraryFragment
import org.kiwix.kiwixmobile.zim_manager.local_file_transfer.LocalFileTransferActivity
@ActivityScope
@Subcomponent(modules = [ActivityModule::class])
interface ActivityComponent {
fun inject(downloadFragment: DownloadFragment)

View File

@ -19,11 +19,13 @@ package org.kiwix.kiwixmobile.di.modules
import dagger.Binds
import dagger.Module
import org.kiwix.kiwixmobile.di.ActivityScope
import org.kiwix.kiwixmobile.utils.AlertDialogShower
import org.kiwix.kiwixmobile.utils.DialogShower
@Module
abstract class ActivityModule {
@Binds
@ActivityScope
abstract fun bindDialogShower(alertDialogShower: AlertDialogShower): DialogShower
}

View File

@ -187,6 +187,7 @@ public class ZimHostActivity extends BaseActivity implements
private void unbindService() {
if (hotspotService != null) {
unbindService(serviceConnection);
hotspotService.registerCallBack(null);
}
}

View File

@ -14,7 +14,7 @@ import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.utils.Constants;
import org.kiwix.kiwixmobile.webserver.ZimHostActivity;
import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.ACTION_STOP;
import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.ACTION_STOP_SERVER;
public class HotspotNotificationManager {
@ -51,7 +51,7 @@ public class HotspotNotificationManager {
hotspotNotificationChannel();
Intent stopIntent = new Intent(context, HotspotService.class).setAction(ACTION_STOP);
Intent stopIntent = new Intent(context, HotspotService.class).setAction(ACTION_STOP_SERVER);
PendingIntent stopHotspot =
PendingIntent.getService(context, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);

View File

@ -32,16 +32,13 @@ public class HotspotService extends Service implements HotspotStateListener, IpA
public static final String ACTION_STOP_SERVER = "stop_server";
public static final String ACTION_CHECK_IP_ADDRESS = "check_ip_address";
public static final String ACTION_STOP = "hotspot_stop";
private ZimHostCallbacks zimHostCallbacks;
private final IBinder serviceBinder = new HotspotBinder();
@Inject
WebServerHelper webServerHelper;
@Inject
WifiHotspotManager hotspotManager;
@Inject
HotspotNotificationManager hotspotNotificationManager;
@ -62,7 +59,9 @@ public class HotspotService extends Service implements HotspotStateListener, IpA
if (hotspotManager.isHotspotStarted()) {
stopHotspotAndDismissNotification();
} else {
zimHostCallbacks.requestLocationAccess();
if (zimHostCallbacks != null) {
zimHostCallbacks.requestLocationAccess();
}
}
}
break;
@ -76,14 +75,18 @@ public class HotspotService extends Service implements HotspotStateListener, IpA
case ACTION_START_SERVER:
if (webServerHelper.startServerHelper(
intent.getStringArrayListExtra(SELECTED_ZIM_PATHS_KEY))) {
zimHostCallbacks.onServerStarted(ServerUtils.getSocketAddress());
if (zimHostCallbacks != null) {
zimHostCallbacks.onServerStarted(ServerUtils.getSocketAddress());
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
startForegroundNotificationHelper();
}
Toast.makeText(this, R.string.server_started__successfully_toast_message,
Toast.LENGTH_SHORT).show();
} else {
zimHostCallbacks.onServerFailedToStart();
if (zimHostCallbacks != null) {
zimHostCallbacks.onServerFailedToStart();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true);
stopSelf();
@ -101,10 +104,6 @@ public class HotspotService extends Service implements HotspotStateListener, IpA
webServerHelper.pollForValidIpAddress();
break;
case ACTION_STOP:
stopHotspotAndDismissNotification();
break;
default:
break;
}
@ -121,7 +120,9 @@ public class HotspotService extends Service implements HotspotStateListener, IpA
hotspotManager.turnOffHotspot();
} else {
webServerHelper.stopAndroidWebServer();
zimHostCallbacks.onServerStopped();
if (zimHostCallbacks != null) {
zimHostCallbacks.onServerStopped();
}
stopForeground(true);
stopSelf();
hotspotNotificationManager.dismissNotification();
@ -139,27 +140,37 @@ public class HotspotService extends Service implements HotspotStateListener, IpA
@Override public void onHotspotTurnedOn(@NonNull WifiConfiguration wifiConfiguration) {
startForegroundNotificationHelper();
zimHostCallbacks.onHotspotTurnedOn(wifiConfiguration);
if (zimHostCallbacks != null) {
zimHostCallbacks.onHotspotTurnedOn(wifiConfiguration);
}
}
@Override public void onHotspotFailedToStart() {
zimHostCallbacks.onHotspotFailedToStart();
if (zimHostCallbacks != null) {
zimHostCallbacks.onHotspotFailedToStart();
}
}
@Override public void onHotspotStopped() {
webServerHelper.stopAndroidWebServer();
zimHostCallbacks.onServerStopped();
if (zimHostCallbacks != null) {
zimHostCallbacks.onServerStopped();
}
stopForeground(true);
stopSelf();
hotspotNotificationManager.dismissNotification();
}
@Override public void onIpAddressValid() {
zimHostCallbacks.onIpAddressValid();
if (zimHostCallbacks != null) {
zimHostCallbacks.onIpAddressValid();
}
}
@Override public void onIpAddressInvalid() {
zimHostCallbacks.onIpAddressInvalid();
if (zimHostCallbacks != null) {
zimHostCallbacks.onIpAddressInvalid();
}
}
public class HotspotBinder extends Binder {