Java Code Examples for android.content.ContentProvider

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project android_packages_apps_Gallery2, under directory /tests/src/com/android/gallery3d/data/.

Source file: LocalDataTest.java

  18 
vote

static GalleryAppStub newGalleryContext(SQLiteDatabase db,Looper mainLooper){
  MockContentResolver cr=new MockContentResolver();
  ContentProvider cp=new DbContentProvider(db,cr);
  cr.addProvider("media",cp);
  return new GalleryAppMock(null,cr,mainLooper);
}
 

Example 2

From project platform_packages_apps_browser, under directory /tests/src/com/android/browser/tests/utils/.

Source file: MockContentResolver2.java

  18 
vote

/** 
 * @hide 
 */
@Override protected IContentProvider acquireExistingProvider(Context context,String name){
  final ContentProvider provider=mProviders.get(name);
  if (provider != null) {
    return provider.getIContentProvider();
  }
 else {
    return null;
  }
}
 

Example 3

From project platform_packages_apps_Gallery2_1, under directory /tests/src/com/android/gallery3d/data/.

Source file: LocalDataTest.java

  18 
vote

static GalleryAppStub newGalleryContext(SQLiteDatabase db,Looper mainLooper){
  MockContentResolver cr=new MockContentResolver();
  ContentProvider cp=new DbContentProvider(db,cr);
  cr.addProvider("media",cp);
  return new GalleryAppMock(null,cr,mainLooper);
}
 

Example 4

From project platform_packages_providers_contactsprovider, under directory /src/com/android/providers/contacts/.

Source file: LocaleChangeReceiver.java

  18 
vote

@Override public void onReceive(Context context,Intent intent){
  IContentProvider iprovider=context.getContentResolver().acquireProvider(ContactsContract.AUTHORITY);
  ContentProvider provider=ContentProvider.coerceToLocalContentProvider(iprovider);
  if (provider instanceof ContactsProvider2) {
    ((ContactsProvider2)provider).onLocaleChanged();
  }
}
 

Example 5

From project platform_packages_providers_contactsprovider, under directory /src/com/android/providers/contacts/.

Source file: PackageIntentReceiver.java

  18 
vote

@Override public void onReceive(Context context,Intent intent){
  Uri packageUri=intent.getData();
  String packageName=packageUri.getSchemeSpecificPart();
  IContentProvider iprovider=context.getContentResolver().acquireProvider(ContactsContract.AUTHORITY);
  ContentProvider provider=ContentProvider.coerceToLocalContentProvider(iprovider);
  if (provider instanceof ContactsProvider2) {
    ((ContactsProvider2)provider).onPackageChanged(packageName);
  }
  handlePackageChangedForVoicemail(context,intent);
}
 

Example 6

From project platform_packages_providers_contactsprovider, under directory /tests/src/com/android/providers/contacts/.

Source file: ContactsActor.java

  18 
vote

public ContentProvider addProvider(Class<? extends ContentProvider> providerClass,String authority) throws Exception {
  ContentProvider provider=providerClass.newInstance();
  ProviderInfo info=new ProviderInfo();
  info.authority=authority;
  provider.attachInfo(mProviderContext,info);
  resolver.addProvider(authority,provider);
  return provider;
}
 

Example 7

From project Locast-Android, under directory /src/edu/mit/mobile/android/content/.

Source file: DBHelperMapper.java

  15 
vote

public Cursor query(int code,ContentProvider provider,SQLiteDatabase db,Uri uri,String[] projection,String selection,String[] selectionArgs,String sortOrder){
  final DBHelperMapItem dbhmi=getMap(TYPE_QUERY,code);
  if (dbhmi.isItem) {
    return dbhmi.dbHelper.queryItem(db,uri,projection,selection,selectionArgs,sortOrder);
  }
 else {
    return dbhmi.dbHelper.queryDir(db,uri,projection,selection,selectionArgs,sortOrder);
  }
}
 

Example 8

From project Locast-Android, under directory /src/edu/mit/mobile/android/content/.

Source file: DBHelperMapper.java

  15 
vote

public int update(int code,ContentProvider provider,SQLiteDatabase db,Uri uri,ContentValues cv,String selection,String[] selectionArgs){
  final DBHelperMapItem dbhmi=getMap(TYPE_QUERY,code);
  if (dbhmi.isItem) {
    return dbhmi.dbHelper.updateItem(db,provider,uri,cv,selection,selectionArgs);
  }
 else {
    return dbhmi.dbHelper.updateDir(db,provider,uri,cv,selection,selectionArgs);
  }
}
 

Example 9

From project Locast-Android, under directory /src/edu/mit/mobile/android/content/.

