Skip to content

Commit

Permalink
version 3.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
a.mochalov committed Feb 4, 2022
1 parent 07f2adf commit 6c7ed50
Show file tree
Hide file tree
Showing 344 changed files with 8,660 additions and 1,892 deletions.
34 changes: 15 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ The available library modules are listed below.
For example, your `app/build.gradle` script will contains such dependencies:
```
dependencies {
implementation 'com.vk:android-sdk-core:3.x.x
implementation 'com.vk:android-sdk-api:3.x.x // generated models and api methods
implementation 'com.vk:android-sdk-core:3.x.x'
implementation 'com.vk:android-sdk-api:3.x.x' // generated models and api methods
}
```

Expand All @@ -93,28 +93,24 @@ SDK Initialization

User Authorization
----------
Use VK.login method:
Create authorization launcher:

```kotlin
VK.login(activity, arrayListOf(VKScope.WALL, VKScope.PHOTOS))
```
Override onActivityResult:

```kotlin
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
val callback = object: VKAuthCallback {
override fun onLogin(token: VKAccessToken) {
// User passed authorization
}

override fun onLoginFailed(errorCode: Int) {
// User didn't pass authorization
}
val authLauncher = VK.login(activity) { result : VKAuthenticationResult ->
when (result) {
is VKAuthenticationResult.Success -> {
// User passed authorization
}
if (data == null || !VK.onActivityResult(requestCode, resultCode, data, callback)) {
super.onActivityResult(requestCode, resultCode, data)
is VKAuthenticationResult.Failed -> {
// User didn't pass authorization
}
}
}
```
In appropriate place call a created launcher:

```kotlin
authLauncher.launch(arrayListOf(VKScope.WALL, VKScope.PHOTOS))
```

Handling token authorization
Expand Down
15 changes: 11 additions & 4 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ version = sdkVersions.name
group = 'com.vk'
sdkPublish.POM_ARTIFACT_ID = sdkPublish.POM_API_ARTIFACT_ID
sdkPublish.POM_NAME = sdkPublish.POM_API_NAME
sdkPublish.POM_DESCRIPTION = sdkPublish.POM_API_DESCRIPTION
sdkPublish.POM_ARTIFACT_DESCRIPTION = sdkPublish.POM_API_DESCRIPTION

android {
compileSdkVersion "$sdkVersions.compileSdk".toInteger()
buildToolsVersion "$sdkVersions.buildTools"

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}

defaultConfig {
Expand All @@ -33,11 +33,18 @@ android {
exclude("autoTest/**")
exclude("META-INF/spring.*")
exclude("META-INF/notice.txt")
exclude("META-INF/resources/**")
exclude("mockito-extensions/org.mockito.plugins.MockMaker")
}

buildTypes {
release {
consumerProguardFile("${projectDir}/consumer-proguard-rules.pro")
}
}

kotlinOptions {
jvmTarget = '1.8'
jvmTarget = JavaVersion.VERSION_11.toString()
}
}

Expand Down
18 changes: 18 additions & 0 deletions api/consumer-proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer


# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}

# Retain generic signatures of TypeToken and its subclasses with R8 version 3.0 and higher.
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken
38 changes: 36 additions & 2 deletions api/src/main/java/com/vk/sdk/api/GsonHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,52 @@ package com.vk.sdk.api

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import com.google.gson.JsonPrimitive
import com.google.gson.JsonSerializationContext
import com.google.gson.JsonSerializer
import com.vk.dto.common.id.UserId
import com.vk.dto.common.id.UserId.GsonSerializer
import com.vk.sdk.api.newsfeed.dto.NewsfeedNewsfeedItem
import com.vk.sdk.api.users.dto.UsersSubscriptionsItem
import java.lang.reflect.Type
import kotlin.Boolean

