Ejemplo n.º 1
0
        @Override
        public void onCompleted(Response response) {
          // TODO Auto-generated method stub

          try {
            GraphObject go = response.getGraphObject();
            JSONObject jso = go.getInnerJSONObject();
            JSONArray arr = jso.getJSONArray("data");
            JSONObject data = arr.getJSONObject(0);
            name = data.getString("name");
            et_name.setText(name);

          } catch (JSONException e) {
            e.printStackTrace();
          }
        }
Ejemplo n.º 2
0
  private static int compareGraphObjects(
      GraphObject a, GraphObject b, Collection<String> sortFields, Collator collator) {
    for (String sortField : sortFields) {
      String sa = (String) a.getProperty(sortField);
      String sb = (String) b.getProperty(sortField);

      if (sa != null && sb != null) {
        int result = collator.compare(sa, sb);
        if (result != 0) {
          return result;
        }
      } else if (!(sa == null && sb == null)) {
        return (sa == null) ? -1 : 1;
      }
    }
    return 0;
  }
 private static String safeGetStringFromResponse(GraphObject response, String propertyName) {
   Object result = "";
   if (response != null) {
     result = response.getProperty(propertyName);
   }
   if (!(result instanceof String)) {
     result = "";
   }
   return (String) result;
 }
 private static boolean safeGetBooleanFromResponse(GraphObject response, String propertyName) {
   Object result = false;
   if (response != null) {
     result = response.getProperty(propertyName);
   }
   if (!(result instanceof Boolean)) {
     result = false;
   }
   return (Boolean) result;
 }
  private void deleteTestAccount(String testAccountId, String appAccessToken) {
    Bundle parameters = new Bundle();
    parameters.putString("access_token", appAccessToken);

    Request request = new Request(null, testAccountId, parameters, HttpMethod.DELETE);
    Response response = request.executeAndWait();

    FacebookRequestError error = response.getError();
    GraphObject graphObject = response.getGraphObject();
    if (error != null) {
      Log.w(
          LOG_TAG,
          String.format(
              "Could not delete test account %s: %s",
              testAccountId, error.getException().toString()));
    } else if (graphObject.getProperty(Response.NON_JSON_RESPONSE_PROPERTY) == (Boolean) false) {
      Log.w(
          LOG_TAG,
          String.format("Could not delete test account %s: unknown reason", testAccountId));
    }
  }
  public static void setAppEventAttributionParameters(
      GraphObject params,
      AttributionIdentifiers attributionIdentifiers,
      String hashedDeviceAndAppId,
      boolean limitEventUsage) {
    // Send attributionID if it exists, otherwise send a hashed device+appid specific value as the
    // advertiser_id.
    if (attributionIdentifiers != null && attributionIdentifiers.getAttributionId() != null) {
      params.setProperty("attribution", attributionIdentifiers.getAttributionId());
    }

    if (attributionIdentifiers != null && attributionIdentifiers.getAndroidAdvertiserId() != null) {
      params.setProperty("advertiser_id", attributionIdentifiers.getAndroidAdvertiserId());
      params.setProperty(
          "advertiser_tracking_enabled", !attributionIdentifiers.isTrackingLimited());
    } else if (hashedDeviceAndAppId != null) {
      params.setProperty("advertiser_id", hashedDeviceAndAppId);
    }

    params.setProperty("application_tracking_enabled", !limitEventUsage);
  }