Source file: DBHelperMapper.java

  15 
vote

public int delete(int code,ContentProvider provider,SQLiteDatabase db,Uri uri,String selection,String[] selectionArgs){
  final DBHelperMapItem dbhmi=getMap(TYPE_QUERY,code);
  if (dbhmi.isItem) {
    return dbhmi.dbHelper.deleteItem(db,provider,uri,selection,selectionArgs);
  }
 else {
    return dbhmi.dbHelper.deleteDir(db,provider,uri,selection,selectionArgs);
  }
}
 

Example 10

From project Locast-Android, under directory /src/edu/mit/mobile/android/content/.

Source file: GenericDBHelper.java

  15 
vote

@Override public Uri insertDir(SQLiteDatabase db,ContentProvider provider,Uri uri,ContentValues values){
  final long id=db.insert(mTable,null,values);
  if (id != -1) {
    return ContentUris.withAppendedId(mContentUri,id);
  }
 else {
    throw new SQLException("error inserting into " + mTable);
  }
}
 

Example 11

From project Locast-Android, under directory /src/edu/mit/mobile/android/content/.

Source file: ManyToMany.java

  15 
vote

/** 
 * Inserts a child into the database and adds a relation to its parent. If the item described by values is already present, only adds the relation.
 * @param db
 * @param uri URI to insert into. This must be a be a hierarchical URI that points to the directory of the desired parent's children. Eg. "/itinerary/1/casts/"
 * @param values values for the child
 * @param childFinder a finder that will look for
 * @return the URI of the child that was either related or inserted.
 */
public Uri insertItemWithRelation(SQLiteDatabase db,ContentProvider provider,Uri parentChildDir,ContentValues values,IdenticalChildFinder childFinder){
  final Uri parent=ProviderUtils.removeLastPathSegment(parentChildDir);
  final long parentId=ContentUris.parseId(parent);
  Uri newItem;
  db.beginTransaction();
  try {
    if (childFinder != null) {
      newItem=childFinder.getIdenticalChild(this,parentChildDir,db,mToTable,values);
    }
 else {
      newItem=null;
    }
    long childId=-1;
    if (newItem != null) {
      childId=ContentUris.parseId(newItem);
    }
 else {
      if (mToContentUri != null) {
        newItem=provider.insert(mToContentUri,values);
        childId=ContentUris.parseId(newItem);
      }
 else {
        childId=db.insert(mToTable,null,values);
        if (childId != -1) {
          newItem=ContentUris.withAppendedId(parentChildDir,childId);
        }
      }
    }
    if (newItem != null && childId != -1) {
      addRelation(db,parentId,childId);
    }
    db.setTransactionSuccessful();
  }
  finally {
    db.endTransaction();
  }
  return newItem;
}
 

Example 12

From project Locast-Android, under directory /src/edu/mit/mobile/android/content/.

Source file: ManyToMany.java

  15 
vote

/** 
 * Updates the item in the "to" table whose URI is specified. XXX Does not verify that there's actually a relationship between from and to.
 * @param db
 * @param provider
 * @param uri the URI of the child. Child uri must end in its ID
 * @param values
 * @param where
 * @param whereArgs
 * @return the number of items that have been updated
 */
@Override public int updateItem(SQLiteDatabase db,ContentProvider provider,Uri uri,ContentValues values,String where,String[] whereArgs){
  int count;
  if (mToContentUri != null) {
    count=provider.update(ContentUris.withAppendedId(mToContentUri,ContentUris.parseId(uri)),values,where,whereArgs);
  }
 else {
    count=db.update(mToTable,values,ProviderUtils.addExtraWhere(where,BaseColumns._ID + "=?"),ProviderUtils.addExtraWhereArgs(whereArgs,uri.getLastPathSegment()));
  }
  return count;
}
 

Example 13

From project Locast-Android, under directory /src/edu/mit/mobile/android/content/.

Source file: ManyToMany.java

  15 
vote

@Override public int updateDir(SQLiteDatabase db,ContentProvider provider,Uri uri,ContentValues values,String where,String[] whereArgs){
  int count;
  if (mToContentUri != null) {
    count=provider.update(mToContentUri,values,where,whereArgs);
  }
 else {
    count=db.update(mToTable,values,where,whereArgs);
  }
  return count;
}
 

Example 14

From project Locast-Android, under directory /src/edu/mit/mobile/android/content/.

Source file: ManyToMany.java

  15 
vote

