예제 #1
0
  int getPosition(String sectionKey, T graphObject) {
    int position = 0;
    boolean found = false;

    // First find the section key and increment position one for each header we will render;
    // increment by the size of each section prior to the one we want.
    for (String key : sectionKeys) {
      if (displaySections) {
        position++;
      }
      if (key.equals(sectionKey)) {
        found = true;
        break;
      } else {
        position += graphObjectsBySection.get(key).size();
      }
    }

    if (!found) {
      return -1;
    } else if (graphObject == null) {
      // null represents the header for a section; we counted this header in position earlier,
      // so subtract it back out.
      return position - (displaySections ? 1 : 0);
    }

    // Now find index of this item within that section.
    for (T t : graphObjectsBySection.get(sectionKey)) {
      if (GraphObject.Factory.hasSameId(t, graphObject)) {
        return position;
      }
      position++;
    }
    return -1;
  }
예제 #2
0
  protected URI getPictureUriOfGraphObject(T graphObject) {
    String uri = null;
    Object o = graphObject.getProperty(PICTURE);
    if (o instanceof String) {
      uri = (String) o;
    } else if (o instanceof JSONObject) {
      ItemPicture itemPicture = GraphObject.Factory.create((JSONObject) o).cast(ItemPicture.class);
      ItemPictureData data = itemPicture.getData();
      if (data != null) {
        uri = data.getUrl();
      }
    }

    if (uri != null) {
      try {
        return new URI(uri);
      } catch (URISyntaxException e) {
      }
    }
    return null;
  }
예제 #3
0
 private ArrayList<GraphUser> restoreByteArray(byte[] bytes) {
   try {
     @SuppressWarnings("unchecked")
     ArrayList<String> usersAsString =
         (ArrayList<String>)
             (new ObjectInputStream(new ByteArrayInputStream(bytes))).readObject();
     if (usersAsString != null) {
       ArrayList<GraphUser> users = new ArrayList<GraphUser>(usersAsString.size());
       for (String user : usersAsString) {
         GraphUser graphUser = GraphObject.Factory.create(new JSONObject(user), GraphUser.class);
         users.add(graphUser);
       }
       return users;
     }
   } catch (ClassNotFoundException e) {
     Log.e(TAG, "Unable to deserialize users.", e);
   } catch (IOException e) {
     Log.e(TAG, "Unable to deserialize users.", e);
   } catch (JSONException e) {
     Log.e(TAG, "Unable to deserialize users.", e);
   }
   return null;
 }
예제 #4
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));
    }
  }