summaryrefslogtreecommitdiffstats
path: root/docs/html/training/sync-adapters/creating-stub-provider.jd
blob: e9e18efd6df1d520332c544c8cf5b10f034857d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
page.title=Creating a Stub Content Provider

trainingnavtop=true
@jd:body


<div id="tb-wrapper">
<div id="tb">

<h2>This lesson teaches you to</h2>
<ol>
    <li>
        <a href="#CreateProvider">Add a Stub Content Provider</a>
    </li>
    <li>
        <a href="#DeclareProvider">Declare the Provider in the Manifest</a>
    </li>
</ol>

<h2>You should also read</h2>
<ul>
    <li>
        <a href="{@docRoot}guide/topics/providers/content-provider-basics.html"
        >Content Provider Basics</a>
    </li>
</ul>

<h2>Try it out</h2>

<div class="download-box">
 <a href="http://developer.android.com/shareables/training/BasicSyncAdapter.zip" class="button">Download the sample</a>
 <p class="filename">BasicSyncAdapter.zip</p>
</div>

</div>
</div>
<p>
    The sync adapter framework is designed to work with device data managed by the flexible and
    highly secure content provider framework. For this reason, the sync adapter framework expects
    that an app that uses the framework has already defined a content provider for its local data.
    If the sync adapter framework tries to run your sync adapter, and your app doesn't have a
    content provider, your sync adapter crashes.
</p>
<p>
    If you're developing a new app that transfers data from a server to the device, you should
    strongly consider storing the local data in a content provider. Besides their importance for
    sync adapters, content providers offer a variety of security benefits and are specifically
    designed to handle data storage on Android systems. To learn more about creating a content
    provider, see <a href="{@docRoot}guide/topics/providers/content-provider-creating.html"
    >Creating a Content Provider</a>.
</p>
<p>
    However, if you're already storing local data in another form, you can still use a sync
    adapter to handle data transfer. To satisfy the sync adapter framework requirement for a
    content provider, add a stub content provider to your app. A stub provider implements the
    content provider class, but all of its required methods return {@code null} or {@code 0}. If you
    add a stub provider, you can then use a sync adapter to transfer data from any storage
    mechanism you choose.
</p>
<p>
    If you already have a content provider in your app, you don't need a stub content provider.
    In that case, you can skip this lesson and proceed to the lesson
    <a href="creating-sync-adapter.html">Creating a Sync Adapter</a>. If you don't yet have a
    content provider, this lesson shows you how to add a stub content provider that allows you to
    plug your sync adapter into the framework.
</p>
<h2 id="CreateProvider">Add a Stub Content Provider</h2>
<p>
    To create a stub content provider for your app, extend the class
    {@link android.content.ContentProvider} and stub out its required methods. The following
    snippet shows you how to create the stub provider:
</p>
<pre>
/*
 * Define an implementation of ContentProvider that stubs out
 * all methods
 */
public class StubProvider extends ContentProvider {
    /*
     * Always return true, indicating that the
     * provider loaded correctly.
     */
    &#64;Override
    public boolean onCreate() {
        return true;
    }
    /*
     * Return no type for MIME type
     */
    &#64;Override
    public String getType(Uri uri) {
        return null;
    }
    /*
     * query() always returns no results
     *
     */
    &#64;Override
    public Cursor query(
            Uri uri,
            String[] projection,
            String selection,
            String[] selectionArgs,
            String sortOrder) {
        return null;
    }
    /*
     * insert() always returns null (no URI)
     */
    &#64;Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }
    /*
     * delete() always returns "no rows affected" (0)
     */
    &#64;Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }
    /*
     * update() always returns "no rows affected" (0)
     */
    public int update(
            Uri uri,
            ContentValues values,
            String selection,
            String[] selectionArgs) {
        return 0;
    }
}
</pre>
<h2 id="DeclareProvider">Declare the Provider in the Manifest</h2>
<p>
    The sync adapter framework verifies that your app has a content provider by checking that your
    app has declared a provider in its app manifest. To declare the stub provider in the
    manifest, add a <code><a href="{@docRoot}guide/topics/manifest/provider-element.html"
    >&lt;provider&gt;</a></code> element with  the following attributes:
</p>
<dl>
    <dt>
        <code>android:name="com.example.android.datasync.provider.StubProvider"</code>
    </dt>
    <dd>
        Specifies the fully-qualified name of the class that implements the stub content provider.
    </dd>
    <dt>
        <code>android:authorities="com.example.android.datasync.provider"</code>
    </dt>
    <dd>
        A URI authority that identifies the stub content provider. Make this value your app's
        package name with the string ".provider" appended to it. Even though you're declaring your
        stub provider to the system, nothing tries to access the provider itself.
   </dd>
    <dt>
        <code>android:exported="false"</code>
    </dt>
    <dd>
        Determines whether other apps can access the content provider. For your stub content
        provider, set the value to {@code false}, since there's no need to allow other apps to see
        the provider. This value doesn't affect the interaction between the sync adapter framework
        and the content provider.
    </dd>
    <dt>
        <code>android:syncable="true"</code>
    </dt>
    <dd>
        Sets a flag that indicates that the provider is syncable. If you set this flag to
        {@code true}, you don't have to call {@link android.content.ContentResolver#setIsSyncable
        setIsSyncable()} in your code. The flag allows the sync adapter framework to make data
        transfers with the content provider, but transfers only occur if you do them explicitly.
    </dd>
</dl>
<p>
    The following snippet shows you how to add the
    <code><a href="{@docRoot}guide/topics/manifest/provider-element.html"
    >&lt;provider&gt;</a></code> element to the app manifest:
</p>
<pre>
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.network.sync.BasicSyncAdapter"
    android:versionCode="1"
    android:versionName="1.0" &gt;
    &lt;application
        android:allowBackup="true"
        android:icon="&#64;drawable/ic_launcher"
        android:label="&#64;string/app_name"
        android:theme="&#64;style/AppTheme" &gt;
    ...
    &lt;provider
        android:name="com.example.android.datasync.provider.StubProvider"
        android:authorities="com.example.android.datasync.provider"
        android:exported="false"
        android:syncable="true"/&gt;
    ...
    &lt;/application&gt;
&lt;/manifest&gt;
</pre>
<p>
    Now that you have created the dependencies required by the sync adapter framework, you can
    create the component that encapsulates your data transfer code. This component is called a
    sync adapter. The next lesson shows you how to add this component to your app.
</p>