public Object decode(Object object) {
    if (object instanceof JSONArray) {
      return convertJSONArrayToList((JSONArray) object);
    }

    if (!(object instanceof JSONObject)) {
      return object;
    }

    JSONObject jsonObject = (JSONObject) object;

    String opString = jsonObject.optString("__op", null);
    if (opString != null) {
      try {
        return ParseFieldOperations.decode(jsonObject, this);
      } catch (JSONException e) {
        throw new RuntimeException(e);
      }
    }

    String typeString = jsonObject.optString("__type", null);
    if (typeString == null) {
      return convertJSONObjectToMap(jsonObject);
    }

    if (typeString.equals("Date")) {
      String iso = jsonObject.optString("iso");
      return ParseDateFormat.getInstance().parse(iso);
    }

    if (typeString.equals("Bytes")) {
      String base64 = jsonObject.optString("base64");
      return Base64.decode(base64, Base64.NO_WRAP);
    }

    if (typeString.equals("Pointer")) {
      return decodePointer(jsonObject.optString("className"), jsonObject.optString("objectId"));
    }

    if (typeString.equals("File")) {
      return new ParseFile(jsonObject, this);
    }

    if (typeString.equals("GeoPoint")) {
      double latitude, longitude;
      try {
        latitude = jsonObject.getDouble("latitude");
        longitude = jsonObject.getDouble("longitude");
      } catch (JSONException e) {
        throw new RuntimeException(e);
      }
      return new ParseGeoPoint(latitude, longitude);
    }

    if (typeString.equals("Object")) {
      return ParseObject.fromJSON(jsonObject, null, true, this);
    }

    if (typeString.equals("Relation")) {
      return new ParseRelation<>(jsonObject, this);
    }

    if (typeString.equals("OfflineObject")) {
      throw new RuntimeException("An unexpected offline pointer was encountered.");
    }

    return null;
  }
Ejemplo n.º 2
0
  /**
   * Authenticates this client as belonging to your application.
   *
   * <p>This method is only required if you intend to use a different {@code applicationId} or
   * {@code clientKey} than is defined by {@code com.parse.APPLICATION_ID} or {@code
   * com.parse.CLIENT_KEY} in your {@code AndroidManifest.xml}.
   *
   * <p>This must be called before your application can use the Parse library. The recommended way
   * is to put a call to {@code Parse.initialize} in your {@code Application}'s {@code onCreate}
   * method:
   *
   * <p>
   *
   * <pre>
   * public class MyApplication extends Application {
   *   public void onCreate() {
   *     Parse.initialize(this, &quot;your application id&quot;, &quot;your client key&quot;);
   *   }
   * }
   * </pre>
   *
   * @param context The active {@link Context} for your application.
   * @param applicationId The application id provided in the Parse dashboard.
   * @param clientKey The client key provided in the Parse dashboard.
   */
  public static void initialize(Context context, String applicationId, String clientKey) {
    ParsePlugins.Android.initialize(context, applicationId, clientKey);
    Context applicationContext = context.getApplicationContext();

    ParseHttpClient.setKeepAlive(true);
    ParseHttpClient.setMaxConnections(20);
    ParseRequest.setDefaultClient(ParsePlugins.get().restClient());
    // If we have interceptors in list, we have to initialize all http clients and add interceptors
    if (interceptors != null) {
      initializeParseHttpClientsWithParseNetworkInterceptors();
    }

    ParseObject.registerParseSubclasses();

    if (isLocalDatastoreEnabled()) {
      offlineStore = new OfflineStore(context);
    } else {
      ParseKeyValueCache.initialize(context);
    }

    // Make sure the data on disk for Parse is for the current
    // application.
    checkCacheApplicationId();
    new Thread("Parse.initialize Disk Check & Starting Command Cache") {
      @Override
      public void run() {
        // Trigger the command cache to flush its contents.
        getEventuallyQueue();
      }
    }.start();

    ParseFieldOperations.registerDefaultDecoders();

    if (!allParsePushIntentReceiversInternal()) {
      throw new SecurityException(
          "To prevent external tampering to your app's notifications, "
              + "all receivers registered to handle the following actions must have "
              + "their exported attributes set to false: com.parse.push.intent.RECEIVE, "
              + "com.parse.push.intent.OPEN, com.parse.push.intent.DELETE");
    }

    // May need to update GCM registration ID if app version has changed.
    // This also primes current installation.
    GcmRegistrar.getInstance()
        .registerAsync()
        .continueWithTask(
            new Continuation<Void, Task<Void>>() {
              @Override
              public Task<Void> then(Task<Void> task) throws Exception {
                // Prime current user in the background
                return ParseUser.getCurrentUserAsync().makeVoid();
              }
            })
        .continueWith(
            new Continuation<Void, Void>() {
              @Override
              public Void then(Task<Void> task) throws Exception {
                // Prime config in the background
                ParseConfig.getCurrentConfig();
                return null;
              }
            },
            Task.BACKGROUND_EXECUTOR);

    if (ManifestInfo.getPushType() == PushType.PPNS) {
      PushService.startServiceIfRequired(applicationContext);
    }

    dispatchOnParseInitialized();

    // FYI we probably don't want to do this if we ever add other callbacks.
    synchronized (MUTEX_CALLBACKS) {
      Parse.callbacks = null;
    }
  }