Пример #1
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;
    }
  }