1 /* 2 * Copyright (C) 2016 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 17 package com.android.settings.testutils.shadow; 18 19 import static android.provider.SearchIndexablesContract.INDEXABLES_RAW_COLUMNS; 20 21 import android.accounts.Account; 22 import android.annotation.UserIdInt; 23 import android.content.ContentResolver; 24 import android.content.SyncAdapterType; 25 import android.database.Cursor; 26 import android.database.MatrixCursor; 27 import android.net.Uri; 28 import android.provider.SearchIndexablesContract; 29 import android.text.TextUtils; 30 31 import org.robolectric.annotation.Implementation; 32 import org.robolectric.annotation.Implements; 33 34 import java.util.HashMap; 35 import java.util.Map; 36 37 @Implements(ContentResolver.class) 38 public class ShadowContentResolver { 39 40 private static SyncAdapterType[] sSyncAdapterTypes = new SyncAdapterType[0]; 41 private static Map<String, Integer> sSyncable = new HashMap<>(); 42 private static Map<String, Boolean> sSyncAutomatically = new HashMap<>(); 43 private static Map<Integer, Boolean> sMasterSyncAutomatically = new HashMap<>(); 44 45 @Implementation getSyncAdapterTypesAsUser(int userId)46 protected static SyncAdapterType[] getSyncAdapterTypesAsUser(int userId) { 47 return sSyncAdapterTypes; 48 } 49 50 @Implementation query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)51 protected final Cursor query(Uri uri, String[] projection, String selection, 52 String[] selectionArgs, String sortOrder) { 53 MatrixCursor cursor = new MatrixCursor(INDEXABLES_RAW_COLUMNS); 54 MatrixCursor.RowBuilder builder = cursor.newRow() 55 .add(SearchIndexablesContract.NonIndexableKey.COLUMN_KEY_VALUE, ""); 56 return cursor; 57 } 58 59 @Implementation getIsSyncableAsUser(Account account, String authority, int userId)60 protected static int getIsSyncableAsUser(Account account, String authority, int userId) { 61 return sSyncable.containsKey(authority) ? sSyncable.get(authority) : 1; 62 } 63 64 @Implementation getSyncAutomaticallyAsUser(Account account, String authority, int userId)65 protected static boolean getSyncAutomaticallyAsUser(Account account, String authority, 66 int userId) { 67 return sSyncAutomatically.containsKey(authority) ? sSyncAutomatically.get(authority) : true; 68 } 69 70 @Implementation setSyncAutomaticallyAsUser(Account account, String authority, boolean sync, int userId)71 protected static void setSyncAutomaticallyAsUser(Account account, String authority, 72 boolean sync, int userId) { 73 if (TextUtils.isEmpty(authority)) { 74 throw new IllegalArgumentException("Authority must be non-empty"); 75 } 76 } 77 78 @Implementation getMasterSyncAutomaticallyAsUser(int userId)79 protected static boolean getMasterSyncAutomaticallyAsUser(int userId) { 80 return sMasterSyncAutomatically.getOrDefault(userId, true); 81 } 82 83 @Implementation setMasterSyncAutomaticallyAsUser(boolean sync, @UserIdInt int userId)84 protected static void setMasterSyncAutomaticallyAsUser(boolean sync, @UserIdInt int userId) { 85 sMasterSyncAutomatically.put(userId, sync); 86 } 87 setSyncAdapterTypes(SyncAdapterType[] syncAdapterTypes)88 public static void setSyncAdapterTypes(SyncAdapterType[] syncAdapterTypes) { 89 sSyncAdapterTypes = syncAdapterTypes; 90 } 91 setSyncable(String authority, int syncable)92 public static void setSyncable(String authority, int syncable) { 93 sSyncable.put(authority, syncable); 94 } 95 setSyncAutomatically(String authority, boolean syncAutomatically)96 public static void setSyncAutomatically(String authority, boolean syncAutomatically) { 97 sSyncAutomatically.put(authority, syncAutomatically); 98 } 99 setMasterSyncAutomatically(int userId, boolean syncAutomatically)100 public static void setMasterSyncAutomatically(int userId, boolean syncAutomatically) { 101 sMasterSyncAutomatically.put(userId, syncAutomatically); 102 } 103 reset()104 public static void reset() { 105 sSyncable.clear(); 106 sSyncAutomatically.clear(); 107 sMasterSyncAutomatically.clear(); 108 sSyncAdapterTypes = new SyncAdapterType[0]; 109 } 110 } 111