Categories
Android

Minimalistic Android Service with Delphi 10 Seattle

I always love making minimalistic demos because then you can see all the essential parts. I put together this short video with Delphi 10 Seattle to show the minimal parts necessary for to create an Android service. It is so simple. The Seattle release supports 4 different types of Android services. One thing this demo does show that is optional, is it create a sticky service that will relaunch if pushed out of memory. It doesn’t show how to talk to methods on the service – there are a few ways, which I can cover later. [Source]

BTW, Delphi 10 Seattle added support for iOS background execution too.

Categories
Android

Adding to the Android User Dictionary

On Android there is a single UserDictionary that works across all keyboards, and any app (with the appropriate permissions) can query, add and remove words. Here is some simple code to add a word to the dictionary (via XE8):

uses
  Androidapi.JNI.Provider, Androidapi.Helpers, Androidapi.JNI.JavaTypes;
 
procedure AddUserWord(const AWord: string);
begin
  // Need WRITE_USER_DICTIONARY permission
  TJUserDictionary_Words.JavaClass.addWord(
    SharedActivityContext, // Context
    StringToJString(AWord),// Word to add
    255,                   // Frequency: 1- 255
    nil,                   // optional shortcut
    SharedActivityContext.getResources.getConfiguration.locale
    );
end;

If you also want to read the dictionary then you need to have the READ_USER_DICTIONARY permission. Check out the documentation for more information on the UserDictionary and it’s Words list.

Categories
Android devices Internet of Things

Google’s Project Brillo, Weave, and Delphi

If you followed Google I/O then you no doubt heard about Google’s announcements in the Internet of Things space: Project Brillo and Weave. Brillo is Google’s new operating system for Internet of Things devices, and Weave is the language for how the devices will communicate. Right now Brillo and Weave are just product announcements. You can sign up for more information, but there is no preview release or developer build available. A lot may change before they are released, so it is tough to talk about them, but you sill may wonder how they will play with Delphi and RAD Studio XE8.

Disclaimer: This is based on public information released by Embarcadero and Google as interpreted by me. I’m not announcing anything, nor sharing any internal secrets. Just connecting the dots. If you connect the dots and get a different picture then let me know.

Neither Brillo or Weave are on our official roadmap because they were just announced. But we do have a good record lately of supporting new platforms quickly with new releases when those platforms are in our area of focus: iOS, Android, Windows and OS X. Just with XE8 we added iOS 64-bit to meet Apple’s new requirement, and it was added in such a way that most projects just need a recompile (which is much better option than the other native tools out there)

Besides wishful thinking, lets look at what they are and what we already support with XE8.

Project Brillo is a modified (or scrubbed down) version of Android. There have already been a few new devices come out that are powered by Android beyond the traditional tablet and phone. This is because Android is an open platform that comes with a rich development and app ecosystem. Brillo is Google’s attempt to make Android more flexible for more new devices in the future. It is a great idea.

Project Brillo may be in response to Microsoft’s announcement of Windows 10 for Devices, specifically targeting Raspberry Pi 2. These devices are going to be huge in the Internet of Things (IoT). That is why Microsoft is targeting the Raspberry Pi 2, and why Google is releasing Project Brillo. They all want to be the operating system of the Internet of Things. This is one place in the IoT where Apple is behind the pack, since they are a hardware company, they don’t want to sell an OS without hardware.

What about Delphi support for Brillo? We can look at the last 3 big modified versions of Android: Amazon’s Fire OS, Google Glass and Android Wear. All three of these we supported “out of the box” with our current release at the time, and some of them we added features in future releases to enhance that support. This is because we have great support of the Android OS directly. So I would suspect we will support Brillo when it is released.

That being said, one of the goals of Brillo is to run on “broad silicon” beyond the common ARVv7 processors that most Android devices run on.  We only currently support ARMv7 and ARMv8 (with NEON being part of the ARM standard going forward, so isn’t hardly worth mentioning). We’ve seen some recent success with Intel ATOM processor support thanks to libHoudini updates in KitKat. Now if a Brillo device is running a processors meeting these specs, it is likely to be supported “out of the box.” But if Brillo is running on an ARMv5 or “Bill and Ted’s Excellent CPU” then support is less likely.

Now it is possible that Brillo will be scrubbed down so much that it is no longer compatible with an Android app. Remember that Android is based on Linux, and Linux console apps are on our official roadmap for a future release, so support is still possible.

That brings us to Google Weave: a common library of terms for how devices will communicate. Its objective is to expose the developer API in a cross-platform way. It is based on JSON and REST from what I can tell. So it will be an agreed standard within REST and JSON. We already have great JSON and REST support, and there are 3rd party libraries that extend that as well. Combine the REST client library and the EMS server REST support and I suspect we will be in a good place to support Weave.