object GsonHolder {
val gson: Gson by lazy {
GsonBuilder()
.registerTypeAdapter(UsersSubscriptionsItem::class.java,
UsersSubscriptionsItem.Deserializer())
.registerTypeAdapter(NewsfeedNewsfeedItem::class.java, NewsfeedNewsfeedItem.Deserializer())
.registerTypeAdapter(UserId::class.java, UserId.GsonSerializer())
.create()
.registerTypeAdapter(UserId::class.java, UserId.GsonSerializer(false))
.registerTypeAdapter(Boolean::class.javaObjectType,
BooleanGsonSerializer()).registerTypeAdapter(Boolean::class.javaPrimitiveType,
BooleanGsonSerializer()).create()
}


class BooleanGsonSerializer : JsonDeserializer<Boolean?>, JsonSerializer<Boolean?> {
override fun serialize(
src: Boolean?,
typeOfSrc: Type?,
context: JsonSerializationContext?
): JsonElement {
val asInt = if (src == true) 1 else 0
return JsonPrimitive(asInt)
}

override fun deserialize(
json: JsonElement?,
typeOfT: Type?,
context: JsonDeserializationContext?
): Boolean? {
if (json !is JsonPrimitive) {
return null
}
val asStr = json.asString
return asStr == "1" || asStr == "true"
}
}
}
86 changes: 86 additions & 0 deletions api/src/main/java/com/vk/sdk/api/NewApiRequest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ package com.vk.sdk.api

import com.google.gson.JsonParser
import com.vk.api.sdk.requests.VKRequest
import com.vk.dto.common.id.UserId
import java.lang.IllegalArgumentException
import kotlin.Double
import kotlin.Float
import kotlin.Int
import kotlin.Long
import kotlin.String
import kotlin.collections.List

