SyncStateContract.java revision c4516a7b62de525e3d6d5e76851bdfaf12c11f05
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package android.provider;
18
19import android.net.Uri;
20import android.content.ContentProviderClient;
21import android.content.ContentValues;
22import android.content.ContentProviderOperation;
23import android.content.ContentUris;
24import android.accounts.Account;
25import android.database.Cursor;
26import android.os.RemoteException;
27import android.util.Pair;
28
29/**
30 * The ContentProvider contract for associating data with ana data array account.
31 * This may be used by providers that want to store this data in a standard way.
32 */
33public class SyncStateContract {
34    public interface Columns extends BaseColumns {
35        /**
36         * A reference to the name of the account to which this data belongs
37         * <P>Type: STRING</P>
38         */
39        public static final String ACCOUNT_NAME = "account_name";
40
41        /**
42         * A reference to the type of the account to which this data belongs
43         * <P>Type: STRING</P>
44         */
45        public static final String ACCOUNT_TYPE = "account_type";
46
47        /**
48         * The sync data associated with this account.
49         * <P>Type: NONE</P>
50         */
51        public static final String DATA = "data";
52    }
53
54    public static class Constants implements Columns {
55        public static final String CONTENT_DIRECTORY = "syncstate";
56    }
57
58    public static final class Helpers {
59        private static final String[] DATA_PROJECTION = new String[]{Columns.DATA, Columns._ID};
60        private static final String SELECT_BY_ACCOUNT =
61                Columns.ACCOUNT_NAME + "=? AND " + Columns.ACCOUNT_TYPE + "=?";
62
63        /**
64         * Get the sync state that is associated with the account or null.
65         * @param provider the {@link ContentProviderClient} that is to be used to communicate
66         * with the {@link android.content.ContentProvider} that contains the sync state.
67         * @param uri the uri of the sync state
68         * @param account the {@link Account} whose sync state should be returned
69         * @return the sync state or null if there is no sync state associated with the account
70         * @throws RemoteException if there is a failure communicating with the remote
71         * {@link android.content.ContentProvider}
72         */
73        public static byte[] get(ContentProviderClient provider, Uri uri,
74                Account account) throws RemoteException {
75            Cursor c = provider.query(uri, DATA_PROJECTION, SELECT_BY_ACCOUNT,
76                    new String[]{account.name, account.type}, null);
77            try {
78                if (c.moveToNext()) {
79                    return c.getBlob(c.getColumnIndexOrThrow(Columns.DATA));
80                }
81            } finally {
82                c.close();
83            }
84            return null;
85        }
86
87        /**
88         * Assigns the data array as the sync state for the given account.
89         * @param provider the {@link ContentProviderClient} that is to be used to communicate
90         * with the {@link android.content.ContentProvider} that contains the sync state.
91         * @param uri the uri of the sync state
92         * @param account the {@link Account} whose sync state should be set
93         * @param data the byte[] that contains the sync state
94         * @throws RemoteException if there is a failure communicating with the remote
95         * {@link android.content.ContentProvider}
96         */
97        public static void set(ContentProviderClient provider, Uri uri,
98                Account account, byte[] data) throws RemoteException {
99            ContentValues values = new ContentValues();
100            values.put(Columns.DATA, data);
101            values.put(Columns.ACCOUNT_NAME, account.name);
102            values.put(Columns.ACCOUNT_TYPE, account.type);
103            provider.insert(uri, values);
104        }
105
106        public static Uri insert(ContentProviderClient provider, Uri uri,
107                Account account, byte[] data) throws RemoteException {
108            ContentValues values = new ContentValues();
109            values.put(Columns.DATA, data);
110            values.put(Columns.ACCOUNT_NAME, account.name);
111            values.put(Columns.ACCOUNT_TYPE, account.type);
112            return provider.insert(uri, values);
113        }
114
115        public static void update(ContentProviderClient provider, Uri uri, byte[] data)
116                throws RemoteException {
117            ContentValues values = new ContentValues();
118            values.put(Columns.DATA, data);
119            provider.update(uri, values, null, null);
120        }
121
122        public static Pair<Uri, byte[]> getWithUri(ContentProviderClient provider, Uri uri,
123                Account account) throws RemoteException {
124            Cursor c = provider.query(uri, DATA_PROJECTION, SELECT_BY_ACCOUNT,
125                    new String[]{account.name, account.type}, null);
126            try {
127                if (c.moveToNext()) {
128                    long rowId = c.getLong(1);
129                    byte[] blob = c.getBlob(c.getColumnIndexOrThrow(Columns.DATA));
130                    return Pair.create(ContentUris.withAppendedId(uri, rowId), blob);
131                }
132            } finally {
133                c.close();
134            }
135            return null;
136        }
137
138        /**
139         * Creates and returns a ContentProviderOperation that assigns the data array as the
140         * sync state for the given account.
141         * @param uri the uri of the sync state
142         * @param account the {@link Account} whose sync state should be set
143         * @param data the byte[] that contains the sync state
144         * @return the new ContentProviderOperation that assigns the data array as the
145         * account's sync state
146         */
147        public static ContentProviderOperation newSetOperation(Uri uri,
148                Account account, byte[] data) {
149            ContentValues values = new ContentValues();
150            values.put(Columns.DATA, data);
151            return ContentProviderOperation
152                    .newInsert(uri)
153                    .withValue(Columns.ACCOUNT_NAME, account.name)
154                    .withValue(Columns.ACCOUNT_TYPE, account.type)
155                    .withValues(values)
156                    .build();
157        }
158
159        /**
160         * Creates and returns a ContentProviderOperation that assigns the data array as the
161         * sync state for the given account.
162         * @param uri the uri of the specific sync state to set
163         * @param data the byte[] that contains the sync state
164         * @return the new ContentProviderOperation that assigns the data array as the
165         * account's sync state
166         */
167        public static ContentProviderOperation newUpdateOperation(Uri uri, byte[] data) {
168            ContentValues values = new ContentValues();
169            values.put(Columns.DATA, data);
170            return ContentProviderOperation
171                    .newUpdate(uri)
172                    .withValues(values)
173                    .build();
174        }
175    }
176}
177