@Override public int deleteItem(SQLiteDatabase db,ContentProvider provider,Uri uri,String where,String[] whereArgs){
  int count;
  try {
    db.beginTransaction();
    final long childId=ContentUris.parseId(uri);
    final Uri parent=ProviderUtils.removeLastPathSegments(uri,2);
    if (mToContentUri != null) {
      count=provider.delete(ContentUris.withAppendedId(mToContentUri,childId),where,whereArgs);
    }
 else {
      count=db.delete(mToTable,ProviderUtils.addExtraWhere(where,BaseColumns._ID + "=?"),ProviderUtils.addExtraWhereArgs(whereArgs,String.valueOf(childId)));
    }
    final int rows=removeRelation(db,ContentUris.parseId(parent),childId);
    if (rows == 0) {
      throw new IllegalArgumentException("There is no relation between " + parent + " and "+ mToTable+ ": ID "+ childId);
    }
    db.setTransactionSuccessful();
  }
  finally {
    db.endTransaction();
  }
  return count;
}
 

Example 15

From project Locast-Android, under directory /src/edu/mit/mobile/android/content/.

Source file: ManyToMany.java

  15 
vote

@Override public int deleteDir(SQLiteDatabase db,ContentProvider provider,Uri uri,String where,String[] whereArgs){
  int count;
  try {
    db.beginTransaction();
    final Uri parent=ProviderUtils.removeLastPathSegment(uri);
    if (mToContentUri != null) {
      count=provider.delete(mToContentUri,where,whereArgs);
    }
 else {
      count=db.delete(mToTable,where,whereArgs);
    }
    removeRelation(db,ContentUris.parseId(parent));
    db.setTransactionSuccessful();
  }
  finally {
    db.endTransaction();
  }
  return count;
}
 

Example 16

From project packages_apps_Calendar, under directory /tests/src/com/android/calendar/.

Source file: AsyncQueryServiceTest.java

  15 
vote

private Context buildTestContext(final OperationInfo[] work){
  MockContext context=new MockContext(){
    MockContentResolver mResolver;
    @Override public ContentResolver getContentResolver(){
      if (mResolver == null) {
        ContentProvider provider=new TestProvider(work);
        mResolver=new MockContentResolver();
        mResolver.addProvider(AUTHORITY,provider);
      }
      return mResolver;
    }
    @Override public String getPackageName(){
      return AsyncQueryServiceTest.class.getPackage().getName();
    }
    public ComponentName startService(    Intent service){
      AsyncQueryServiceTest.this.startService(service);
      return service.getComponent();
    }
  }
;
  return context;
}
 

Example 17

From project platform_packages_apps_calendar, under directory /tests/src/com/android/calendar/.

Source file: AsyncQueryServiceTest.java

  15 
vote

private Context buildTestContext(final OperationInfo[] work){
  MockContext context=new MockContext(){
    MockContentResolver mResolver;
    @Override public ContentResolver getContentResolver(){
      if (mResolver == null) {
        ContentProvider provider=new TestProvider(work);
        mResolver=new MockContentResolver();
        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;
}
 

Example 18

From project platform_packages_providers_contactsprovider, under directory /tests/src/com/android/providers/contacts/.

Source file: ContactsActor.java

  15 
vote

/** 
 * Create an "actor" using the given parent  {@link Context} and the specificpackage name. Internally, all  {@link Context} method calls are passed toa new instance of  {@link RestrictionMockContext}, which stubs out the security infrastructure.
 */
public ContactsActor(Context overallContext,String packageName,Class<? extends ContentProvider> providerClass,String authority) throws Exception {
  resolver=new MockContentResolver();
  context=new RestrictionMockContext(overallContext,packageName,resolver,mGrantedPermissions,mGrantedUriPermissions);
  this.packageName=packageName;
  android.provider.Settings.Secure.getString(overallContext.getContentResolver(),"dummy");
  RenamingDelegatingContext targetContextWrapper=new RenamingDelegatingContext(context,overallContext,FILENAME_PREFIX);
  mProviderContext=new IsolatedContext(resolver,targetContextWrapper){
    private final MockSharedPreferences mPrefs=new MockSharedPreferences();
    @Override public File getFilesDir(){
      return new File("/data/data/com.android.providers.contacts.tests/files");
    }
    @Override public Object getSystemService(    String name){
      if (Context.COUNTRY_DETECTOR.equals(name)) {
        return mMockCountryDetector;
      }
      if (Context.ACCOUNT_SERVICE.equals(name)) {
        return mMockAccountManager;
      }
      return super.getSystemService(name);
    }
    @Override public SharedPreferences getSharedPreferences(    String name,    int mode){
      return mPrefs;
    }
  }
;
  mMockAccountManager=new MockAccountManager(mProviderContext);
  provider=addProvider(providerClass,authority);
}