Skip to content

Commit

Permalink
新增验证账号、退出应用&支持磁力链接后台跳转
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Mar 10, 2024
1 parent cf86d5b commit 7cec8e3
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 57 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ dependencies {
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:$lifecycle_version")
// LiveData
implementation("androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version")

implementation 'androidx.work:work-runtime-ktx:2.8.0'

implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
Expand Down
42 changes: 25 additions & 17 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +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"/>

<application
android:name=".util.App"
Expand All @@ -17,21 +18,11 @@
android:theme="@style/Theme.Nap511"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<!-- android:screenOrientation="reverseLandscape" -->
<activity
android:name=".activity.VideoActivity"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation|keyboardHidden"
android:screenOrientation="sensorLandscape"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen" />
<activity
android:name=".MainActivity"
android:name=".activity.OfflineTaskActivity"
android:exported="true"
android:theme="@style/Theme.Nap511">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
android:theme="@android:style/Theme.Translucent.NoTitleBar"
tools:ignore="AppLinkUrlError">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.SEND" />
Expand All @@ -45,11 +36,28 @@
<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" />-->
<!-- <data android:scheme="magnet" /> -->
<!-- <data android:scheme="http" /> -->
<!-- <data android:scheme="ftp" /> -->
<!-- <data android:scheme="ed2k" /> -->
</intent-filter>
</activity>
<!-- android:screenOrientation="reverseLandscape" -->
<activity
android:name=".activity.VideoActivity"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation|keyboardHidden"
android:screenOrientation="sensorLandscape"
android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen" />
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.Nap511">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>

<service
Expand Down
103 changes: 76 additions & 27 deletions app/src/main/java/github/zerorooot/nap511/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package github.zerorooot.nap511

import android.annotation.SuppressLint
import android.app.Activity
import android.app.AppOpsManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
Expand All @@ -11,11 +14,11 @@ import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.google.gson.Gson
import com.google.gson.JsonElement
import github.zerorooot.nap511.bean.LoginBean
import github.zerorooot.nap511.factory.CookieViewModelFactory
import github.zerorooot.nap511.screen.*
Expand All @@ -29,6 +32,9 @@ import github.zerorooot.nap511.viewmodel.RecycleViewModel
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.Request
import java.lang.reflect.Field
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import kotlin.concurrent.thread


Expand All @@ -48,12 +54,70 @@ class MainActivity : ComponentActivity() {
return@Surface
}
Init(cookie)

//允许通知, 方便离线下载交互 OfflineTaskActivity
if (!isNotificationEnabled(this)) {
App.instance.toast("检测到未开启通知权限,为保证交互效果,建议开启")
goToNotificationSetting(this)
}
}
}
}
}

/**
* 判断允许通知,是否已经授权
* 返回值为true时,通知栏打开,false未打开。
* @param context 上下文
*/
private fun isNotificationEnabled(context: Context): Boolean {
val CHECK_OP_NO_THROW = "checkOpNoThrow"
val OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION"
val mAppOps = context.getSystemService(APP_OPS_SERVICE) as AppOpsManager
val appInfo = context.applicationInfo
val pkg = context.applicationContext.packageName
val uid = appInfo.uid
var appOpsClass: Class<*>? = null
/* Context.APP_OPS_MANAGER */try {
appOpsClass = Class.forName(AppOpsManager::class.java.name)
val checkOpNoThrowMethod: Method = appOpsClass.getMethod(
CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE,
String::class.java
)
val opPostNotificationValue: Field = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION)
val value = opPostNotificationValue.get(Int::class.java) as Int
return checkOpNoThrowMethod.invoke(
mAppOps,
value,
uid,
pkg
) as Int == AppOpsManager.MODE_ALLOWED
} catch (e: ClassNotFoundException) {
e.printStackTrace()
} catch (e: NoSuchMethodException) {
e.printStackTrace()
} catch (e: NoSuchFieldException) {
e.printStackTrace()
} catch (e: InvocationTargetException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
}
return false
}

/**
* 跳转到app的设置界面--开启通知
* @param context
*/
private fun goToNotificationSetting(context: Context) {
val intent = Intent()
// android 8.0引导
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS")
intent.putExtra("android.provider.extra.APP_PACKAGE", context.packageName)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
}

@Composable
private fun Init(cookie: String) {
val fileViewModel by viewModels<FileViewModel> {
Expand All @@ -71,31 +135,6 @@ class MainActivity : ComponentActivity() {
cookie, this.application
)
}


//处理输入链接
if (intent.action == Intent.ACTION_VIEW || intent.action == Intent.ACTION_PROCESS_TEXT) {
val urlList = (intent.dataString ?: run {
intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT)
} ?: run { "" }).split("\n").filter { i ->
i.startsWith("http", true) || i.startsWith(
"ftp",
true
) || i.startsWith("magnet", true) || i.startsWith("ed2k", true)
}.toList()
if (urlList.isNotEmpty()) {
offlineFileViewModel.addTask(
urlList,
DataStoreUtil.getData(ConfigUtil.defaultOfflineCid, ""),
true
)
} else {
App.instance.toast("仅支持以http、ftp、magnet、ed2k开头的链接")
finishAndRemoveTask()
}
intent.action = ""
return
}
//恢复因MyPhotoScreen而造成的isSystemBarsVisible为false的情况
var visible by remember {
mutableStateOf(true)
Expand Down Expand Up @@ -127,9 +166,11 @@ class MainActivity : ComponentActivity() {
R.drawable.baseline_cloud_24 to "我的文件",
R.drawable.baseline_cloud_download_24 to "离线下载",
R.drawable.baseline_cloud_done_24 to "离线列表",
R.drawable.baseline_add_moderator_24 to "验证账号",
R.drawable.baseline_web_24 to "网页版",
R.drawable.ic_baseline_delete_24 to "回收站",
R.drawable.baseline_settings_24 to "高级设置",
R.drawable.android_exit to "退出应用"
)

ModalNavigationDrawer(gesturesEnabled = App.gesturesEnabled,
Expand Down Expand Up @@ -160,6 +201,13 @@ class MainActivity : ComponentActivity() {
"网页版" -> WebViewScreen()
"回收站" -> RecycleScreen(recycleViewModel)
"高级设置" -> SettingScreen()
"验证账号" -> {
App.captchaUrl =
"https://captchaapi.115.com/?ac=security_code&type=web&cb=Close911_" + System.currentTimeMillis()
CaptchaWebViewScreen()
}

"退出应用" -> ExitApp()
"captchaWebView" -> CaptchaWebViewScreen()
"loginWebView" -> LoginWebViewScreen()
"photo" -> {
Expand All @@ -169,6 +217,7 @@ class MainActivity : ComponentActivity() {
})
}


@SuppressLint("UnrememberedMutableState")
@Composable
private fun Login() {
Expand Down
Loading

0 comments on commit 7cec8e3

Please sign in to comment.