+ All Categories
Home > Documents > Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab...

Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab...

Date post: 10-Mar-2020
Category:
Upload: others
View: 5 times
Download: 0 times
Share this document with a friend
24
Lab 3: Using Worklight Server and Environment Optimization – Lab Exercise
Transcript
Page 1: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

Lab 3: Using Worklight Server and Environment Optimization – Lab Exercise

Page 2: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-2

Table of Contents

Lab 3 Using the Worklight Server and Environment Optimizations................................................. 3-4 3.1 Building and Testing on the Android Platform..................................................................................................................3-4

3.1.1 Adding a new Worklight environment........................................................................................................................... 3-5 3.1.2 Building and Testing the Android Application ........................................................................................................... 3-8 3.1.3 Optimizing for the Android Environment ...................................................................................................................3-13

3.2 Using In-App Notification via the Worklight Console.................................................................................................. 3-20 3.3 Summary............................................................................................................................................................................................. 3-24

Page 3: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-3

Page 4: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-4

Lab 3 Using the Worklight Server and Environment Optimizations

In Lab 1 and 2, you familiarized yourself with building and testing a mobile application using Worklight Studio. In Lab 3, you are going to continue to work with the application in the Worklight Studio, but you will also work with the Worklight Server.

In this lab, you will learn how to do the following:

Create a new Worklight environment in Worklight Studio

Build and test an application for Android environments

Use environment optimization capabilities in Worklight

Use the direct update feature of Worklight Server

3.1 Building and Testing on the Android Platform IBM Worklight supports the deployment of mobile applications to multiple different operating system platforms. In Lab 1, you saw how to build and deploy applications to the iOS platform. In this section of the lab, you will use the Worklight Studio to add support for the Android platform.

Adding support for an additional platform that is supported by Worklight is an easy task. You start by adding a new Worklight environment for the desired platform, and then you can use the appropriate native tools to produce a device installable for the target platform. In the case of Android, Worklight integrates directly with the Android SDK, which means that you can build the device installable application and launch it on an emulator without leaving the Worklight Studio.

Page 5: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-5

3.1.1 Adding a new Worklight environment In this part of the lab, you will add a new Worklight environment for Android.

__1. In the workbench, expand the MyMemories Worklight project > expand apps folder > right-click on the MyMemories application folder > New > Worklight Environment.

__2. Select the Android phones and tablets checkbox and click Finish.

__3. You will see a new folder named android appears under the MyMemories application. In

addition, a top-level project called MyMemoriesMyMemoriesAndroid now exists.

Page 6: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-6

__4. The android environment folder under the MyMemories application folder will hold web and native content specific to the Android platform. The MyMemoriesMyMemoriesAndroid project was created automatically because the Android Development Tools (ADT) Eclipse Plugin has been installed into this environment. You can use this project to develop any native (Java for Android) code for your application.

__5. Expand the android environment > native > res > xml > open config.xml.

__6. Switch to the config.xml tab.

Page 7: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-7

__7. Add the following line at the <preference> section. <preference name="loadUrlTimeoutValue" value="60000" />

This is snippet #1 in C:\WorklightLab\imports\lab3\Lab3.snippets.txt.

__8. Save the config.xml file.

__9. Open AndroidManifest.xml file into the editor. It’s under the native folder of the android environment.

Page 8: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-8

__10. Switch to the AndroidManifest.xml tab into the editor.

__11. Replace <uses-sdk android:minSdkVersion=”8” /> with the following line. <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16"/>

This is snippet #2 in C:\WorklightLab\imports\lab3\Lab3.snippets.txt.

__12. Save the AndroidManifest.xml file.

3.1.2 Building and Testing the Android Application In this part of the lab, you will test the MyMemories application on an Android Virtual Device (AVD).

__1. Before testing the application on an AVD, you need to build and deploy the updated application to the Worklight Server. Expand MyMemories project > apps> right-click on the MyMemories > Run As > Build All and Deploy.

Page 9: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-9

__2. This will build the application with the new Android environment and deploy the updated artifacts

to the embedded Worklight Server. When the build and deploy is complete, you should see success messages in the console view in the Worklight Studio.

Page 10: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-10

__3. In the workbench, select Window > Android Virtual Device Manager.

