Skip to content

Commit

Permalink
V2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
teprinciple committed Apr 2, 2020
1 parent 470a327 commit f0eefed
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 29 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[ ![](https://img.shields.io/badge/platform-android-green.svg) ](http://developer.android.com/index.html)
[ ![Download](https://api.bintray.com/packages/teprinciple/maven/updateapputils/images/download.svg) ](https://bintray.com/teprinciple/maven/updateapputils/_latestVersion)

### 一行代码,快速实现app在线下载更新 A simple library for Android update app

#### UpdateAppUtils2.0 特点
Expand All @@ -24,22 +25,28 @@ UpdateAppUtils2.0功能结构变化巨大,建议使用2.0以上版本;[2.0
### 集成
```
repositories {
jcenter()
jcenter()
}
// Support
implementation 'com.teprinciple:updateapputils:2.2.1'
implementation 'com.teprinciple:updateapputils:2.3.0'
```

AndroidX项目
```
// AndroidX
implementation 'com.teprinciple:updateapputilsX:2.2.1'
implementation 'com.teprinciple:updateapputilsX:2.3.0'
```

### 使用
下面为kotlin使用示例,Java示例请参考[JavaDemo](https://github.com/teprinciple/UpdateAppUtils/blob/master/app/src/main/java/com/example/teprinciple/updateappdemo/JavaDemoActivity.java)
#### 1、快速使用

##### 注意:部分手机SDK内部初始化不了context,造成context空指针,建议在application或者使用SDK前先初始化
```
UpdateAppUtils.init(context)
```

```
UpdateAppUtils
.getInstance()
Expand Down Expand Up @@ -175,7 +182,7 @@ implementation 'com.teprinciple:updateapputilsX:2.2.1'
<img src="https://github.com/teprinciple/UpdateAppUtils/blob/master/img/demo.png" width="220">

### 更新日志
#### 2.2.1
* 优化代码
* 修复部分bug

#### 2.3.0
* 修复部分手机context空指针异常
##### [更多历史版本](https://github.com/teprinciple/UpdateAppUtils/blob/master/readme/version.md)
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java_demo);

UpdateAppUtils.init(this);

findViewById(R.id.btn_java).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -43,7 +45,7 @@ public void onClick(View v) {
uiConfig.setUiType(UiType.PLENTIFUL);

UpdateAppUtils
.getInstance(JavaDemoActivity.this)
.getInstance()
.apkUrl(apkUrl)
.updateTitle(updateTitle)
.updateContent(updateContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

UpdateAppUtils.init(this)

// 基本使用
btn_basic_use.setOnClickListener {
UpdateAppUtils
Expand Down
2 changes: 2 additions & 0 deletions readme/version.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
### 更新日志
#### 2.3.0
* 修复部分手机context空指针异常
#### 2.2.1
* 优化代码
* 修复部分bug
Expand Down
2 changes: 1 addition & 1 deletion updateapputils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ publish {
userOrg = 'teprinciple'
groupId = 'com.teprinciple'
artifactId = 'updateapputils'
publishVersion = '2.2.2'
publishVersion = '2.3.0'
desc = 'A Simple library for Android update app'
website = 'https://github.com/teprinciple/UpdateAppUtils'
}
8 changes: 4 additions & 4 deletions updateapputils/src/main/java/extension/CoreKtx.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import kotlin.system.exitProcess
/**
* 全局context
*/
val globalContext by lazy { GlobalContextProvider.mContext }
fun globalContext() = GlobalContextProvider.mContext


/**
Expand All @@ -32,12 +32,12 @@ fun log(content: String?) = UpdateAppUtils.updateInfo.config.isDebug.yes {
/**
* 获取color
*/
fun color(color: Int) = if (globalContext == null) 0 else ContextCompat.getColor(globalContext!!, color)
fun color(color: Int) = if (globalContext() == null) 0 else ContextCompat.getColor(globalContext()!!, color)

/**
* 获取 String
*/
fun string(string: Int) = globalContext?.getString(string) ?: ""
fun string(string: Int) = globalContext()?.getString(string) ?: ""

/**
* view 显示隐藏
Expand All @@ -54,7 +54,7 @@ fun View.visibleOrGone(show: Boolean) {
* 退出app
*/
fun exitApp() {
val manager = globalContext!!.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val manager = globalContext()!!.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
manager.appTasks.forEach { it.finishAndRemoveTask() }
} else {
Expand Down
4 changes: 2 additions & 2 deletions updateapputils/src/main/java/ui/UpdateAppActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,10 @@ internal class UpdateAppActivity : AppCompatActivity() {

companion object {

fun launch() = globalContext.let {
fun launch() = globalContext()?.let {
val intent = Intent(it, UpdateAppActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
it?.startActivity(intent)
it.startActivity(intent)
}

private const val permission = Manifest.permission.WRITE_EXTERNAL_STORAGE
Expand Down
2 changes: 1 addition & 1 deletion updateapputils/src/main/java/update/DownloadAppUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal object DownloadAppUtils {
/**
* context
*/
private val context by lazy { globalContext!! }
private val context by lazy { globalContext()!! }

/**
* 是否在下载中
Expand Down
27 changes: 18 additions & 9 deletions updateapputils/src/main/java/update/UpdateAppUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import util.SPUtil
object UpdateAppUtils {

// 更新信息对象
internal val updateInfo = UpdateInfo()
internal val updateInfo by lazy { UpdateInfo() }

// 下载监听
internal var downloadListener: UpdateDownloadListener? = null
Expand Down Expand Up @@ -124,7 +124,13 @@ object UpdateAppUtils {
* 检查更新
*/
fun update() {
val keyName = (globalContext?.packageName ?: "") + updateInfo.config.serverVersionName

if(globalContext() == null){
log("请先调用初始化init")
return
}

val keyName = (globalContext()?.packageName ?: "") + updateInfo.config.serverVersionName
// 设置每次显示,设置本次显示及强制更新 每次都显示弹窗
(updateInfo.config.alwaysShow || updateInfo.config.thisTimeShow || updateInfo.config.force).yes {
UpdateAppActivity.launch()
Expand Down Expand Up @@ -153,14 +159,17 @@ object UpdateAppUtils {

/**
* 获取单例对象
* @param context 提供全局context。解决部分手机 通过UpdateFileProvider 获取不到
*/
@JvmStatic
fun getInstance(context: Context? = null): UpdateAppUtils{
context?.let {
GlobalContextProvider.mContext = context.applicationContext
log("外部初始化context")
}
return this
fun getInstance() = this

/**
* 初始化,非必须。解决部分手机 通过UpdateFileProvider 获取不到context情况使用
* * @param context 提供全局context。
*/
@JvmStatic
fun init(context: Context){
GlobalContextProvider.mContext = context.applicationContext
log("外部初始化context")
}
}
2 changes: 1 addition & 1 deletion updateapputils/src/main/java/update/UpdateFileProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class UpdateFileProvider : FileProvider() {
val result = super.onCreate()
(GlobalContextProvider.mContext == null && context != null).yes {
GlobalContextProvider.mContext = context
log("Provider初始化context" + GlobalContextProvider.mContext)
log("内部Provider初始化context" + GlobalContextProvider.mContext)
}
return result
}
Expand Down
4 changes: 2 additions & 2 deletions updateapputils/src/main/java/util/SPUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal object SPUtil {
}

private fun getSp(): SharedPreferences? {
if (globalContext == null) return null
return globalContext!!.getSharedPreferences(globalContext!!.packageName, Activity.MODE_PRIVATE)
if (globalContext() == null) return null
return globalContext()!!.getSharedPreferences(globalContext()!!.packageName, Activity.MODE_PRIVATE)
}
}
4 changes: 2 additions & 2 deletions updateapputils/src/main/java/util/SignMd5Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal object SignMd5Util {
* 获取当前应用签名文件md5
*/
fun getAppSignatureMD5(): String {
val packageName = globalContext?.packageName ?: ""
val packageName = globalContext()?.packageName ?: ""
if (packageName.isEmpty()) return ""
val signature = getAppSignature(packageName)
return if (signature == null || signature.isEmpty()) {
Expand Down Expand Up @@ -62,7 +62,7 @@ internal object SignMd5Util {
private fun getAppSignature(packageName: String): Array<Signature>? {
if (packageName.isEmpty()) return null
return try {
val pm = globalContext?.packageManager
val pm = globalContext()?.packageManager
val pi = pm?.getPackageInfo(packageName, PackageManager.GET_SIGNATURES)
pi?.signatures
} catch (e: Exception) {
Expand Down

0 comments on commit f0eefed

Please sign in to comment.