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?
- 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.
- Since it provides mnemonic names for its constants, developers are
less likely to use incorrect values for column names or URIs.
- 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);
}
.
.
.
}
}