Exemplo n.º 1
0
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_favorite) {
      Log.d(DEBUG_TAG, "Favorite toggled");
      if (FavoritesManager.isFavorite(ResultActivity.this, ResultActivity.symbol)) {
        FavoritesManager.removeFavorite(ResultActivity.this, ResultActivity.symbol);
      } else {
        FavoritesManager.addFavorite(ResultActivity.this, ResultActivity.symbol);
        // Toast at 1:23
        Toast.makeText(
                ResultActivity.this, "Bookmarked " + ResultActivity.name + "!!", Toast.LENGTH_SHORT)
            .show();
      }
      setFavoriteIcon();
      return true;
    } else if (id == R.id.action_facebook) {
      Log.d(DEBUG_TAG, "Facebook share button clicked");
      // Toast at 1:27
      Toast.makeText(
              ResultActivity.this, "Sharing " + ResultActivity.name + "!!", Toast.LENGTH_SHORT)
          .show();
      shareDialog = new ShareDialog(this);
      shareDialog.registerCallback(callbackManager, shareCallback);
      if (ShareDialog.canShow(ShareLinkContent.class)) {
        ShareLinkContent linkContent =
            new ShareLinkContent.Builder()
                .setContentTitle(
                    "Current Stock Price of "
                        + ResultActivity.name
                        + " is $"
                        + ResultActivity.lastPrice)
                .setContentDescription(
                    "Stock Information of "
                        + ResultActivity.name
                        + " ("
                        + ResultActivity.symbol
                        + ")")
                .setImageUrl(
                    Uri.parse(
                        "https://chart.yahoo.com/t?s="
                            + ResultActivity.symbol
                            + "&lang=en-US&width=400&height=300"))
                .setContentUrl(Uri.parse("https://finance.yahoo.com/q?s=" + symbol))
                .build();
        shareDialog.setShouldFailOnDataError(true);
        shareDialog.show(linkContent, ShareDialog.Mode.FEED);
      }
      return true;
    }

    return super.onOptionsItemSelected(item);
  }
