예제 #1
0
  @Override
  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    AndroidSystem system =
        (AndroidSystem)
            AndroidServiceLocator.GetInstance()
                .GetService(AndroidServiceLocator.SERVICE_TYPE_SYSTEM);
    boolean locked = system.IsOrientationLocked();
    if (locked) {
      int configOrientation;
      DisplayOrientation lockedOrientation = system.GetLockedOrientation();
      if (DisplayOrientation.Portrait.equals(lockedOrientation)) {
        configOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
      } else if (DisplayOrientation.Landscape.equals(lockedOrientation)) {
        configOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
      } else {
        // Portrait as default orientation
        configOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
      }
      if (newConfig.orientation != configOrientation) {
        LOG.Log(
            Module.GUI,
            "Main Activity onConfigurationChanged setting requested orientation: "
                + configOrientation);

        setRequestedOrientation(configOrientation);
      }
    } else {
      activityManager.layoutSplashscreen();
      appView.requestLayout();
    }
  }
  /** Initializes the appverse context exposing data to the WebView Javascript DOM. */
  private void InitializeAppverseContext(int networkType) {

    long startTime = System.currentTimeMillis();

    try {

      LOG.Log(
          Module.GUI,
          "Before loading the main HTML, platform will expose some information directly to javascript...");

      AndroidSystem systemService =
          (AndroidSystem)
              AndroidServiceLocator.GetInstance()
                  .GetService(AndroidServiceLocator.SERVICE_TYPE_SYSTEM);
      AndroidI18N i18nService =
          (AndroidI18N)
              AndroidServiceLocator.GetInstance()
                  .GetService(AndroidServiceLocator.SERVICE_TYPE_I18N);
      AndroidIO ioService =
          (AndroidIO)
              AndroidServiceLocator.GetInstance().GetService(AndroidServiceLocator.SERVICE_TYPE_IO);
      IActivityManager am =
          (IActivityManager)
              AndroidServiceLocator.GetInstance()
                  .GetService(AndroidServiceLocator.SERVICE_ANDROID_ACTIVITY_MANAGER);

      // 1. Appverse Context (Appverse.is)
      UnityContext unityContext = systemService.GetUnityContext();

      String unityContextJsonString = JSONSerializer.serialize(unityContext);
      unityContextJsonString = "_AppverseContext = " + unityContextJsonString;
      LOG.LogDebug(Module.GUI, "InitializeAppverseContext: " + unityContextJsonString);
      am.executeJS(unityContextJsonString);

      // 2. OS Info (Appverse.OSInfo)
      OSInfo osInfo = systemService.GetOSInfo();

      String osInfoJsonString = JSONSerializer.serialize(osInfo);
      osInfoJsonString = "_OSInfo = " + osInfoJsonString;
      LOG.LogDebug(Module.GUI, "InitializeAppverseContext: " + osInfoJsonString);
      am.executeJS(osInfoJsonString);

      // 3. Hardware Info (Appverse.HardwareInfo)
      HardwareInfo hwInfo = systemService.GetOSHardwareInfo();
      String hwInfoJsonString = JSONSerializer.serialize(hwInfo);
      hwInfoJsonString = "_HwInfo = " + hwInfoJsonString;
      LOG.LogDebug(Module.GUI, "InitializeAppverseContext: " + hwInfoJsonString);
      am.executeJS(hwInfoJsonString);
      try {
        // 4. Get all configured localized keys (Appverse.i18n)
        Locale[] supportedLocales = i18nService.GetLocaleSupported();
        String localizedStrings =
            "_i18n = {}; _i18n['default'] = '" + i18nService.getDefaultLocale() + "'; ";
        String localeLiterals = "";
        for (Locale supportedLocale : supportedLocales) {
          ResourceLiteralDictionary literals = i18nService.GetResourceLiterals(supportedLocale);
          String literalsJsonString = JSONSerializer.serialize(literals);
          localeLiterals =
              localeLiterals
                  + " _i18n['"
                  + supportedLocale.toString()
                  + "'] = "
                  + literalsJsonString
                  + "; ";
        }
        localizedStrings = localizedStrings + localeLiterals;
        LOG.LogDebug(Module.GUI, "InitializeAppverseContext: " + localizedStrings);
        am.executeJS(localizedStrings);
      } catch (Exception ex) {
        LOG.LogDebug(
            Module.GUI, "Unable to load all languages. Exception message: " + ex.getMessage());
      }
      // 5. Current device locale
      com.gft.unity.core.system.Locale currentLocale = systemService.GetLocaleCurrent();
      String currentLocaleJsonString = JSONSerializer.serialize(currentLocale);
      currentLocaleJsonString = "_CurrentDeviceLocale = " + currentLocaleJsonString;
      LOG.LogDebug(Module.GUI, "InitializeAppverseContext: " + currentLocaleJsonString);
      am.executeJS(currentLocaleJsonString);

      try {
        // 6. Configured IO services endpoints
        IOService[] services = ioService.GetServices();
        String servicesJsonString = "_IOServices = {}; ";
        for (IOService service : services) {
          String serviceJson = JSONSerializer.serialize(service);
          servicesJsonString =
              servicesJsonString
                  + " _IOServices['"
                  + service.getName()
                  + "-"
                  + JSONSerializer.serialize(service.getType())
                  + "'] = "
                  + serviceJson
                  + "; ";
        }
        LOG.LogDebug(Module.GUI, "InitializeAppverseContext: " + servicesJsonString);
        am.executeJS(servicesJsonString);
      } catch (Exception ex) {
        LOG.LogDebug(
            Module.GUI, "Unable to load all services. Exception message: " + ex.getMessage());
      }
      String networkStatusString = "_NetworkStatus = " + networkType + ";";
      LOG.LogDebug(Module.GUI, "InitializeAppverseContext: networkType: " + networkType);
      am.executeJS(networkStatusString);

    } catch (Exception ex) {
      LOG.LogDebug(
          Module.GUI, "Unable to load Appverse Context. Exception message: " + ex.getMessage());
    }

    long timetaken = System.currentTimeMillis() - startTime;
    LOG.Log(Module.GUI, "# Time elapsed initializing Appverse Context: " + timetaken);
  }