Skip to content

Commit

Permalink
优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
teprinciple committed Mar 27, 2020
1 parent 920f95b commit 6da3afc
Show file tree
Hide file tree
Showing 25 changed files with 235 additions and 368 deletions.
17 changes: 4 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ repositories {
}
// Support
implementation 'com.teprinciple:updateapputils:2.2.0'
implementation 'com.teprinciple:updateapputils:2.2.1'
```

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

### 使用
Expand Down Expand Up @@ -108,15 +108,6 @@ implementation 'com.teprinciple:updateapputilsX:2.2.0'
UpdateAppUtils内置了两套UI,你可以通过修改[UiConfig](#UiConfig)进行UI内容的自定义;
当然当内部UI模板与你期望UI差别很大时,你可以采用[完全自定义UI](https://github.com/teprinciple/UpdateAppUtils/blob/master/readme/%E8%87%AA%E5%AE%9A%E4%B9%89UI.md)

#### 5、删除已安装APK
UpdateAppUtils在应用启动时,通过对比当前应用和已下载apk的VersionCode是否相同来判断已下载apk是否已安装;
如果两者VersionCode相同则认为apk已经安装,应被删除。

```
// 在Application或者MainActivity中调用,以达到安装成功启动后删除已安装apk
UpdateAppUtils.getInstance().deleteInstalledApk()
```

### Api说明
#### 1、UpdateAppUtils Api

Expand Down Expand Up @@ -184,7 +175,7 @@ UpdateAppUtils在应用启动时,通过对比当前应用和已下载apk的Ver
<img src="https://github.com/teprinciple/UpdateAppUtils/blob/master/img/demo.png" width="220">

### 更新日志
#### 2.2.0
* 适配Android 10
#### 2.2.1
* 优化代码
* 修复部分bug
##### [更多历史版本](https://github.com/teprinciple/UpdateAppUtils/blob/master/readme/version.md)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import org.jetbrains.annotations.NotNull;

import constacne.UiType;
import constant.UiType;
import listener.Md5CheckResultListener;
import listener.UpdateDownloadListener;
import model.UiConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.TextView
import android.widget.Toast
import constacne.DownLoadBy
import constacne.UiType
import constant.DownLoadBy
import constant.UiType
import kotlinx.android.synthetic.main.activity_main.*
import listener.OnBtnClickListener
import listener.OnInitUiListener
Expand All @@ -30,9 +30,6 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// 启动应用后删除安装包
UpdateAppUtils.getInstance().deleteInstalledApk()

// 基本使用
btn_basic_use.setOnClickListener {
UpdateAppUtils
Expand Down
21 changes: 12 additions & 9 deletions readme/version.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#### 更新日志
### 更新日志
#### 2.2.1
* 优化代码
* 修复部分bug
#### 2.2.0
* 适配Android 10
* 修复部分bug
Expand All @@ -15,7 +18,7 @@
* 9.0Http适配
#### 2.0.1
* 自定义FileProvide,防止provider冲突
##### 2.0.0
#### 2.0.0
* Kotlin重构
* 支持AndroidX
* 安装包签名文件md5校验
Expand All @@ -24,20 +27,20 @@
* 适配中英文
* 增加下载回调等api
* 修复部分bug
##### 1.5.2
#### 1.5.2
* 修复部分bug
##### 1.5.1
#### 1.5.1
* 库内部适配至Android8.0
##### 1.4
#### 1.4
* 使用[filedownloader](https://github.com/lingochamp/FileDownloader)替换DownloadManager,避免部分手机DownLoadManager无效,同时解决了重复下载的问题,且提高了下载速度
* 增加接口UpdateAppUtils.needFitAndroidN(false),避免不需要适配7.0,也要设置FileProvider
##### 1.3.1
#### 1.3.1
* 修复部分bug,在demo中加入kotlin调用代码
##### 1.3
#### 1.3
* 增加接口方法 showNotification(false) //是否显示下载进度到通知栏;
* updateInfo(info) //更新日志信息;
* 下载前WiFi判断。
##### 1.2
#### 1.2
* 适配Android7.0,并在demo中加入适配6.0和7.0的代码
##### 1.1
#### 1.1
* 适配更多SdkVersion
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package constacne
package constant

/**
* desc: 下载方式
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package constacne
package constant

/**
* desc: UI 类型
Expand Down
30 changes: 0 additions & 30 deletions updateapputils/src/main/java/extension/Any.kt

This file was deleted.

57 changes: 57 additions & 0 deletions updateapputils/src/main/java/extension/ContextKtx.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package extension

import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Build
import android.support.v4.content.FileProvider
import java.io.File

/**
* desc: context 相关扩展
* author: teprinciple on 2020/3/27.
*/


/**
* appName
*/
val Context.appName
get() = packageManager.getPackageInfo(packageName, 0)?.applicationInfo?.loadLabel(packageManager).toString()

/**
* 检测wifi是否连接
*/
fun Context.isWifiConnected(): Boolean {
val cm = this.getSystemService(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager
cm ?: return false
val networkInfo = cm.activeNetworkInfo
return networkInfo != null && networkInfo.type == ConnectivityManager.TYPE_WIFI
}


/**
* 跳转安装
*/
fun Context.installApk(apkPath: String?) {

if (apkPath.isNullOrEmpty())return

val intent = Intent(Intent.ACTION_VIEW)
val apkFile = File(apkPath)

// android 7.0 fileprovider 适配
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val contentUri = FileProvider.getUriForFile(this, this.packageName + ".fileprovider", apkFile)
intent.setDataAndType(contentUri, "application/vnd.android.package-archive")
} else {
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive")
}

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
this.startActivity(intent)
}


63 changes: 63 additions & 0 deletions updateapputils/src/main/java/extension/CoreKtx.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package extension

import android.app.ActivityManager
import android.content.Context
import android.os.Build
import android.support.v4.content.ContextCompat
import android.util.Log
import android.view.View
import update.UpdateAppUtils
import util.GlobalContextProvider
import kotlin.system.exitProcess

/**
* desc: 扩展
* author: teprinc
* iple on 2020/3/27.
*/

/**
* 全局context
*/
val globalContext by lazy { GlobalContextProvider.mContext }


/**
* 打印日志
*/
fun log(content: String?) = UpdateAppUtils.updateInfo.config.isDebug.yes {
Log.e("[UpdateAppUtils]", content ?: "")
}

/**
* 获取color
*/
fun color(color: Int) = if (globalContext == null) 0 else ContextCompat.getColor(globalContext!!, color)

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

/**
* view 显示隐藏
*/
fun View.visibleOrGone(show: Boolean) {
if (show) {
this.visibility = View.VISIBLE
} else {
this.visibility = View.GONE
}
}

/**
* 退出app
*/
fun exitApp() {
val manager = globalContext!!.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
manager.appTasks.forEach { it.finishAndRemoveTask() }
} else {
exitProcess(0)
}
}
23 changes: 23 additions & 0 deletions updateapputils/src/main/java/extension/StringKtx.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package extension

import java.io.File

/**
* desc: string 相关扩展
* author: teprinciple on 2020/3/27.
*/

/**
* 根据文件路径删除文件
*/
fun String?.deleteFile() {
kotlin.runCatching {
val file = File(this ?: "")
(file.isFile).yes {
file.delete()
log("删除成功")
}
}.onFailure {
log(it.message)
}
}
16 changes: 0 additions & 16 deletions updateapputils/src/main/java/extension/View.kt

This file was deleted.

14 changes: 7 additions & 7 deletions updateapputils/src/main/java/model/UiConfig.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package model

import com.teprinciple.updateapputils.R
import constacne.UiType
import util.GlobalContextProvider
import constant.UiType
import extension.string

/**
* desc: UiConfig UI 配置
Expand All @@ -27,18 +27,18 @@ data class UiConfig(
var updateBtnBgRes: Int? = null,
var updateBtnTextColor: Int? = null,
var updateBtnTextSize: Float? = null,
var updateBtnText: CharSequence = GlobalContextProvider.getGlobalContext().getString(R.string.update_now),
var updateBtnText: CharSequence = string(R.string.update_now),
// 取消按钮相关设置
var cancelBtnBgColor: Int? = null,
var cancelBtnBgRes: Int? = null,
var cancelBtnTextColor: Int? = null,
var cancelBtnTextSize: Float? = null,
var cancelBtnText: CharSequence = GlobalContextProvider.getGlobalContext().getString(R.string.update_cancel),
var cancelBtnText: CharSequence = string(R.string.update_cancel),

// 开始下载时的Toast提示文字
var downloadingToastText: CharSequence = GlobalContextProvider.getGlobalContext().getString(R.string.toast_download_apk),
var downloadingToastText: CharSequence = string(R.string.toast_download_apk),
// 下载中 下载按钮以及通知栏标题前缀,进度自动拼接在后面
var downloadingBtnText: CharSequence = GlobalContextProvider.getGlobalContext().getString(R.string.downloading),
var downloadingBtnText: CharSequence = string(R.string.downloading),
// 下载出错时,下载按钮及通知栏标题
var downloadFailText: CharSequence = GlobalContextProvider.getGlobalContext().getString(R.string.download_fail)
var downloadFailText: CharSequence = string(R.string.download_fail)
)
2 changes: 1 addition & 1 deletion updateapputils/src/main/java/model/UpdateConfig.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package model

import constacne.DownLoadBy
import constant.DownLoadBy

data class UpdateConfig(
var isDebug: Boolean = true, // 是否是调试模式,调试模式会输出日志
Expand Down
6 changes: 3 additions & 3 deletions updateapputils/src/main/java/model/UpdateInfo.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package model

import com.teprinciple.updateapputils.R
import util.GlobalContextProvider
import extension.string

/**
* desc: UpdateInfo
Expand All @@ -10,9 +10,9 @@ import util.GlobalContextProvider
*/
internal data class UpdateInfo(
// 更新标题
var updateTitle: CharSequence = GlobalContextProvider.getGlobalContext().getString(R.string.update_title),
var updateTitle: CharSequence = string(R.string.update_title),
// 更新内容
var updateContent: CharSequence = GlobalContextProvider.getGlobalContext().getString(R.string.update_content),
var updateContent: CharSequence = string(R.string.update_content),
// apk 下载地址
var apkUrl: String = "",
// 更新配置
Expand Down
Loading

0 comments on commit 6da3afc

Please sign in to comment.