/*
 * Copyright 2017-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.android.support.exopackage;

import java.util.ArrayList;
import java.util.List;

/** Support and Utility Functions for Exopackage, including hotswap helper functions */
public class ExoHelper {
  private static final List<OnModulesChangedCallback> sCallbacks =
      new ArrayList<OnModulesChangedCallback>();

  /**
   * Add a callback which will fire whenever code definitions have changed in the app. This callback
   * will be executed on the main thread.
   *
   * @param callback the callback to add
   */
  public static void addOnModulesChangedCallback(OnModulesChangedCallback callback) {
    synchronized (sCallbacks) {
      sCallbacks.add(callback);
    }
  }

  /**
   * Remove a previously added callback
   *
   * @param callback the callback to remove
   */
  public static void removeOnModulesChangedCallback(OnModulesChangedCallback callback) {
    synchronized (sCallbacks) {
      sCallbacks.remove(callback);
    }
  }

  /**
   * Trigger all callbacks which have registered to receive notifications when the module
   * definitions have changed on disk
   */
  static void triggerCallbacks() {
    synchronized (sCallbacks) {
      for (OnModulesChangedCallback mCallback : sCallbacks) {
        mCallback.onModulesChanged();
      }
    }
  }
}

@@ -655,16 +655,102 @@
 ckage;%0A%0A
+import android.app.Activity;%0Aimport android.content.Intent;%0Aimport android.os.Looper;%0A
 import j
@@ -769,16 +769,16 @@
 ayList;%0A
-
 import j
@@ -1023,16 +1023,432 @@
 ck%3E();%0A%0A
+  /**%0A   * Restart the given activity. The same intent will be used to launch the new activity, preserving%0A   * actions, extras, etc. Must be run on the main thread.%0A   *%0A   * @param activity the activity to restart%0A   */%0A  public static void restartActivity(Activity activity) %7B%0A    assertIsUiThread();%0A    final Intent intent = activity.getIntent();%0A    activity.startActivity(intent);%0A    activity.finish();%0A  %7D%0A%0A
   /**%0A  
@@ -2372,24 +2372,24 @@
 sChanged();%0A
-
       %7D%0A    
@@ -2393,11 +2393,220 @@
    %7D%0A  %7D
+%0A%0A  private static void assertIsUiThread() %7B%0A    if (Looper.getMainLooper().getThread() != Thread.currentThread()) %7B%0A      throw new RuntimeException(%22This method must be called from the UI thread%22);%0A    %7D%0A  %7D
 %0A%7D%0A

Add helper function to restart Activity