Weave is the protocol, but the channel will most likely be WiFi via HTTPS or Bluetooth LE. Also already covered great in XE8.

Naturally once Brillo and Weave are more than just vague product announcements we may have specific support for them that would make them easier to work with and unlock more features of those platforms beyond the stuff you already get with XE8. The moral of the story is to start developing your IoT solutions with XE8 today and make sure you have Update Subscription so you are ready for the future.

Categories
Android Funny iOS Source Code Video podCast

Code Monkey in Delphi Code

In this special musical number I’ve created a music video based on Jonathan Coulton’s Code Monkey written in Delphi’s Object Pascal.

[Download the code] [Download the video (mp4)]

Categories
Android Architecture iOS Mobile Source Code

Parallel For Loops

Parallel For Loops are a hassle-free way to supercharge your program with the Parallel Programming Library. The syntax is similar to the standard For loop, with the advantage of each iteration running on in a different task on the thread pool. This allows multiple iterations to run at the same time, taking advantage of the multi-core and hyper-threaded architecture common on laptops, desktops and mobile devices today.

Here is the replay of the Skill Sprint:

Here are the slides:

Parallel For Loops with the Parallel Programming Library from Jim McKeeth

Update: I was chatting with Allen Bauer today and he mentioned that while you technically can use Queue and Synchronize from within a Parallel For loop, he wouldn’t recommend it because it will dramatically reduce the speed of the loop. It is still faster than a linear loop, but not as fast as it could be. I’ll leave these examples here, but keep that in mind when optimizing your parallel code.

Here is the syntax in Object Pascal. The stride is the first parameter is it is optional. It controls how the iterations are grouped when being sent to the CPUs. Min and Max are your usual start and stop range for the loop. The last parameter is an anonymous method that represents the code to be executed on each iterations. It takes an Index parameter that is either an Integer or Int64 that provides the value of the current iteration.

TParallel.For(Stride, Min, Max, procedure (Idx: Integer)
begin
  if IsPrime(Idx) then
  begin
    TInterlocked.Increment (Tot);
    TThread.Queue(TThread.CurrentThread, procedure
    begin
      Memo1.Lines.Add(Idx.ToString);
    end);
  end;
end);

Here is the C++ code syntax. It takes a event instead of an anonymous method.

// . . .
TParallel::For(NULL, Min, Max, MyIteratorEvent);
// . . . 

void __fastcall TFormThreading::MyIteratorEvent(TObject *Sender, int AIndex)
{
	if(IsPrime(AIndex)){
		TInterlocked::Increment(Tot);
	};
}

There are some great blog posts and videos on the Parallel Programming Library

Stephen Ball has a series of blog posts on the Parallel Programming Library, including a Quick Introduction and one on managing the thread pool. As Stephen points out, while you can customize the thread pool, that doesn’t alway mean you should. Malcolm Groves also has some great blog posts on PPL.

Danny Wind has a great Code Rage 9 session on the Parallel Programming Library (be sure to download his samples). David I. did the C++ version.

Allen Bauer, our Chief Scientist, also has a CodeRage 9 session on the PPL Architecture.

If you want to take advantage of the new C++11 Lambda syntax on Win64 and mobile, then check out this community article or watch this CodeRage 9 video snippet. Get Bruneau and David’s code for C++ and Parallel libraries.

Update: Keep in mind that a Parallel For loop isn’t always the best performance option, but it is really easy to use. Check out Stefan Glienke’s Stack Overflow answer for an alternative using the PPL TTask for even better performance.

Categories
Android design iOS Tools

Looking at Radiant Shapes

RadiantShapes_Logo
I’ve been playing with Raize Software‘s new Radiant Shapes components this week. These are the brand new primitive shape component set for FireMonkey on all platforms: Windows, OS X, iOS and Android. I’ve been a long time fan of Raize Components because of their attention to detail and high quality. Radiant Shapes continues this tradition.

Radiant Shapes PaletteRadiant Shapes is made up of 35 reusable shape controls that are all pretty flexible. If you caught Ray Konopka’s RAD In Action: Seeing is Believing on Data Visualization then you have a pretty good idea the importance of using primitive shapes like these to communicate useful information to your users, especially in mobile development.

All of the shapes include useful design time menus to make common changes quickly and easily. You can probably get away without using the Object Inspector for a lot of your common tasks. They also have various customizations that make them very flexible.

One thing that is interesting is they introduce the idea of a TRadiantDimension they allows you to specify some of the sizes as either absolute pixels, or as a scale factor. This gives great flexibility in how they behave when resized.

