Android Development: Android Content Provider

ODROID Magazine Android Content Provider SQLite

Like any other operating system, Android internally needs to have persistence storage for storing system information. This data needs to be in persistent storage, as it will always need to refer to those data after every reboot to put the device in a particular state. User and device information like screen brightness, volume, accounts, calendar, etc will need to be stored somewhere. Android uses what is called Content Provider. Basically, it is a SQLite-backed persistent mechanism, or more simply known as a database. Most of the data is internally stored inside of several SQLite databases. In this article we will take a look at some of the content providers that are used internally by the operating system.

This article will look at some of the databases that are used internally by the operating system. A good starting point to learn more about content provider is to go to the Android Developer website from Google (http://bit.ly/2hkvljq).

What and where
Content providers are just normal Android applications that have a job to serve and process database requests from a client. Data from the internal content providers are stored inside /data/data folder as shown in Figure 1. We are interested in apps that has the following package format:

com.android.providers.< app_name >

Figure 1 list the internal Android content providers that are available.

Figure 1 – Package content providers inside /data/data folder

Take, for example, the DownloadManager service that is provided by the Android SDK. This service allow apps to download file asynchronously. Internally, the framework uses this content provider to keep persistent information about a file status that is going to be downloaded. The follow SQL schema shows the declaration that is used internally to persist the downloaded file information.

CREATE TABLE android_metadata (locale TEXT);

CREATE TABLE downloads(_id INTEGER PRIMARY KEY AUTOINCREMENT,uri TEXT, method INTEGER, entity TEXT, no_integrity BOOLEAN, hint TEXT, otaupdate BOOLEAN, _data TEXT, mimetype TEXT, destination INTEGER, no_system BOOLEAN, visibility INTEGER, control INTEGER, status INTEGER, numfailed INTEGER, lastmod BIGINT, notificationpackage TEXT, notificationclass TEXT, notificationextras TEXT, cookiedata TEXT, useragent TEXT, referer TEXT, total_bytes INTEGER, current_bytes INTEGER, etag TEXT, uid INTEGER, otheruid INTEGER, title TEXT, description TEXT, scanned BOOLEAN, is_public_api INTEGER NOT NULL DEFAULT 0, allow_roaming INTEGER NOT NULL DEFAULT 0, allowed_network_types INTEGER NOT NULL DEFAULT 0, is_visible_in_downloads_ui INTEGER NOT NULL DEFAULT 1, bypass_recommended_size_limit INTEGER NOT NULL DEFAULT 0, mediaprovider_uri TEXT, deleted BOOLEAN NOT NULL DEFAULT 0, errorMsg TEXT, allow_metered INTEGER NOT NULL DEFAULT 1, allow_write BOOLEAN NOT NULL DEFAULT 0, flags INTEGER NOT NULL DEFAULT 0);

CREATE TABLE request_headers(id INTEGER PRIMARY KEY AUTOINCREMENT,download_id INTEGER NOT NULL,header TEXT NOT NULL,value TEXT NOT NULL);

CREATE TABLE android_metadata (locale TEXT);

CREATE TABLE downloads(_id INTEGER PRIMARY KEY AUTOINCREMENT,uri TEXT, method INTEGER, entity TEXT, no_integrity BOOLEAN, hint TEXT, otaupdate BOOLEAN, _data TEXT, mimetype TEXT, destination INTEGER, no_system BOOLEAN, visibility INTEGER, control INTEGER, status INTEGER, numfailed INTEGER, lastmod BIGINT, notificationpackage TEXT, notificationclass TEXT, notificationextras TEXT, cookiedata TEXT, useragent TEXT, referer TEXT, total_bytes INTEGER, current_bytes INTEGER, etag TEXT, uid INTEGER, otheruid INTEGER, title TEXT, description TEXT, scanned BOOLEAN, is_public_api INTEGER NOT NULL DEFAULT 0, allow_roaming INTEGER NOT NULL DEFAULT 0, allowed_network_types INTEGER NOT NULL DEFAULT 0, is_visible_in_downloads_ui INTEGER NOT NULL DEFAULT 1, bypass_recommended_size_limit INTEGER NOT NULL DEFAULT 0, mediaprovider_uri TEXT, deleted BOOLEAN NOT NULL DEFAULT 0, errorMsg TEXT, allow_metered INTEGER NOT NULL DEFAULT 1, allow_write BOOLEAN NOT NULL DEFAULT 0, flags INTEGER NOT NULL DEFAULT 0);

CREATE TABLE request_headers(id INTEGER PRIMARY KEY AUTOINCREMENT,download_id INTEGER NOT NULL,header TEXT NOT NULL,value TEXT NOT NULL);

The following code block shows an example of a data is stored for the downloaded file:

1|https://www.gstatic.com/android/config_update/08202014-metadata.txt|0|||||/data/user/0/com.android.providers.downloads/cache/08202014-metadata.txt|text/plain|2||2||200|0||com.google.android.configupdater||||||0|||||08202014-metadata.txt|||1|1|-1|0|0||0||1|0|0 

2|http://www.gstatic.com/android/config_update/07252017-sms-blacklist.metadata.txt|0|||||/data/user/0/com.android.providers.downloads/cache/07252017-sms-blacklist.metadata.txt|text/plain|2||2||200|0||com.google.android.configupdater||||||385|385||||07252017-sms-blacklist.metadata.”txt|||1|1|-1|0|0||0||1|0|0

Content provider declaration
The content providers provided by the operating system, which are not all made available to a user application, normally have the following declaration in their AndroidManifest.xml:

Listing 1 – Settings content provider

< manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.providers.settings" coreApp="true" android:sharedUserId="android.uid.system">
    < application android:allowClearUserData="false" android:label="@string/app_label" android:process="system" android:backupAgent="SettingsBackupAgent" android:killAfterRestore="false" android:icon="@mipmap/ic_launcher_settings" android:defaultToDeviceProtectedStorage="true" android:directBootAware="true" >
        < provider android:name="SettingsProvider" android:authorities="settings" android:multiprocess="false" android:exported="true" android:singleUser="true" android:initOrder="100" />
    < /application >
< /manifest >

Listing-1 is the AndroidManifest.xml for the Settings application which is stored under the package com.android.providers.settings. Another example can be seen in Listing-2 which shows the declaration for Contacts Provider used to stored contacts information:

Listing 2 – Contacts content provider

< manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.providers.contacts" android:sharedUserId="android.uid.shared" android:sharedUserLabel="@string/sharedUserLabel" >
    < uses-permission android:name="android.permission.BIND_DIRECTORY_SEARCH" />
    < uses-permission android:name="android.permission.GET_ACCOUNTS" />
     .....
     .....
     .....
    < permission android:name="android.permission.SEND_CALL_LOG_CHANGE" android:label="Broadcast that a change happened to the call log." android:protectionLevel="signature|system" />
        
        < provider android:name="ContactsProvider2" android:authorities="contacts;com.android.contacts" android:label="@string/provider_label" …... …... />
                 .....
                 .....
                 .....
        < /provider >

The following table lists some of the content providers that exist inside Android version 7.1.2:

Description Package Name Source Location
CalendarProvider com.android.providers.calendar
ContactsProvider com.android.providers.contacts
DownloadProvider com.android.providers.downloads

com.android.providers.downloads.ui

MediaProvider com.android.providers.media
SettingsProvider com.android.providers.settings frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
TelephonyProvider com.android.providers.telephony packages/providers/TelephonyProvider/src/com/android/providers/telephony/TelephonyProvider.java
UserDictionaryProvider com.android.providers.userdictionary
BlockedNumberCall com.android.providers.blockednumber
PartnerbookmarksProvider packages/providers/PartnerBookmarksProvider/src/com/android/providers/partnerbookmarks/PartnerBookmarksProvider.java
EmailProvider packages/apps/Email/provider_src/com/android/email/provider/EmailProvider.java
LauncherProvider packages/apps/Launcher3/src/com/android/launcher3/LauncherProvider.java
CellBroadcastReceiver packages/apps/CellBroadcastReceiver/src/com/android/cellbroadcastreceiver/CellBroadcastContentProvider.java
WearPackageIconProvider.java packages/apps/PackageInstaller/src/com/android/packageinstaller/wear/WearPackageIconProvider.java
GalleryProvider packages/apps/Gallery2/src/com/android/gallery3d/provider/GalleryProvider.java
DeskClock packages/apps/DeskClock/src/com/android/deskclock/provider/ClockProvider.java
SearchRecentSuggestionsProvider frameworks/base/core/java/android/content/SearchRecentSuggestionsProvider.java
RecentsProvider frameworks/base/packages/DocumentsUI/src/com/android/documentsui/RecentsProvider.java
MtpDocumentsProvider frameworks/base/packages/MtpDocumentsProvider/src/com/android/mtp/MtpDocumentsProvider.java
ExternalStorageProvider frameworks/base/packages/ExternalStorageProvider/src/com/android/externalstorage/ExternalStorageProvider.java
BugreportStorageProvider frameworks/base/packages/Shell/src/com/android/shell/BugreportStorageProvider.java

Be the first to comment

Leave a Reply