@Implementation protected static void addPeriodicSync( Account account, String authority, Bundle extras, long pollFrequency) { validateSyncExtrasBundle(extras); removePeriodicSync(account, authority, extras); getStatus(account, authority, true) .syncs .add(new PeriodicSync(account, authority, extras, pollFrequency)); }
@Implementation protected final ContentProviderClient acquireUnstableContentProviderClient(String name) { ContentProvider provider = getProvider(name); if (provider == null) { return null; } return getContentProviderClient(provider, false); }
@Implementation protected void notifyChange(Uri uri, ContentObserver observer) { notifyChange(uri, observer, false); }
@Implementation protected static void requestSync(Account account, String authority, Bundle extras) { validateSyncExtrasBundle(extras); Status status = getStatus(account, authority, true); status.syncRequests++; status.syncExtras = extras; }
@Implementation protected static void removePeriodicSync(Account account, String authority, Bundle extras) { validateSyncExtrasBundle(extras); Status status = getStatus(account, authority); if (status != null) { for (int i = 0; i < status.syncs.size(); ++i) { if (isBundleEqual(extras, status.syncs.get(i).extras)) { status.syncs.remove(i); break; } } } }
@Implementation protected final Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { ContentProvider provider = getProvider(uri); if (provider != null) { return provider.query(uri, projection, selection, selectionArgs, sortOrder); } else { BaseCursor returnCursor = getCursor(uri); if (returnCursor == null) { return null; } returnCursor.setQuery(uri, projection, selection, selectionArgs, sortOrder); return returnCursor; } }
public static Status getStatus(Account account, String authority) { return getStatus(account, authority, false); }
public static ContentProvider getProvider(Uri uri) { if (uri == null || !ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) { return null; } return getProvider(uri.getAuthority()); }
@Implementation protected final IContentProvider acquireProvider(String name) { return acquireUnstableProvider(name); }
@Test public void testObserversFire() { Uri[] uris = new Uri[]{Uri.parse("doist.com"), Uri.parse("todoist.com"), Uri.parse("twistapp.com")}; for (int i = 0; i < uris.length; i++) { jobStore.add(JobStatus.createFromJobInfo( JobCreator.create(context, i, 5000) .addTriggerContentUri(new JobInfo.TriggerContentUri(uris[i], 0)) .build(), AlarmScheduler.TAG)); } service.startCommand(0, 0); ShadowApplication application = ShadowApplication.getInstance(); ShadowContentResolver contentResolver = shadowOf(context.getContentResolver()); for (Uri uri : uris) { assertEquals(0, application.getBoundServiceConnections().size()); assertEquals(1, contentResolver.getContentObservers(uri).size()); contentResolver.notifyChange(uri, null); DeviceTestUtils.advanceTime(JobStatus.DEFAULT_TRIGGER_MAX_DELAY); assertEquals(1, contentResolver.getContentObservers(uri).size()); assertEquals(AlarmJobService.class.getCanonicalName(), application.getNextStartedService().getComponent().getClassName()); } } }
private static synchronized ContentProvider getProvider(String authority) { if (!providers.containsKey(authority)) { ProviderInfo providerInfo = RuntimeEnvironment.application.getPackageManager().resolveContentProvider(authority, 0); if (providerInfo != null) { providers.put(providerInfo.authority, createAndInitialize(providerInfo)); } } return providers.get(authority); }
@Implementation(minSdk = JELLY_BEAN_MR1) protected void registerContentObserver( Uri uri, boolean notifyForDescendents, ContentObserver observer, int userHandle) { registerContentObserver(uri, notifyForDescendents, observer); }
@Implementation protected static void removePeriodicSync(Account account, String authority, Bundle extras) { validateSyncExtrasBundle(extras); Status status = getStatus(account, authority); if (status != null) { for (int i = 0; i < status.syncs.size(); ++i) { if (isBundleEqual(extras, status.syncs.get(i).extras)) { status.syncs.remove(i); break; } } } }
@Implementation protected Cursor query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal cancellationSignal) { ContentProvider provider = getProvider(uri); if (provider != null) { return provider.query( uri, projection, selection, selectionArgs, sortOrder, cancellationSignal); } else { BaseCursor returnCursor = getCursor(uri); if (returnCursor == null) { return null; } returnCursor.setQuery(uri, projection, selection, selectionArgs, sortOrder); return returnCursor; } }
@Implementation protected static boolean isSyncActive(Account account, String authority) { ShadowContentResolver.Status status = getStatus(account, authority); // TODO: this means a sync is *perpetually* active after one request return status != null && status.syncRequests > 0; }
@Implementation public static void requestSync(Account account, String authority, Bundle extras) { validateSyncExtrasBundle(extras); Status status = getStatus(account, authority, true); status.syncRequests++; status.syncExtras = extras; }
@Implementation protected ContentProviderResult[] applyBatch( String authority, ArrayList<ContentProviderOperation> operations) throws OperationApplicationException { ContentProvider provider = getProvider(authority); if (provider != null) { return provider.applyBatch(operations); } else { contentProviderOperations.put(authority, operations); return contentProviderResults; } }
@Implementation protected final IContentProvider acquireProvider(Uri uri) { return acquireUnstableProvider(uri); }
private static synchronized ContentProvider getProvider(String authority) { if (!providers.containsKey(authority)) { ProviderInfo providerInfo = RuntimeEnvironment.application.getPackageManager().resolveContentProvider(authority, 0); if (providerInfo != null) { providers.put(providerInfo.authority, createAndInitialize(providerInfo)); } } return providers.get(authority); }
@Implementation(minSdk = JELLY_BEAN_MR1) protected void registerContentObserver( Uri uri, boolean notifyForDescendents, ContentObserver observer, int userHandle) { registerContentObserver(uri, notifyForDescendents, observer); }