Integrating Utility with Intents

Posted on


Written in collaboration with Michael Burton, Mob.ly; Ivan Mitrovic, uLocate; and Josh Garnier, OpenTable.

OpenTable, uLocate, and Mob.ly labored collectively to create an excellent consumer expertise on Android. We noticed a chance to allow WHERE and GoodFood customers to make reservations on OpenTable simply and seamlessly. This can be a scenario the place everybody wins — OpenTable will get extra site visitors, WHERE and GoodFood achieve performance to make their purposes stickier, and customers profit as a result of they’ll make reservations with just a few faucets of a finger. We had been in a position to obtain this deep integration between our purposes through the use of Android’s Intent mechanism. Intents are maybe one in all Android’s coolest, most unusual, and under-appreciated options. Here is how we exploited them to compose a brand new consumer expertise from elements every of us have.

Designing

One of many first steps is to design your Intent interface, or API. The principle public Intent that OpenTable exposes is the RESERVE Intent, which helps you to make a reservation at a specific restaurant and optionally specify the date, time, and get together measurement.

Hereʼs an instance of make a reservation utilizing the RESERVE Intent:

startActivity(new Intent("com.opentable.motion.RESERVE",
Uri.parse("reserve://opentable.com/2947?partySize=3")));

Our goal was to make it easy and clear to the developer utilizing the Intent. So how did we resolve what it will appear like?

First, we would have liked an Motion. We thought-about utilizing Intent.ACTION_VIEW, however determined this did not map properly to creating a reservation, so we made up a brand new motion. Following the conventions of the Android platform (roughly <package-name>.motion.<action-name>), we selected “com.opentable.motion.RESERVE”. Actions actually are simply strings, so it is essential to namespace them. Not all purposes might want to define their very own actions. In reality, frequent actions corresponding to Intent.ACTION_VIEW (aka “android.intent.motion.VIEW”) are sometimes a better option if youʼre not doing one thing uncommon.

Subsequent we would have liked to find out how knowledge can be despatched in our Intent. We determined to have the information encoded in a URI, though you may select to obtain your knowledge as a group of things within the Intent’s knowledge Bundle. We used a scheme of “reserve:” to be in line with our motion. We then put our area authority and the restaurant ID into the URI path because it was required, and we shunted off the entire different, non-compulsory inputs to URI question parameters.

Exposing

As soon as we knew what we wished the Intent to appear like, we would have liked to register the Intent with the system so Android would know to begin up the OpenTable software. That is achieved by inserting an Intent filter into the suitable Exercise declaration in AndroidManifest.xml:

<exercise android:identify=".exercise.Splash" ... >
...
<intent-filter>
<motion android:identify="com.opentable.motion.RESERVE"/>
<class android:identify="android.intent.class.DEFAULT" />
<knowledge android:scheme="reserve" android:host="opentable.com"/>
</intent-filter>
...
</exercise>

In our case, we wished customers to see a quick OpenTable splash display screen as we loaded up particulars about their restaurant choice, so we put the Intent Filter within the splash Exercise definition. We set our class to be DEFAULT. This may guarantee our software is launched with out asking the consumer what software to make use of, so long as no different Actions additionally record themselves as default for this motion.

Discover that issues just like the URI question parameter (“partySize” in our instance) usually are not specified by the Intent filter. This is the reason documentation is essential when defining your Intents, which weʼll discuss a bit later.

Processing

Now the one factor left to do was write the code to deal with the intent.

    protected void onCreate(Bundle savedInstanceState) {
tremendous.onCreate(savedInstanceState);
remaining Uri uri;
remaining int restaurantId;
strive {
uri = getIntent().getData();
restaurantId = Integer.parseInt( uri.getPathSegments().get(0));
} catch(Exception e) {
// Restaurant ID is required
Log.e(e);
startActivity( FindTable.begin(FindTablePublic.this));
end();
return;
}
remaining String partySize = uri.getQueryParameter("partySize");
...
}

Though this isn’t fairly all of the code, you get the thought. The toughest half right here was the error dealing with. OpenTable wished to have the ability to gracefully deal with inaccurate Intents that could be despatched by companion purposes, so if now we have any drawback parsing the restaurant ID, we go the consumer off to a different Exercise the place they’ll discover the restaurant manually. It is essential to confirm the enter simply as you’ll in a desktop or internet software to guard towards injection assaults which may hurt your app or your customers.

Calling and Dealing with Uncertainty with Grace

Truly invoking the goal software from inside the requester is kind of straight-forward, however there are just a few instances we have to deal with. What if OpenTable is not put in? What if WHERE or GoodFood does not know the restaurant ID?

Restaurant ID identified Restaurant ID unknown
Consumer has OpenTable Name OpenTable Intent Do not present reserve button
Consumer does not have OpenTable Name Market Intent Do not present reserve button

You may most likely want to work along with your companion to resolve precisely what to do if the consumer does not have the goal software put in. On this case, we determined we’d take the consumer to Android Market to obtain OpenTable if s/he wished to take action.

    public void showReserveButton() {

// setup the Intent to name OpenTable
Uri reserveUri = Uri.parse(String.format( "reserve://opentable.com/%s?refId=5449",
opentableId));
Intent opentableIntent = new Intent("com.opentable.motion.RESERVE", reserveUri);

// setup the Intent to deep hyperlink into Android Market
Uri marketUri = Uri.parse("market://search?q=pname:com.opentable");
Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(marketUri);

opentableButton.setVisibility(opentableId > 0 ? View.VISIBLE : View.GONE);
opentableButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
PackageManager pm = getPackageManager();
startActivity(pm.queryIntentActivities(opentableIntent, 0).measurement() == 0 ?
opentableIntent : marketIntent);
}
});
}

Within the case the place the ID for the restaurant is unavailable, whether or not as a result of they do not take reservations or they don’t seem to be a part of the OpenTable community, we merely cover the reserve button.

Publishing the Intent Specification

Now that each one the technical work is completed, how are you going to get different builders to make use of your Intent-based API apart from 1:1 outreach? The reply is straightforward: publish documentation in your web site. This makes it extra seemingly that different purposes will hyperlink to your performance and in addition makes your software out there to a wider neighborhood than you may in any other case attain.

If there’s an software that you just’d prefer to faucet into that does not have any revealed data, strive contacting the developer. It is usually of their finest curiosity to encourage third events to make use of their APIs, and in the event that they have already got an API sitting round, it could be easy to get you the documentation for it.

Abstract

It is actually simply this straightforward. Now when any of us is in a brand new metropolis or simply across the neighborhood its simple to examine which place is the brand new sizzling spot and instantly seize an out there desk. Its nice to not have to discover a restaurant in a single software, launch OpenTable to see if there is a desk, discover on the market is not, launch the primary software once more, and on and on. We hope you will discover this write-up helpful as you develop your personal public intents and that you’re going to think about sharing them with the larger Android neighborhood.





oflapps.xyz

Leave a Reply

Your email address will not be published. Required fields are marked *