Skip to content

Commit

Permalink
1.增加apk安装工具
Browse files Browse the repository at this point in the history
2.下载文件增加缓存机制(如果本地已经有文件了,直接使用本地文件,反之才远程下载文件)
  • Loading branch information
tli2 committed Nov 20, 2020
1 parent 6be1baa commit 126b8c9
Show file tree
Hide file tree
Showing 26 changed files with 295 additions and 80 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ dependencies {
//图片加载框架
implementation "com.github.bumptech.glide:glide:$glide_version"
kapt "com.github.bumptech.glide:compiler:$glide_version"
//权限
implementation "com.github.tbruyelle:rxpermissions:$rxpermissions_version"
}
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity> <!-- 适配小米(xiaomi)刘海屏 -->
</activity>
<!-- 适配小米(xiaomi)刘海屏 -->
<activity android:name=".mvp.child.ChildActivity" />
<activity android:name=".mvp.apk.view.InstallApkActivity"/>

<meta-data
android:name="android.notch_support"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/catchpig/kotlin_mvp/app/KotlinMvpApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.catchpig.kotlin_mvp.app
import android.app.Application
import com.catchpig.kotlin_mvp.R
import com.catchpig.kotlin_mvp.di.networkModule
import com.catchpig.kotlin_mvp.di.mainModule
import com.catchpig.kotlin_mvp.di.scopeModule
import com.catchpig.mvp.di.appModule
import com.catchpig.mvp.di.downloadModule
import com.scwang.smart.refresh.footer.ClassicsFooter
Expand All @@ -26,7 +26,7 @@ class KotlinMvpApp:Application(){
printLogger(Level.DEBUG)
androidLogger(Level.DEBUG)
androidContext(this@KotlinMvpApp)
modules(appModule, downloadModule, networkModule, mainModule)
modules(appModule, downloadModule, networkModule, scopeModule)
}
}
init {
Expand Down
24 changes: 0 additions & 24 deletions app/src/main/java/com/catchpig/kotlin_mvp/di/MainModule.kt

This file was deleted.

39 changes: 39 additions & 0 deletions app/src/main/java/com/catchpig/kotlin_mvp/di/ScopeModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.catchpig.kotlin_mvp.di

import com.catchpig.kotlin_mvp.mvp.apk.InstallApkContract
import com.catchpig.kotlin_mvp.mvp.apk.model.InstallApkModel
import com.catchpig.kotlin_mvp.mvp.apk.presenter.InstallApkPresenter
import com.catchpig.kotlin_mvp.mvp.apk.view.InstallApkActivity
import com.catchpig.kotlin_mvp.mvp.main.MainContract
import com.catchpig.kotlin_mvp.mvp.main.model.MainModel
import com.catchpig.kotlin_mvp.mvp.main.presenter.MainPresenter
import com.catchpig.kotlin_mvp.mvp.main.view.MainActivity
import org.koin.core.qualifier.named
import org.koin.dsl.bind
import org.koin.dsl.module

/**
* @author catchpig
* @date 2019/8/18 00:18
*/
val scopeModule = module {
scope<MainActivity> {
scoped { (view:MainContract.View)->
MainPresenter(view,get())
} bind MainContract.Presenter::class

scoped {
MainModel(get())
} bind MainContract.Model::class
}

scope<InstallApkActivity> {
scoped { (view:InstallApkContract.View)->
InstallApkPresenter(view, get())
} bind InstallApkContract.Presenter::class

scoped {
InstallApkModel(get())
} bind InstallApkContract.Model::class
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.catchpig.kotlin_mvp.mvp.apk

import com.catchpig.mvp.base.BaseContract
import com.catchpig.mvp.bean.DownloadInfo
import com.catchpig.mvp.network.listener.DownloadCallback

/**
*
* @author catchpig
* @date 2020/11/20 15:52
*/
interface InstallApkContract {
interface View:BaseContract.View{
/**
* 设置进度
* @param progress Int
*/
fun setDownloadProgress(progress:Int)
}
interface Presenter:BaseContract.Presenter{
fun download()
}
interface Model{
/**
* 下载
* @param downloadInfo DownloadInfo
* @param downloadCallback DownloadCallback
*/
fun download(downloadInfo: DownloadInfo, downloadCallback: DownloadCallback)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.catchpig.kotlin_mvp.mvp.apk.model

import com.catchpig.kotlin_mvp.mvp.apk.InstallApkContract
import com.catchpig.mvp.bean.DownloadInfo
import com.catchpig.mvp.network.download.DownloadManager
import com.catchpig.mvp.network.listener.DownloadCallback

/**
*
* @author catchpig
* @date 2020/11/20 15:51
*/
class InstallApkModel(private val downloadManager: DownloadManager):InstallApkContract.Model {
override fun download(downloadInfo: DownloadInfo, downloadCallback: DownloadCallback) {
downloadManager.download(downloadInfo,downloadCallback)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.catchpig.kotlin_mvp.mvp.apk.presenter

import com.catchpig.kotlin_mvp.mvp.apk.InstallApkContract
import com.catchpig.mvp.base.BasePresenter
import com.catchpig.mvp.bean.DownloadInfo
import com.catchpig.mvp.network.listener.DownloadCallback
import com.catchpig.utils.ext.installApk

/**
*
* @author catchpig
* @date 2020/11/20 15:51
*/
class InstallApkPresenter(private val view: InstallApkContract.View,private val model: InstallApkContract.Model):BasePresenter(),InstallApkContract.Presenter{
override fun download() {
val downloadInfo = DownloadInfo("https://wanandroid.com/","blogimgs/2d120094-e1ee-47fb-a155-6eb4ca49d01f.apk")
model.download(downloadInfo,object : DownloadCallback {
override fun onStart() {

}

override fun onSuccess(path: String) {
view.activity().installApk(path)
}

override fun onComplete() {
}

override fun onProgress(readLength: Long, countLength: Long) {
view.setDownloadProgress((readLength*100/countLength).toInt())
}

override fun onError(t: Throwable) {
println(t.message)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.catchpig.kotlin_mvp.mvp.apk.view

import android.Manifest
import com.catchpig.annotation.Title
import com.catchpig.kotlin_mvp.R
import com.catchpig.kotlin_mvp.mvp.apk.InstallApkContract
import com.catchpig.mvp.base.activity.BasePresenterActivity
import com.tbruyelle.rxpermissions2.RxPermissions
import kotlinx.android.synthetic.main.activity_install_apk.*
import org.koin.core.parameter.parametersOf
import org.koin.core.scope.inject

/**
*
* @author catchpig
* @date 2020/11/20 15:50
*/
@Title(R.string.download_install_apk)
class InstallApkActivity:BasePresenterActivity<InstallApkContract.Presenter>(),InstallApkContract.View {
override val mPresenter: InstallApkContract.Presenter by inject{ parametersOf(this) }
private val rxPermissions by lazy { RxPermissions(this) }
override fun initParam() {

}

override fun initView() {
rxPermissions
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe {
if (it) {
mPresenter.download()
}
}
}

override fun layoutId(): Int {
return R.layout.activity_install_apk
}

override fun setDownloadProgress(progress: Int) {
progressBar.progress = progress
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,5 @@ interface MainContract {
}
interface Model{
fun banner():Flowable<Result<Any>>

fun download(downloadInfo: DownloadInfo, downloadCallback: DownloadCallback)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@ import com.catchpig.mvp.network.listener.DownloadCallback
import com.catchpig.mvp.network.download.DownloadManager
import io.reactivex.Flowable

class MainModel(private val wanAndroidService: WanAndroidService, private val downloadManager: DownloadManager):MainContract.Model {
class MainModel(private val wanAndroidService: WanAndroidService):MainContract.Model {
override fun banner(): Flowable<Result<Any>> {
return wanAndroidService.banner()
}

override fun download(downloadInfo: DownloadInfo,downloadCallback: DownloadCallback) {
downloadManager.download(downloadInfo,downloadCallback)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ package com.catchpig.kotlin_mvp.mvp.main.presenter

import com.catchpig.annotation.TimeLog
import com.catchpig.kotlin_mvp.mvp.main.MainContract
import com.catchpig.kotlin_mvp.network.Callback
import com.catchpig.kotlin_mvp.network.Result
import com.catchpig.mvp.base.BasePresenter
import com.catchpig.mvp.bean.DownloadInfo
import com.catchpig.mvp.network.listener.DownloadCallback

/**
* @author catchpig
Expand All @@ -15,31 +11,10 @@ import com.catchpig.mvp.network.listener.DownloadCallback
class MainPresenter @TimeLog constructor(private val view:MainContract.View,private val model:MainContract.Model): BasePresenter(),MainContract.Presenter {
override fun onCreate() {
super.onCreate()
execute(model.banner(), object :Callback<Result<Any>>(){
override fun onNext(result: Result<Any>?) {
view.toast(result!!.errorCode.toString())
}
})
val downloadInfo = DownloadInfo("https://ss1.bdstatic.com/","70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2583035764,1571388243&fm=26&gp=0.jpg")
model.download(downloadInfo,object : DownloadCallback {
override fun onStart() {

}

override fun onSuccess(path: String) {
println(path)
}

override fun onComplete() {
}

override fun onProgress(readLength: Long, countLength: Long) {
}

override fun onError(t: Throwable) {
println(t.message)
}

})
// execute(model.banner(), object :Callback<Result<Any>>(){
// override fun onNext(result: Result<Any>?) {
// view.toast(result!!.errorCode.toString())
// }
// })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.catchpig.annotation.MethodLog
import com.catchpig.annotation.StatusBar
import com.catchpig.annotation.enums.LEVEL
import com.catchpig.kotlin_mvp.R
import com.catchpig.kotlin_mvp.mvp.apk.view.InstallApkActivity
import com.catchpig.kotlin_mvp.mvp.child.ChildActivity
import com.catchpig.kotlin_mvp.mvp.fullscreen.FullScreenActivity
import com.catchpig.kotlin_mvp.mvp.main.MainContract
Expand All @@ -26,6 +27,7 @@ class MainActivity : BasePresenterActivity<MainContract.Presenter>(),MainContrac
@MethodLog(LEVEL.I)
override fun initView() {


}

override fun layoutId(): Int {
Expand All @@ -48,6 +50,9 @@ class MainActivity : BasePresenterActivity<MainContract.Presenter>(),MainContrac
R.id.recycle -> {
startKtActivity<RecycleActivity>()
}
R.id.installApk ->{
startKtActivity<InstallApkActivity>()
}
else -> {
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.catchpig.kotlin_mvp.mvp.transparent

import com.catchpig.kotlin_mvp.R
import com.catchpig.annotation.StatusBar
import com.catchpig.kotlin_mvp.R
import com.catchpig.mvp.base.activity.BaseActivity

@StatusBar(transparent = true)
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout/activity_install_apk.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="10dp"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"/>
</LinearLayout>
8 changes: 8 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@
android:clickable="true"
android:onClick="openChild"
android:text="Recycle列表显示"/>
<Button
android:id="@+id/installApk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:clickable="true"
android:onClick="openChild"
android:text="下载并安装apk"/>
</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<string name="child_title">子页面</string>
<string name="more">更多</string>
<string name="detail">详情</string>
<string name="download_install_apk">下载并安装apk</string>
</resources>
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ buildscript {
aspectj_version = '1.9.6'
auto_service_version = '1.0-rc6'
kotlinpoet_version = '1.4.0'
rxpermissions_version = "0.11"
}
repositories {
google()
Expand Down
Loading

0 comments on commit 126b8c9

Please sign in to comment.