470 likes | 616 Views
Connecting Android to Windows Azure. Windows Azure UG December 2012 Monthly Meeting. Welcome to the Windows Azure UG. Presenter today Jim White – jwhite@intertech.com Intertech Instructor & Director of Training Android and Azure instructor Practice lead for Cloud Computing at Intertech
E N D
Connecting Android to Windows Azure Windows Azure UG December 2012 Monthly Meeting
Welcome to the Windows Azure UG • Presenter today • Jim White – jwhite@intertech.com • Intertech Instructor & Director of Training • Android and Azure instructor • Practice lead for Cloud Computing at Intertech • Windows Azure Bootcamp Facilitator • Author of Windows® Azure™ Solutions with Microsoft® Visual Studio® 2010 – in Microsoft Courseware Library this October This presentation& sample code will be made available through azureug.net
Agenda • What is the Windows Azure Toolkit for Android • A quick Android tutorial • Setup • Learn how to get & setup the WAT for Android • See how to setup the Cloud Ready Packages for Devices • Building an Azure accessing Android application • Sample the WAT for Android API • Authentication • Storage Access • Witness some demos • Limitations & More Info • Q & A
Assumptions • You already have a pretty good understanding of Windows Azure • In particular, you understand Windows Azure Storage • I.e. – storage account creation, setup, API, etc. • You understand how to use the Management Portal • You may have minimal or no Android development experience. • Given this is a Windows Azure UG, I’ll provide a little bit of a background on Android development. • I.e. – where to get the Android SDK, how to set it up, how to get going. • Android is a Java platform. A Java tutorial is beyond the scope of this talk. Survey 1 - background
Windows Azure Toolkit (WAT) for Android • May 2011, MSFT announces Windows Azure Toolkits for Mobile devices • iOS and Windows 7 Phone first • Android version released – Aug 31, 2011 • Current version: 0.8 • Available as an open source product • Under the Apache License, Version 2.0 • Provides Android/Java libraries for: • Accessing Windows Azure Storage • Authentication (via various means) Note: Windows Phone and iOS versions provide more capability (push notifications, SQL Azure access)
WAT for Android • You could use Windows Azure RESTful API to do the same work. • Would require a lot more work/maintenance • WAT for Android Libraries • Written in Java – allow for native coding • Make it easy to are write/maintain Android apps • Obfuscates RESTful calls. • Will presumably offer even more features in the future
Windows Azure Storage Access • WAT for Android provides Java libraries to access • Table Storage • Queue Storage • Blog Storage • No SQL Azure access at this time • Before accessing, your application must authenticate • Directly (using storage account id and key) • Via proxy service
Authentication • WAT for Android provides three (3) Authentication Options • Direct access: requires the application know Windows Azure Storage account and key (not a best practice) • Use a membership service: applications need/use service URL and username/password • Use a Windows Azure Access Control Service (ACS): applications need/use federated/social identity • Last two (2) options require proxy service • Provide via Cloud Ready Package for Mobile Devices
Authentication – via Direct Access • Java application has or is provided account URL/key. • Easy to setup, test, etc. • Security concerns • Account “secrets” in the app • Access revocation is difficult • Maintenance concerns • Account changes • Storage changes
Authentication – via Membership • Available through Cloud Ready Packages for Devices • ASP.NET Web Role provides membership management • Registration, password selection, access and user management, etc. • Users create account through Web role or through your application if you provide the means (see the sample application) • Mobile devices use account to get authentication token to access Windows Azure • More work to setup • Applications know nothing about account (except membership URL) • Membership management an issue for large user bases
Authentication – via ACS • Available through Cloud Ready Packages for Devices • Users must have federated/social identity (Windows Live ID, Facebook, Google, etc.) • Setup a Window Azure Access Control Service Namespace with Multiple Identity Providers in your account • Hosted service authenticates through ACS/Identity providers • ACS setup is a bit complex • Application knows nothing about account (except service URL) • No membership management (excellent for large user bases)
Android Development Tutorial
Android Development – The Basics • Android Architecture • Android Development – what you need • Emulators • Android Versions and Platforms • Android Project • Android Applications • Android Market (and other deployments) • Further Android Resources Survey 2 – Android Knowledge
Android Architecture • At the center of the Oracle/Google lawsuit • Write applications in Java • Java code is compiled to bytecode (.class)… • …then into .dex (Dalvik executable) files run by DVM • Application Framework • Minus some Java SE libraries/classes • Plus many Android libraries • Android Runtime • The Dalvik Virtual Machine + core Java libraries • Not a standard “Java” virtual machine • Java coding, but not Java SE, not Java ME • No Swing, RMI, Naming, Management, etc. APIs • Each application runs in its own process/own instance of DVM • Linux kernel – version 2.6 • Drivers, C/C++ Libraries, etc. • Generally exposed through Application Framework Java Apps Application Framework Activity Manager, Content Provider, Telephony Manager, etc. (Java classes) Android Runtime DVM along with core libraries Libraries SQLite, OpenGL ES, WebKit, SGL, SSL, Libc, Media Framework, etc. Linux kernel Drivers (display, camera, etc.)
Android Development – What You Need • Java JDK • www.oracle.com/technetwork/java/javase/downloads • Android SDK • developer.android.com/sdk • Eclipse IDE • www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/indigosr1 • Android Eclipse Plugin • developer.android.com/sdk/eclipse-adt.html • You don’t need a device • No additional tools are necessary for Windows Azure development
Understanding Android Development • JDK • Not just the Java Runtime Environment (JRE) • Android currently supports Java 5 and 6 • Android SDK Starter Package • What you originally download • Windows, Mac and Linux versions • Starter Package is not a full development environment • Download components, emulators, tools, add-ons, etc. • Start android.bat (in the Starter Package) • Starts the Android SDK and Android Virtual Device (AVD) Manager • Use this UI to get the rest of the SDK and all the extras • Eclipse IDE • Android Development Tools Plug-in for Eclipse • Installed via Eclipse’s Install New Software… option • Not required but highlyrecommended
Emulator • You don’t have to have a device to develop/test Android apps • Nothing beats the real thing for final testing • Most work by simple USB port connection • Emulator(s) come with the Android SDK Does require some device configuration
Android Versions • You determine what versions to support • By default… • Applications support all versions • Applications support all device types • Tablets • Big/small displays • Etc. • Use the AndroidManifest to change these defaults • WAT for Android libraries written to version 2.2 • Can probably be used with all versions > 2.0 • But not tested *Per developer.android.com – Dec 2012
Android Projects • Your application code in src • Your Android components • Package name matters • Only one app per package name per device • Source code Android generates • Don’t touch! • R.java = constants tied to resources • Auto-generated class indexing all the resources of your project • Res = project resources • Graphics (for different size devices) • Layouts (XML form of the UI) • Labels (i.e. strings) • May also include menus, xml, media, etc. • Manifest – foundation of the application • Describes app components • Android version support • And more
Android Applications • Android applications are really a loose affiliation of components • Android applications have no single entry point • Components defined in one “application” can be used by another application • Android applications are comprised of one or more of the following “building block” components: • Activities – “screens” that contain views (“widgets”) • Intents – “messages” between components • Services – think worker roles • Content Providers – think storage • Other “components” • Components are specified in AndroidManifest.xml Demo 1 – Hello Android
Android Market • market.android.com • Owned and operated by Google • A place to sell/offer you Android applications • You need Google Checkout • Not available in all countries • 30% fee goes to Android market • Fee to the mobile carriers who distribute and support the Android Market • Google Checkout may add an additional fee(s) • There are other market places/sites (Amazon, AndAppStore, …) • Unlike some mobile OS platforms… • You can install your apps directly onto devices through USB port • You can distribute your app as a download off your Web, FTP, etc. site • Make your APK (Android Package) file available like any download • Make sure the Android APK MIME type is configured on your Web server
More on Android • Intertech’s Complete Android course (4 day – virtual available) • www.intertech.com • Books (there are a ton – many out of date or poor) • The Busy Coder's Guide to Android Development • Android in Action (Manning) • Professional Android Application Development (Wrox) • Web sites (there are a ton – here are some I found useful) • developer.android.com • www.anddev.org • androidcommunity.com • www.androidpolice.com • gorgando.com/blog/topics/technology/android_development/awesome-android-tutorials-for-beginners
WAT for Android Setup
Getting WAT for Android • Download WAT for Android from GitHub • https://github.com/microsoft-dpe/wa-toolkit-android • You’ll need Git or Git plugin through Eclipse • http://git-scm.com/download • The download contains several Eclipse-ready projects • The projects contain • Access control and storage API • Unit tests for same • Samples app for demonstration use • Docs • Very minimal • Helps setup WAT for Android in Eclipse • See GettingStarted.docx in docs!
Import the Projects into Eclipse • Minimally, import… • The Storage for Android project • Access Control for Android project • The Storage for Android Sample App • Optionally, import … • ACS for Android Sample App • Unit test for all projects • Usually requires some minor project fixes • Fix project classpaths • Update the projects to point to your Android SDK location • Reference the libraries correctly
Cloud Ready Packages for Devices • Provides the authentication membership and ACS service packages • Not necessary if you are planning on using direct access. • Designed to make it easier for you to build mobile applications that leverage Windows Azure services. • Download contains a Windows Azure service package that deploys to a Windows Azure account. • Available in extra large to extra small VM size • Available at: github.com/microsoft-dpe/wa-toolkit-cloudreadypackages
Cloud Ready Package for Devices Setup • Create a new Hosted service (but don’t deploy yet) • Create and deploy a certificate to the new hosted service • Cloud Ready package needs to communicate via HTTPS • Can be self-signed but then you get the warnings • Better if it is CA-signed (requires domain name/DNS routing) • Change the CloudReady service configuration (.cscfg file) • Update storage location • Update cert thumbprint • See next page • Deploy Hosted Service DNSAzure.com
CloudReady - ServiceConfiguration.Cloud.cscfg • Update storage account info • Update cert thumbprint
CloudReady Test • Once deployed, you can test your service via browser • Go to https://<cloud ready service name>.cloudnet.app • Membership service version shown here
Test WAT for Android • With the Cloud Ready service of your choice service deployed… • …and with WAT for Android projects imported into Eclipse… • …you can now test WAT for Android in Eclipse (using an Emulator or real device) • Open the Windows Azure for Android Sample App • Find and open the strings.xml file (in res/values)
Configure WAT for Android Sample App • In the Android Resources editor… • First change the toolkit_connection_type value to be one of following: • Direct – for direct access to your storage account • CloudReadySimple – to use CloudReady membership service • CloudReadyACS– to use CloudReady ACS service
Configure WAT for Android Sample App Continued • For direct access, also change… • direct_account_name • To your storage account name • direct_access_key • To your storage account key • For membership access, also change… • cloud_ready_simple_proxy_service • To your proxy service URL For ACS access, you need to setup even more string resources
Run WAT for Android Sample App Demo 2 – WAT Sample App
WAT for Android API
WAT for Android API • Unfortunately, it is not very well packaged API • .storageclient contains Storage API (both public and lower level classes ) • This package also contains direct access auth API • .storageclient.wazservice contains proxy service auth API • The access API is new and unseen • Direct access • Two forms of proxy server access APIs • Proxy form API descends from direct access form • The storage access API largely mimics Windows Azure storage APIs • Knowledge of WA storage API is almost required given lack of documentation • Sample application helps • Sometimes you have to dig into the source code
A Peek at the Direct Access Authentication API try { CloudClientAccount cloudAccount = new CloudStorageAccount( new StorageCredentialsAccountAndKey(accountIdString, accountKeyString)); // to actually login and check that storage is available try getting a storage client cloudAccount.createCloudTableClient(); } catch (Exception e) { Log.println(Log.ERROR, "Login Activity", "Failure in authentication"); }
A Peek at the Proxy Server Access Authentication API • WAZServiceAccount is a more specific type of CloudClientAccount try { WAZServiceAccount cloudAccount = new WAZServiceAccount( new WAZServiceUsernameAndPassword(usernameString, passwordString), new URI(proxyURLString); // to check the login, but specific to WAZServiceAccount object cloudAccount = loginToWAZService(); } catch (Exception e) { Log.println(Log.ERROR, "Login Activity", "Failure in authentication"); } // otherwise you can again try to get storage client try { … cloudAccount.createCloudTableClient(); } catch (Exception e) { Log.println(Log.ERROR, "Login Activity", "Failure in authentication"); }
A Peek at Table Storage API • Getting all entities from a known table • try { • CloudTableClient tableClient = cloudAccount.createCloudTableClient(); • StorageCredentials tableCredentials = tableClient.getCredentials(); • if (tableClient.doesTableExist(tableName)) { • // get all the rows from the database table • Iterable<Map<String, Object>> entities = CloudTableObject.query( • tableClient.getEndpoint(), • tableCredentials, • tableName); • Iterator<Map<String, Object>> iterator = entities.iterator(); • while (iterator.hasNext()) { • Map<String, Object> entity = iterator.next(); • String part = entity.get(“PartitionKey”).toString()); • String key = entity.get(“RowKey”).toString()); • String xProp = entity.get(“x”).toString()); • String yProp = entity.get(“y”).toString()); • } • } • } catch (Exception e) {…}
A Peek at Table Storage API (continued) • Deleting an entity try { CloudTableClient tableClient = cloudAccount.createCloudTableClient(); StorageCredentials tableCredentials = tableClient.getCredentials(); CloudTableObject<CloudTableEntity> tableObject = new CloudTableObject<CloudTableEntity>(tableName, tableClient.getEndpoint(), tableCredentials); tableObject.delete(partitionString,keyString); } catch (Exception e) { Log.println(Log.ERROR, "SelectRow", "Problems deleting entity"); e.printStackTrace(); }
A Peek at Table Storage API (continued) • Inserting an entity • try { • Map<String, Object> entity = new Hashtable<String, Object>(); • entity.put(“PartitionKey”, partitionString); • entity.put(“RowKey”, keyString); • entity.put(“x”, x); //property key and value • entity.put(“y”, y); //property key and value • CloudTableClient tableClient = cloudAccount.createCloudTableClient(); • StorageCredentials tableCredentials = tableClient.getCredentials(); • if (tableClient.doesTableExist(tableName)) { • CloudTableObject.insert(tableClient.getEndpoint(), • tableCredentials, • tableName, • entity); • } • } catch (Exception e) { • Log.println(Log.ERROR, "AddRow","Problem adding row to the default table"); • e.printStackTrace(); • }
A Peek at Table Storage API (continued) • Some other operations • Query • Update String filter = String.format("PartitionKey eq '%s' and RowKey eq '%s'", encodeValueForFilter(partitionKey), encodeValueForFilter(rowKey)); Iterable<Map<String, Object>> entities = CloudTableObject.query(tableClient.getEndpoint(), tableCredentials, tableName, filter); Map<String, Object> entity = new Hashtable<String, Object>(); for (EditText editText : getEditTexts()) { entity.put((String) editText.getTag(), editText.getText().toString()); } CloudTableObject.update(tableClient.getEndpoint(), tableCredentials, tableName, entity); Demo 3 – Simplified Access and Table Storage Example Not following Android or Azure best practices
Limitations and Future • Toolkit lacks documentation • Other than setup guides, there is little or no documentation • But, at least the source code is provided to you • Sample app helps and it is much better than the first release • No access to SQL Azure - Yet • Wade Wegner states updates to Windows Azure Toolkits for iOS and Android are coming… • “so that they are in full parity with the Windows Azure Toolkit for Windows Phone” • Not there yet • Worker role communication API • Service management API • Diagnostic API • Improvements coming? • Better organization/structure • Simplifications • More examples - promised, but not there yet. • Ample room for other 3rd parties to provide extensions • Marshaling/unmarshaling code for entities, messages, etc. • Asynctasking • Etc.
More on WAT for Android • Windows Azure Mobile • www.windowsazure.com/en-us/home/scenarios/mobile/ • WAT for Android announcement page • blogs.msdn.com/b/windowsazure/archive/2011/08/31/now-available-windows-azure-toolkit-for-android.aspx • Wade Wegner’s Blog page and how-to • www.wadewegner.com/2011/08/windows-azure-toolkits-for-devices-now-with-android/ • Wade Wegner Video on use of WAD for Android • blogs.msdn.com/b/zxue/archive/2011/11/21/developing-android-apps-using-windows-azure-toolkit.aspx • WAT for Windows Phone (good documentation on Cloud Ready packages) • watwp.codeplex.com/documentation
Thanks for coming!Happy Holidays and see you “virtually” next month. jwhite@intertech.com www.intertech.com/blog