@@ -43,14 +43,17 @@ import java.io.IOException
4343 * This is an entry point of VK SDK
4444 */
4545object 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 () {
0 commit comments