@Override public boolean onCreate() {	// As lazy as possible
		Log.v(TAG, "onCreate");

		final ProviderInfo target_provider = findTargetProvider(context());
		if (target_provider == null) {		// Logging is done in findTargetProvider().
			mDelegate = new PseudoContentProvider();
			return false;
		}
		mTargetAuthority = target_provider.authority;

		// Install IContentService interceptor to transform the URI of content changes notification sent by ExternalStorageProvider.
		try {	// Ensure ContentResolver.sContentService is initialized with the actual IContentService instance.
			@SuppressWarnings("unused") @SuppressLint("MissingPermission") final List<?> syncs = ContentResolver.getCurrentSyncs();
		} catch (final RuntimeException ignored) {}		// SecurityException is expected here.
		try {
			final Field ContentResolver_sContentService = ContentResolver.class.getDeclaredField("sContentService");
			ContentResolver_sContentService.setAccessible(true);
			final Object content_service = ContentResolver_sContentService.get(null);
			if (! Proxy.isProxyClass(content_service.getClass())) {		// There should be no other interceptor in this process.
				final Class<?> IContentService = ContentResolver_sContentService.getType();
				final Object proxy = Proxy.newProxyInstance(IContentService.getClassLoader(), new Class[] { IContentService }, (o, method, args) -> {
					if ("registerContentObserver".equals(method.getName())) args[0] = toProxyUri((Uri) args[0]);
					return method.invoke(content_service, args);
				});
				ContentResolver_sContentService.set(null, proxy);
			} else Analytics.$().event("esp_proxy_error").with(ITEM_CATEGORY, "content_service_proxy")
					.with(ITEM_ID, Proxy.getInvocationHandler(content_service).getClass().getCanonicalName()).send();

			final Context context = context().createPackageContext(target_provider.packageName, CONTEXT_INCLUDE_CODE | CONTEXT_IGNORE_SECURITY);
			Log.d(TAG, "Initializing " + target_provider.name);
			@SuppressLint("PrivateApi") final Class<?> ExternalStorageProvider = context.getClassLoader().loadClass(target_provider.name);
			mDelegate = (ContentProvider) ExternalStorageProvider.newInstance();
			mDelegate.attachInfo(wrapWithResolverWrapper(context), target_provider);
			// onCreate() is indirectly invoked by attachInfo().
			return true;
		} catch (final PackageManager.NameNotFoundException/* Should not happen */| ReflectiveOperationException e) {
			Analytics.$().logAndReport(TAG, "Failed to init due to incompatibility.", e);
		} catch (final Throwable t) {	// No just RuntimeException, but also Error expected. (e.g. NoSuchFieldError thrown by ExternalStorageProvider)
			Analytics.$().logAndReport(TAG, "Failed to init.", t);
		}
		mDelegate = new PseudoContentProvider();
		return false;
	}
Documents provider for accessing files in / outside Island.

Created by Oasis on 2017/8/29.
fileprovider/src/main/java/com/oasisfeng/island/fileprovider/ExternalStorageProviderProxy.java