Skip to content

Commit

Permalink
Merge pull request #1506 from kiwix/iadeelzafar/manual-hotspot
Browse files Browse the repository at this point in the history
Iadeelzafar/manual hotspot
  • Loading branch information
macgills authored Sep 23, 2019
2 parents 04d6bb7 + 32b349e commit f9e722f
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 402 deletions.
3 changes: 0 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<!-- Devices with version >= Oreo need location permission to start/stop the hotspot -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<!-- Device with versions >= Pie need this permission -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

Expand Down
36 changes: 14 additions & 22 deletions app/src/main/java/org/kiwix/kiwixmobile/di/modules/ServiceModule.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package org.kiwix.kiwixmobile.di.modules

import android.app.Application
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.net.wifi.WifiManager
import dagger.Module
import dagger.Provides
import org.kiwix.kiwixlib.JNIKiwixLibrary
import org.kiwix.kiwixlib.JNIKiwixServer
import org.kiwix.kiwixmobile.di.ServiceScope
import org.kiwix.kiwixmobile.webserver.WebServerHelper
import org.kiwix.kiwixmobile.wifi_hotspot.HotspotNotificationManager
import org.kiwix.kiwixmobile.wifi_hotspot.HotspotStateListener
import org.kiwix.kiwixmobile.wifi_hotspot.HotspotStateReceiver
import org.kiwix.kiwixmobile.wifi_hotspot.HotspotStateReceiver.Callback
import org.kiwix.kiwixmobile.wifi_hotspot.IpAddressCallbacks
import org.kiwix.kiwixmobile.wifi_hotspot.WifiHotspotManager

@Module
class ServiceModule {
Expand All @@ -27,19 +25,6 @@ class ServiceModule {
ipAddressCallbacks: IpAddressCallbacks
): WebServerHelper = WebServerHelper(jniKiwixLibrary, kiwixServer, ipAddressCallbacks)

@Provides
@ServiceScope
fun providesWifiHotspotManager(
wifiManager: WifiManager,
hotspotStateListener: HotspotStateListener
): WifiHotspotManager =
WifiHotspotManager(wifiManager, hotspotStateListener)

@Provides
@ServiceScope
fun providesHotspotStateListener(service: Service): HotspotStateListener =
service as HotspotStateListener

@Provides
@ServiceScope
fun providesIpAddressCallbacks(service: Service): IpAddressCallbacks =
Expand All @@ -54,16 +39,23 @@ class ServiceModule {
fun providesJNIKiwixServer(jniKiwixLibrary: JNIKiwixLibrary): JNIKiwixServer =
JNIKiwixServer(jniKiwixLibrary)

@Provides
@ServiceScope
fun providesWifiManager(context: Application): WifiManager =
context.getSystemService(Context.WIFI_SERVICE) as WifiManager

@Provides
@ServiceScope
fun providesHotspotNotificationManager(
notificationManager: NotificationManager,
context: Context
): HotspotNotificationManager =
HotspotNotificationManager(notificationManager, context)

@Provides
@ServiceScope
fun providesHotspotStateReceiver(
callback: Callback
): HotspotStateReceiver = HotspotStateReceiver(callback)

@Provides
@ServiceScope
fun providesHotspotStateReceiverCallback(
service: Service
): HotspotStateReceiver.Callback = service as Callback
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package org.kiwix.kiwixmobile.webserver;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.net.wifi.WifiConfiguration;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
Expand All @@ -18,8 +14,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import java.io.File;
Expand All @@ -40,13 +34,11 @@
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.adapter.BooksOnDiskListItem;

import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.ACTION_CHECK_IP_ADDRESS;
import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.ACTION_LOCATION_ACCESS_GRANTED;
import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.ACTION_START_SERVER;
import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.ACTION_STOP_SERVER;
import static org.kiwix.kiwixmobile.wifi_hotspot.HotspotService.ACTION_TOGGLE_HOTSPOT;

public class ZimHostActivity extends BaseActivity implements
ZimHostCallbacks, ZimHostContract.View, LocationCallbacks {
ZimHostCallbacks, ZimHostContract.View {

@BindView(R.id.startServerButton)
Button startServerButton;
Expand All @@ -61,11 +53,7 @@ public class ZimHostActivity extends BaseActivity implements
@Inject
AlertDialogShower alertDialogShower;

@Inject
LocationServicesHelper locationServicesHelper;

private static final String TAG = "ZimHostActivity";
private static final int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102;
private static final String IP_STATE_KEY = "ip_state_key";
public static final String SELECTED_ZIM_PATHS_KEY = "selected_zim_paths";

Expand Down Expand Up @@ -133,15 +121,11 @@ public void onServiceDisconnected(ComponentName arg0) {
}

private void startHotspotHelper() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
toggleHotspot();
} else {
if (ServerUtils.isServerStarted) {
startService(createHotspotIntent(ACTION_STOP_SERVER));
} else {
startHotspotManuallyDialog();
}
}
}

private ArrayList<String> getSelectedBooksPath() {
Expand Down Expand Up @@ -190,21 +174,6 @@ private void unbindService() {
}
}

private void toggleHotspot() {
//Check if location permissions are granted
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
//Toggle hotspot if location permissions are granted
startService(createHotspotIntent(
ACTION_TOGGLE_HOTSPOT));
} else {
//Ask location permission if not granted
ActivityCompat.requestPermissions(this,
new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
MY_PERMISSIONS_ACCESS_FINE_LOCATION);
}
}

@Override protected void onResume() {
super.onResume();
presenter.loadBooks();
Expand Down Expand Up @@ -233,23 +202,6 @@ private void layoutServerStopped() {
booksAdapter.notifyDataSetChanged();
}

@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == MY_PERMISSIONS_ACCESS_FINE_LOCATION) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
toggleHotspot();
}
}
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
locationServicesHelper.onActivityResult(requestCode, resultCode, (data));
}

