1 of 18

How To Generate a Perfect ContentProvider

Pure Android Magic

2 of 18

Install Eclipse and the plugins

  • The plugins can be installed in any distribution of Eclipse, but it's easiest to start with the Eclipse Modeling Tools distribution: Eclipse Modeling Tools
  • Install extra add-ons: Help->Install New Software. select the drop down labeled "Work with" and pick "Juno" (or whatever happens to be the newest version at the time of reading). Expand the tree under "Modeling"

3 of 18

Install Eclipse and the plugins

  • Make sure you have the following installed under the modeling tree:
    • Eclipse Modeling Discovery UI
    • Ecore Tools SDK
    • EMF Model Query SDK
    • EMF Model Transaction SDK
    • EMF Validation Framework SDK
    • Extended Editing Framework - EMF Edit generation using Acceleo
    • Xpand SDK
    • Xtext SDK

4 of 18

Install Eclipse and the plugins

5 of 18

Create a new project

  • Prerequisite: download and install the Android SDK and tools for Eclipse. Visit developer.android.com for more details
  • Create a new Android project configured to your liking.
    • Set a package name and min SDK value
    • Set a preference for your navigation style

6 of 18

Create a new model

  • Right click your new project and navigation to: new->other. Under Android you should see the following. Select Android Content Provider Model and follow the prompts.

7 of 18

Open the model.ecorediag file

8 of 18

Edit the model.ecorediag file

  • Create new tables with the Eclass object
  • Simply click EClass and then click anywhere on the canvas to create a new table

9 of 18

Edit your new table

  • Hover over the middle row on your new table and you will see a tooltip with a symbol that looks like a minus.
  • Click the "minus" button to add a new attribute. Name it whatever you want

10 of 18

Set the attribute types for each attribute

  • Before you can generate your ContentProvider you must specify an E<Type> attribute type for each attribute you specified. The mappings are intuitive:
    • String -> EString
    • int -> EInt
    • boolean -> EBoolean
    • etc...
  • You set these types from the properties window (next slide)

11 of 18

Set the attribute types for each attribute

  • The properties icon looks like this:
  • If you can't find this, you may need to enable the properties window from the toolbar.

12 of 18

Set the attribute types for each attribute

  • Proceed to set all the correct attributes for each field using the properties window

13 of 18

Change the package name for your model

  • Click on a blank space in the modeling canvas (removing the focus from any of your EClasses or attributes)
  • In your Properties window you should see the following view:

  • Set the Name to something sensible like: Model
  • Set NS Prefix and NS URI both to the base package name of your application

14 of 18

Save your model

  • This can actually be trickier than it sounds. You must click back into the canvas (assuming you are still working in the properties window) and then hit save for the changes to take effect.

15 of 18

Generate Your Model - the fun part

  • Locate your model.ecore file next to your model.ecorediag file.
  • Right click the model.ecore file
  • Select Android Tools ->Generate Content Provider

16 of 18

Add Your New ContentProvider to your Android Manifest

  • locate your new src-gen folder in your project. This source package will contain all your generated ContentProvider code.
  • In the root of that package there is a file called ContentProvider.xml. Open it
  • Copy the <provider /> line and paste it into the <application></application> tag in your Android Manifest.
  • Set an additional property of android:exported="false" unless you know what you are doing (see next slide)

17 of 18

Add Your New ContentProvider to your Android Manifest

18 of 18

Finished!

You are now free to move about the cabin. If you need to make a change to the model at any time, just go make the change in the visual editor, save your model, and regenerate the ContentProvider. It's that easy!