예제 #1
0
    @OnClick(R.id.post_share_button)
    void share() {

      VKRequest request =
          VKApi.wall()
              .repost(
                  VKParameters.from(
                      VKApiConst.OBJECT,
                      "wall"
                          + String.valueOf(mPost.source_id)
                          + "_"
                          + String.valueOf(mPost.post_id)));

      request.useSystemLanguage = false;

      request = VKRequest.getRegisteredRequest(request.registerObject());
      if (request != null) {
        request.unregisterObject();
      }
      request.executeWithListener(
          new VKRequestListener() {
            @Override
            public void onComplete(VKResponse response) {
              mPost.reposts_count += 1;
              postCountOfShare.setText(String.valueOf(mPost.reposts_count));

              mPost.user_reposted = true;
            }

            @Override
            public void onError(VKError error) {
              Log.d("Error", error.toString());
            }
          });
    }
  private void makePost(VKAttachments attachments, String message) {

    final VKRequest post =
        VKApi.wall()
            .post(
                VKParameters.from(
                    VKApiConst.ATTACHMENTS, attachments, VKApiConst.MESSAGE, message));

    post.setModelClass(VKWallPostResult.class);
    post.executeWithListener(
        new VKRequest.VKRequestListener() {

          @Override
          public void onComplete(VKResponse response) {
            try {

              super.onComplete(response);

              Log.d("LOG_TAG", "post ok");

              VKWallPostResult result = (VKWallPostResult) response.parsedModel;

              Intent intent =
                  new Intent(
                      Intent.ACTION_VIEW,
                      Uri.parse(
                          String.format("https://vk.com/wall" + userID + "_" + result.post_id)));
              startActivity(intent);

            } catch (NullPointerException e) {
              e.printStackTrace();
              Log.d(LOG_TAG, "error");
            }
          }
        });
  }
예제 #3
0
  private void getNewPosts() {
    /*Log.d(TAG, "onComplete: timeForNotificationUpdate = " + Settings.timeForNotificationUpdate);
    Log.d(TAG, "onComplete: TIME_FOR_NOTIFICATION_UPDATE = " + settings.getInt(Settings.APP_PREFERENCES_TIME_FOR_NOTIFICATION_UPDATE, 0));
    Log.d(TAG, "onComplete: LAST_SEEN_POST_ID" + settings.getLong(Settings.APP_PREFERENCES_LAST_SEEN_POST_ID, 0));*/

    VKApi.wall()
        .get(VKParameters.from(VKApiConst.OWNER_ID, Info.GROUP_ID, VKApiConst.COUNT, 100))
        .executeSyncWithListener(
            new VKRequest.VKRequestListener() {

              @Override
              public void onComplete(VKResponse response) {
                try {
                  JSONArray postsNew =
                      response.json.getJSONObject("response").getJSONArray("items");
                  for (int i = 0; i < postsNew.length(); i++) {
                    VKApiPost vkPost = new VKApiPost(postsNew.getJSONObject(i));

                    if (!havePinned) {
                      try {
                        havePinned =
                            response
                                    .json
                                    .getJSONObject("response")
                                    .getJSONArray("items")
                                    .getJSONObject(i)
                                    .getInt("is_pinned")
                                == 1;
                      } catch (JSONException e) {
                      }
                    }
                    if (vkPost.id
                        == settings.getLong(Settings.APP_PREFERENCES_LAST_SEEN_POST_ID, 0)) {
                      if (havePinned) newPostsCount = i - 1;
                      else newPostsCount = i;
                    }
                    // если добавилось newPostsForNotificationCount постов, то выводить уведомление
                    // о новом посте
                    if (newPostsCount >= Settings.newPostsForNotificationCount) {
                      PendingIntent resultPendingIntent =
                          PendingIntent.getActivity(
                              NotificationService.this,
                              0,
                              resultIntent,
                              PendingIntent.FLAG_UPDATE_CURRENT);

                      Notification.Builder builder =
                          new Notification.Builder(NotificationService.this)
                              .setDefaults(Notification.DEFAULT_SOUND)
                              .setDefaults(Notification.DEFAULT_VIBRATE)
                              .setTicker("Новые посты!")
                              .setContentTitle(
                                  getResources().getString(R.string.notification_title))
                              .setContentText(
                                  getResources()
                                      .getQuantityString(
                                          R.plurals.numberOfNewPosts, newPostsCount, newPostsCount))
                              .setSmallIcon(R.mipmap.ic_app)
                              .setContentIntent(resultPendingIntent)
                              .setAutoCancel(true);

                      // выводится верно
                      Notification notification =
                          new Notification.BigTextStyle(builder)
                              .bigText(
                                  getResources()
                                      .getQuantityString(
                                          R.plurals.numberOfNewPosts, newPostsCount, newPostsCount))
                              .build();

                      NotificationManager notificationManager =
                          (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                      notificationManager.notify(1, notification);
                    }

                    /*Log.i(TAG, vkPost.id
                    + " = " + settings.getLong(Settings.APP_PREFERENCES_LAST_SEEN_POST_ID, 0)
                    + "\nhavePinned = " + havePinned
                    + "\ni = " + i
                    + "\nnewPostsCount = " + newPostsCount
                    + "\nnewPostsForNotificationCount = " + settings.getInt(Settings.APP_PREFERENCES_NEW_POSTS_FOR_NOTIFICATION_COUNT, 0));*/
                  }

                } catch (JSONException e) {
                  e.printStackTrace();
                }
              }
            });
  }