Ray Konopka introduced the Radiant Shapes during CodeRage 9 with a couple great sessions. You can catch the replay for both Object Pascal and C++.

I really like the TRadiantGear component, so I decided to play with it in detail. You can specify the number of cogs (teeth), their size (as a Radiant Dimension) and the size and visibility of the hole. Just like all the other shapes, they handle hit tests correctly, so at runtime, you can click between the cogs of the gear and it doesn’t produce an onClick event.

Gears

Just for fun I put down three gears and used LiveBindings to connect a TTrackBar.Value to their rotation. A little math in the OnAssigningValue event and I had all the gears rotating in unison. The fact that the gears stayed synced up, and the teeth meshed perfectly was really impressive.

procedure TForm4.RotateGearBigAssigningValue(Sender: TObject;
  AssignValueRec: TBindingAssignValueRec; var Value: TValue;
  var Handled: Boolean);
begin
  Value := TValue.From(-1 * (Value.AsExtended / 2 + 18));
end;

procedure TForm4.RotateGearRightAssigningValue(Sender: TObject;
  AssignValueRec: TBindingAssignValueRec; var Value: TValue;
  var Handled: Boolean);
begin
  Value := TValue.From(-1 * (Value.AsExtended + 18));
end;

18 is the offset for the gears (360° / 10 cogs / 2 (half offset) = 18) and the 2 comes from the big gear being twice as big (20 cogs), then the -1 is so they rotate the opposite direction.

Overall I am impressed with the Radiant Shapes. Something I would like to see include a polygon component where I can specify the number of sizes. You can do that with the star and gear, but a flexible polygon would be nice. Also, the shapes can be rotated with the rotation property, but it would be cool if there was a way to rotate it in the designer too. That might be a big undertaking though.

You can buy the Radiant Shapes from Raize Software for $49, which gives you a 1 year subscription for updates. I did get a complimentary copy from Raize Software to review them.

Be sure to join Ray on Friday the 23rd as he is featured in the Embarcadero Technology Partner Spotlight.

Categories
Android iOS Mobile

Mobile Push Notifications without a BaaS

Delphi, C++Builder and RAD Studio XE7 include support for mobile push (remote) notifications via a Parse and Kinvey BaaS providers. This makes it really easy to send push notifications to your users on mobile devices. Both Parse and Kinvey offer free service levels (as well as paid), and you can also download App42 SDK for Appmethod and use the App42 BaaS instead.

BaaS or Backend As A Service Providers are companies that maintain the backend servers necessary for many application development tasks. They handle things like user authentication, data storage, push notifications, etc. Sometimes they are referred to as mBaaS or Mobile-BaaS because if the heavy focus on mobile application development these days, but they typically are not tied to mobile.

This doesn’t mean you have to use a BaaS provider to send mobile push notifications. This is just the easy way. During CodeRage we’ve had sessions on how to do push notifications without a BaaS provider. It is different for both iOS and Android, so you are looking at a lot more code and effort, but it is possible.

CodeRage 9 had a session by Jeff LeFebvre had a session on Android Push notifications via Google Cloud Messaging (GCM):

Here is a transcript of the Q&A as well as download links.

For iOS & iPhone use of Apple Push Notifications (APN) Luis Felipe and Anders Ohlsson have some blog posts and videos on the subject. Luis did the original post and video in Spanish, and then Anders translated and expanded on it.

Luis’ post on iOS notifications with XE4 (Spanish but you can use Google Translate). It includes some source code downloads too. The video is also in Spanish but it shows a lot of source code, so it is easy enough to follow along.

Ander’s blog post expanding on it (English), and his CodeRage video on the subject (English)

Keep in mind that this is about the same level of complexity to use most other tools for sending and receiving push notifications.

Categories
Android devices

Developing on the Samsung Gear Live Smart Watch

Previously I created a blog post about using Delphi and RAD Studio XE7 to develop for the Moto 360. The new FireUI Multi Device Designer (MDD) makes is a breeze to design for the new smaller UI. I’ve since updated the FireUI Devices project on GitHub to cover the Samsung Gear Live & LG-G watches in addition to the Moto 360.

