In the recently updated Android Dev Guide, the documentation for Content Providers contains a section titled Contract Classes. Though there is a link to an example for Contacts, it was not immediately clear what is a Contract Class and how do I create one for my custom Content Provider
Would appreciate some help on this.
Thanks!
A contract class defines constants that help applications work with the content URIs, column names, intent actions, and other features of a content provider. Contract classes are not included automatically with a provider; the provider's developer has to define them and then make them available to other developers.
A contract class is a public final class that contains constant definitions for the URIs, column names, MIME types, and other meta-data about the ContentProvider. It can also contain static helper methods to manipulate the URIs.
The contract of a class or interface, in Java or any other OO language, generally refers to the publicly exposed methods (or functions) and properties (or fields or attributes) of that class interface along with any comments or documentation that apply to those public methods and properties.
Code contract classes let you specify preconditions, postconditions, and object invariants in your code. Preconditions are requirements that must be met when entering a method or property. Postconditions describe expectations at the time the method or property code exits.
What is a Contract class?
A contract class is a public
final
class that contains constant definitions for the URIs, column names, MIME types, and other meta-data about the ContentProvider
. It can also contain static
helper methods to manipulate the URIs.
Why is it used?
How is it used?
Here is a sample Contract class snippet designed for a weather app containing two tables: weather table and location table. The comments and some methods are skipped to keep it small. Generally it should be well commented.
public class WeatherContract { public static final String CONTENT_AUTHORITY = "com.example.android.sunshine.app"; public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY); public static final String PATH_WEATHER = "weather"; public static final String PATH_LOCATION = "location"; /**Inner class that defines the table contents of the location table. */ public static final class LocationEntry implements BaseColumns { public static final String TABLE_NAME = "location"; public static final String COLUMN_LOCATION_SETTING = "location_setting"; public static final String COLUMN_CITY_NAME = "city_name"; public static final String COLUMN_COORD_LAT = "coord_lat"; public static final String COLUMN_COORD_LONG = "coord_long"; public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH_LOCATION).build(); // Custom MIME types public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_LOCATION; public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_LOCATION; // Helper method public static Uri buildLocationUri(long id) { return ContentUris.withAppendedId(CONTENT_URI, id); } } /** Inner class that defines the table contents of the weather table. */ public static final class WeatherEntry implements BaseColumns { public static final String TABLE_NAME = "weather"; public static final String COLUMN_LOC_KEY = "location_id"; public static final String COLUMN_DATE = "date"; public static final String COLUMN_WEATHER_ID = "weather_id"; public static final String COLUMN_SHORT_DESC = "short_desc"; public static final String COLUMN_MIN_TEMP = "min"; public static final String COLUMN_MAX_TEMP = "max"; public static final String COLUMN_HUMIDITY = "humidity"; public static final String COLUMN_PRESSURE = "pressure"; public static final String COLUMN_WIND_SPEED = "wind"; public static final String COLUMN_DEGREES = "degrees"; public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon().appendPath(PATH_WEATHER).build(); public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_WEATHER; public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_WEATHER; // Helper method. public static Uri buildWeatherUri(long id) { return ContentUris.withAppendedId(CONTENT_URI, id); } . . . } }
A contract class defines constants that help applications work with the content URIs, column names, intent actions, and other features of a content provider. Contract classes are not included automatically with a provider; the provider's developer has to define them and then make them available to other developers.
You can make your own Contract class and define some constants there. For example, column names that you can later call in code that makes queries to the database etc.
Nice example how Contract class is used see this thread Android - How do I load a contact Photo?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With