use of android.content.ContentProvider in project Etar-Calendar by Etar-Group.
the class AsyncQueryServiceTest method buildTestContext.
private Context buildTestContext(final OperationInfo[] work) {
MockContext context = new MockContext() {
MockContentResolver mResolver;
@Override
public ContentResolver getContentResolver() {
if (mResolver == null) {
mResolver = new MockContentResolver();
final String filenamePrefix = "test.";
RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(new MockContext2(), getContext(), filenamePrefix);
IsolatedContext providerContext = new IsolatedContext(mResolver, targetContextWrapper);
ContentProvider provider = new TestProvider(work);
provider.attachInfo(providerContext, null);
mResolver.addProvider(AUTHORITY, provider);
}
return mResolver;
}
@Override
public String getPackageName() {
return AsyncQueryServiceTest.class.getPackage().getName();
}
@Override
public ComponentName startService(Intent service) {
AsyncQueryServiceTest.this.startService(service);
return service.getComponent();
}
};
return context;
}
use of android.content.ContentProvider in project android_frameworks_base by AOSPA.
the class ActivityThread method installProvider.
/**
* Installs the provider.
*
* Providers that are local to the process or that come from the system server
* may be installed permanently which is indicated by setting noReleaseNeeded to true.
* Other remote providers are reference counted. The initial reference count
* for all reference counted providers is one. Providers that are not reference
* counted do not have a reference count (at all).
*
* This method detects when a provider has already been installed. When this happens,
* it increments the reference count of the existing provider (if appropriate)
* and returns the existing provider. This can happen due to concurrent
* attempts to acquire the same provider.
*/
private IActivityManager.ContentProviderHolder installProvider(Context context, IActivityManager.ContentProviderHolder holder, ProviderInfo info, boolean noisy, boolean noReleaseNeeded, boolean stable) {
ContentProvider localProvider = null;
IContentProvider provider;
if (holder == null || holder.provider == null) {
if (DEBUG_PROVIDER || noisy) {
Slog.d(TAG, "Loading provider " + info.authority + ": " + info.name);
}
Context c = null;
ApplicationInfo ai = info.applicationInfo;
if (context.getPackageName().equals(ai.packageName)) {
c = context;
} else if (mInitialApplication != null && mInitialApplication.getPackageName().equals(ai.packageName)) {
c = mInitialApplication;
} else {
try {
c = context.createPackageContext(ai.packageName, Context.CONTEXT_INCLUDE_CODE);
} catch (PackageManager.NameNotFoundException e) {
// Ignore
}
}
if (c == null) {
Slog.w(TAG, "Unable to get context for package " + ai.packageName + " while loading content provider " + info.name);
return null;
}
try {
final java.lang.ClassLoader cl = c.getClassLoader();
localProvider = (ContentProvider) cl.loadClass(info.name).newInstance();
provider = localProvider.getIContentProvider();
if (provider == null) {
Slog.e(TAG, "Failed to instantiate class " + info.name + " from sourceDir " + info.applicationInfo.sourceDir);
return null;
}
if (DEBUG_PROVIDER)
Slog.v(TAG, "Instantiating local provider " + info.name);
// XXX Need to create the correct context for this provider.
localProvider.attachInfo(c, info);
} catch (java.lang.Exception e) {
if (!mInstrumentation.onException(null, e)) {
throw new RuntimeException("Unable to get provider " + info.name + ": " + e.toString(), e);
}
return null;
}
} else {
provider = holder.provider;
if (DEBUG_PROVIDER)
Slog.v(TAG, "Installing external provider " + info.authority + ": " + info.name);
}
IActivityManager.ContentProviderHolder retHolder;
synchronized (mProviderMap) {
if (DEBUG_PROVIDER)
Slog.v(TAG, "Checking to add " + provider + " / " + info.name);
IBinder jBinder = provider.asBinder();
if (localProvider != null) {
ComponentName cname = new ComponentName(info.packageName, info.name);
ProviderClientRecord pr = mLocalProvidersByName.get(cname);
if (pr != null) {
if (DEBUG_PROVIDER) {
Slog.v(TAG, "installProvider: lost the race, " + "using existing local provider");
}
provider = pr.mProvider;
} else {
holder = new IActivityManager.ContentProviderHolder(info);
holder.provider = provider;
holder.noReleaseNeeded = true;
pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
mLocalProviders.put(jBinder, pr);
mLocalProvidersByName.put(cname, pr);
}
retHolder = pr.mHolder;
} else {
ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
if (prc != null) {
if (DEBUG_PROVIDER) {
Slog.v(TAG, "installProvider: lost the race, updating ref count");
}
// system process).
if (!noReleaseNeeded) {
incProviderRefLocked(prc, stable);
try {
ActivityManagerNative.getDefault().removeContentProvider(holder.connection, stable);
} catch (RemoteException e) {
//do nothing content provider object is dead any way
}
}
} else {
ProviderClientRecord client = installProviderAuthoritiesLocked(provider, localProvider, holder);
if (noReleaseNeeded) {
prc = new ProviderRefCount(holder, client, 1000, 1000);
} else {
prc = stable ? new ProviderRefCount(holder, client, 1, 0) : new ProviderRefCount(holder, client, 0, 1);
}
mProviderRefCountMap.put(jBinder, prc);
}
retHolder = prc.holder;
}
}
return retHolder;
}
use of android.content.ContentProvider in project robolectric by robolectric.
the class ShadowContentResolver method createAndInitialize.
private static ContentProvider createAndInitialize(ContentProviderData providerData) {
try {
ContentProvider provider = (ContentProvider) Class.forName(providerData.getClassName()).newInstance();
initialize(provider, providerData.getAuthorities());
return provider;
} catch (InstantiationException | ClassNotFoundException | IllegalAccessException e) {
throw new RuntimeException("Error instantiating class " + providerData.getClassName());
}
}
use of android.content.ContentProvider in project XobotOS by xamarin.
the class ActivityThread method installProvider.
private IContentProvider installProvider(Context context, IContentProvider provider, ProviderInfo info, boolean noisy) {
ContentProvider localProvider = null;
if (provider == null) {
if (noisy) {
Slog.d(TAG, "Loading provider " + info.authority + ": " + info.name);
}
Context c = null;
ApplicationInfo ai = info.applicationInfo;
if (context.getPackageName().equals(ai.packageName)) {
c = context;
} else if (mInitialApplication != null && mInitialApplication.getPackageName().equals(ai.packageName)) {
c = mInitialApplication;
} else {
try {
c = context.createPackageContext(ai.packageName, Context.CONTEXT_INCLUDE_CODE);
} catch (PackageManager.NameNotFoundException e) {
// Ignore
}
}
if (c == null) {
Slog.w(TAG, "Unable to get context for package " + ai.packageName + " while loading content provider " + info.name);
return null;
}
try {
final java.lang.ClassLoader cl = c.getClassLoader();
localProvider = (ContentProvider) cl.loadClass(info.name).newInstance();
provider = localProvider.getIContentProvider();
if (provider == null) {
Slog.e(TAG, "Failed to instantiate class " + info.name + " from sourceDir " + info.applicationInfo.sourceDir);
return null;
}
if (false)
Slog.v(TAG, "Instantiating local provider " + info.name);
// XXX Need to create the correct context for this provider.
localProvider.attachInfo(c, info);
} catch (java.lang.Exception e) {
if (!mInstrumentation.onException(null, e)) {
throw new RuntimeException("Unable to get provider " + info.name + ": " + e.toString(), e);
}
return null;
}
} else if (localLOGV) {
Slog.v(TAG, "Installing external provider " + info.authority + ": " + info.name);
}
synchronized (mProviderMap) {
// Cache the pointer for the remote provider.
String[] names = PATTERN_SEMICOLON.split(info.authority);
for (int i = 0; i < names.length; i++) {
ProviderClientRecord pr = new ProviderClientRecord(names[i], provider, localProvider);
try {
provider.asBinder().linkToDeath(pr, 0);
mProviderMap.put(names[i], pr);
} catch (RemoteException e) {
return null;
}
}
if (localProvider != null) {
mLocalProviders.put(provider.asBinder(), new ProviderClientRecord(null, provider, localProvider));
}
}
return provider;
}
use of android.content.ContentProvider in project robolectric by robolectric.
the class ShadowContentResolverTest method getProvider_shouldSetAuthority.
@Test
public void getProvider_shouldSetAuthority() throws RemoteException {
AndroidManifest manifest = ShadowApplication.getInstance().getAppManifest();
ContentProviderData testProviderData = new ContentProviderData("org.robolectric.shadows.ShadowContentResolverTest$TestContentProvider", null, AUTHORITY, null, null, null);
try {
manifest.getContentProviders().add(testProviderData);
Uri uri = Uri.parse("content://" + AUTHORITY + "/shadows");
ContentProvider provider = ShadowContentResolver.getProvider(uri);
// unfortunately, there is no direct way of testing if authority is set or not
// however, it's checked in ContentProvider.Transport method calls (validateIncomingUri), so it's the closest we can test against
// should not throw
provider.getIContentProvider().getType(uri);
} finally {
manifest.getContentProviders().remove(testProviderData);
}
}
Aggregations