I thought I would walk through the steps for developing with the Samsung Gear Live. One advantage it has over the Moto 360 is that it has a physical USB cable connection, so you don’t need to deploy via BlueTooth. This makes for a much faster deploy cycles. With a USB cable though, you need to install the ADB USB Drivers.

  1. Put the device in USB Debugging Mode
    1. Hold the home / side button until the settings menu appears (couple seconds)
    2. Select About and tap Build Number until it notifies you that developer options are enabled.
    3. Swipe left to right to go back
    4. Select Developer Options and enable USB Debugging.
  2. You still need to have the watch paired with a phone via the Android Wear app since the confirmation dialog is displayed there.
  3. Run the SDK Manager / Android Tools and Make sure you have Android SDK Tools, Platform-tools and Build tools updated (this moves the ZipAlign.exe, so you need to tell the IDE where to find it.)
  4. Install the Samsung Android USB Driver for Windows
  5. Gear Live should appear as an Other Device in device manager once you connect it to windows via USB.
  6. Select Update Driver Software
  7. Browse my computer for Driver software
  8. Let me pick from a list of device drivers on my computer
  9. Then select ADB Interface
  10. Select SAMSUNG Android ADB Interface
  11. On your phone you will see a dialog “Allow Wear Debugging” Check “Always allow .. . ” and then select OK.

Gear Live - Device Manager Driver Update

 

Once you have done all of that, it will show up in your IDE as a target, and when you load the FireUI custom device for it, then you will have a great design surface for it too.

GearLive in XE7 IDE

 

And you are ready to build your Gear Live app with Delphi XE7.

Delphi XE7 on the Samsung Gear Live

I’m sure I’ll have more coverage on Android Wear in the coming months too.

Categories
Android Mobile

Voice Enable Your Android Apps

During CodeRage 9 I revisited adding voice support to Android apps. There are some updates from my previous skill sprint coverage on the topic and my original post on launching Google Glass apps via Voice.

You can download the Samples and Components on GitHub. It covers voice recognition, text to speech and launching apps on Google glass with your voice. The examples also cover Android Wear.

Voice Launching Google Glass Apps

  • Add a Voice Trigger XML file:
  • Modify the Android Manifest Template:
    • Add an Intent Filter
      • <action android:name=”com.google.android.glass.action.VOICE_TRIGGER”/>
  • Add Meta Data for Filter
    • <meta-data android:name=”com.google.android.glass.VoiceTrigger”
             android:resource=”@xml/my_voice_trigger” />

Custom Glass Voice Trigger

  • Change XML to from Command to Keyword.
  • Use arbitrary text for voice trigger.
  • Use the Development permission:
    • <uses-permission
      android:name=”com.google.android.glass.permission.DEVELOPMENT”/>
    • Added to android manifest template
  • Not allowed for app store distribution
    • Usable for in-house or ad-hoc use

Additional Prompts on Google Glass

  • Collect additional Voice recognition input when app is launched.
    • Add an Input Prompt to the Voice Trigger XML
      • <?xml version=”1.0″ encoding=”UTF-8″?>
      • <trigger command=”TAKE_A_NOTE”>
      •     <input prompt=”What shall I say?” />
      • </trigger>
  • In FormCreate get speech guesses from Intent Extras
    • SharedActivity.getIntent.getExtras. getStringArrayList(TJRecognizerIntent.JavaClass.EXTRA_RESULTS);

Voice Recognition on AndroidAndroid/Google - Speak Now

  • Prompts user for voice input
  • Returns up to 5 “guesses”
  • Works offline too
    • (only returns 1 guess)
  • Reusable component for download.
  • Requires RECORD_AUDIO & INTERNET permissions.
  • Sends audio to Google’s servers.
  • Uses context to select words.
  • Pronounce punctuation (period, comma, etc.)
  • Works on Android phones & tablets
  • Works on Google Glass
  • Works on Android Wear
  • Doesn’t work on iOS (no exposed API)

Using TSpeechRecognition Component

  • Properties
    • Language: en-US
    • Prompt: Speak now
    • AlwaysGuesses: True
  • Methods
    • Listen
    • ListenFor
  • Events
    • OnRecognition
    • OnRecognitionEx
    • OnCommand

Text-To-Speech on Android

  • Converts Text to spoken word.
  • Reusable component based on Jeff Overcash’s translation.
  • Code shows example of handling Java Listener events.
  • Works on Android phones & tablets
  • Works on Google Glass
  • Doesn’t work on Android Wear (no speaker)
  • iOS Support is possible . . . . (needs implementation)

Using TAndroidTTS Component (component name may change)

  • Just one procedure: Speak

A Note About iOS

  • iOS does not expose voice recognition API
    • (Need to use 3rd party)
  • iOS 7 supports Text to Speech API
    • AVSpeechSynthesizer
    • Just haven’t implemented in component yet

Google Glass Voice Trigger Sample

The replay video will be available here later.

Download the XE7 Trial today and check out the special offers.

Categories
Android Mobile Tools Video podCast

New XE7 Android Features Skill Sprint

XE7 is full of new features everywhere, but there are some really nice ones specific to Android. Here is a replay of my Skill Sprint session on New Android Features.

The new Android specific features in XE7 include:

There are lots of other new features that are not specific to Android, but that will still help make your apps amazing for Android, iOS, Windows and OS X.