From bbf52c4f1db24fb0854b28005906865c8f560289 Mon Sep 17 00:00:00 2001 From: Mygod Date: Wed, 17 May 2017 14:33:36 +0800 Subject: [PATCH] Add support for notification channels for O preview --- mobile/src/main/res/values/strings.xml | 1 + .../com/github/shadowsocks/ShadowsocksApplication.scala | 6 +++++- .../com/github/shadowsocks/ShadowsocksNotification.scala | 6 +++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/mobile/src/main/res/values/strings.xml b/mobile/src/main/res/values/strings.xml index ca21e907..594b7fc7 100644 --- a/mobile/src/main/res/values/strings.xml +++ b/mobile/src/main/res/values/strings.xml @@ -56,6 +56,7 @@ Forward all DNS requests to remote + Service Shadowsocks started. Invalid server name Failed to connect the remote server diff --git a/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala b/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala index 96840c4b..4dd063a6 100644 --- a/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala +++ b/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala @@ -24,11 +24,12 @@ import java.io.{File, FileOutputStream, IOException} import java.util.Locale import android.annotation.SuppressLint -import android.app.Application +import android.app.{Application, NotificationChannel, NotificationManager} import android.content._ import android.content.res.Configuration import android.os.{Build, LocaleList} import android.preference.PreferenceManager +import android.support.v4.os.BuildCompat import android.support.v7.app.AppCompatDelegate import android.util.Log import com.evernote.android.job.JobManager @@ -154,6 +155,9 @@ class ShadowsocksApplication extends Application { JobManager.create(this).addJobCreator(DonaldTrump) TcpFastOpen.enabled(settings.getBoolean(Key.tfo, TcpFastOpen.sendEnabled)) + + if (BuildCompat.isAtLeastO) getSystemService(classOf[NotificationManager]).createNotificationChannel( + new NotificationChannel("service", getText(R.string.service), NotificationManager.IMPORTANCE_MIN)) } def crashRecovery() { diff --git a/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksNotification.scala b/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksNotification.scala index 3d81bd6c..9fc38fc9 100644 --- a/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksNotification.scala +++ b/mobile/src/main/scala/com/github/shadowsocks/ShadowsocksNotification.scala @@ -28,6 +28,7 @@ import android.os.{Build, PowerManager} import android.support.v4.app.NotificationCompat import android.support.v4.app.NotificationCompat.BigTextStyle import android.support.v4.content.ContextCompat +import android.support.v4.os.BuildCompat import com.github.shadowsocks.aidl.IShadowsocksServiceCallback.Stub import com.github.shadowsocks.utils.{Action, State, TrafficMonitor, Utils} @@ -60,6 +61,7 @@ class ShadowsocksNotification(private val service: BaseService, profileName: Str .setContentIntent(PendingIntent.getActivity(service, 0, new Intent(service, classOf[MainActivity]) .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0)) .setSmallIcon(R.drawable.ic_stat_shadowsocks) + .setChannel("service") if (Build.VERSION.SDK_INT < 24) builder.addAction(R.drawable.ic_navigation_close, service.getString(R.string.stop), PendingIntent.getBroadcast(service, 0, new Intent(Action.CLOSE), 0)) private lazy val style = new BigTextStyle(builder) @@ -97,7 +99,9 @@ class ShadowsocksNotification(private val service: BaseService, profileName: Str show() } else if (forceShow) show() - def show(): Unit = service.startForeground(1, builder.build) + def show(): Unit = + if (BuildCompat.isAtLeastO) nm.startServiceInForeground(new Intent(service, service.getClass), 1, builder.build()) + else service.startForeground(1, builder.build) def destroy() { if (lockReceiver != null) {