mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-24 05:04:50 -04:00
Hotspot foreground service.
Added HotspotService Build foreground notification. Added broadcast receiver for stop button. Changed code in MainActivity for API<26. Added string values Declared service in manifest.
This commit is contained in:
parent
e4e333ae21
commit
dd76a41bf2
@ -199,6 +199,9 @@
|
||||
android:resource="@xml/kiwix_widget_provider_info"/>
|
||||
</receiver>
|
||||
|
||||
<service android:name=".downloader.DownloadService"/>
|
||||
<service android:name=".wifi_hotspot.HotspotService"/>
|
||||
|
||||
<activity
|
||||
android:name=".error.ErrorActivity"
|
||||
android:process=":error_activity"/>
|
||||
|
@ -125,6 +125,7 @@ import org.kiwix.kiwixmobile.utils.LanguageUtils;
|
||||
import org.kiwix.kiwixmobile.utils.NetworkUtils;
|
||||
import org.kiwix.kiwixmobile.utils.StyleUtils;
|
||||
import org.kiwix.kiwixmobile.utils.files.FileUtils;
|
||||
import org.kiwix.kiwixmobile.wifi_hotspot.HotspotService;
|
||||
import org.kiwix.kiwixmobile.wifi_hotspot.WifiHotspotManager;
|
||||
import org.kiwix.kiwixmobile.zim_manager.ZimManageActivity;
|
||||
import org.kiwix.kiwixmobile.zim_manager.fileselect_view.StorageObserver;
|
||||
@ -175,6 +176,10 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
|
||||
|
||||
private static final String NEW_TAB = "NEW_TAB";
|
||||
private static final String HOME_URL = "file:///android_asset/home.html";
|
||||
public static final String ACTION_TURN_ON_BEFORE_O = "Turn_on_hotspot_before_oreo";
|
||||
public static final String ACTION_TURN_OFF_BEFORE_O = "Turn_aff_hotspot_before_oreo";
|
||||
public static final String ACTION_TURN_ON_AFTER_O = "Turn_on_hotspot_after_oreo";
|
||||
public static final String ACTION_TURN_OFF_AFTER_O = "Turn_off_hotspot_after_oreo";
|
||||
public static boolean isFullscreenOpened;
|
||||
public static boolean refresh;
|
||||
public static boolean wifiOnly;
|
||||
@ -184,6 +189,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
|
||||
private static Uri KIWIX_BROWSER_MARKET_URI;
|
||||
private final ArrayList<String> bookmarks = new ArrayList<>();
|
||||
private final List<KiwixWebView> webViewList = new ArrayList<>();
|
||||
private Intent serviceIntent;
|
||||
@BindView(R.id.activity_main_root)
|
||||
ConstraintLayout root;
|
||||
@BindView(R.id.activity_main_toolbar)
|
||||
@ -406,6 +412,8 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
|
||||
wifiHotspotManager = new WifiHotspotManager(this);
|
||||
wifiHotspotManager.showWritePermissionSettings();
|
||||
|
||||
serviceIntent = new Intent(this, HotspotService.class);
|
||||
|
||||
}
|
||||
|
||||
//End of onCreate
|
||||
@ -1075,18 +1083,27 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
|
||||
// Checks if wifi access point is already enabled then turns it off, otherwise enables it.
|
||||
private void switchHotspot() {
|
||||
if (wifiHotspotManager.isWifiApEnabled()) {
|
||||
wifiHotspotManager.setWifiEnabled(null, false);
|
||||
startService(ACTION_TURN_OFF_BEFORE_O);
|
||||
//wifiHotspotManager.setWifiEnabled(null, false);
|
||||
} else {
|
||||
//Check if user's hotspot is enabled
|
||||
if (isMobileDataEnabled(this)) {
|
||||
|
||||
mobileDataDialog();
|
||||
} else {
|
||||
wifiHotspotManager.setWifiEnabled(null, true);
|
||||
// potentially add data to the intent
|
||||
//i.putExtra("TURN_ON_HOTSPOT_BEFORE_O", "turnOnHotspotBeforeO");
|
||||
startService(ACTION_TURN_ON_BEFORE_O);
|
||||
//wifiHotspotManager.setWifiEnabled(null, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void startService(String ACTION) {
|
||||
serviceIntent.setAction(ACTION);
|
||||
this.startService(serviceIntent);
|
||||
}
|
||||
|
||||
private void mobileDataDialog() {
|
||||
if (Build.VERSION.SDK_INT < VERSION_CODES.O) {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this, dialogStyle());
|
||||
@ -2299,9 +2316,11 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
|
||||
// All location settings are satisfied. The client can initialize location
|
||||
// requests here.
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
wifiHotspotManager.turnOnHotspot();
|
||||
}
|
||||
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
serviceIntent.setAction(ACTION_TURN_ON_AFTER_O);
|
||||
getApplicationContext().startService(serviceIntent);
|
||||
//wifiHotspotManager.turnOnHotspot();
|
||||
//}
|
||||
} catch (ApiException exception) {
|
||||
switch (exception.getStatusCode()) {
|
||||
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
|
||||
|
@ -0,0 +1,125 @@
|
||||
package org.kiwix.kiwixmobile.wifi_hotspot;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import org.kiwix.kiwixmobile.R;
|
||||
import org.kiwix.kiwixmobile.main.MainActivity;
|
||||
|
||||
import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_AFTER_O;
|
||||
import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_OFF_BEFORE_O;
|
||||
import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_AFTER_O;
|
||||
import static org.kiwix.kiwixmobile.main.MainActivity.ACTION_TURN_ON_BEFORE_O;
|
||||
|
||||
public class HotspotService extends Service {
|
||||
private static final int HOTSPOT_NOTIFICATION_ID = 666;
|
||||
public static final String ACTION_START = "hotspot_start";
|
||||
public static final String ACTION_STOP = "hotspot_stop";
|
||||
public static final String ACTION_STATUS = "hotspot_status";
|
||||
private WifiHotspotManager wifiHotspotManager;
|
||||
private BroadcastReceiver stopReceiver;
|
||||
|
||||
@Override public void onCreate() {
|
||||
super.onCreate();
|
||||
wifiHotspotManager = new WifiHotspotManager(this);
|
||||
stopReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent != null && intent.getAction().equals(ACTION_STOP)) {
|
||||
stopHotspot();
|
||||
}
|
||||
}
|
||||
};
|
||||
registerReceiver(stopReceiver, new IntentFilter(ACTION_STOP));
|
||||
startForeground(HOTSPOT_NOTIFICATION_ID,
|
||||
buildForegroundNotification(getString(R.string.hotspot_start), false));
|
||||
}
|
||||
|
||||
@Override public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
switch (intent.getAction()) {
|
||||
case ACTION_TURN_ON_BEFORE_O:
|
||||
if (wifiHotspotManager.setWifiEnabled(null, true)) {
|
||||
updateNotification(getString(R.string.hotspot_running), true);
|
||||
}
|
||||
break;
|
||||
case ACTION_TURN_ON_AFTER_O:
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
wifiHotspotManager.turnOnHotspot();
|
||||
}
|
||||
break;
|
||||
case ACTION_TURN_OFF_BEFORE_O:
|
||||
wifiHotspotManager.setWifiEnabled(null, false);
|
||||
stopForeground(true);
|
||||
stopSelf();
|
||||
break;
|
||||
case ACTION_TURN_OFF_AFTER_O:
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
wifiHotspotManager.turnOffHotspot();
|
||||
}
|
||||
stopForeground(true);
|
||||
stopSelf();
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return START_NOT_STICKY;
|
||||
}
|
||||
|
||||
@Nullable @Override public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
private Notification buildForegroundNotification(String status, boolean showStopButton) {
|
||||
Log.v("DANG","Building notification "+status);
|
||||
NotificationCompat.Builder b = new NotificationCompat.Builder(this);
|
||||
b.setContentTitle("Kiwix Hotspot").setContentText(status);
|
||||
Intent targetIntent = new Intent(this, MainActivity.class);
|
||||
targetIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
||||
PendingIntent contentIntent =
|
||||
PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
b.setContentIntent(contentIntent)
|
||||
.setSmallIcon(R.mipmap.kiwix_icon)
|
||||
.setWhen(System.currentTimeMillis());
|
||||
if (showStopButton) {
|
||||
Intent stopIntent = new Intent(ACTION_STOP);
|
||||
PendingIntent stopHotspot =
|
||||
PendingIntent.getBroadcast(this, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
b.addAction(R.drawable.ic_close_white_24dp, getString(R.string.tts_stop), stopHotspot);
|
||||
}
|
||||
return (b.build());
|
||||
}
|
||||
|
||||
private void updateNotification(String status, boolean stopAction) {
|
||||
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
nm.notify(HOTSPOT_NOTIFICATION_ID, buildForegroundNotification(status, stopAction));
|
||||
}
|
||||
|
||||
private void stopHotspot() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
wifiHotspotManager.turnOffHotspot();
|
||||
} else {
|
||||
Log.v("DANG", "Coming yes");
|
||||
wifiHotspotManager.setWifiEnabled(null, false);
|
||||
}
|
||||
stopForeground(true);
|
||||
stopSelf();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
if (stopReceiver != null) {
|
||||
unregisterReceiver(stopReceiver);
|
||||
}
|
||||
super.onDestroy();
|
||||
}
|
||||
}
|
@ -23,6 +23,8 @@
|
||||
<string name="choose_file">Select a Content File (*.zim)</string>
|
||||
<string name="open_in_new_tab">Open link in new tab?</string>
|
||||
<string name="error_nozimfilesfound">No content found on your device.\nTake a look at the Help Page to get directions on how to load content into Kiwix.\nIf you did put a ZIM file on your device/external storage, you might retry in a minute or restart your device.</string>
|
||||
<string name="hotspot_start">Starting hotspot</string>
|
||||
<string name="hotspot_running">Running Hotspot</string>
|
||||
<string name="hotspot_turned_on">Hotspot turned on</string>
|
||||
<string name="hotspot_details_message">Following are the details of your local hotspot.</string>
|
||||
<string name="hotspot_ssid_label">SSID : </string>
|
||||
|
Loading…
x
Reference in New Issue
Block a user