Skip to content

Commit

Permalink
Add update ver check
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 committed Apr 18, 2020
1 parent 66eca7a commit 504a9f4
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 13 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
xmlns:tools="http://schemas.android.com/tools"
package="me.iacn.biliroaming">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
65 changes: 53 additions & 12 deletions app/src/main/java/me/iacn/biliroaming/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")

package me.iacn.biliroaming

import android.annotation.SuppressLint
Expand All @@ -14,11 +16,15 @@ import android.preference.Preference.OnPreferenceChangeListener
import android.preference.Preference.OnPreferenceClickListener
import android.preference.PreferenceCategory
import android.preference.PreferenceFragment
import android.util.AttributeSet
import android.util.Log
import android.widget.Toast
import android.widget.Toast.LENGTH_SHORT
import me.iacn.biliroaming.utils.CheckVersionTask
import me.iacn.biliroaming.utils.OnTaskReturn
import org.json.JSONObject
import java.io.File

import java.net.URL

/**
* Created by iAcn on 2019/3/23
Expand All @@ -31,9 +37,9 @@ class MainActivity : Activity() {
fragmentManager.beginTransaction().replace(android.R.id.content, PrefsFragment()).commit()
}

class PrefsFragment : PreferenceFragment(), OnPreferenceChangeListener, OnPreferenceClickListener {
class PrefsFragment : PreferenceFragment(), OnPreferenceChangeListener, OnPreferenceClickListener, OnTaskReturn<JSONObject> {
private var runningStatusPref: Preference? = null
private var counter: Int = 0;
private var counter: Int = 0
private var prefs: SharedPreferences? = null
private var toast: Toast? = null

Expand All @@ -55,6 +61,7 @@ class MainActivity : Activity() {
findPreference("version").onPreferenceClickListener = this
findPreference("author").onPreferenceClickListener = this
findPreference("test_cdn").onPreferenceClickListener = this
CheckVersionTask(this).execute(URL(resources.getString(R.string.version_url)))
}

override fun onPreferenceChange(preference: Preference, newValue: Any): Boolean {
Expand Down Expand Up @@ -115,35 +122,68 @@ class MainActivity : Activity() {
if (counter == 7) {
preferenceScreen.removeAll()
addPreferencesFromResource(R.xml.prefs_setting)
prefs?.edit()?.putBoolean("hidden", true)?.commit()
prefs?.edit()?.putBoolean("hidden", true)?.apply()
toast?.setText("已开启隐藏功能")
toast?.duration = LENGTH_SHORT;
toast?.show();
toast?.duration = LENGTH_SHORT
toast?.show()
preferenceScreen.removeAll()
onCreate()
onResume()
} else if (counter >= 4) {
toast?.let {
it.setText(text);
it.duration = LENGTH_SHORT;
it.show();
it.setText(text)
it.duration = LENGTH_SHORT
it.show()
} ?: run {
toast = Toast.makeText(activity, text, LENGTH_SHORT)
toast?.setText(text)
toast?.show()
}
}
return true
}

override fun onReturn(result: JSONObject?) {
try {
result?.getString("name")?.let {
if (BuildConfig.VERSION_NAME != it) {
findPreference("version").summary = "${BuildConfig.VERSION_NAME}(最新版${it}"
val aboutGroup = findPreference("about") as PreferenceCategory
val updatePreference = Preference(activity)
updatePreference.key = "update"
updatePreference.title = resources.getString(R.string.update_title)
var log = ""
try {
val body = result.getString("body")
log = body.substring(body.lastIndexOf("更新日志"))
}catch(e : Throwable){}
updatePreference.summary = if(log.isNotEmpty()) log else resources.getString(R.string.update_summary)
updatePreference.onPreferenceClickListener=this
updatePreference.order=1
aboutGroup.addPreference(updatePreference)

}
}
} catch (e: Throwable) {
}
}

private fun onAuthorClick(): Boolean {
val uri = Uri.parse("https://github.com/yujincheng08/BiliRoaming")
val uri = Uri.parse(resources.getString(R.string.github_url))
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
return true
}

private fun onTestCDNClick(): Boolean {
val uri = Uri.parse("https://yujincheng08.github.io/BiliRoaming/cdn_test.html")
val uri = Uri.parse(resources.getString(R.string.cdn_url))
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
return true
}

private fun onUpdateCheck(): Boolean {
val uri = Uri.parse(resources.getString(R.string.update_url))
val intent = Intent(Intent.ACTION_VIEW, uri)
startActivity(intent)
return true
Expand All @@ -154,13 +194,13 @@ class MainActivity : Activity() {
"version" -> onVersionClick()
"author" -> onAuthorClick()
"test_cdn" -> onTestCDNClick()
"update" -> onUpdateCheck()
else -> false
}
}
}

companion object {

@android.support.annotation.Keep
fun isModuleActive(): Boolean {
Log.i("大不自多", "海纳江河")
Expand All @@ -178,4 +218,5 @@ class MainActivity : Activity() {
}
}
}

}
24 changes: 24 additions & 0 deletions app/src/main/java/me/iacn/biliroaming/utils/VersionCheck.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.iacn.biliroaming.utils

import android.os.AsyncTask
import org.json.JSONObject
import java.net.URL

interface OnTaskReturn<T> {
fun onReturn(result: T?)
}

class CheckVersionTask(private val onResult: OnTaskReturn<JSONObject>) : AsyncTask<URL, Void, JSONObject?>() {
override fun doInBackground(vararg url: URL?): JSONObject? {
return try {
JSONObject(url[0]?.readText()!!)
} catch (e: Throwable) {
null
}
}

override fun onPostExecute(result: JSONObject?) {
onResult.onReturn(result)
}

}
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,10 @@
<string name="setting_category">设置</string>
<string name="mini_program_title">去你大爷的小程序</string>
<string name="mini_program_summary">不再以小程序的方式分享到QQ或者微信(同时复制分享链接变为av号)</string>
<string name="version_url">https://api.github.com/repos/yujincheng08/BiliRoaming/releases/latest</string>
<string name="update_title">更新</string>
<string name="update_summary">点击打开最新版下载地址</string>
<string name="update_url">https://github.com/yujincheng08/BiliRoaming/releases/latest</string>
<string name="github_url">https://github.com/yujincheng08/BiliRoaming</string>
<string name="cdn_url">https://yujincheng08.github.io/BiliRoaming/cdn_test.html</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/xml/prefs_setting.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
android:title="@string/purify_drawer_title" />
</PreferenceCategory>

<PreferenceCategory android:title="@string/about">
<PreferenceCategory android:key="about" android:title="@string/about">

<Preference
android:key="version"
Expand Down

0 comments on commit 504a9f4

Please sign in to comment.