Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make app F-Droid ready #212

Merged
merged 18 commits into from
Jan 2, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ android {
}

}
flavorDimensions "releaseChannel"
productFlavors {
play {
dimension "releaseChannel"
}
fdroid {
dimension "releaseChannel"
}
}
kotlinOptions {
jvmTarget = '1.8'
}
Expand Down Expand Up @@ -121,7 +130,7 @@ dependencies {
implementation "androidx.viewpager2:viewpager2:1.1.0-alpha01"

implementation 'com.squareup.okhttp3:okhttp:4.9.0'
implementation 'com.google.android.play:core:1.9.0'//for new version updater
playImplementation 'com.google.android.play:core:1.9.0'//for new version updater

// For go-tun2socks
implementation project(":tun2socks")
Expand Down
84 changes: 84 additions & 0 deletions app/src/main/java/com/celzero/bravedns/NonStoreAppUpdater.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2020 RethinkDNS and its authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.celzero.bravedns

import android.app.Activity
import android.util.Log
import com.celzero.bravedns.service.AppUpdater
import com.celzero.bravedns.service.PersistentState
import com.celzero.bravedns.util.Constants
import okhttp3.*
import org.json.JSONObject
import java.io.IOException

class NonStoreAppUpdater(val baseURL:String, private val persistentState: PersistentState):AppUpdater {
private val LOG_TAG = "${Constants.LOG_TAG}/NonStoreAppUpdater"

override fun checkForAppUpdate(isUserInitiated: Boolean, activity: Activity, listener: AppUpdater.InstallStateListener) {
Log.d(LOG_TAG, "Beginning update check.")
val url = baseURL + BuildConfig.VERSION_CODE

val client = OkHttpClient()
val request = Request.Builder()
.url(url)
.build()

client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
Log.d(LOG_TAG, "onFailure - ${call.isCanceled()}, ${call.isExecuted()}")
if(isUserInitiated) {
listener.onUpdateCheckFailed()
}
call.cancel()
}

override fun onResponse(call: Call, response: Response) {
Log.d(LOG_TAG, "onResponse")
try {
val stringResponse = response.body!!.string()
//creating json object
val jsonObject = JSONObject(stringResponse)
val responseVersion = jsonObject.getInt("version")
val updateValue = jsonObject.getBoolean("update")
val latestVersion = jsonObject.getInt("latest")
persistentState.lastAppUpdateCheck = System.currentTimeMillis() // FIXME move to NTP
Log.i(Constants.LOG_TAG, "Server response for the new version download is true, version number- $latestVersion")
if (responseVersion == 1) {
if (updateValue) {
listener.onUpdateAvailable()
} else {
if(isUserInitiated) listener.onUpToDate()
}
}
response.close()
client.connectionPool.evictAll()
} catch (e: Exception) {
if (isUserInitiated) {
listener.onUpdateCheckFailed()
}
}
}
})
}

override fun completeUpdate() {
/* no-op */
}

override fun unregisterListener(listener: AppUpdater.InstallStateListener) {
/* no-op */
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.celzero.bravedns
import android.content.ContentResolver
import com.celzero.bravedns.data.DataModule
import com.celzero.bravedns.database.DatabaseModule
import com.celzero.bravedns.service.AppUpdater
import com.celzero.bravedns.service.ServiceModule
import com.celzero.bravedns.util.Constants
import com.celzero.bravedns.viewmodel.ViewModelModule
import org.koin.android.ext.koin.androidContext
import org.koin.core.module.Module
Expand All @@ -27,6 +29,10 @@ import org.koin.dsl.module
private val RootModule = module {
single<ContentResolver> { androidContext().contentResolver }
}
private val updaterModule = module {
single { NonStoreAppUpdater(Constants.APP_DOWNLOAD_AVAILABLE_CHECK, get())}
single<AppUpdater> { get<NonStoreAppUpdater>() }
}

val AppModules:List<Module> by lazy {
mutableListOf<Module>().apply {
Expand All @@ -36,5 +42,6 @@ val AppModules:List<Module> by lazy {
addAll(ViewModelModule.modules)
addAll(DataModule.modules)
addAll(ServiceModule.modules)
add(updaterModule)
}
}
35 changes: 35 additions & 0 deletions app/src/main/java/com/celzero/bravedns/service/AppUpdater.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2020 RethinkDNS and its authors
Ch4t4r marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.celzero.bravedns.service

import android.app.Activity

interface AppUpdater {
fun checkForAppUpdate(isUserInitiated:Boolean, activity: Activity, listener:InstallStateListener)
fun completeUpdate()
fun unregisterListener(listener: InstallStateListener)

interface InstallStateListener {
fun onStateUpdate(state:InstallState)
fun onUpdateAvailable()
fun onUpToDate()
fun onUpdateCheckFailed()
}
data class InstallState(val status:InstallStatus)
enum class InstallStatus {
CANCELED, DOWNLOADED, DOWNLOADING, FAILED, INSTALLED, INSTALLING, PENDING, UNKNOWN
}
}
6 changes: 5 additions & 1 deletion app/src/main/java/com/celzero/bravedns/ui/AboutFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import com.celzero.bravedns.R
import com.celzero.bravedns.service.AppUpdater
import org.koin.android.ext.android.inject


class AboutFragment : Fragment(), View.OnClickListener {
Expand All @@ -43,6 +45,8 @@ class AboutFragment : Fragment(), View.OnClickListener {
private lateinit var appUpdateTxt : TextView
private lateinit var whatsNewTxt : TextView

private val appUpdater by inject<AppUpdater>()

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down Expand Up @@ -126,7 +130,7 @@ class AboutFragment : Fragment(), View.OnClickListener {
startActivity(intent)
}
view == appUpdateTxt ->{
(requireContext() as HomeScreenActivity).checkForAppUpdate(true)
(requireContext() as HomeScreenActivity).checkForUpdate(true)
}
view == whatsNewTxt ->{
showNewFeaturesDialog()
Expand Down
Loading