Add foreground notification for API<26

This commit is contained in:
Adeel Zafar 2019-08-09 19:33:19 +05:00
parent 7bbc07d3bf
commit 05d7546c9c
2 changed files with 32 additions and 9 deletions

View File

@ -57,6 +57,7 @@ public class ZimHostActivity extends AppCompatActivity implements
public static final String ACTION_TURN_OFF_AFTER_O = "Turn_off_hotspot_after_oreo";
public static final String ACTION_CHECK_HOTSPOT_STATE = "Check_hotspot_state";
public static final String ACTION_START_SERVER = "start_server";
public static final String ACTION_STOP_SERVER = "stop_server";
private final String IP_STATE_KEY = "ip_state_key";
private static final int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 102;
private Intent serviceIntent;
@ -117,7 +118,7 @@ public class ZimHostActivity extends AppCompatActivity implements
// mobileDataDialog();
//} else {
if (isStarted) {
serverStopped();
startService(ACTION_STOP_SERVER);
} else {
startHotspotDialog();
}

View File

@ -17,13 +17,14 @@ import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import org.kiwix.kiwixmobile.R;
import org.kiwix.kiwixmobile.main.MainActivity;
import org.kiwix.kiwixmobile.utils.Constants;
import org.kiwix.kiwixmobile.webserver.ServerStateListener;
import org.kiwix.kiwixmobile.webserver.WebServerHelper;
import org.kiwix.kiwixmobile.webserver.ZimHostActivity;
import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_CHECK_HOTSPOT_STATE;
import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_START_SERVER;
import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_STOP_SERVER;
import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_TURN_OFF_AFTER_O;
import static org.kiwix.kiwixmobile.webserver.ZimHostActivity.ACTION_TURN_ON_AFTER_O;
@ -59,6 +60,15 @@ public class HotspotService extends Service {
}
}
};
} else {
stopReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null && intent.getAction().equals(ACTION_STOP)) {
dismissNotification();
}
}
};
}
registerReceiver(stopReceiver, new IntentFilter(ACTION_STOP));
@ -94,6 +104,13 @@ public class HotspotService extends Service {
case ACTION_START_SERVER:
webServerHelper.startServerHelper(serverStateListener);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
updateNotification(getString(R.string.hotspot_running), true);
}
break;
case ACTION_STOP_SERVER:
dismissNotification();
break;
default:
break;
@ -109,8 +126,8 @@ public class HotspotService extends Service {
Log.v(TAG, "Building notification " + status);
builder = new NotificationCompat.Builder(this);
builder.setContentTitle("Kiwix Hotspot").setContentText(status);
Intent targetIntent = new Intent(this, MainActivity.class);
targetIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Intent targetIntent = new Intent(this, ZimHostActivity.class);
targetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, targetIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent)
@ -123,7 +140,7 @@ public class HotspotService extends Service {
Intent stopIntent = new Intent(ACTION_STOP);
PendingIntent stopHotspot =
PendingIntent.getBroadcast(this, 0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.drawable.ic_close_white_24dp, getString(R.string.tts_stop), stopHotspot);
builder.addAction(R.drawable.ic_close_white_24dp, "STOP", stopHotspot);
}
return (builder.build());
}
@ -133,16 +150,21 @@ public class HotspotService extends Service {
buildForegroundNotification(status, stopAction));
}
//Dismiss notification and turn off hotspot for devices>=O
@RequiresApi(Build.VERSION_CODES.O)
void stopHotspot() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
hotspotManager.turnOffHotspot();
}
webServerHelper.stopAndroidWebServer(serverStateListener);
stopForeground(true);
stopSelf();
webServerHelper.stopAndroidWebServer(serverStateListener);
}
//Dismiss notification and turn off hotspot for devices < O
void dismissNotification() {
webServerHelper.stopAndroidWebServer(serverStateListener);
stopForeground(true);
stopSelf();
}
@Override
public void onDestroy() {
if (stopReceiver != null) {