Skip to content

Commit

Permalink
更新版本号&修复主页在搜索时返回退出的问题&添加离线任务缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Mar 30, 2024
1 parent 4ac39d4 commit 945be01
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 67 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
applicationId "github.zerorooot.nap511"
minSdk 26
targetSdk 33
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
11 changes: 4 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

<application
android:name=".util.App"
Expand All @@ -25,21 +25,18 @@
tools:ignore="AppLinkUrlError">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data android:scheme="magnet" />
<!-- <data android:scheme="http" />-->
<data android:scheme="ftp" />
<data android:scheme="ed2k" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PROCESS_TEXT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
<!-- <data android:scheme="magnet" /> -->
<!-- <data android:scheme="http" /> -->
<!-- <data android:scheme="ftp" /> -->
<!-- <data android:scheme="ed2k" /> -->
</intent-filter>
</activity>
<!-- android:screenOrientation="reverseLandscape" -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.ClipData
import android.content.ClipboardManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.work.Data
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
Expand Down Expand Up @@ -43,23 +46,56 @@ class OfflineTaskActivity : Activity() {
i.startsWith("http", true) || i.startsWith(
"ftp", true
) || i.startsWith("magnet", true) || i.startsWith("ed2k", true)
}.toList()
}.toSet()
// App.instance.toast(urlList.toString())
val listType = object : TypeToken<List<String?>?>() {}.type
val list = Gson().toJson(urlList, listType)
println(list)
println(urlList.toString())
//非空列表
if (urlList.isNotEmpty()) {
App.instance.toast("${urlList.size} 个链接添加中......")
val data: Data =
Data.Builder().putString("cookie", App.cookie).putString("list", list).build()
val request: OneTimeWorkRequest =
OneTimeWorkRequest.Builder(OfflineTaskWorker::class.java).setInputData(data)
.build()
WorkManager.getInstance(applicationContext).enqueue(request)
//检查离线任务缓存数
val offlineCount = try {
DataStoreUtil.getData(ConfigUtil.defaultOfflineCount, "5").toInt()
} catch (e: Exception) {
5
}
val currentOfflineTaskList =
DataStoreUtil.getData(ConfigUtil.currentOfflineTask, "")
.split("\n")
.filter { i -> i != "" && i != " " }
.toSet()
.toMutableList()
//添加所有
currentOfflineTaskList.addAll(urlList)
if (currentOfflineTaskList.size >= offlineCount) {
App.instance.toast("${currentOfflineTaskList.size} 个链接添加中......")
val listType = object : TypeToken<List<String?>?>() {}.type
val list = Gson().toJson(currentOfflineTaskList, listType)
val data: Data =
Data.Builder().putString("cookie", App.cookie).putString("list", list)
.build()
val request: OneTimeWorkRequest =
OneTimeWorkRequest.Builder(OfflineTaskWorker::class.java).setInputData(data)
.build()
WorkManager.getInstance(applicationContext).enqueue(request)
} else {
val stringJoiner = StringJoiner("\n")
currentOfflineTaskList.forEach { stringJoiner.add(it) }
//写入缓存
DataStoreUtil.putData(
ConfigUtil.currentOfflineTask,
stringJoiner.toString()
)
App.instance.toast("已添加${currentOfflineTaskList.size}个链接到缓存中,剩余${offlineCount - currentOfflineTaskList.size}")
}

} else {
App.instance.toast("仅支持以http、ftp、magnet、ed2k开头的链接")
}
}
if (intent.action == "copy") {
val clipboard = ContextCompat.getSystemService(this, ClipboardManager::class.java)
val clip = ClipData.newPlainText("label", intent.getStringExtra("link"))
clipboard?.setPrimaryClip(clip)
}
finishAndRemoveTask()
}
}
Expand All @@ -75,22 +111,29 @@ class OfflineTaskWorker(
val a: List<String> = Gson().fromJson(inputData.getString("list").toString(), listType)
val addTaskData = addTask(a, cookie)
val message = addTaskData.getString("return").toString()
if (message.contains("任务添加成功")) {
//清空缓存
DataStoreUtil.putData(
ConfigUtil.currentOfflineTask,
""
)
}
println(message)
toast(message)
toast(message, a)
return Result.success(addTaskData);
}