@Override protected void onDestroy() {
super.onDestroy();
presenter.detachView();
Expand Down Expand Up @@ -302,18 +254,6 @@ private Intent createHotspotIntent(String action) {
Toast.makeText(this, R.string.server_failed_toast_message, Toast.LENGTH_LONG).show();
}

@Override public void onHotspotTurnedOn(@NonNull WifiConfiguration wifiConfiguration) {
alertDialogShower.show(new KiwixDialog.ShowHotspotDetails(wifiConfiguration),
(Function0<Unit>) () -> {
progressDialog =
ProgressDialog.show(this,
getString(R.string.progress_dialog_starting_server), "",
true);
startService(createHotspotIntent(ACTION_CHECK_IP_ADDRESS));
return Unit.INSTANCE;
});
}

private void launchTetheringSettingsScreen() {
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
Expand All @@ -324,19 +264,6 @@ private void launchTetheringSettingsScreen() {
startActivity(intent);
}

@Override public void onHotspotFailedToStart() {
//Show a dialog to turn off default hotspot
alertDialogShower.show(KiwixDialog.TurnOffHotspotManually.INSTANCE,
(Function0<Unit>) () -> {
launchTetheringSettingsScreen();
return Unit.INSTANCE;
});
}

@Override public void requestLocationAccess() {
locationServicesHelper.setupLocationServices();
}

@Override protected void onSaveInstanceState(@Nullable Bundle outState) {
super.onSaveInstanceState(outState);
if (ServerUtils.isServerStarted) {
Expand All @@ -348,10 +275,6 @@ private void launchTetheringSettingsScreen() {
booksAdapter.setItems(books);
}

@Override public void onLocationSet() {
startService(createHotspotIntent(ACTION_LOCATION_ACCESS_GRANTED));
}

@Override public void onIpAddressValid() {
progressDialog.dismiss();
startService(createHotspotIntent(ACTION_START_SERVER).putStringArrayListExtra(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ public interface ZimHostCallbacks {

void onServerFailedToStart();

void onHotspotTurnedOn(@NonNull WifiConfiguration wifiConfiguration);

void onHotspotFailedToStart();

void requestLocationAccess();

void onIpAddressValid();

void onIpAddressInvalid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,5 @@ ZimHostContract.Presenter provideZimHostPresenter(ZimHostPresenter zimHostPresen
@Provides Activity providesActivity(ZimHostActivity zimHostActivity) {
return zimHostActivity;
}

@ActivityScope
@Provides LocationServicesHelper providesLocationServicesHelper(ZimHostActivity activity,
LocationCallbacks locationCallbacks) {
return new LocationServicesHelper(activity, locationCallbacks);
}

@ActivityScope
@Provides LocationCallbacks providesLocationCallbacks(ZimHostActivity activity) {
return activity;
}
}

Loading

0 comments on commit f9e722f

Please sign in to comment.