__4. Select MyMemoriesAVD and click Start.

__5. Press Launch to start the virtual device.

Page 11: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-11

__6. It takes the Android emulator awhile to start. Once the Android emulator started, you can close the Android Virtual Device Manager.

__7. Change to the LogCat view while waiting for the emulator to start. You will begin to see logs

from the emulator.

__8. After the Android emulator is fully started, unlock the emulator by dragging the lock to the right.

You will be at the home screen.

Page 12: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-12

__9. To run the app in the Android Virtual Device, right-click on the MyMemoriesMyMemoriesAndroid project. Click Run As > Android Application.

__10. The MyMemories application will automatically open and start on the Android Virtual Device

instance. If the MyMemories application is not open after unlocking, wait a few moments as the application installation completes. Once the application is installed and opened, you should see the home screen for the MyMemories application (Note: The layout of your Android Virtual Device may vary from the below image, depending on which target platform and API level are configured for you emulator).

Page 13: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-13

__11. You can now test the application on the Android Virtual Device instance. Navigate to different screens by clicking on any of the icons or navigational buttons.

__12. To take a picture, the emulator will provide a green square animation and you can click the circle center button to take the picture. Click the checkmark to accept it.

__13. When done testing the application exit the application on the Android Virtual Device. Do not

close the Android Virtual Device instance as you will be using the application in the next section.

3.1.3 Optimizing for the Android Environment In this part of the lab, you will learn how to make optimizations for the MyMemories application when running on an Android platform. Additionally, you will discover the Direct Update feature of the Worklight runtime.

You will be introducing JavaScript that is specific to the Android platform. In particular, you will use the Worklight client-side JavaScript API to dynamically create menu items. The menu items will provide an alternative means of navigation within the application so that you can get to any screen from any other screen in the application. The ability to create menu items is only applicable on the Android and Windows Phone platforms as other platforms (such as iOS) do not have the concept of menus.

__1. In the Eclipse workbench, navigate to the MyMemories project > apps > MyMemories > android > js directory. This directory holds all application JavaScript that is specific to the Android platform. The environment specific JavaScript directories follow a sparse model whereby developers only write code that overrides or augments what is in the common directory. In this way you can avoid writing duplicate code. Note that we are editing the Android JS file, not the common JS file.

__2. Open MyMemories.js file into the editor. Currently the project does not contain environment specific code, so the MyMemories.js file under the android folder is mostly empty. It contains a single function called wlEnvInit that is used to call the common initialization code when the application starts.

Page 14: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-14

__3. Delete all the code in the MyMemories.js file. Now you should have an empty editor.

__4. Copy and paste Snippet #3 from C:\WorklightLab\imports\lab3\Lab3.snippets.txt to the empty

editor. The code snippet replaces the original wlEnvInit() method with contents which was deleted in the previous step. Save the file.