internal class NewApiRequest<T> internal constructor(
methodName: String,
Expand All @@ -39,4 +46,83 @@ internal class NewApiRequest<T> internal constructor(
val responseJson = JsonParser.parseString(response).asJsonObject.get("response")
return parseResponse(responseJson)
}

fun addParam(
name: String,
value: String?,
minLength: Int = 0,
maxLength: Int = Int.MAX_VALUE
) {
if (value != null) {
if (value.length !in minLength..maxLength) {
throw IllegalArgumentException("Param $name not in $minLength..$maxLength")
}
params[name] = value
}
}

fun addParam(
name: String,
value: Int,
min: Int = Int.MIN_VALUE,
max: Int = Int.MAX_VALUE
) {
if (value !in min..max) {
throw IllegalArgumentException("Param $name not in $min..$max")
}
params[name] = value.toString()
}

fun addParam(
name: String,
value: Long,
min: Long = Long.MIN_VALUE,
max: Long = Long.MAX_VALUE
) {
if (value !in min..max) {
throw IllegalArgumentException("Param $name not in $min..$max")
}
params[name] = value.toString()
}

fun addParam(
name: String,
value: Float,
min: Double = Double.MIN_VALUE,
max: Double = Double.MAX_VALUE
) {
if (value !in min..max) {
throw IllegalArgumentException("Param $name not in $min..$max")
}
params[name] = value.toString()
}

fun addParam(
name: String,
value: UserId?,
min: Long = Long.MIN_VALUE,
max: Long = Long.MAX_VALUE
) {
if (value != null) {
if (value.value !in min..max) {
throw IllegalArgumentException("Param $name not in $min..$max")
}
params[name] = value.value.toString()
}
}

fun addParam(
name: String,
values: List<UserId>,
min: Long = Long.MIN_VALUE,
max: Long = Long.MAX_VALUE
) {
addParam(name, values.joinToString(",", transform = {
if (it.value !in min..max) {
throw IllegalArgumentException("Param $name not in $min..$max")
}
it.value.toString()
}
))
}
}
48 changes: 16 additions & 32 deletions api/src/main/java/com/vk/sdk/api/account/AccountService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import com.vk.sdk.api.account.dto.AccountSaveProfileInfoBdateVisibility
import com.vk.sdk.api.account.dto.AccountSaveProfileInfoRelation
import com.vk.sdk.api.account.dto.AccountSaveProfileInfoResponse
import com.vk.sdk.api.account.dto.AccountSaveProfileInfoSex
import com.vk.sdk.api.account.dto.AccountSetInfoName
import com.vk.sdk.api.account.dto.AccountUserSettings
import com.vk.sdk.api.base.dto.BaseOkResponse
import kotlin.Boolean
Expand Down Expand Up @@ -84,7 +85,7 @@ class AccountService {
GsonHolder.gson.fromJson(it, AccountChangePasswordResponse::class.java)
}
.apply {
addParam("new_password", newPassword)
addParam("new_password", newPassword, minLength = 6)
restoreSid?.let { addParam("restore_sid", it) }
changePasswordHash?.let { addParam("change_password_hash", it) }
oldPassword?.let { addParam("old_password", it) }
Expand All @@ -103,8 +104,8 @@ class AccountService {
GsonHolder.gson.fromJson(it, AccountGetActiveOffersResponse::class.java)
}
.apply {
offset?.let { addParam("offset", it) }
count?.let { addParam("count", it) }
offset?.let { addParam("offset", it, min = 0) }
count?.let { addParam("count", it, min = 0, max = 100) }
}

/**
Expand All @@ -118,7 +119,7 @@ class AccountService {
GsonHolder.gson.fromJson(it, Int::class.java)
}
.apply {
addParam("user_id", userId)
addParam("user_id", userId, min = 1)
}

/**
Expand All @@ -133,8 +134,8 @@ class AccountService {
GsonHolder.gson.fromJson(it, AccountGetBannedResponse::class.java)
}
.apply {
offset?.let { addParam("offset", it) }
count?.let { addParam("count", it) }
offset?.let { addParam("offset", it, min = 0) }
count?.let { addParam("count", it, min = 0, max = 200) }
}

/**
Expand All @@ -153,7 +154,7 @@ class AccountService {
it.value
}
filterJsonConverted?.let { addParam("filter", it) }
userId?.let { addParam("user_id", it) }
userId?.let { addParam("user_id", it, min = 0) }
}

/**
Expand Down Expand Up @@ -265,7 +266,7 @@ class AccountService {
cancelRequestId: Int? = null,
sex: AccountSaveProfileInfoSex? = null,
relation: AccountSaveProfileInfoRelation? = null,
relationPartnerId: Int? = null,
relationPartnerId: UserId? = null,
bdate: String? = null,
bdateVisibility: AccountSaveProfileInfoBdateVisibility? = null,
homeTown: String? = null,
Expand All @@ -280,15 +281,15 @@ class AccountService {
lastName?.let { addParam("last_name", it) }
maidenName?.let { addParam("maiden_name", it) }
screenName?.let { addParam("screen_name", it) }
cancelRequestId?.let { addParam("cancel_request_id", it) }
cancelRequestId?.let { addParam("cancel_request_id", it, min = 0) }
sex?.let { addParam("sex", it.value) }
relation?.let { addParam("relation", it.value) }
relationPartnerId?.let { addParam("relation_partner_id", it) }
relationPartnerId?.let { addParam("relation_partner_id", it, min = 0) }
bdate?.let { addParam("bdate", it) }
bdateVisibility?.let { addParam("bdate_visibility", it.value) }
homeTown?.let { addParam("home_town", it) }
countryId?.let { addParam("country_id", it) }
cityId?.let { addParam("city_id", it) }
countryId?.let { addParam("country_id", it, min = 0) }
cityId?.let { addParam("city_id", it, min = 0) }
status?.let { addParam("status", it) }
}

Expand All @@ -299,32 +300,15 @@ class AccountService {
* @param value - Setting value.
* @return [VKRequest] with [BaseOkResponse]
*/
fun accountSetInfo(name: String? = null, value: String? = null): VKRequest<BaseOkResponse> =
NewApiRequest("account.setInfo") {
fun accountSetInfo(name: AccountSetInfoName? = null, value: String? = null):
VKRequest<BaseOkResponse> = NewApiRequest("account.setInfo") {
GsonHolder.gson.fromJson(it, BaseOkResponse::class.java)
}
.apply {
name?.let { addParam("name", it) }
name?.let { addParam("name", it.value) }
value?.let { addParam("value", it) }
}

/**
* Sets an application screen name (up to 17 characters), that is shown to the user in the left
* menu.
*
* @param userId - User ID.
* @param name - Application screen name.
* @return [VKRequest] with [BaseOkResponse]
*/
fun accountSetNameInMenu(userId: UserId, name: String? = null): VKRequest<BaseOkResponse> =
NewApiRequest("account.setNameInMenu") {
GsonHolder.gson.fromJson(it, BaseOkResponse::class.java)
}
.apply {
addParam("user_id", userId)
name?.let { addParam("name", it) }
}

/**
* Marks a current user as offline.
*
Expand Down
Loading

0 comments on commit 6c7ed50

Please sign in to comment.