Skip to content

Commit 6c7ed50

Browse files
author
a.mochalov
committed
version 3.5.0
1 parent 07f2adf commit 6c7ed50

File tree

344 files changed

+8660
-1892
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

344 files changed

+8660
-1892
lines changed

README.md

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ The available library modules are listed below.
6868
For example, your `app/build.gradle` script will contains such dependencies:
6969
```
7070
dependencies {
71-
implementation 'com.vk:android-sdk-core:3.x.x
72-
implementation 'com.vk:android-sdk-api:3.x.x // generated models and api methods
71+
implementation 'com.vk:android-sdk-core:3.x.x'
72+
implementation 'com.vk:android-sdk-api:3.x.x' // generated models and api methods
7373
}
7474
```
7575

@@ -93,28 +93,24 @@ SDK Initialization
9393

9494
User Authorization
9595
----------
96-
Use VK.login method:
96+
Create authorization launcher:
9797

9898
```kotlin
99-
VK.login(activity, arrayListOf(VKScope.WALL, VKScope.PHOTOS))
100-
```
101-
Override onActivityResult:
102-
103-
```kotlin
104-
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
105-
val callback = object: VKAuthCallback {
106-
override fun onLogin(token: VKAccessToken) {
107-
// User passed authorization
108-
}
109-
110-
override fun onLoginFailed(errorCode: Int) {
111-
// User didn't pass authorization
112-
}
99+
val authLauncher = VK.login(activity) { result : VKAuthenticationResult ->
100+
when (result) {
101+
is VKAuthenticationResult.Success -> {
102+
// User passed authorization
113103
}
114-
if (data == null || !VK.onActivityResult(requestCode, resultCode, data, callback)) {
115-
super.onActivityResult(requestCode, resultCode, data)
104+
is VKAuthenticationResult.Failed -> {
105+
// User didn't pass authorization
116106
}
117107
}
108+
}
109+
```
110+
In appropriate place call a created launcher:
111+
112+
```kotlin
113+
authLauncher.launch(arrayListOf(VKScope.WALL, VKScope.PHOTOS))
118114
```
119115

120116
Handling token authorization

api/build.gradle

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ version = sdkVersions.name
77
group = 'com.vk'
88
sdkPublish.POM_ARTIFACT_ID = sdkPublish.POM_API_ARTIFACT_ID
99
sdkPublish.POM_NAME = sdkPublish.POM_API_NAME
10-
sdkPublish.POM_DESCRIPTION = sdkPublish.POM_API_DESCRIPTION
10+
sdkPublish.POM_ARTIFACT_DESCRIPTION = sdkPublish.POM_API_DESCRIPTION
1111