Exemplo n.º 2
0
  @OnClick(R.id.btn_share)
  public void facebookShare() {
    if (ShareDialog.canShow(ShareLinkContent.class)) {
      ShareLinkContent content =
          new ShareLinkContent.Builder()
              .setContentUrl(Uri.parse("http://collide.com"))
              .setContentDescription(getIntent().getStringExtra("note"))
              .setContentTitle("I unlocked the song " + getIntent().getStringExtra("title"))
              .setImageUrl(Uri.parse(getIntent().getStringExtra("image_link")))
              .build();

      shareDialog.show(this, content);
    }
  }
  private void shareOnFB() {
    // Create an object
    Set<String> permissions = AccessToken.getCurrentAccessToken().getPermissions();
    if (!permissions.contains("publish_actions")) {
      LoginManager.getInstance()
          .registerCallback(
              callbackManager,
              new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                  if (AccessToken.getCurrentAccessToken()
                      .getPermissions()
                      .contains("publish_actions")) {
                    shareOnFB();
                  }
                }

                @Override
                public void onCancel() {}

                @Override
                public void onError(FacebookException e) {}
              });
      LoginManager.getInstance()
          .logInWithPublishPermissions(PaymentActivity.this, Arrays.asList("publish_actions"));
    } else {
      String dateString = "";
      String dateString2 = "";
      try {
        Date date = new Date(mEventData.getSchedule().getStart_date());
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.ENGLISH);
        dateString = sdf2.format(date);
        date.setTime(mEventData.getSchedule().getEnd_date());
        dateString2 = sdf2.format(date);

      } catch (Exception e) {
        e.printStackTrace();
        Crashlytics.logException(e);
      }

      String url = "http://purplesq.com/event/" + mEventData.get_id();

      ShareOpenGraphObject eventObject =
          new ShareOpenGraphObject.Builder()
              .putString("fb:app_id", "852292264845107")
              .putString("og:type", "purplesquirrel:event")
              .putString("og:title", mEventData.getName())
              .putString("og:description", mEventData.getSummary())
              .putString("og:url", url)
              .putString("og:image", mEventData.getThumbnail())
              .build();

      ShareOpenGraphAction action =
          new ShareOpenGraphAction.Builder()
              .setActionType("purplesquirrel:attend")
              .putString("start_time", dateString)
              .putString("expires_time", dateString2)
              .putObject("event", eventObject)
              .build();

      ShareOpenGraphContent content =
          new ShareOpenGraphContent.Builder()
              .setPreviewPropertyName("event")
              .setAction(action)
              .build();

      if (ShareDialog.canShow(ShareOpenGraphContent.class)) {
        ShareDialog shareDialog = new ShareDialog(this);
        shareDialog.registerCallback(
            callbackManager,
            new FacebookCallback<Result>() {
              @Override
              public void onSuccess(Result result) {
                if (!TextUtils.isEmpty(result.getPostId())) {
                  if (Config.DEBUG) {
                    Log.i("Nish", "FB Share Success : " + result.getPostId());
                  }
                  findViewById(R.id.activity_payment_coupon_cardview).setVisibility(View.GONE);
                } else {
                  if (Config.DEBUG) {
                    Log.i("Nish", "FB Share Success without postId ");
                  }
                }
              }

              @Override
              public void onCancel() {}

              @Override
              public void onError(FacebookException e) {}
            });
        shareDialog.show(content);
      }
    }
  }
  private void executeDialog(JSONArray args, CallbackContext callbackContext) {
    Map<String, String> params = new HashMap<String, String>();
    String method = null;
    JSONObject parameters;

    try {
      parameters = args.getJSONObject(0);
    } catch (JSONException e) {
      parameters = new JSONObject();
    }

    Iterator<String> iter = parameters.keys();
    while (iter.hasNext()) {
      String key = iter.next();
      if (key.equals("method")) {
        try {
          method = parameters.getString(key);
        } catch (JSONException e) {
          Log.w(TAG, "Nonstring method parameter provided to dialog");
        }
      } else {
        try {
          params.put(key, parameters.getString(key));
        } catch (JSONException e) {
          // Need to handle JSON parameters
          Log.w(TAG, "Non-string parameter provided to dialog discarded");
        }
      }
    }

    if (method == null) {
      callbackContext.error("No method provided");
    } else if (method.equalsIgnoreCase("apprequests")) {

      if (!GameRequestDialog.canShow()) {
        callbackContext.error("Cannot show dialog");
        return;
      }
      showDialogContext = callbackContext;
      PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
      pr.setKeepCallback(true);
      showDialogContext.sendPluginResult(pr);

      GameRequestContent.Builder builder = new GameRequestContent.Builder();
      if (params.containsKey("message")) builder.setMessage(params.get("message"));
      if (params.containsKey("to")) builder.setTo(params.get("to"));
      if (params.containsKey("data")) builder.setData(params.get("data"));
      if (params.containsKey("title")) builder.setTitle(params.get("title"));
      if (params.containsKey("objectId")) builder.setObjectId(params.get("objectId"));
      if (params.containsKey("actionType")) {
        try {
          final GameRequestContent.ActionType actionType =
              GameRequestContent.ActionType.valueOf(params.get("actionType"));
          builder.setActionType(actionType);
        } catch (IllegalArgumentException e) {
          Log.w(TAG, "Discarding invalid argument actionType");
        }
      }
      if (params.containsKey("filters")) {
        try {
          final GameRequestContent.Filters filters =
              GameRequestContent.Filters.valueOf(params.get("filters"));
          builder.setFilters(filters);
        } catch (IllegalArgumentException e) {
          Log.w(TAG, "Discarding invalid argument filters");
        }
      }

      gameRequestDialog.show(builder.build());

    } else if (method.equalsIgnoreCase("share")) {
      if (!ShareDialog.canShow(ShareLinkContent.class)) {
        callbackContext.error("Cannot show dialog");
        return;
      }
      showDialogContext = callbackContext;
      PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
      pr.setKeepCallback(true);
      showDialogContext.sendPluginResult(pr);

      ShareLinkContent content = buildContent(params);
      shareDialog.show(content);

    } else if (method.equalsIgnoreCase("feed")) {
      ShareLinkContent content = buildContent(params);
      ShareApi.share(content, null);
      callbackContext.success();

    } else if (method.equalsIgnoreCase("share_open_graph")) {
      if (!ShareDialog.canShow(ShareOpenGraphContent.class)) {
        callbackContext.error("Cannot show dialog");
        return;
      }
      showDialogContext = callbackContext;
      PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
      pr.setKeepCallback(true);
      showDialogContext.sendPluginResult(pr);

      if (!params.containsKey("action")) {
        callbackContext.error("Missing required parameter \"action\"");
      }
      ShareOpenGraphAction openGraphAction =
          new ShareOpenGraphAction.Builder().setActionType(params.get("action")).build();

      ShareOpenGraphContent.Builder content =
          new ShareOpenGraphContent.Builder().setAction(openGraphAction);

      if (params.containsKey("previewPropertyName"))
        content.setPreviewPropertyName(params.get("previewPropertyName"));

      shareDialog.show(content.build());

    } else if (method.equalsIgnoreCase("send")) {
      if (!MessageDialog.canShow(ShareLinkContent.class)) {
        callbackContext.error("Cannot show dialog");
        return;
      }
      showDialogContext = callbackContext;
      PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
      pr.setKeepCallback(true);
      showDialogContext.sendPluginResult(pr);

      ShareLinkContent.Builder builder = new ShareLinkContent.Builder();
      if (params.containsKey("link")) builder.setContentUrl(Uri.parse(params.get("link")));
      if (params.containsKey("caption")) builder.setContentTitle(params.get("caption"));
      if (params.containsKey("picture")) builder.setImageUrl(Uri.parse(params.get("picture")));
      if (params.containsKey("description"))
        builder.setContentDescription(params.get("description"));

      messageDialog.show(builder.build());

    } else {
      callbackContext.error("Unsupported dialog method.");
    }
  }