Skip to content

Commit 2e4f33a

Browse files
author
Oleg Smirnov
committed
version 2.2.3
1 parent d75ab6b commit 2e4f33a

26 files changed

Lines changed: 282 additions & 177 deletions

README-putyavka.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

dependencies.gradle

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,43 @@
1+
/*
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2019 vk.com
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
125
ext.sdkVersions = [
2-
code : '15',
3-
name : '2.2.1',
26+
code : '17',
27+
name : '2.2.3',
428

529
minSdk : 16,
630
targetSdk : 28,
731
compileSdk : '29',
8-
buildTools : '29.0.2',
32+
buildTools : '29.0.3',
933

10-
kotlin : '1.3.61',
11-
androidX : '1.0.0',
34+
kotlin : '1.3.71',
35+
androidX : '1.1.0',
1236
okHttp : '3.12.1',
1337

1438
picasso : '2.71828',
1539

16-
androidGradlePlugin : '3.5.0',
40+
androidGradlePlugin : '3.6.0',
1741
]
1842

1943
ext.sdkLibraries = [
@@ -27,8 +51,7 @@ ext.sdkLibraries = [
2751
ext.sdkLibrariesSupport = [
2852
appCompat : "androidx.appcompat:appcompat:$sdkVersions.androidX",
2953
recyclerView : "androidx.recyclerview:recyclerview:$sdkVersions.androidX",
30-
supportV4 : "androidx.legacy:legacy-support-v4:$sdkVersions.androidX",
31-
support : "androidx.legacy:legacy-support-v13:$sdkVersions.androidX",
54+
collection : "androidx.collection:collection:$sdkVersions.androidX",
3255
]
3356

3457
ext.sdkGradlePlugins = [

samples/app/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ dependencies {
5757
implementation sdkLibraries.kotlin
5858
implementation sdkLibrariesSupport.recyclerView
5959
implementation sdkLibrariesSupport.appCompat
60-
implementation sdkLibrariesSupport.support
6160
implementation (sdkLibraries.picasso) { transitive = false }
6261
implementation sdkLibraries.okHttp
6362

samples/app/src/main/java/com/vk/sdk/sample/UserActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import androidx.recyclerview.widget.RecyclerView
3838
import com.squareup.picasso.Picasso
3939
import com.vk.api.sdk.VK
4040
import com.vk.api.sdk.VKApiCallback
41-
import com.vk.api.sdk.exceptions.VKApiExecutionException
4241
import com.vk.sdk.sample.models.VKUser
4342
import com.vk.sdk.sample.requests.VKFriendsRequest
4443
import com.vk.sdk.sample.requests.VKUsersCommand
@@ -139,6 +138,7 @@ class UserActivity: Activity() {
139138
}
140139
VK.execute(VKWallPostCommand(messageField.text.toString(), photos), object: VKApiCallback<Int> {
141140
override fun success(result: Int) {
141+
// TODO Use ToastUtils
142142
Toast.makeText(this@UserActivity, R.string.wall_ok, Toast.LENGTH_SHORT).show()
143143
}
144144

vk-sdk-core/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ apply plugin: 'com.android.library'
2626
apply plugin: 'kotlin-android'
2727
apply from: '../dependencies.gradle'
2828

29-
ext.POM_GROUP_ID = "com.vk"
29+
version = sdkVersions.name
30+
group = 'com.vk'
3031
ext.POM_ARTIFACT_ID = sdkPublish.POM_ARTIFACT_ID
31-
ext.POM_VERSION = sdkVersions.name
3232

3333
android {
3434
compileSdkVersion "$sdkVersions.compileSdk".toInteger()
@@ -55,7 +55,7 @@ dependencies {
5555
implementation sdkLibraries.kotlin
5656
implementation sdkLibraries.okHttp
5757
implementation sdkLibraries.okHttpLoggingInterceptor
58-
implementation sdkLibrariesSupport.support
58+
implementation sdkLibrariesSupport.collection
5959
}
6060

6161
apply from: '../publish.gradle'

vk-sdk-core/src/main/java/com/vk/api/sdk/VK.kt

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@ import java.io.IOException
4343
* This is an entry point of VK SDK
4444
*/
4545
object VK {
46+
private const val SDK_APP_ID = "com_vk_sdk_AppId"
47+
4648
@SuppressLint("StaticFieldLeak")
4749
private lateinit var config: VKApiConfig
4850
internal lateinit var apiManager: VKApiManager
49-
50-
private val authManager: VKAuthManager = VKAuthManager()
51+
private lateinit var authManager: VKAuthManager
5152

5253
private val tokenExpiredHandlers = ArrayList<VKTokenExpiredHandler>()
5354

55+
private var cachedAppId = 0
56+
5457
/**
5558
* This method initializes VK SDK with your custom config
5659
* @param config contains all required data for vk api
@@ -59,7 +62,8 @@ object VK {
5962
fun setConfig(config: VKApiConfig) {
6063
this.config = config
6164
apiManager = VKApiManager(config)
62-
authManager.getCurrentToken(config.context)?.let {
65+
authManager = VKAuthManager(config.keyValueStorage)
66+
authManager.getCurrentToken()?.let {
6367
apiManager.setCredentials(it.accessToken, it.secret)
6468
}
6569
}
@@ -78,35 +82,54 @@ object VK {
7882

7983
/**
8084
* This method is used to set new credentials for future requests. E.g. if you login via your own lib
85+
*
8186
* @param userId userId of saving user
8287
* @param accessToken accessToken for future requests
8388
* @param secret secret for future requests
84-
* @param saveAccessTokenToPrefs create access token info and save it to prefs. If you pass {@code false},
85-
* you will not able to use sdk execute methods
89+
* @param saveAccessTokenToStorage create access token info and save it to keyValueStorage provided
90+
* by [VKApiConfig] ([VKApiConfig.keyValueStorage]).
91+
* If you pass {@code false} you will not able to use sdk execute methods!
8692
*/
8793
@JvmStatic
88-
fun setCredentials(context: Context, userId: Int, accessToken: String, secret: String?, saveAccessTokenToPrefs: Boolean) {
89-
if (saveAccessTokenToPrefs) {
90-
VKAccessToken(userId, accessToken, secret).save(authManager.getPreferences(context))
94+
fun setCredentials(context: Context, userId: Int, accessToken: String, secret: String?, saveAccessTokenToStorage: Boolean = true) {
95+
if (saveAccessTokenToStorage) {
96+
VKAccessToken(userId, accessToken, secret).save(config.keyValueStorage)
9197
}
9298
apiManager.setCredentials(accessToken, secret)
9399
}
94100

101+
/**
102+
* Save access token to keyValueStorage provided by [VKApiConfig] ([VKApiConfig.keyValueStorage]).
103+
*
104+
* @param userId userId of saving user
105+
* @param accessToken accessToken for future requests
106+
* @param secret secret for future requests
107+
*/
108+
@JvmStatic
109+
fun saveAccessToken(context: Context, userId: Int, accessToken: String, secret: String?) {
110+
setCredentials(context, userId, accessToken, secret, true)
111+
}
112+
95113
/**
96114
* This method clears information about access token and cookies
97115
*/
98116
@JvmStatic
99117
fun logout() {
100-
authManager.logout(config.context)
118+
authManager.clearAccessToken()
101119
VKUtils.clearAllCookies(config.context)
102120
}
103121

104122
/**
105123
* This method checks if user is already logged in
106124
*/
107125
@JvmStatic
108-
fun isLoggedIn() = authManager.isLoggedIn(config.context)
126+
fun isLoggedIn() = authManager.isLoggedIn()
109127

128+
/**
129+
* This method returns userId of currently logged in user or 0 if there is no logged in user
130+
*/
131+
@JvmStatic
132+
fun getUserId() = authManager.getCurrentToken()?.userId ?: 0
110133

111134
/**
112135
* This method provide you an api version of current config
@@ -120,7 +143,7 @@ object VK {
120143
*/
121144
@JvmStatic
122145
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?, callback: VKAuthCallback): Boolean {
123-
val result = authManager.onActivityResult(requestCode, resultCode, data, callback, config.context)
146+
val result = authManager.onActivityResult(requestCode, resultCode, data, callback)
124147
if (result && isLoggedIn()) {
125148
trackVisitor()
126149
}
@@ -145,7 +168,7 @@ object VK {
145168
}
146169

147170
internal fun handleTokenExpired() {
148-
authManager.logout(config.context)
171+
authManager.clearAccessToken()
149172

150173
tokenExpiredHandlers.forEach {
151174
it.onTokenExpired()
@@ -192,10 +215,7 @@ object VK {
192215
*/
193216
@JvmStatic
194217
fun initialize(context: Context) {
195-
val appId = authManager.getAppId(context)
196-
if (appId == 0) {
197-
throw RuntimeException("<integer name=\"com_vk_sdk_AppId\">your_app_id</integer> is not found in your resources.xml")
198-
}
218+
val appId = getAppId(context)
199219
setConfig(VKApiConfig(
200220
context = context,
201221
appId = appId,
@@ -211,12 +231,27 @@ object VK {
211231
*/
212232
@JvmStatic
213233
fun getAppId(context: Context): Int {
214-
return authManager.getAppId(context)
234+
if (cachedAppId != 0) {
235+
return cachedAppId
236+
}
237+
238+
val resId = context.resources.getIdentifier(SDK_APP_ID, "integer", context.packageName)
239+
cachedAppId = try {
240+
context.resources.getInteger(resId)
241+
} catch (e: Exception) {
242+
0
243+
}
244+
245+
if (cachedAppId == 0) {
246+
throw RuntimeException("<integer name=\"com_vk_sdk_AppId\">your_app_id</integer> is not found in your resources.xml")
247+
}
248+
249+
return cachedAppId
215250
}
216251

217252
@JvmStatic
218253
fun clearAccessToken(context: Context) {
219-
authManager.getPreferences(context).edit().clear().apply()
254+
authManager.clearAccessToken()
220255
}
221256

222257
private fun trackVisitor() {

vk-sdk-core/src/main/java/com/vk/api/sdk/VKApiConfig.kt

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,36 @@ import com.vk.api.sdk.utils.log.Logger
3030
import java.util.concurrent.TimeUnit
3131

3232
/**
33-
* Configuration for VK SDK
34-
* By using VK.initialize(config) you can customize configuration
33+
* Configuration for VK SDK.
34+
* By using SuperappKitConfig.setApiConfig(config) you can customize configuration.
35+
* VKInitProvider calls VK.initialize() on start of application and provides default implementation
36+
* of VKApiConfig.
3537
*/
3638
@Suppress("ConvertSecondaryConstructorToPrimary")
37-
data class VKApiConfig(val context: Context,
38-
val appId: Int = 0,
39-
val validationHandler: VKApiValidationHandler?,
40-
val deviceId: Lazy<String> = lazy { "" },
41-
val version: String = DEFAULT_API_VERSION,
42-
val okHttpProvider: VKOkHttpProvider = VKOkHttpProvider.DefaultProvider(),
43-
val defaultTimeoutMs: Long = TimeUnit.SECONDS.toMillis(10),
44-
val postRequestsTimeout: Long = TimeUnit.MINUTES.toMillis(5),
45-
val logger: Logger = DefaultApiLogger(lazy { Logger.LogLevel.NONE }, "VKSdkApi"),
46-
val accessToken: Lazy<String> = lazy { "" },
47-
val secret: Lazy<String?> = lazy { null },
48-
val logFilterCredentials: Boolean = true,
49-
val callsPerSecondLimit: Int = 3,
50-
val httpApiHost: Lazy<String> = lazy { DEFAULT_API_DOMAIN },
51-
val lang: String = "en") {
39+
data class VKApiConfig(
40+
val context: Context,
41+
val appId: Int = 0,
42+
val validationHandler: VKApiValidationHandler?,
43+
val deviceId: Lazy<String> = lazy { "" },
44+
val version: String = DEFAULT_API_VERSION,
45+
val okHttpProvider: VKOkHttpProvider = VKOkHttpProvider.DefaultProvider(),
46+
val defaultTimeoutMs: Long = TimeUnit.SECONDS.toMillis(10),
47+
val postRequestsTimeout: Long = TimeUnit.MINUTES.toMillis(5),
48+
val logger: Logger = DefaultApiLogger(lazy { Logger.LogLevel.NONE }, "VKSdkApi"),
49+
val accessToken: Lazy<String> = lazy { "" },
50+
val secret: Lazy<String?> = lazy { null },
51+
val logFilterCredentials: Boolean = true,
52+
val debugCycleCalls: Lazy<Boolean> = lazy { false },
53+
val callsPerSecondLimit: Int = 3,
54+
val httpApiHost: Lazy<String> = lazy { DEFAULT_API_DOMAIN },
55+
val lang: String = "en",
56+
val keyValueStorage: VKKeyValueStorage = VKPreferencesKeyValueStorage(context)
57+
) {
5258

5359
companion object {
5460
const val DEFAULT_DOMAIN = "vk.com"
5561
const val DEFAULT_API_VERSION = "5.90"
5662
const val DEFAULT_API_DOMAIN = "api.vk.com"
63+
const val DEFAULT_OAUTH_DOMAIN = "oauth.vk.com"
5764
}
5865
}

vk-sdk-core/src/main/java/com/vk/api/sdk/VKApiManager.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ open class VKApiManager(val config: VKApiConfig) {
6969
var cc: ChainCall<T> = if (call.skipValidation) {
7070
chainCall
7171
} else {
72-
ValidationHandlerChainCall(this, call.retryCount, chainCall)
72+
createValidationHandlerChainCall(call.retryCount, chainCall)
7373
}
7474
cc = InvalidCredentialsObserverChainCall(this, cc)
7575
cc = TooManyRequestRetryChainCall(this, call.retryCount, cc)
@@ -88,13 +88,16 @@ open class VKApiManager(val config: VKApiConfig) {
8888
}
8989

9090
protected open fun <T> wrapCall(call: VKHttpPostCall, chainCall: ChainCall<T>): ChainCall<T> {
91-
var cc: ChainCall<T> = ValidationHandlerChainCall(this, call.retryCount, chainCall)
91+
var cc: ChainCall<T> = createValidationHandlerChainCall(call.retryCount, chainCall)
9292
if (call.retryCount > 0) {
9393
cc = InternalErrorRetryChainCall(this, call.retryCount, cc)
9494
}
9595
return cc
9696
}
9797

98+
private fun <T> createValidationHandlerChainCall(retryCount: Int, chainCall: ChainCall<T>) =
99+
ValidationHandlerChainCall(this, retryCount, chainCall)
100+
98101
@Throws(InterruptedException::class, IOException::class, VKApiException::class)
99102
protected open fun <T> executeWithExceptionAdjust(cc: ChainCall<T>) = cc.call(ChainArgs())!!
100103

vk-sdk-core/src/main/java/com/vk/api/sdk/VKApiValidationHandler.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
package com.vk.api.sdk
2626

27+
import com.vk.api.sdk.exceptions.VKApiExecutionException
2728
import java.util.concurrent.CountDownLatch
2829

2930
/**
@@ -42,12 +43,17 @@ interface VKApiValidationHandler {
4243
* This method will called, when confirmation from user required
4344
*/
4445
fun handleConfirm(confirmationText: String, cb: Callback<Boolean>)
46+
/**
47+
* This method will called, when no other method can handle exception
48+
*/
49+
@Throws(VKApiExecutionException::class)
50+
fun tryToHandleException(ex: VKApiExecutionException, apiManager: VKApiManager): Unit = throw ex
4551

46-
class Callback<T>(private val latch: CountDownLatch) {
52+
open class Callback<T>(val latch: CountDownLatch) {
4753
@Volatile var value: T? = null
4854

49-
fun cancel() = latch.countDown()
50-
fun submit(value: T) {
55+
open fun cancel() = latch.countDown()
56+
open fun submit(value: T) {
5157
this.value = value
5258
latch.countDown()
5359
}

vk-sdk-core/src/main/java/com/vk/api/sdk/VKDefaultValidationHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import com.vk.api.sdk.utils.VKValidationLocker
3434
* Default implementation for VKApiValidationHandler
3535
* It uses internal sdk activities
3636
*/
37-
class VKDefaultValidationHandler(val context: Context): VKApiValidationHandler {
37+
open class VKDefaultValidationHandler(val context: Context): VKApiValidationHandler {
3838

3939
override fun handleCaptcha(img: String, cb: VKApiValidationHandler.Callback<String>) {
4040
VKCaptchaActivity.start(context, img)

0 commit comments

Comments
 (0)