1212
android {
1313
compileSdkVersion "$sdkVersions.compileSdk".toInteger()
1414
buildToolsVersion "$sdkVersions.buildTools"
1515

1616
compileOptions {
17-
sourceCompatibility JavaVersion.VERSION_1_8
18-
targetCompatibility JavaVersion.VERSION_1_8
17+
sourceCompatibility JavaVersion.VERSION_11
18+
targetCompatibility JavaVersion.VERSION_11
1919
}
2020

2121
defaultConfig {
@@ -33,11 +33,18 @@ android {
3333
exclude("autoTest/**")
3434
exclude("META-INF/spring.*")
3535
exclude("META-INF/notice.txt")
36+
exclude("META-INF/resources/**")
3637
exclude("mockito-extensions/org.mockito.plugins.MockMaker")
3738
}
3839

40+
buildTypes {
41+
release {
42+
consumerProguardFile("${projectDir}/consumer-proguard-rules.pro")
43+
}
44+
}
45+
3946
kotlinOptions {
40-
jvmTarget = '1.8'
47+
jvmTarget = JavaVersion.VERSION_11.toString()
4148
}
4249
}
4350

api/consumer-proguard-rules.pro

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Application classes that will be serialized/deserialized over Gson
2+
-keep class com.google.gson.examples.android.model.** { *; }
3+
4+
# Prevent proguard from stripping interface information from TypeAdapterFactory,
5+
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
6+
-keep class * implements com.google.gson.TypeAdapterFactory
7+
-keep class * implements com.google.gson.JsonSerializer
8+
-keep class * implements com.google.gson.JsonDeserializer
9+
10+
11+
# Prevent R8 from leaving Data object members always null
12+
-keepclassmembers,allowobfuscation class * {
13+
@com.google.gson.annotations.SerializedName <fields>;
14+
}
15+
16+
# Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
17+
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
18+
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken

api/src/main/java/com/vk/sdk/api/GsonHolder.kt

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,52 @@ package com.vk.sdk.api
2929

3030
import com.google.gson.Gson
3131
import com.google.gson.GsonBuilder
32+
import com.google.gson.JsonDeserializationContext
33+
import com.google.gson.JsonDeserializer
34+
import com.google.gson.JsonElement
35+
import com.google.gson.JsonPrimitive
36+
import com.google.gson.JsonSerializationContext
37+
import com.google.gson.JsonSerializer
3238
import com.vk.dto.common.id.UserId
39+
import com.vk.dto.common.id.UserId.GsonSerializer
3340
import com.vk.sdk.api.newsfeed.dto.NewsfeedNewsfeedItem
3441
import com.vk.sdk.api.users.dto.UsersSubscriptionsItem
42+
import java.lang.reflect.Type
43+
import kotlin.Boolean
3544

3645
object GsonHolder {
3746
val gson: Gson by lazy {
3847
GsonBuilder()
3948
.registerTypeAdapter(UsersSubscriptionsItem::class.java,
4049
UsersSubscriptionsItem.Deserializer())
4150
.registerTypeAdapter(NewsfeedNewsfeedItem::class.java, NewsfeedNewsfeedItem.Deserializer())
42-
.registerTypeAdapter(UserId::class.java, UserId.GsonSerializer())
43-
.create()
51+
.registerTypeAdapter(UserId::class.java, UserId.GsonSerializer(false))
52+
.registerTypeAdapter(Boolean::class.javaObjectType,
53+
BooleanGsonSerializer()).registerTypeAdapter(Boolean::class.javaPrimitiveType,
54+
BooleanGsonSerializer()).create()
4455
}
4556

57+
58+
class BooleanGsonSerializer : JsonDeserializer<Boolean?>, JsonSerializer<Boolean?> {
59+
override fun serialize(
60+
src: Boolean?,
61+
typeOfSrc: Type?,
62+
context: JsonSerializationContext?
63+
): JsonElement {
64+
val asInt = if (src == true) 1 else 0
65+
return JsonPrimitive(asInt)
66+
}
67+
68+
override fun deserialize(
69+
json: JsonElement?,
70+
typeOfT: Type?,
71+
context: JsonDeserializationContext?
72+
): Boolean? {
73+
if (json !is JsonPrimitive) {
74+
return null
75+
}
76+
val asStr = json.asString
77+
return asStr == "1" || asStr == "true"
78+
}
79+
}
4680
}

api/src/main/java/com/vk/sdk/api/NewApiRequest.kt

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ package com.vk.sdk.api
2929

3030
import com.google.gson.JsonParser
3131
import com.vk.api.sdk.requests.VKRequest
32+
import com.vk.dto.common.id.UserId
33+
import java.lang.IllegalArgumentException
34+
import kotlin.Double
35+
import kotlin.Float
36+
import kotlin.Int
37+
import kotlin.Long
3238
import kotlin.String
39+
import kotlin.collections.List
3340

3441
internal class NewApiRequest<T> internal constructor(
3542
methodName: String,
@@ -39,4 +46,83 @@ internal class NewApiRequest<T> internal constructor(
3946
val responseJson = JsonParser.parseString(response).asJsonObject.get("response")
4047
return parseResponse(responseJson)
4148
}
49+
50+
fun addParam(
51+
name: String,
52+
value: String?,
53+
minLength: Int = 0,
54+
maxLength: Int = Int.MAX_VALUE
55+
) {
56+
if (value != null) {
57+
if (value.length !in minLength..maxLength) {
58+
throw IllegalArgumentException("Param $name not in $minLength..$maxLength")
59+
}
60+
params[name] = value
61+
}
62+
}
63+
64+
fun addParam(
65+
name: String,
66+
value: Int,
67+
min: Int = Int.MIN_VALUE,
68+
max: Int = Int.MAX_VALUE
69+
) {
70+
if (value !in min..max) {
71+
throw IllegalArgumentException("Param $name not in $min..$max")
72+
}
73+
params[name] = value.toString()
74+
}
75+
76+
fun addParam(
77+
name: String,
78+
value: Long,
79+
min: Long = Long.MIN_VALUE,
80+
max: Long = Long.MAX_VALUE
81+
) {
82+
if (value !in min..max) {
83+
throw IllegalArgumentException("Param $name not in $min..$max")
84+
}
85+
params[name] = value.toString()
86+
}
87+
88+
fun addParam(
89+
name: String,
90+
value: Float,
91+
min: Double = Double.MIN_VALUE,
92+
max: Double = Double.MAX_VALUE
93+
) {
94+
if (value !in min..max) {
95+
throw IllegalArgumentException("Param $name not in $min..$max")
96+
}
97+
params[name] = value.toString()
98+
}
99+
100+
fun addParam(
101+
name: String,
102+
value: UserId?,
103+
min: Long = Long.MIN_VALUE,
104+
max: Long = Long.MAX_VALUE
105+
) {
106+
if (value != null) {
107+
if (value.value !in min..max) {
108+
throw IllegalArgumentException("Param $name not in $min..$max")
109+
}
110+
params[name] = value.value.toString()
111+
}
112+
}
113+
114+
fun addParam(
115+
name: String,
116+
values: List<UserId>,
117+
min: Long = Long.MIN_VALUE,
118+
max: Long = Long.MAX_VALUE
119+
) {
120+
addParam(name, values.joinToString(",", transform = {
121+
if (it.value !in min..max) {
122+
throw IllegalArgumentException("Param $name not in $min..$max")
123+
}
124+
it.value.toString()
125+
}
126+
))
127+
}
42128
}

api/src/main/java/com/vk/sdk/api/account/AccountService.kt

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import com.vk.sdk.api.account.dto.AccountSaveProfileInfoBdateVisibility
4343
import com.vk.sdk.api.account.dto.AccountSaveProfileInfoRelation
4444
import com.vk.sdk.api.account.dto.AccountSaveProfileInfoResponse
4545
import com.vk.sdk.api.account.dto.AccountSaveProfileInfoSex
46+
import com.vk.sdk.api.account.dto.AccountSetInfoName
4647
import com.vk.sdk.api.account.dto.AccountUserSettings
4748
import com.vk.sdk.api.base.dto.BaseOkResponse
4849
import kotlin.Boolean
@@ -84,7 +85,7 @@ class AccountService {
8485
GsonHolder.gson.fromJson(it, AccountChangePasswordResponse::class.java)
8586
}
8687
.apply {
87-
addParam("new_password", newPassword)
88+
addParam("new_password", newPassword, minLength = 6)
8889
restoreSid?.let { addParam("restore_sid", it) }
8990
changePasswordHash?.let { addParam("change_password_hash", it) }
9091
oldPassword?.let { addParam("old_password", it) }
@@ -103,8 +104,8 @@ class AccountService {
103104
GsonHolder.gson.fromJson(it, AccountGetActiveOffersResponse::class.java)
104105
}
105106
.apply {
106-
offset?.let { addParam("offset", it) }
107-
count?.let { addParam("count", it) }
107+
offset?.let { addParam("offset", it, min = 0) }
108+
count?.let { addParam("count", it, min = 0, max = 100) }
108109
}
109110

110111
/**
@@ -118,7 +119,7 @@ class AccountService {
118119
GsonHolder.gson.fromJson(it, Int::class.java)
119120
}
120121
.apply {
121-
addParam("user_id", userId)
122+
addParam("user_id", userId, min = 1)
122123
}
123124

124125
/**
@@ -133,8 +134,8 @@ class AccountService {
133134
GsonHolder.gson.fromJson(it, AccountGetBannedResponse::class.java)
134135
}
135136
.apply {
136-
offset?.let { addParam("offset", it) }
137-
count?.let { addParam("count", it) }
137+
offset?.let { addParam("offset", it, min = 0) }
138+
count?.let { addParam("count", it, min = 0, max = 200) }
138139
}
139140

140141
/**
@@ -153,7 +154,7 @@ class AccountService {
153154
it.value
154155
}
155156
filterJsonConverted?.let { addParam("filter", it) }
156-
userId?.let { addParam("user_id", it) }
157+
userId?.let { addParam("user_id", it, min = 0) }
157158
}
158159

159160
/**
@@ -265,7 +266,7 @@ class AccountService {
265266
cancelRequestId: Int? = null,
266267
sex: AccountSaveProfileInfoSex? = null,
267268
relation: AccountSaveProfileInfoRelation? = null,
268-
relationPartnerId: Int? = null,
269+
relationPartnerId: UserId? = null,
269270
bdate: String? = null,
270271
bdateVisibility: AccountSaveProfileInfoBdateVisibility? = null,
271272
homeTown: String? = null,
@@ -280,15 +281,15 @@ class AccountService {
280281
lastName?.let { addParam("last_name", it) }
281282
maidenName?.let { addParam("maiden_name", it) }
282283
screenName?.let { addParam("screen_name", it) }
283-
cancelRequestId?.let { addParam("cancel_request_id", it) }
284+
cancelRequestId?.let { addParam("cancel_request_id", it, min = 0) }
284285
sex?.let { addParam("sex", it.value) }
285286
relation?.let { addParam("relation", it.value) }
286-
relationPartnerId?.let { addParam("relation_partner_id", it) }
287+
relationPartnerId?.let { addParam("relation_partner_id", it, min = 0) }
287288
bdate?.let { addParam("bdate", it) }
288289
bdateVisibility?.let { addParam("bdate_visibility", it.value) }
289290
homeTown?.let { addParam("home_town", it) }
290-
countryId?.let { addParam("country_id", it) }
291-
cityId?.let { addParam("city_id", it) }
291+
countryId?.let { addParam("country_id", it, min = 0) }
292+
cityId?.let { addParam("city_id", it, min = 0) }
292293
status?.let { addParam("status", it) }
293294
}
294295

@@ -299,32 +300,15 @@ class AccountService {
299300
* @param value - Setting value.
300301
* @return [VKRequest] with [BaseOkResponse]
301302
*/
302-
fun accountSetInfo(name: String? = null, value: String? = null): VKRequest<BaseOkResponse> =
303-
NewApiRequest("account.setInfo") {
303+
fun accountSetInfo(name: AccountSetInfoName? = null, value: String? = null):
304+
VKRequest<BaseOkResponse> = NewApiRequest("account.setInfo") {
304305
GsonHolder.gson.fromJson(it, BaseOkResponse::class.java)
305306
}
306307
.apply {
307-
name?.let { addParam("name", it) }
308+
name?.let { addParam("name", it.value) }
308309
value?.let { addParam("value", it) }
309310
}
310311

311-
/**
312-
* Sets an application screen name (up to 17 characters), that is shown to the user in the left
313-
* menu.
314-
*
315-
* @param userId - User ID.
316-
* @param name - Application screen name.
317-
* @return [VKRequest] with [BaseOkResponse]
318-
*/
319-
fun accountSetNameInMenu(userId: UserId, name: String? = null): VKRequest<BaseOkResponse> =
320-
NewApiRequest("account.setNameInMenu") {
321-
GsonHolder.gson.fromJson(it, BaseOkResponse::class.java)
322-
}
323-
.apply {
324-
addParam("user_id", userId)
325-
name?.let { addParam("name", it) }
326-
}
327-
328312
/**
329313
* Marks a current user as offline.
330314
*

0 commit comments

Comments
 (0)