/** * Check if this activity was launched from a local notification, and send details to application */ private void checkLaunchedFromNotification() { if (this.lastIntentExtras != null) { LOG.Log(Module.GUI, "Activity was launched from Notification Manager... "); final String message = lastIntentExtras.getString(NotificationUtils.EXTRA_MESSAGE); final String notificationSound = this.lastIntentExtras.getString(NotificationUtils.EXTRA_SOUND); final String customJSONString = this.lastIntentExtras.getString(NotificationUtils.EXTRA_CUSTOM_JSON); final String notificationId = lastIntentExtras.getString(NotificationUtils.EXTRA_NOTIFICATION_ID); final String notificationType = lastIntentExtras.getString(NotificationUtils.EXTRA_TYPE); LOG.Log(Module.GUI, notificationType + " Notification ID = " + notificationId); NotificationData notif = new NotificationData(); notif.setAlertMessage(message); notif.setSound(notificationSound); notif.setCustomDataJsonString(customJSONString); if (notificationType != null && notificationType.equals(NotificationUtils.NOTIFICATION_TYPE_LOCAL)) { appView.loadUrl( "javascript:try{Unity.OnLocalNotificationReceived(" + JSONSerializer.serialize(notif) + ")}catch(e){}"); } else if (notificationType != null && notificationType.equals(NotificationUtils.NOTIFICATION_TYPE_REMOTE)) { appView.loadUrl( "javascript:try{Unity.OnRemoteNotificationReceived(" + JSONSerializer.serialize(notif) + ")}catch(e){}"); } this.lastIntentExtras = null; } }
private void startServer() { if (server == null) { AssetManager am = (AssetManager) AndroidServiceLocator.GetInstance() .GetService(AndroidServiceLocator.SERVICE_ANDROID_ASSET_MANAGER); if (serverProperties == null) { serverProperties = new Properties(); try { serverProperties.load(am.open(SERVER_PROPERTIES)); } catch (IOException ex) { LOG.Log(Module.GUI, ex.toString()); } } LOG.Log( Module.GUI, "The Port is: " + serverProperties.getProperty(SERVER_PORT_PROPERTY, "Missing")); try { serverPort = Integer.parseInt(serverProperties.getProperty(SERVER_PORT_PROPERTY)); server = new HttpServer(serverPort, this, this.appView); server.start(); } catch (Exception ex) { LOG.Log(Module.GUI, ex.toString()); } LOG.Log(Module.GUI, "Server started."); } }
@Override protected void onDestroy() { LOG.Log(Module.GUI, "onDestroy"); // Stop HTTP server stopServer(); super.onDestroy(); LOG.Log(Module.GUI, "killing process..."); android.os.Process.killProcess(android.os.Process.myPid()); }
private boolean checkUnityProperty(String propertyName) { int resourceIdentifier = getResources().getIdentifier(propertyName, "string", getPackageName()); try { boolean propertyValue = Boolean.parseBoolean(getResources().getString(resourceIdentifier)); LOG.Log(Module.GUI, propertyName + "? " + propertyValue); return propertyValue; } catch (Exception ex) { LOG.Log(Module.GUI, "Exception getting value for " + propertyName + ": " + ex.getMessage()); return false; } }
@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(); } }
@Override protected void onResume() { super.onResume(); if (ProxySettings.checkSystemProxyProperties()) { ProxySettings.shouldSetProxySetting = true; } // Save the context for further access AndroidServiceLocator.setContext(this); NotificationManager nMngr = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); nMngr.cancelAll(); LOG.Log(Module.GUI, "onResume"); /* // security reasons if(splashShownOnBackground) { activityManager.dismissSplashScreen(); splashShownOnBackground = false; } */ // Start HTTP server startServer(); appView.loadUrl("javascript:try{Unity._toForeground()}catch(e){}"); // storing last intent extras if (this.getIntent() != null) { this.lastIntentExtras = this.getIntent().getExtras(); Bundle nullExtras = null; this.getIntent().replaceExtras(nullExtras); } }
private void stopServer() { if (server != null) { server.shutdown(); server = null; LOG.Log(Module.GUI, "Server stopped."); } }
@Override protected void onStop() { LOG.Log(Module.GUI, "onStop"); // Stop HTTP server stopServer(); super.onStop(); }
@Override public boolean onCreateThumbnail(Bitmap outBitmap, Canvas canvas) { LOG.Log(Module.GUI, "onCreateThumbnail"); if (!disableThumbnails) { return super.onCreateThumbnail(outBitmap, canvas); } else { return true; // for security reasons, thumbnails are not allowed } }
@Override protected void onPause() { LOG.Log(Module.GUI, "onPause"); appView.loadUrl("javascript:try{Unity._toBackground()}catch(e){}"); // Stop HTTP server stopServer(); super.onPause(); }
@Override public void onWindowFocusChanged(boolean hasFocus) { LOG.Log(Module.GUI, "onWindowFocusChanged"); if (hasFocus) { LOG.Log(Module.GUI, "application has focus; calling foreground listener"); appView.loadUrl("javascript:try{Unity._toForeground()}catch(e){}"); // check for notification details this.checkLaunchedFromNotification(); } else { LOG.Log(Module.GUI, "application lost focus; calling background listener"); appView.loadUrl("javascript:try{Unity._toBackground()}catch(e){}"); /* if (server == null) { // security reasons; the splash screen is shown when application enters in background (hiding sensitive data) // it will be dismissed "onResume" method if(!splashShownOnBackground) { splashShownOnBackground = activityManager.showSplashScreen(appView); } } */ } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LOG.Log(Module.GUI, "onCreate"); // GUI initialization code getWindow().requestFeature(Window.FEATURE_NO_TITLE); getWindow() .setFlags( WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); disableThumbnails = checkUnityProperty("Unity_DisableThumbnails"); // security reasons; don't allow screen shots while this window is displayed /* not valid for builds under level 14 */ if (disableThumbnails && Build.VERSION.SDK_INT >= 14) { getWindow() .setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE); } appView = new WebView(this); appView.enablePlatformNotifications(); setGlobalProxy(); appView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); appView.setId(APPVIEW_ID); appView.setWebViewClient(new UnityWebViewClient()); appView.getSettings().setJavaScriptEnabled(true); appView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); appView.getSettings().setAllowFileAccess(true); appView.getSettings().setSupportZoom(false); appView.getSettings().setAppCacheEnabled(false); appView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); appView.getSettings().setAppCacheMaxSize(0); appView.getSettings().setSavePassword(false); appView.getSettings().setSaveFormData(false); appView.getSettings().setDefaultTextEncodingName("UTF-8"); appView.getSettings().setGeolocationEnabled(true); appView.getSettings().setLightTouchEnabled(true); appView.getSettings().setRenderPriority(RenderPriority.HIGH); appView.getSettings().setDomStorageEnabled(true); // [MOBPLAT-129] enable HTML5 local storage appView.setVerticalScrollBarEnabled(false); // Required settings to enable HTML5 database storage appView.getSettings().setDatabaseEnabled(true); String databasePath = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); appView.getSettings().setDatabasePath(databasePath); webChromeClient = new WebChromeClient() { // Required settings to enable HTML5 database storage @Override public void onExceededDatabaseQuota( String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { quotaUpdater.updateQuota(estimatedSize * 2); }; @Override public void onReachedMaxAppCacheSize( long spaceNeeded, long totalUsedQuota, android.webkit.WebStorage.QuotaUpdater quotaUpdater) { quotaUpdater.updateQuota(0); }; @Override public boolean onConsoleMessage(ConsoleMessage cm) { LOG.Log( Module.GUI, cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId()); return true; } }; appView.setWebChromeClient(webChromeClient); // create the application logger LogManager.setDelegate(new AndroidLoggerDelegate()); // save the context for further access AndroidServiceLocator.setContext(this); // initialize the service locator activityManager = new AndroidActivityManager(this, appView); // killing previous background processes from the same package activityManager.killBackgroundProcesses(); AndroidServiceLocator serviceLocator = (AndroidServiceLocator) AndroidServiceLocator.GetInstance(); serviceLocator.RegisterService( this.getAssets(), AndroidServiceLocator.SERVICE_ANDROID_ASSET_MANAGER); serviceLocator.RegisterService( activityManager, AndroidServiceLocator.SERVICE_ANDROID_ACTIVITY_MANAGER); startServer(); /* THIS COULD NOT BE CHECKED ON API LEVEL < 11; NO suchmethodexception boolean hwAccelerated = appView.isHardwareAccelerated(); if(hwAccelerated) LOG.Log(Module.GUI,"Application View is HARDWARE ACCELERATED"); else LOG.Log(Module.GUI,"Application View is NOT hardware accelerated"); */ final IntentFilter actionFilter = new IntentFilter(); actionFilter.addAction(android.net.ConnectivityManager.CONNECTIVITY_ACTION); // actionFilter.addAction("android.intent.action.SERVICE_STATE"); registerReceiver(new AndroidNetworkReceiver(appView), actionFilter); final Activity currentContext = this; new Thread( new Runnable() { public void run() { currentContext.runOnUiThread( new Runnable() { public void run() { appView.loadUrl(WEBVIEW_MAIN_URL); } }); } }) .start(); holdSplashScreenOnStartup = checkUnityProperty("Unity_HoldSplashScreenOnStartup"); hasSplash = activityManager.showSplashScreen(appView); RemoteNotificationIntentService.loadNotificationOptions(getResources(), appView, this); LocalNotificationReceiver.initialize(appView, this); }
@Override protected void finalize() throws Throwable { LOG.Log(Module.GUI, "on finalize method. Stopping server..."); stopServer(); super.finalize(); }