-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adrian Arencibia Herrera
committed
Jan 22, 2019
1 parent
fc3a6ca
commit 064669d
Showing
21 changed files
with
596 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
apklisupdate/src/androidTest/java/cu/uci/apklisupdate/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
apklisupdate/src/main/java/cu/uci/apklisupdate/ApklisUpdate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cu.uci.apklisupdate | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.os.Build | ||
import io.reactivex.schedulers.Schedulers | ||
|
||
|
||
object ApklisUpdate { | ||
|
||
@SuppressLint("CheckResult") | ||
fun hasAppUpdate(context: Context, callback: UpdateCallback) { | ||
|
||
LastReleaseClient.instance() | ||
.lastRelease(context.packageName) | ||
.subscribeOn(Schedulers.newThread()) | ||
.subscribe({ | ||
|
||
val manager = context.packageManager | ||
val info = manager.getPackageInfo( | ||
context.packageName, 0 | ||
) | ||
|
||
val versionCode: Long = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { | ||
info.longVersionCode | ||
} else { | ||
info.versionCode.toLong() | ||
} | ||
if (versionCode < it.last_release.version_code && info.versionName != it.last_release.version_name) | ||
callback.onNewUpdate(it) | ||
else | ||
callback.onOldUpdate(it) | ||
}, { | ||
it.printStackTrace() | ||
callback.onError(it) | ||
}) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
apklisupdate/src/main/java/cu/uci/apklisupdate/LastReleaseClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package cu.uci.apklisupdate | ||
|
||
import cu.uci.apklisupdate.base.RestClient | ||
import cu.uci.apklisupdate.model.AppUpdateInfo | ||
import io.reactivex.Single | ||
|
||
/** | ||
* Created by Adrian Arencibia Herrera on 5/29/18. | ||
* Email: adrian011494@gmail.com | ||
*/ | ||
|
||
class LastReleaseClient : RestClient<LastReleaseApi>(LastReleaseApi::class.java) { | ||
|
||
fun lastRelease(appPackage: String): Single<AppUpdateInfo> { | ||
return mApi.lastRelease(appPackage) | ||
} | ||
|
||
companion object { | ||
fun instance() = LastReleaseClient() | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
apklisupdate/src/main/java/cu/uci/apklisupdate/UpdateCallback.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package cu.uci.apklisupdate | ||
|
||
import cu.uci.apklisupdate.model.AppUpdateInfo | ||
import java.lang.Exception | ||
|
||
interface UpdateCallback { | ||
fun onNewUpdate(appUpdateInfo: AppUpdateInfo) | ||
fun onOldUpdate(appUpdateInfo: AppUpdateInfo) | ||
fun onError(e: Throwable) | ||
} |
64 changes: 64 additions & 0 deletions
64
apklisupdate/src/main/java/cu/uci/apklisupdate/base/RestClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cu.uci.apklisupdate.base | ||
|
||
import cu.uci.apklisupdate.BuildConfig | ||
import okhttp3.Interceptor | ||
import okhttp3.OkHttpClient | ||
import retrofit2.Retrofit | ||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
import java.util.concurrent.TimeUnit | ||
|
||
abstract class RestClient<T>( | ||
type: Class<T> | ||
) { | ||
|
||
|
||
companion object { | ||
|
||
val LOG_TAG: String = this::class.java.simpleName | ||
|
||
|
||
private const val CONNECTION_TIMEOUT_MS = 30000L | ||
private const val READ_TIMEOUT_MS = 30000L | ||
private const val WRITE_TIMEOUT_MS = 30000L | ||
} | ||
|
||
var mApi: T | ||
|
||
// http client | ||
private var mOkHttpClient: OkHttpClient | ||
|
||
private val BASE_URL: String = "https://www.apklis.cu/" | ||
|
||
init { | ||
|
||
|
||
val userAgentInterceptor = Interceptor { chain -> | ||
val original = chain.request() | ||
val requestBuilder = original.newBuilder() | ||
.header("User-Agent", BuildConfig.APPLICATION_ID) | ||
val request = requestBuilder.build() | ||
return@Interceptor chain.proceed(request) | ||
} | ||
|
||
mOkHttpClient = OkHttpClient().newBuilder() | ||
.addInterceptor(userAgentInterceptor) | ||
.connectTimeout(CONNECTION_TIMEOUT_MS, TimeUnit.MILLISECONDS) | ||
.readTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS) | ||
.writeTimeout(WRITE_TIMEOUT_MS, TimeUnit.MILLISECONDS) | ||
|
||
.build() | ||
|
||
val retrofit = Retrofit.Builder() | ||
.client(mOkHttpClient) | ||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
.baseUrl(BASE_URL) | ||
|
||
.build() | ||
|
||
mApi = retrofit.create(type) | ||
} | ||
|
||
|
||
} |
75 changes: 75 additions & 0 deletions
75
apklisupdate/src/main/java/cu/uci/apklisupdate/view/ApklisUpdateFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cu.uci.apklisupdate.view; | ||
|
||
import android.content.Intent | ||
import android.graphics.Color | ||
import android.graphics.drawable.ColorDrawable | ||
import android.graphics.drawable.Drawable | ||
import android.net.Uri | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.fragment.app.Fragment | ||
import com.squareup.picasso.Picasso | ||
import cu.uci.apklisupdate.R | ||
import cu.uci.apklisupdate.model.AppUpdateInfo | ||
import kotlinx.android.synthetic.main.apklis_fragment_update.* | ||
|
||
class ApklisUpdateFragment : Fragment() { | ||
|
||
fun layout(): Int = R.layout.apklis_fragment_update | ||
|
||
lateinit var updateInfo: AppUpdateInfo | ||
|
||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||
return inflater.inflate(layout(), container, false) | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
changelog.setHtml("${context?.getString(R.string.changelog)}\n${updateInfo.last_release.changelog}") | ||
version.text = updateInfo.last_release.version_name | ||
title.text = updateInfo.name | ||
fromApklis.setOnClickListener { | ||
val i = Intent(Intent.ACTION_VIEW) | ||
i.data = Uri.parse("https://www.apklis.cu/es/application/${updateInfo.package_name}/latest") | ||
requireContext().startActivity(Intent.createChooser(i, "")) | ||
} | ||
|
||
download.setOnClickListener { | ||
val i = Intent(Intent.ACTION_VIEW) | ||
i.data = Uri.parse(updateInfo.last_release.apk_file) | ||
requireContext().startActivity(Intent.createChooser(i, "")) | ||
} | ||
|
||
Picasso.get().load(updateInfo.last_release.icon).into(logo) | ||
|
||
getView()?.setBackgroundDrawable(background) | ||
|
||
|
||
fromApklis.setTextColor(actionsColor) | ||
download.setTextColor(actionsColor) | ||
} | ||
|
||
|
||
private var background: Drawable = ColorDrawable(Color.WHITE) | ||
|
||
private var actionsColor: Int = 0 | ||
|
||
companion object { | ||
|
||
fun newInstance(updateInfo: AppUpdateInfo, background: Drawable = ColorDrawable(Color.WHITE), actionsColor: Int = Color.BLACK): ApklisUpdateFragment { | ||
return ApklisUpdateFragment().apply { | ||
this.updateInfo = updateInfo | ||
this.background = background | ||
this.actionsColor = actionsColor | ||
} | ||
} | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
apklisupdate/src/main/java/cu/uci/apklisupdate/view/UpdateFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package cu.uci.apklisupdate.view; | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.annotation.ColorRes | ||
import androidx.annotation.DrawableRes | ||
import androidx.fragment.app.Fragment | ||
import cu.uci.apklisupdate.R | ||
import cu.uci.apklisupdate.model.AppUpdateInfo | ||
import kotlinx.android.synthetic.main.fragment_update.* | ||
|
||
class UpdateFragment : Fragment() { | ||
|
||
fun layout(): Int = R.layout.fragment_update | ||
|
||
lateinit var updateInfo: AppUpdateInfo | ||
|
||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||
return inflater.inflate(layout(), container, false) | ||
} | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
changelog.setHtml("${context?.getString(R.string.changelog)}\n${updateInfo.last_release.changelog}") | ||
version.text = updateInfo.last_release.changelog | ||
|
||
fromApklis.setOnClickListener { | ||
val i = Intent(Intent.ACTION_VIEW) | ||
i.data = Uri.parse("https://www.apklis.cu/es/application/${updateInfo.package_name}/latest") | ||
requireContext().startActivity(Intent.createChooser(i, "")) | ||
} | ||
|
||
download.setOnClickListener { | ||
val i = Intent(Intent.ACTION_VIEW) | ||
i.data = Uri.parse(updateInfo.last_release.apk_file) | ||
requireContext().startActivity(Intent.createChooser(i, "")) | ||
} | ||
} | ||
|
||
|
||
private var background: Int = 0 | ||
|
||
private var actionsColor: Int = 0 | ||
|
||
companion object { | ||
|
||
fun newInstance(updateInfo: AppUpdateInfo, @DrawableRes background: Int = android.R.color.white, @ColorRes actionsColor: Int = android.R.color.black): UpdateFragment { | ||
return UpdateFragment().apply { | ||
this.updateInfo = updateInfo | ||
this.background = background | ||
this.actionsColor = actionsColor | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.