SyncStateContract.java revision 0f4e1ab773d4d52bfb85a9ad2f050ead3b8b4e49
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.accounts.Account; 24import android.database.Cursor; 25import android.os.RemoteException; 26 27/** 28 * The ContentProvider contract for associating data with ana data array account. 29 * This may be used by providers that want to store this data in a standard way. 30 */ 31public class SyncStateContract { 32 public interface Columns extends BaseColumns { 33 /** 34 * A reference to the name of the account to which this data belongs 35 * <P>Type: STRING</P> 36 */ 37 public static final String ACCOUNT_NAME = "account_name"; 38 39 /** 40 * A reference to the type of the account to which this data belongs 41 * <P>Type: STRING</P> 42 */ 43 public static final String ACCOUNT_TYPE = "account_type"; 44 45 /** 46 * The sync data associated with this account. 47 * <P>Type: NONE</P> 48 */ 49 public static final String DATA = "data"; 50 } 51 52 public static class Constants implements Columns { 53 public static final String CONTENT_DIRECTORY = "syncstate"; 54 } 55 56 public static final class Helpers { 57 private static final String[] DATA_PROJECTION = new String[]{Columns.DATA}; 58 private static final String SELECT_BY_ACCOUNT = 59 Columns.ACCOUNT_NAME + "=? AND " + Columns.ACCOUNT_TYPE + "=?"; 60 61 /** 62 * Get the sync state that is associated with the account or null. 63 * @param provider the {@link ContentProviderClient} that is to be used to communicate 64 * with the {@link android.content.ContentProvider} that contains the sync state. 65 * @param uri the uri of the sync state 66 * @param account the {@link Account} whose sync state should be returned 67 * @return the sync state or null if there is no sync state associated with the account 68 * @throws RemoteException if there is a failure communicating with the remote 69 * {@link android.content.ContentProvider} 70 */ 71 public static byte[] get(ContentProviderClient provider, Uri uri, 72 Account account) throws RemoteException { 73 Cursor c = provider.query(uri, DATA_PROJECTION, SELECT_BY_ACCOUNT, 74 new String[]{account.mName, account.mType}, null); 75 try { 76 if (c.moveToNext()) { 77 return c.getBlob(c.getColumnIndexOrThrow(Columns.DATA)); 78 } 79 } finally { 80 c.close(); 81 } 82 return null; 83 } 84 85 /** 86 * Assigns the data array as the sync state for the given account. 87 * @param provider the {@link ContentProviderClient} that is to be used to communicate 88 * with the {@link android.content.ContentProvider} that contains the sync state. 89 * @param uri the uri of the sync state 90 * @param account the {@link Account} whose sync state should be set 91 * @param data the byte[] that contains the sync state 92 * @throws RemoteException if there is a failure communicating with the remote 93 * {@link android.content.ContentProvider} 94 */ 95 public static void set(ContentProviderClient provider, Uri uri, 96 Account account, byte[] data) throws RemoteException { 97 ContentValues values = new ContentValues(); 98 values.put(Columns.DATA, data); 99 values.put(Columns.ACCOUNT_NAME, account.mName); 100 values.put(Columns.ACCOUNT_TYPE, account.mType); 101 provider.insert(uri, values); 102 } 103 104 /** 105 * Creates and returns a ContentProviderOperation that assigns the data array as the 106 * sync state for the given account. 107 * @param uri the uri of the sync state 108 * @param account the {@link Account} whose sync state should be set 109 * @param data the byte[] that contains the sync state 110 * @return the new ContentProviderOperation that assigns the data array as the 111 * account's sync state 112 */ 113 public static ContentProviderOperation newSetOperation(Uri uri, 114 Account account, byte[] data) { 115 ContentValues values = new ContentValues(); 116 values.put(Columns.DATA, data); 117 return ContentProviderOperation 118 .newInsert(uri) 119 .withValue(Columns.ACCOUNT_NAME, account.mName) 120 .withValue(Columns.ACCOUNT_TYPE, account.mType) 121 .withValues(values) 122 .build(); 123 } 124 } 125} 126