private static String initGeckoEnvironment() {
    final Context context = GeckoAppShell.getApplicationContext();
    GeckoLoader.loadMozGlue(context);
    setState(State.MOZGLUE_READY);

    final Locale locale = Locale.getDefault();
    final Resources res = context.getResources();
    if (locale.toString().equalsIgnoreCase("zh_hk")) {
      final Locale mappedLocale = Locale.TRADITIONAL_CHINESE;
      Locale.setDefault(mappedLocale);
      Configuration config = res.getConfiguration();
      config.locale = mappedLocale;
      res.updateConfiguration(config, null);
    }

    String[] pluginDirs = null;
    try {
      pluginDirs = GeckoAppShell.getPluginDirectories();
    } catch (Exception e) {
      Log.w(LOGTAG, "Caught exception getting plugin dirs.", e);
    }

    final String resourcePath = context.getPackageResourcePath();
    GeckoLoader.setupGeckoEnvironment(context, pluginDirs, context.getFilesDir().getPath());

    GeckoLoader.loadSQLiteLibs(context, resourcePath);
    GeckoLoader.loadNSSLibs(context, resourcePath);
    GeckoLoader.loadGeckoLibs(context, resourcePath);
    setState(State.LIBS_READY);

    return resourcePath;
  }
    @Override // NativeEventListener
    public void handleMessage(
        final String event, final NativeJSObject message, final EventCallback callback) {
      final Context context = GeckoAppShell.getApplicationContext();
      switch (event) {
        case "Gecko:ScheduleRun":
          if (DEBUG) {
            Log.d(
                LOGTAG,
                "Scheduling "
                    + message.getString("action")
                    + " @ "
                    + message.getInt("interval")
                    + "ms");
          }

          final Intent intent = getIntentForAction(context, message.getString("action"));
          final PendingIntent pendingIntent =
              PendingIntent.getService(
                  context, /* requestCode */ 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

          final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
          // Cancel any previous alarm and schedule a new one.
          am.setInexactRepeating(
              AlarmManager.ELAPSED_REALTIME,
              message.getInt("trigger"),
              message.getInt("interval"),
              pendingIntent);
          break;

        default:
          throw new UnsupportedOperationException(event);
      }
    }
 public double[] getCurrentInformation() {
   final Context applicationContext = GeckoAppShell.getApplicationContext();
   final ConnectionType connectionType = currentConnectionType;
   return new double[] {
     connectionType.value,
     connectionType == ConnectionType.WIFI ? 1.0 : 0.0,
     connectionType == ConnectionType.WIFI ? wifiDhcpGatewayAddress(applicationContext) : 0.0
   };
 }
  /** Send current network state and connection type as a GeckoEvent, to whomever is listening. */
  private void sendNetworkStateToListeners() {
    final Context applicationContext = GeckoAppShell.getApplicationContext();
    final GeckoEvent networkEvent =
        GeckoEvent.createNetworkEvent(
            currentConnectionType.value,
            currentConnectionType == ConnectionType.WIFI,
            wifiDhcpGatewayAddress(applicationContext),
            currentConnectionSubtype.value);
    final GeckoEvent networkLinkChangeValueEvent =
        GeckoEvent.createNetworkLinkChangeEvent(currentNetworkStatus.value);
    final GeckoEvent networkLinkChangeNotificationEvent =
        GeckoEvent.createNetworkLinkChangeEvent(LINK_DATA_CHANGED);

    GeckoAppShell.sendEventToGecko(networkEvent);
    GeckoAppShell.sendEventToGecko(networkLinkChangeValueEvent);
    GeckoAppShell.sendEventToGecko(networkLinkChangeNotificationEvent);
  }
 /** Update current network state and connection types. */
 private void updateNetworkStateAndConnectionType() {
   final Context applicationContext = GeckoAppShell.getApplicationContext();
   final ConnectivityManager connectivityManager =
       (ConnectivityManager) applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
   // Type/status getters below all have a defined behaviour for when connectivityManager == null
   if (connectivityManager == null) {
     Log.e(LOGTAG, "ConnectivityManager does not exist.");
   }
   currentConnectionType = NetworkUtils.getConnectionType(connectivityManager);
   currentNetworkStatus = NetworkUtils.getNetworkStatus(connectivityManager);
   currentConnectionSubtype = NetworkUtils.getConnectionSubType(connectivityManager);
   Log.d(
       LOGTAG,
       "New network state: "
           + currentNetworkStatus
           + ", "
           + currentConnectionType
           + ", "
           + currentConnectionSubtype);
 }
  // This function only works for IPv4
  private void getWifiIPAddress(final EventCallback callback) {
    final WifiManager mgr =
        (WifiManager) GeckoAppShell.getApplicationContext().getSystemService(Context.WIFI_SERVICE);

    if (mgr == null) {
      callback.sendError("Cannot get WifiManager");
      return;
    }

    final WifiInfo info = mgr.getConnectionInfo();
    if (info == null) {
      callback.sendError("Cannot get connection info");
      return;
    }

    int ip = info.getIpAddress();
    if (ip == 0) {
      callback.sendError("Cannot get IPv4 address");
      return;
    }
    callback.sendSuccess(Formatter.formatIpAddress(ip));
  }
  @Override
  /** Handles native messages, not part of the state machine flow. */
  public void handleMessage(
      final String event, final NativeJSObject message, final EventCallback callback) {
    final Context applicationContext = GeckoAppShell.getApplicationContext();
    switch (event) {
      case "Wifi:Enable":
        final WifiManager mgr =
            (WifiManager) applicationContext.getSystemService(Context.WIFI_SERVICE);

        if (!mgr.isWifiEnabled()) {
          mgr.setWifiEnabled(true);
        } else {
          // If Wifi is enabled, maybe you need to select a network
          Intent intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          applicationContext.startActivity(intent);
        }
        break;
      case "Wifi:GetIPAddress":
        getWifiIPAddress(callback);
        break;
    }
  }
 @JNITarget
 public static int getMNC() {
   return getNetworkOperator(InfoType.MNC, GeckoAppShell.getApplicationContext());
 }
 /** Start listening for network state updates. */
 private void registerBroadcastReceiver() {
   final IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
   GeckoAppShell.getApplicationContext().registerReceiver(this, filter);
 }
 /** Stop listening for network state updates. */
 private void unregisterBroadcastReceiver() {
   GeckoAppShell.getApplicationContext().unregisterReceiver(this);
 }