Ejemplo n.º 7
0
 /** Parse the interest field from response */
 private void handleStatusResponse(Response response) {
   if (response != null) {
     if (response.getError() == null) {
       GraphObject graphObject = response.getGraphObject();
       if (graphObject != null) {
         JSONObject jsonObject = graphObject.getInnerJSONObject();
         if (jsonObject != null) {
           try {
             JSONArray jsonArray = jsonObject.getJSONArray("data");
             if (jsonArray != null) {
               int length = jsonArray.length();
               if (length > 0) {
                 mStatusListAdapter.getStatusObjectList().clear();
                 for (int i = 0; i < length; i++) {
                   JSONObject object = jsonArray.getJSONObject(i);
                   String status_message = object.optString("message");
                   String status_time = object.optString("updated_time");
                   mStatusListAdapter
                       .getStatusObjectList()
                       .add(new StatusMessageObject(status_message, status_time));
                 }
                 // Notify to update user status message list.
                 mStatusListAdapter.notifyDataSetChanged();
               }
             }
           } catch (JSONException e) {
             Toast.makeText(
                     getActivity(), "StatusesRequest return wrong json data !", Toast.LENGTH_LONG)
                 .show();
           }
         }
       }
     } else {
       Utils.handleError(getActivity(), response.getError(), Utils.getUserStatusReadPermission());
     }
   } else {
     Toast.makeText(getActivity(), "StatusesRequest return null !", Toast.LENGTH_LONG).show();
   }
 }
 @Override
 public void onCompleted(Response response) {
   FacebookRequestError error = response.getError();
   if (error != null) {
     JSONObject jsonObject = new JSONObject();
     try {
       jsonObject.put("type", error.getErrorType());
       jsonObject.put("message", error.getErrorMessage());
       nativeCallback(mCallbackIndex, "{\"error\":" + jsonObject.toString() + "}");
     } catch (JSONException e) {
       e.printStackTrace();
     }
   } else {
     GraphObject object = response.getGraphObject();
     Object tObject = object.getProperty(Response.NON_JSON_RESPONSE_PROPERTY);
     if (tObject == null && object != null) {
       JSONObject jsonObject = object.getInnerJSONObject();
       if (jsonObject != null) nativeCallback(mCallbackIndex, jsonObject.toString());
     } else {
       nativeCallback(mCallbackIndex, tObject.toString());
     }
   }
 }
 private static void populateTestAccounts(Collection<TestSession.TestAccount> paramCollection, GraphObject paramGraphObject)
 {
   try
   {
     paramCollection = paramCollection.iterator();
     while (paramCollection.hasNext())
     {
       TestSession.TestAccount localTestAccount = (TestSession.TestAccount)paramCollection.next();
       localTestAccount.setName(((GraphUser)paramGraphObject.getPropertyAs(localTestAccount.getId(), GraphUser.class)).getName());
       storeTestAccount(localTestAccount);
     }
   }
   finally {}
 }
Ejemplo n.º 10
0
  static Response publishInstallAndWaitForResponse(
      final Context context, final String applicationId, final boolean isAutoPublish) {
    try {
      if (context == null || applicationId == null) {
        throw new IllegalArgumentException("Both context and applicationId must be non-null");
      }
      AttributionIdentifiers identifiers =
          AttributionIdentifiers.getAttributionIdentifiers(context);
      SharedPreferences preferences =
          context.getSharedPreferences(ATTRIBUTION_PREFERENCES, Context.MODE_PRIVATE);
      String pingKey = applicationId + "ping";
      String jsonKey = applicationId + "json";
      long lastPing = preferences.getLong(pingKey, 0);
      String lastResponseJSON = preferences.getString(jsonKey, null);

      // prevent auto publish from occurring if we have an explicit call.
      if (!isAutoPublish) {
        setShouldAutoPublishInstall(false);
      }

      GraphObject publishParams = GraphObject.Factory.create();
      publishParams.setProperty(ANALYTICS_EVENT, MOBILE_INSTALL_EVENT);

      Utility.setAppEventAttributionParameters(
          publishParams,
          identifiers,
          Utility.getHashedDeviceAndAppID(context, applicationId),
          getLimitEventAndDataUsage(context));
      publishParams.setProperty(AUTO_PUBLISH, isAutoPublish);
      publishParams.setProperty("application_package_name", context.getPackageName());

      String publishUrl = String.format(PUBLISH_ACTIVITY_PATH, applicationId);
      Request publishRequest = Request.newPostRequest(null, publishUrl, publishParams, null);

      if (lastPing != 0) {
        GraphObject graphObject = null;
        try {
          if (lastResponseJSON != null) {
            graphObject = GraphObject.Factory.create(new JSONObject(lastResponseJSON));
          }
        } catch (JSONException je) {
          // return the default graph object if there is any problem reading the data.
        }
        if (graphObject == null) {
          return Response.createResponsesFromString(
                  "true", null, new RequestBatch(publishRequest), true)
              .get(0);
        } else {
          return new Response(null, null, null, graphObject, true);
        }
      } else if (identifiers == null
          || (identifiers.getAndroidAdvertiserId() == null
              && identifiers.getAttributionId() == null)) {
        throw new FacebookException("No attribution id available to send to server.");
      } else {
        if (!Utility.queryAppSettings(applicationId, false).supportsAttribution()) {
          throw new FacebookException("Install attribution has been disabled on the server.");
        }

        Response publishResponse = publishRequest.executeAndWait();

        // denote success since no error threw from the post.
        SharedPreferences.Editor editor = preferences.edit();
        lastPing = System.currentTimeMillis();
        editor.putLong(pingKey, lastPing);

        // if we got an object response back, cache the string of the JSON.
        if (publishResponse.getGraphObject() != null
            && publishResponse.getGraphObject().getInnerJSONObject() != null) {
          editor.putString(
              jsonKey, publishResponse.getGraphObject().getInnerJSONObject().toString());
        }
        editor.apply();

        return publishResponse;
      }
    } catch (Exception e) {
      // if there was an error, fall through to the failure case.
      Utility.logd("Facebook-publish", e);
      return new Response(null, null, new FacebookRequestError(null, e));
    }
  }
