Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Contract Class and how is it used

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!

like image 682
Ya. Perelman Avatar asked Feb 11 '12 19:02

Ya. Perelman


People also ask

What is the use of the contract class?

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.

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.

What is a class contract in Java?

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.

What is contract class in C#?

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.


2 Answers

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?

  1. The Contract Class establishes a contract between the content provider and other applications. It ensures that your content provider can be accessed correctly even if there are changes to the actual values of URIs, column names etc.
  2. Since it provides mnemonic names for its constants, developers are less likely to use incorrect values for column names or URIs.
  3. It's easy to make the Javadoc documentation available to the clients that want to use your content provider.

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);         }          .         .         .     } } 
like image 66
Yogesh Umesh Vaity Avatar answered Sep 20 '22 12:09

Yogesh Umesh Vaity


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?

like image 36
evilone Avatar answered Sep 17 '22 12:09

evilone