@SuppressLint("WrongConstant")
private fun toast(message: String) {
private fun toast(message: String, urlList: List<String>) {
//渠道Id
val channelId = "toast"
//渠道名
val channelName = "离线下载结果"
//渠道重要级
val importance = NotificationManagerCompat.IMPORTANCE_MAX
//通知Id
val notificationId = 1
val notificationId = System.currentTimeMillis().toInt()

val notificationManager =
applicationContext.getSystemService(AppCompatActivity.NOTIFICATION_SERVICE) as NotificationManager
Expand All @@ -101,22 +144,36 @@ class OfflineTaskWorker(
)
)
val notification = NotificationCompat.Builder(applicationContext, channelId).apply {
setSmallIcon(R.mipmap.ic_launcher)
setContentTitle("离线下载结果")//标题
setAutoCancel(true)
setSmallIcon(R.mipmap.ic_launcher)
setContentTitle("离线下载结果")//标题
setAutoCancel(true)
// setContentText(message)
setDefaults(Notification.DEFAULT_VIBRATE);
setStyle(NotificationCompat.BigTextStyle().bigText(message))
setDefaults(Notification.DEFAULT_VIBRATE);
setStyle(NotificationCompat.BigTextStyle().bigText(message))
}

if (message.contains("任务添加失败")) {
val pendingIntent = if (message.contains("请验证账号")) {
val intent = Intent(this.applicationContext, MainActivity::class.java)
intent.action = "jump"
notification.setContentText("$message。点我跳转验证账号页面")
PendingIntent.getActivity(
this.applicationContext, 0, intent, PendingIntent.FLAG_IMMUTABLE
)
} else {
val intent = Intent(this.applicationContext, OfflineTaskActivity::class.java)
intent.action = "copy"
val stringJoiner = StringJoiner("\n")
urlList.forEach { stringJoiner.add(it) }
intent.putExtra("link", stringJoiner.toString())
notification.setContentText("$message。点我复制链接")
PendingIntent.getActivity(
this.applicationContext, 0, intent, PendingIntent.FLAG_IMMUTABLE
)
}
if (message.contains("请验证账号")) {
val intent = Intent(this.applicationContext, MainActivity::class.java)
intent.action = "jump"
val pendingIntent = PendingIntent.getActivity(
this.applicationContext, 0, intent, PendingIntent.FLAG_IMMUTABLE
)
notification.setContentText("$message。点我跳转验证账号页面")

notification.setContentIntent(pendingIntent)
}else{
} else {
notification.setContentText(message)
}

Expand All @@ -128,7 +185,7 @@ class OfflineTaskWorker(
val cid = DataStoreUtil.getData(ConfigUtil.defaultOfflineCid, "")
val downloadPath = setDownloadPath(cid, cookie)
if (!downloadPath.state) {
resultMessage.add("设置离线位置失败,默认保存到\"云下载\"目录")
resultMessage.add("设置离线位置失败,默认保存到\"云下载\"目录\n")
}
val map = HashMap<String, String>()
map["savepath"] = ""
Expand Down
23 changes: 12 additions & 11 deletions app/src/main/java/github/zerorooot/nap511/screen/FileScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ fun FileScreen(


val itemOnLongClick = { i: Int ->
fileViewModel.isLongClick = !fileViewModel.isLongClick
if (fileViewModel.isLongClick) {
fileViewModel.isLongClickState = !fileViewModel.isLongClickState
if (fileViewModel.isLongClickState) {
fileViewModel.select(i)
} else {
fileViewModel.appBarTitle = "nap511"
Expand Down Expand Up @@ -140,7 +140,7 @@ fun FileScreen(
}

val myItemOnClick = { i: Int ->
if (fileViewModel.isLongClick) {
if (fileViewModel.isLongClickState) {
fileViewModel.select(i)
} else {
// //记录上级目录当前的位置
Expand Down Expand Up @@ -191,24 +191,25 @@ fun FileScreen(
}
).toString()
//appBar 也调用了这个,所以再判断一次
if (path != "/根目录" && !fileViewModel.isLongClick) {
if (path != "/根目录" && !fileViewModel.isLongClickState) {
//当前目录的位置
fileViewModel.setListLocation(path)
fileViewModel.getListLocation(parentDirectory)
}
fileViewModel.back()
}
BackHandler(path != "/根目录" || fileViewModel.isLongClick, onBack)

BackHandler(
path != "/根目录" || fileViewModel.isLongClickState || fileViewModel.isSearchState,
onBack
)

val myAppBarOnClick = { i: String ->
if (i == "back") {
if (path == "/根目录") {
if (path == "/根目录" && !fileViewModel.isSearchState) {
App.instance.openDrawerState()
} else {
onBack()
}

}
appBarOnClick.invoke(i)
}
Expand All @@ -217,7 +218,7 @@ fun FileScreen(

val clipboardManager: ClipboardManager = LocalClipboardManager.current
Column {
AnimatedContent(targetState = fileViewModel.isLongClick, transitionSpec = {
AnimatedContent(targetState = fileViewModel.isLongClickState, transitionSpec = {
fadeIn() with fadeOut()
}, label = "") {
if (it) {
Expand Down Expand Up @@ -251,7 +252,7 @@ fun FileScreen(
}

Scaffold(floatingActionButton = {
AnimatedContent(targetState = fileViewModel.isCut, transitionSpec = {
AnimatedContent(targetState = fileViewModel.isCutState, transitionSpec = {
fadeIn() with fadeOut()
}, label = "") {
if (it) {
Expand Down Expand Up @@ -400,7 +401,7 @@ private fun checkAria2(aria2Url: String, aria2Token: String, context: Context) {
} else {
DataStoreUtil.putData(ConfigUtil.aria2Url, aria2Url)
DataStoreUtil.putData(ConfigUtil.aria2Token, aria2Token)
"aria2配置成功"
"aria2配置成功,请重新下载文件"
}

} catch (e: Exception) {
Expand Down
39 changes: 37 additions & 2 deletions app/src/main/java/github/zerorooot/nap511/screen/SettingScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ fun SettingScreen() {
key = ConfigUtil.requestLimitCount,
title = "每次请求文件数",
summary = DataStoreUtil.getData(ConfigUtil.requestLimitCount, "100"),
defaultValue = DataStoreUtil.getData(ConfigUtil.requestLimitCount, "100"),
dialogTitle = "每次请求文件数",
)
}
Expand All @@ -80,12 +81,46 @@ fun SettingScreen() {
key = ConfigUtil.defaultOfflineCid,
title = "默认离线位置",
summary = DataStoreUtil.getData(
ConfigUtil.defaultOfflineCid,
"输入文件夹cid,长按目录可复制当前目录cid"
ConfigUtil.defaultOfflineCid, "输入文件夹cid,长按目录可复制当前目录cid"
),
dialogTitle = "长按路径可当前目录cid",
)
}
prefsItem {
EditTextPref(
key = ConfigUtil.defaultOfflineCount,
title = "离线任务缓存数",
summary = DataStoreUtil.getData(
ConfigUtil.defaultOfflineCount, "5"
),
defaultValue = DataStoreUtil.getData(
ConfigUtil.defaultOfflineCount, "5"
),
dialogTitle = "集满${
DataStoreUtil.getData(
ConfigUtil.defaultOfflineCount, "5"
)
}个链接后统一离线下载",
)
}
prefsItem {
EditTextPref(
key = ConfigUtil.currentOfflineTask,
title = "离线任务缓存",
summary = DataStoreUtil.getData(
ConfigUtil.currentOfflineTask, "当前尚未添加离线任务的链接"
),
dialogTitle = "尚未离线的任务链接 ${
DataStoreUtil.getData(
ConfigUtil.currentOfflineTask, ""
).split("\n").filter { i -> i != "" && i != " " }.toSet().size
}/${
DataStoreUtil.getData(
ConfigUtil.defaultOfflineCount, "5"
)
}",
)
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/github/zerorooot/nap511/util/ConfigUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ class ConfigUtil {
const val autoRotate = "autoRotate"
const val defaultOfflineCid = "0"
const val requestLimitCount = "requestLimitCount"
const val defaultOfflineCount = "defaultOfflineCount"
const val currentOfflineTask = ""

}
}
Loading

0 comments on commit 945be01

Please sign in to comment.