Ejemplo n.º 11
0
  private Photo(GraphObject graphObject) {

    if (graphObject == null) return;

    // id
    mId = Utils.getPropertyString(graphObject, ID);

    // album
    mAlbum = Album.create(graphObject.getPropertyAs(ALBUM, GraphObject.class));

    // back date time
    mBackDatetime = Utils.getPropertyLong(graphObject, BACKDATED_TIME);

    // back date time granularity
    String granularity = Utils.getPropertyString(graphObject, BACKDATED_TIME_GRANULARITY);
    mBackDatetimeGranularity = BackDatetimeGranularity.fromValue(granularity);

    // created time
    mCreatedTime = Utils.getPropertyLong(graphObject, CREATED_TIME);

    // from
    mFrom = Utils.createUser(graphObject, FROM);

    // height
    mHeight = Utils.getPropertyInteger(graphObject, HEIGHT);

    // icon
    mIcon = Utils.getPropertyString(graphObject, ICON);

    // image sources
    mImageSources =
        Utils.createList(
            graphObject,
            IMAGES,
            new Converter<ImageSource>() {
              @Override
              public ImageSource convert(GraphObject graphObject) {
                ImageSource imageSource = new ImageSource();
                imageSource.mHeight = Utils.getPropertyInteger(graphObject, HEIGHT);
                imageSource.mWidth = Utils.getPropertyInteger(graphObject, WIDTH);
                imageSource.mSource = Utils.getPropertyString(graphObject, SOURCE);
                return imageSource;
              }
            });

    // link
    mLink = Utils.getPropertyString(graphObject, LINK);

    // name
    mName = Utils.getPropertyString(graphObject, NAME);

    // page story id
    mPageStoryId = Utils.getPropertyString(graphObject, PAGE_STORY_ID);

    // picture
    mPicture = Utils.getPropertyString(graphObject, PICTURE);

    // source
    mSource = Utils.getPropertyString(graphObject, SOURCE);

    // updated time
    mUpdatedTime = Utils.getPropertyLong(graphObject, UPDATED_TIME);

    // width
    mWidth = Utils.getPropertyInteger(graphObject, WIDTH);

    // place
    mPlace = Place.create(graphObject.getPropertyAs(PLACE, GraphObject.class));
  }
  @Override
  protected void executeImpl() {
    if (FacebookDialog.canPresentOpenGraphActionDialog(
        sessionManager.getActivity(), OpenGraphActionDialogFeature.OG_ACTION_DIALOG)) {

      FacebookDialog shareDialog = null;

      /*
       * Publishing open graph can be in 2 ways: 1. Publish actions on
       * app-owned objects Means, you as developer of the app define Open
       * Graph Object on your server with <meta> tags or you have
       * published object to facebook server. This is predefined Object
       * and user can't change it. 2. Publish actions on user-owned
       * objects You didn't add anything to server, but you give the user
       * the option to define Object Graph properties from the app and
       * publish it.
       */

      String objectId = mStory.getStoryObject().getId();
      String objectUrl = mStory.getStoryObject().getHostedUrl();
      final boolean predefineObject;
      if (objectId != null || objectUrl != null) {
        predefineObject = true;
      } else {
        predefineObject = false;
      }

      // set the option 1
      if (predefineObject) {
        OpenGraphAction action = OpenGraphAction.Factory.createForPost(mStory.getPath());
        action.setProperty(
            mStory.getStoryObject().getNoun(), objectId != null ? objectId : objectUrl);
        Iterator<String> actionProperties = mStory.getStoryAction().getParams().keySet().iterator();
        while (actionProperties.hasNext()) {
          String property = actionProperties.next();
          action.setProperty(property, mStory.getStoryAction().getParams().get(property));
        }

        // set share dialog
        shareDialog =
            new FacebookDialog.OpenGraphActionDialogBuilder(
                    sessionManager.getActivity(), action, mStory.getStoryObject().getNoun())
                .build();

      } else {
        // set the option 2
        OpenGraphObject object = OpenGraphObject.Factory.createForPost(mStory.getObjectType());
        Iterator<String> objectProperties =
            mStory.getStoryObject().getObjectProperties().keySet().iterator();
        while (objectProperties.hasNext()) {
          String property = objectProperties.next();
          object.setProperty(property, mStory.getStoryObject().getObjectProperties().get(property));
        }

        // set custom object properties
        GraphObject data = mStory.getStoryObject().getData();
        if (data != null) {
          for (Entry<String, Object> property : data.asMap().entrySet()) {
            object.getData().setProperty(property.getKey(), property.getValue());
          }
        }

        OpenGraphAction action = OpenGraphAction.Factory.createForPost(mStory.getPath());
        action.setProperty(mStory.getStoryObject().getNoun(), object);

        // set custom action properties
        Iterator<String> actionProperties = mStory.getStoryAction().getParams().keySet().iterator();
        while (actionProperties.hasNext()) {
          String property = actionProperties.next();
          action.setProperty(property, mStory.getStoryAction().getParams().get(property));
        }

        shareDialog =
            new FacebookDialog.OpenGraphActionDialogBuilder(
                    sessionManager.getActivity(), action, mStory.getStoryObject().getNoun())
                .build();
      }

      PendingCall pendingCall = shareDialog.present();
      sessionManager.trackFacebookDialogPendingCall(
          pendingCall,
          new FacebookDialog.Callback() {

            @Override
            public void onError(PendingCall pendingCall, Exception error, Bundle data) {
              sessionManager.untrackPendingCall();
              Logger.logError(
                  PublishStoryDialogAction.class, "Failed to share by using native dialog", error);
              if ("".equals(error.getMessage())) {
                Logger.logError(
                    PublishStoryDialogAction.class,
                    "Make sure to have 'app_id' meta data value in your manifest",
                    error);
              }
              mOnPublishListener.onFail(
                  "Have you added com.facebook.NativeAppCallContentProvider to your manifest? "
                      + error.getMessage());
            }

            @Override
            public void onComplete(PendingCall pendingCall, Bundle data) {
              sessionManager.untrackPendingCall();
              boolean didComplete = FacebookDialog.getNativeDialogDidComplete(data);
              String postId = FacebookDialog.getNativeDialogPostId(data);
              String completeGesture = FacebookDialog.getNativeDialogCompletionGesture(data);
              if (completeGesture != null) {
                if (completeGesture.equals("post")) {
                  mOnPublishListener.onComplete(postId != null ? postId : "no postId return");
                } else {
                  mOnPublishListener.onFail("Canceled by user");
                }
              } else if (didComplete) {
                mOnPublishListener.onComplete(
                    postId != null
                        ? postId
                        : "published successfully. (post id is not availaible if you are not logged in)");
              } else {
                mOnPublishListener.onFail("Canceled by user");
              }
            }
          });
    } else {
      mOnPublishListener.onFail("Open graph sharing dialog isn't supported");
    }
  }