Skip to content

Commit

Permalink
fix: correct notification action impl
Browse files Browse the repository at this point in the history
  • Loading branch information
shunf4 committed Jun 30, 2023
1 parent aacc477 commit 2d595ab
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
8 changes: 8 additions & 0 deletions app/src/main/java/com/jim/sharetocomputer/ActionActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,26 @@ class ActionActivity : AppCompatActivity() {
} else if (action == ACTION_STOP_DOWNLOAD) {
MyLog.i("*Stopping download service")
stopService(DownloadService.createIntent(this, null))
} else if (action == ACTION_STOP_RECEIVING) {
MyLog.i("*Stopping receiving service")
stopService(WebUploadService.createIntent(this))
}
finish()
}

companion object {
const val ACTION_STOP_SHARE = "com.jim.sharetocomputer.STOP_SHARE"
const val ACTION_STOP_DOWNLOAD = "com.jim.sharetocomputer.STOP_DOWNLOAD"
const val ACTION_STOP_RECEIVING = "com.jim.sharetocomputer.STOP_RECEIVING"

fun stopShareIntent(context: Context) = Intent(context, ActionActivity::class.java).apply {
action = ACTION_STOP_SHARE
}

fun stopReceivingIntent(context: Context) = Intent(context, ActionActivity::class.java).apply {
action = ACTION_STOP_RECEIVING
}

fun stopDownloadIntent(context: Context) =
Intent(context, ActionActivity::class.java).apply {
action = ACTION_STOP_DOWNLOAD
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/jim/sharetocomputer/WebServerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.jim.sharetocomputer.coroutines.TestableDispatchers
import com.jim.sharetocomputer.ext.getPrimaryUrl
import com.jim.sharetocomputer.ext.getServerBaseUrls
import com.jim.sharetocomputer.logging.MyLog
import com.jim.sharetocomputer.ui.main.MainActivity
import com.jim.sharetocomputer.webserver.WebServer
import com.jim.sharetocomputer.webserver.WebServerMultipleFiles
import com.jim.sharetocomputer.webserver.WebServerSingleFile
Expand Down Expand Up @@ -209,6 +210,13 @@ class WebServerService : Service() {
))
)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(TaskStackBuilder.create(this).run {
addNextIntentWithParentStack(Intent(this@WebServerService, MainActivity::class.java).apply {
action = "com.jim.sharetocomputer.VIEW_SERVE"
putExtra("viewTabPos", 0)
})
getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
})
.addAction(
R.mipmap.ic_launcher, getString(R.string.stop_share),
stopPendingIntent
Expand Down
16 changes: 14 additions & 2 deletions app/src/main/java/com/jim/sharetocomputer/WebUploadService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.jim.sharetocomputer.coroutines.TestableDispatchers
import com.jim.sharetocomputer.ext.getPrimaryUrl
import com.jim.sharetocomputer.ext.getServerBaseUrls
import com.jim.sharetocomputer.logging.MyLog
import com.jim.sharetocomputer.ui.main.MainActivity
import com.jim.sharetocomputer.webserver.WebServerReceive
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -143,7 +144,7 @@ class WebUploadService : Service() {
val notificationManager = NotificationManagerCompat.from(this)
notificationManager.createNotificationChannel(channel)
}
val stopIntent = ActionActivity.stopShareIntent(this)
val stopIntent = ActionActivity.stopReceivingIntent(this)
val stopPendingIntent = TaskStackBuilder.create(this).run {
addNextIntentWithParentStack(stopIntent)
getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
Expand All @@ -164,8 +165,15 @@ class WebUploadService : Service() {
))
)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(TaskStackBuilder.create(this).run {
addNextIntentWithParentStack(Intent(this@WebUploadService, MainActivity::class.java).apply {
action = "com.jim.sharetocomputer.VIEW_SERVE"
putExtra("viewTabPos", 1)
})
getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
})
.addAction(
R.mipmap.ic_launcher, getString(R.string.stop_share),
R.mipmap.ic_launcher, getString(R.string.stop_receiving),
stopPendingIntent
)
return builder.build()
Expand Down Expand Up @@ -195,6 +203,10 @@ class WebUploadService : Service() {

var isRunning = MutableLiveData<Boolean>().apply { value = false }
var port = MutableLiveData<Int>().apply { value = 8080 }

fun createIntent(context: Context): Intent {
return Intent(context, WebUploadService::class.java)
}
}
}

15 changes: 13 additions & 2 deletions app/src/main/java/com/jim/sharetocomputer/ui/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,23 @@ class MainActivity : AppCompatActivity() {
R.layout.activity_main
)

val request: ShareRequest? = intent.generateShareRequest()
val bundle = if (request != null) {
var request: ShareRequest? = null
var viewTabPos: Int? = null

if (intent.action == "com.jim.sharetocomputer.VIEW_SERVE") {
viewTabPos = intent.getIntExtra("viewTabPos", -1)
} else {
request = intent.generateShareRequest()
}

val bundle = if (viewTabPos != null) {
MainFragment.createViewTabBundle(viewTabPos)
} else if (request != null) {
MainFragment.createBundle(request)
} else {
Bundle()
}

navController = Navigation.findNavController(this, R.id.main_nav_fragment)
navController.setGraph(R.navigation.nav_main, bundle)
}
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/jim/sharetocomputer/ui/main/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class MainFragment : Fragment() {
mainViewModel.setRequest(it)
}

(arguments?.getInt(ARGS_VIEW_TAB, -1))?.let {
if (it != -1) {
binding.viewPager.currentItem = it
}
}

return binding.root
}

Expand All @@ -59,11 +65,18 @@ class MainFragment : Fragment() {

companion object {
private const val ARGS_REQUEST = "request"
private const val ARGS_VIEW_TAB = "viewTabPos"

fun createBundle(request: ShareRequest): Bundle {
return Bundle().apply {
putParcelable(ARGS_REQUEST, request)
}
}

fun createViewTabBundle(position: Int): Bundle {
return Bundle().apply {
putInt(ARGS_VIEW_TAB, position)
}
}
}
}

0 comments on commit 2d595ab

Please sign in to comment.