com.vk.sdk.api.VKResponse Java Examples

The following examples show how to use com.vk.sdk.api.VKResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AddTrackToPlaylistDialogFragment.java    From vk_music_android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onPlaylistClicked(VkApiAlbum playlist, int position) {
    if(binding.loadingIndicator.getVisibility() == View.VISIBLE){
        return; // Don't accept click events when loading
    }

    binding.loadingIndicator.setVisibility(View.VISIBLE);

    VKApi.audio().add(VKParameters.from("audio_id", audioToAdd.id, "owner_id", audioToAdd.owner_id, VKApiConst.ALBUM_ID, playlist.getId())).executeWithListener(new VKRequest.VKRequestListener() {
        @Override
        public void onComplete(VKResponse response) {
            if(listener != null){
                listener.onAudioAddedToPlaylist(audioToAdd, playlist);
            }
            dismissAllowingStateLoss();
        }
    });
}
 
Example #2
Source File: AudioListFragment.java    From vk_music_android with GNU General Public License v3.0 6 votes vote down vote up
private void removeAudio(VKApiAudio audio, int position) {
    VKApi.audio().delete(VKParameters.from("audio_id", audio.id, "owner_id", audio.owner_id)).executeWithListener(new VKRequest.VKRequestListener() {
        @Override
        public void onComplete(VKResponse response) {
            try {
                int responseCode = response.json.getInt("response");
                if (responseCode == 1) {
                    audioArray.remove(position);
                    adapter.notifyItemRemoved(position);
                    Snackbar.make(binding.getRoot(), R.string.track_removed, Snackbar.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                onError(null);
            }
        }

        @Override
        public void onError(VKError error) {
            Snackbar.make(binding.getRoot(), R.string.error_deleting_track, Snackbar.LENGTH_LONG).show();
        }
    });
}
 
Example #3
Source File: MainActivity.java    From vk_music_android with GNU General Public License v3.0 6 votes vote down vote up
private void onAddPlaylistClicked() {
    View alertView = LayoutInflater.from(this).inflate(R.layout.layout_dialog_createplaylist, null, false);
    EditText playlistTitle = (EditText) alertView.findViewById(R.id.playlist_title);

    new AlertDialog.Builder(this)
            .setTitle(R.string.create_playlist)
            .setView(alertView)
            .setNegativeButton(android.R.string.cancel, null)
            .setPositiveButton(android.R.string.ok, (dialog, which) -> {
                VKApi.audio().addAlbum(VKParameters.from("title", playlistTitle.getText())).executeWithListener(new VKRequest.VKRequestListener() {
                    @Override
                    public void onComplete(VKResponse response) {
                        loadPlaylists();
                    }
                });
            })
            .show();
}
 
Example #4
Source File: MainActivity.java    From vk_music_android with GNU General Public License v3.0 6 votes vote down vote up
private void onPlaylistItemLongClicked(VkApiAlbum playlist) {
    new AlertDialog.Builder(this)
            .setMessage("Are you sure you want to delete " + playlist.getTitle() + "?")
            .setNegativeButton(android.R.string.cancel, null)
            .setPositiveButton(android.R.string.ok, (dialog, which) -> {
                VKApi.audio().deleteAlbum(VKParameters.from(VKApiConst.ALBUM_ID, playlist.getId())).executeWithListener(new VKRequest.VKRequestListener() {
                    @Override
                    public void onComplete(VKResponse response) {
                        drawer.removeItem(playlist.getId());
                        loadPlaylists();
                        Snackbar.make(binding.slidinglayout, R.string.deleted_playlist, Snackbar.LENGTH_SHORT).show();
                    }
                });
            })
            .show();
}
 
Example #5
Source File: VKShareDialogDelegate.java    From cordova-social-vk with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(View view) {
	setIsLoading(true);
	if (mAttachmentImages != null && VKSdk.getAccessToken() != null) {
		final Long userId = Long.parseLong(VKSdk.getAccessToken().userId);
		VKUploadWallPhotoRequest photoRequest = new VKUploadWallPhotoRequest(mAttachmentImages, userId, 0);
		photoRequest.executeWithListener(new VKRequest.VKRequestListener() {
			@Override
			public void onComplete(VKResponse response) {
				VKPhotoArray photos = (VKPhotoArray) response.parsedModel;
				VKAttachments attachments = new VKAttachments(photos);
				makePostWithAttachments(attachments);
			}

			@Override
			public void onError(VKError error) {
				setIsLoading(false);
				if (mListener != null) {
					mListener.onVkShareError(error);
				}
			}
		});
	} else {
		makePostWithAttachments(null);
	}
}
 
Example #6
Source File: AddTrackToPlaylistDialogFragment.java    From vk_music_android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onResume() {
    super.onResume();

    binding.loadingIndicator.setVisibility(View.VISIBLE);
    VKApi.audio().getAlbums(VKParameters.from(VKApiConst.OWNER_ID, user.getId())).executeWithListener(new VKRequest.VKRequestListener() {
        @Override
        public void onComplete(VKResponse response) {
            VkApiAlbumArrayResponse albumsResponse = gson.fromJson(response.responseString, VkApiAlbumArrayResponse.class);
            playlists.clear();
            playlists.addAll(albumsResponse.getResponse().getItems());

            for (VkApiAlbum album : playlists) {
                String fixedTitle;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    fixedTitle = Html.fromHtml(album.getTitle(), Html.FROM_HTML_MODE_LEGACY).toString();
                } else {
                    fixedTitle = Html.fromHtml(album.getTitle()).toString();
                }
                album.setTitle(fixedTitle);
            }

            adapter.notifyDataSetChanged();
            binding.loadingIndicator.setVisibility(View.GONE);
        }

        @Override
        public void onError(VKError error) {
            binding.loadingIndicator.setVisibility(View.VISIBLE);
            Snackbar.make(binding.rcvPlaylists, "Error loading playlists", Snackbar.LENGTH_LONG).show();
        }
    });
}
 
Example #7
Source File: RadioFragment.java    From vk_music_android with GNU General Public License v3.0 5 votes vote down vote up
public void loadRadio(@Nullable VKApiAudio radioTrack, @Nullable Action0 onCompletedCallback) {
    binding.loadingIndicator.setVisibility(View.VISIBLE);

    VKParameters parameters;
    if (radioTrack == null) {
        parameters = VKParameters.from(VKApiConst.COUNT, 100, "shuffle", 1);
    } else {
        parameters = VKParameters.from("target_audio", radioTrack.owner_id + "_" + radioTrack.id, VKApiConst.COUNT, 100, "shuffle", 1);
    }

    VKApi.audio().getRecommendations(parameters).executeWithListener(new VKRequest.VKRequestListener() {
        @Override
        public void onComplete(VKResponse response) {
            audioArray.clear();
            audioArray.addAll((VkAudioArray) response.parsedModel);
            adapter.notifyDataSetChanged();
            binding.loadingIndicator.setVisibility(View.GONE);

            if (audioArray.size() == 0) {
                Snackbar.make(binding.getRoot(), R.string.no_similar_tracks, Snackbar.LENGTH_LONG).show();
                binding.noData.getRoot().setVisibility(View.VISIBLE);
            } else {
                binding.noData.getRoot().setVisibility(View.GONE);
            }

            if (onCompletedCallback != null) onCompletedCallback.call();
        }

        @Override
        public void onError(VKError error) {
            Snackbar.make(binding.rcvAudio, R.string.error_loading_radio, Snackbar.LENGTH_LONG);
            binding.loadingIndicator.setVisibility(View.GONE);
        }
    });
}
 
Example #8
Source File: VKSdk.java    From cordova-social-vk with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if an access token exist and performs a try to use it again
 *
 * @param context            An application context for store an access token
 * @param loginStateCallback if callback specified, {@link VKCallback#onResult(Object)} method will be called after login state changed
 * @return true, if an access token exists and not expired
 */
public static boolean wakeUpSession(@NonNull final Context context, final VKCallback<LoginState> loginStateCallback) {
    final Context appContext = context.getApplicationContext();
    VKUIHelper.setApplicationContext(appContext);

    VKAccessToken token = VKAccessToken.currentToken();

    if (token != null && token.accessToken != null && !token.isExpired()) {
        forceLoginState(LoginState.Pending, loginStateCallback);
        trackVisitor(new VKRequest.VKRequestListener() {
            @Override
            public void onComplete(VKResponse response) {
                updateLoginState(context, loginStateCallback);
            }

            @Override
            public void onError(VKError error) {
                //Possible double call of access token invalid
                if (error != null && error.apiError != null && error.apiError.errorCode == 5) {
                    onAccessTokenIsInvalid(appContext);
                }
                updateLoginState(context, loginStateCallback);
            }
        });
        return true;
    }
    updateLoginState(context, loginStateCallback);
    return false;
}
 
Example #9
Source File: VKSyncRequestUtil.java    From cordova-social-vk with Apache License 2.0 5 votes vote down vote up
@Override
public void onComplete(VKResponse response) {
    synchronized (this.syncObj) {
        try {
            listener.onComplete(response);
        } catch (Exception e) {
            // nothing
        }
        isFinish = true;
        syncObj.notifyAll();
    }
}
 
Example #10
Source File: VKShareDialogDelegate.java    From cordova-social-vk with Apache License 2.0 5 votes vote down vote up
private void makePostWithAttachments(VKAttachments attachments) {

		if (attachments == null) {
			attachments = new VKAttachments();
		}
		if (mExistingPhotos != null) {
			attachments.addAll(mExistingPhotos);
		}
		if (mAttachmentLink != null) {
			attachments.add(new VKApiLink(mAttachmentLink.linkUrl));
		}
		String message = mShareTextField.getText().toString();

		final Long userId = Long.parseLong(VKSdk.getAccessToken().userId);
		VKRequest wallPost = VKApi.wall().post(VKParameters.from(VKApiConst.OWNER_ID, userId, VKApiConst.MESSAGE, message, VKApiConst.ATTACHMENTS, attachments.toAttachmentsString()));
		wallPost.executeWithListener(new VKRequest.VKRequestListener() {
			@Override
			public void onError(VKError error) {
				setIsLoading(false);
				if (mListener != null) {
					mListener.onVkShareError(error);
				}
			}

			@Override
			public void onComplete(VKResponse response) {
				setIsLoading(false);
				VKWallPostResult res = (VKWallPostResult) response.parsedModel;
				if (mListener != null) {
					mListener.onVkShareComplete(res.post_id);
				}
				dialogFragmentI.dismissAllowingStateLoss();
			}
		});
	}
 
Example #11
Source File: AudioListFragment.java    From vk_music_android with GNU General Public License v3.0 4 votes vote down vote up
public void loadData() {
    binding.swiperefresh.setRefreshing(true);

    VKParameters parameters = null;
    switch (listType) {
        case MY_AUDIO:
            parameters = VKParameters.from();
            break;

        case PLAYLIST:
            parameters = VKParameters.from(VKApiConst.ALBUM_ID, playlist.getId());
            break;

        case SEARCH:
            parameters = VKParameters.from(VKApiConst.Q, searchQuery, VKApiConst.COUNT, 100);
            break;

        case POPULAR:
            parameters = VKParameters.from("only_eng", 1);
            break;
    }

    VKRequest.VKRequestListener listener = new VKRequest.VKRequestListener() {
        @Override
        public void onComplete(VKResponse response) {
            audioArray.clear();
            audioArray.addAll((VkAudioArray) response.parsedModel);
            adapter.notifyDataSetChanged();
            binding.swiperefresh.setRefreshing(false);

            if (audioArray.size() == 0) {
                binding.noData.getRoot().setVisibility(View.VISIBLE);
            } else {
                binding.noData.getRoot().setVisibility(View.GONE);
            }
        }

        @Override
        public void onError(VKError error) {
            Snackbar.make(binding.rcvAudio, "Error loading search results", Snackbar.LENGTH_LONG);
            binding.swiperefresh.setRefreshing(false);
        }
    };

    if (listType == AudioListType.SEARCH) {
        VKApi.audio().search(parameters).executeWithListener(listener);
    } else if (listType == AudioListType.POPULAR) {
        VKApi.audio().getPopular(parameters).executeWithListener(listener);
    } else {
        VKApi.audio().get(parameters).executeWithListener(listener);
    }
}
 
Example #12
Source File: MusicService.java    From vk_music_android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    if (intent != null && intent.hasExtra(KEY_ACTION)) {
        switch (intent.getIntExtra(KEY_ACTION, 0)) {
            case ACTION_PLAY_PAUSE:
                playPause();
                break;

            case ACTION_PREVIOUS:
                playPreviousTrackInQueue();
                break;

            case ACTION_NEXT:
                playNextTrackInQueue();
                break;

            case ACTION_DISMISS:
                mediaPlayer.stop();
                notificationManager.destroyNotification();

                for (MusicServiceListener listener : listeners) {
                    listener.onFinishRequested();
                }

                stopSelf();
                break;

            case ACTION_OPEN_ACTIVITY:
                if (listeners.size() == 0) {
                    Intent mainActivityIntent = new Intent(this, MainActivity.class);
                    mainActivityIntent.putExtra(MainActivity.KEY_INITIAL_FRAGMENT, MainActivity.FRAG_NOW_PLAYING);
                    mainActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                    startActivity(mainActivityIntent);
                }
                break;

            case ACTION_ADD:
                final VKApiAudio audio = currentAudio.get();
                VKApi.audio().add(VKParameters.from("audio_id", audio.id, "owner_id", audio.owner_id)).executeWithListener(new VKRequest.VKRequestListener() {
                    @Override
                    public void onComplete(VKResponse response) {
                        notificationManager.showAddCompleteIndicator();
                    }
                });
                break;
        }
    }

    return START_STICKY;
}