What is a Contract Class and how is it used
Asked Answered
H

4

29

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!

Hanway answered 11/2, 2012 at 19:29 Comment(1)
Also see #17452431Discomposure
C
18

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?

Cumulate answered 11/2, 2012 at 19:33 Comment(0)
O
22

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);
        }

        .
        .
        .
    }
}
Orgiastic answered 28/3, 2016 at 15:36 Comment(0)
C
18

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?

Cumulate answered 11/2, 2012 at 19:33 Comment(0)
D
3

Contract is a container for constants that define names for URIs tables and columns. It also provides the same constants across all the other classes in the same package.

Diomedes answered 29/1, 2015 at 12:38 Comment(0)
D
0

Contract class is a simple class that holds the constants of a table that is to be created in SQL Lite DB. We can add inner classes based upon the number of tables required. Make the contract class final so that the constants of the table cannot be changed.

Contract class acts as a container of table classes, no need for any further modifications

The table classes (inner classes) may or may not implements BaseColumns.

Hope this gives a clear picture of contract class!! :-)

Dichy answered 12/10, 2019 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.