Skip to content

Commit

Permalink
fix for android 8.1 (issue magnusja#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
magnusja committed Feb 22, 2019
1 parent 9431542 commit 1ab5887
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application
android:allowBackup="true"
Expand Down
1 change: 1 addition & 0 deletions httpserver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {

api fileTree(include: ['*.jar'], dir: 'libs')
api 'androidx.annotation:annotation:1.0.0-beta01'
api 'androidx.core:core:1.0.0-beta01'
api project(':libaums')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@
package com.github.mjdev.libaums.server.http;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import android.util.Log;

import com.github.mjdev.libaums.fs.UsbFile;
Expand Down Expand Up @@ -50,7 +58,11 @@ public UsbFileHttpServerService getService() {
protected UsbFileHttpServer server;

public void startServer(UsbFile file, HttpServer server) throws IOException {
startAsForeground();
startServer(file, server, "com.github.magnusja.libaums.http_service_channel", "libaums_http");
}

public void startServer(UsbFile file, HttpServer server, String notificationChannelId, CharSequence notificationName) throws IOException {
startAsForeground(notificationChannelId, notificationName);

this.server = new UsbFileHttpServer(file, server);
this.server.start();
Expand Down Expand Up @@ -84,13 +96,33 @@ public IBinder onBind(Intent intent) {
return new ServiceBinder();
}

protected void startAsForeground() {
Notification notification = new Notification.Builder(this)
protected void startAsForeground(String notificationId, CharSequence notificationName) {

String channelId = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
channelId = createNotificationChannel(notificationId, notificationName);
}

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
Notification notification = notificationBuilder.setOngoing(true)
.setCategory("service")
.setContentTitle("Serving via HTTP")
.build();
startForeground(ONGOING_NOTIFICATION_ID, notification);
}

@RequiresApi(Build.VERSION_CODES.O)
private String createNotificationChannel(String id, CharSequence name) {
NotificationChannel chan = new NotificationChannel(id,
name, NotificationManager.IMPORTANCE_DEFAULT);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager service = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
service.createNotificationChannel(chan);

return id;
}

protected void stopForeground() {
stopForeground(true);
}
Expand Down

0 comments on commit 1ab5887

Please sign in to comment.