/* * Licensed Materials - Property of IBM * 5725-G92 (C) Copyright IBM Corp. 2011, 2012. All Rights Reserved. * US Government Users Restricted Rights - Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ var itemList = new Array(); // This method is invoked after loading the main HTML and successful initialization of the Worklight runtime. function wlEnvInit(){ wlCommonInit(); // Environment initialization code goes here WL.OptionsMenu.init({opacity: "0.9"}); WL.OptionsMenu.addItem('homePage', function() {changePage("#homePage");}, 'Go to Home', {image:'', enabled : true}); itemList.push('homePage'); WL.OptionsMenu.addItem('createMem', function() {changePage("#cameraPage");}, 'Add Memory', {image:'', enabled : true}); itemList.push('createMem'); WL.OptionsMenu.addItem('listMem', function() {changePage("#listPage");}, 'My Memories', {image:'', enabled : true}); itemList.push('listMem'); WL.OptionsMenu.addItem('closeApp', function() {WL.App.close();}, 'Exit', {image:'', enabled : true});

Page 15: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-15

itemList.push('closeApp'); WL.OptionsMenu.setEnabled(true); WL.OptionsMenu.setVisible(true); $(document).on('pagechange', handlePageChange); } function changePage(targetPage) { console.log('Changing page to ' + targetPage); $.mobile.changePage(targetPage); } function handlePageChange(arg, obj) { enableAll(); var pageId = $.mobile.activePage.attr('id'); var disableItem = null; if(pageId == 'homePage') { disableItem = WL.OptionsMenu.getItem('homePage'); } else if(pageId == 'cameraPage') { disableItem = WL.OptionsMenu.getItem('createMem'); } else if(pageId == 'listPage') { disableItem = WL.OptionsMenu.getItem('listMem'); } if(disableItem != null) { disableItem.setEnabled(false); } } function enableAll() { itemList.forEach(function(item) {WL.OptionsMenu.getItem(item).setEnabled(true);});

}

Page 16: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-16

In the wlEnvInit() function, we will initiate the option menus. The majority of the code in the wlEnvInit() function deals with using the WL.OptionsMenu API. This API is provided by Worklight and allows you to programmatically add menu items from within your application code. After creating each menu item using the WL.OptionsMenu.addItem API, the code enables the menu and makes it visible using the WL.OptionsMenu.setEnabled and WL.OptionsMenu.setVisible APIs.

When creating the menu items, a function called changePage is referenced. The changePage function is called when a menu item is clicked and uses the changePage functionality built into jQuery Mobile to navigate between screens in the application.

The last line $(document).on('pagechange', handlePageChange)registers a callback handler on a page change event. This means that anytime a user navigates from one screen to another, the handlePageChange function is called. This function will take care of ensuring the correct menu items are enabled based on the screen that is currently active in the application.

__5. Now that the changes are done, you need to build the updated version of the application. To do this, expand MyMemories project > apps > right-click on the MyMemories application folder > Run As > Build All and Deploy. This will build the application package with the changes, and it will deploy the updated application to the Worklight Server.

Page 17: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-17

__6. Wait until the build completes. Go back to the opened Android Virtual Device instance. Click on the home button which is the up arrow on the bottom of the emulator.

__7. Click on the full application button to see all the available applications.

Page 18: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-18

__8. Click the MyMemories app to open the app again.

__9. Once the application is open, you should see a prompt to update the application.

Page 19: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-19

__10. Click the Update button to begin the update process. Once clicked, the Worklight Device runtime will download the updated application contents from the Worklight Server.

__11. Once retrieved, the existing application contents are cleared from the disk cache, and the new contents are unpacked. When complete, the application is restarted from the new contents.

__12. To test your changes, you need to bring up the menu on the emulator instance. You can do this by pressing the menu icon on the emulator instance. You should see a menu that has an item for each screen in the application.

Page 20: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-20

__13. Click on My Memories, Add Memory, or Go Home to navigate to any of the main application screens.

__14. Click the Home button to go to the Android home screen.

__15. Keep the Android emulator running. DO NOT close the Android Virtual Device instance.

3.2 Using In-App Notification via the Worklight Console The Worklight console is where you control the mobile application. Each Worklight project has its own console for maintaining its assets which include the mobile app and the adapters.

__1. To go to the Worklight Management Console for your Worklight project, right-click MyMemories project > Open Worklight Console.

Page 21: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-21

__2. The Worklight console should appear in a browser with URL similar to this: http://<hostname>:10080/<projectname>/console/#catalog

Notice that there are the iPhone and Android environments. The console is running on the Worklight Development Server with a default port of 10080. You can see the mobile app and adapter are both deployed in the console.

Page 22: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-22

__3. In the Worklight console, select Active, Notifying from the Android active drop-down box.

__4. Enter Maintenance as the Notification Text and click Save.

__5. You should see a message at the top of the console saying that the rule has been set.

Page 23: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-23

__6. Go back to the Android Virtual Device emulator. Click on the App button.

__7. Relaunch the MyMemories app.

Page 24: Lab 3: Using Worklight Server and Environment Optimization ... · Page 3-4 Worklight 6.0 POT - Lab 3 Lab 3 Using the Worklight Server and Environment Optimizations In Lab 1 and 2,

IBM Software

Worklight 6.0 POT - Lab 3 Page 3-24

__8. You should see the in-app notification. Click Close on the notification to continue using the app.

__9. Close the MyMemoriesAVD – the Android emulator.

3.3 Summary Congratulations! You have completed Lab 3.


Recommended