Java Code Examples for android.content.ContentProvider

The following are top voted examples for showing how to use android.content.ContentProvider. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: VirtualAPK   File: RemoteContentProvider.java   View source code 7 votes vote down vote up
@Override
public Bundle call(String method, String arg, Bundle extras) {
    Log.d(TAG, "call " + method + " with extras : " + extras);

    if (extras == null || extras.getString(KEY_WRAPPER_URI) == null) {
        return null;
    }

    Uri uri = Uri.parse(extras.getString(KEY_WRAPPER_URI));
    ContentProvider provider = getContentProvider(uri);
    if (provider != null) {
        return provider.call(method, arg, extras);
    }

    return null;
}
 
Example 2
Project: VirtualAPK   File: RemoteContentProvider.java   View source code 6 votes vote down vote up
@NonNull
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException {
    try {
        Field uriField = ContentProviderOperation.class.getDeclaredField("mUri");
        uriField.setAccessible(true);
        for (ContentProviderOperation operation : operations) {
            Uri pluginUri = Uri.parse(operation.getUri().getQueryParameter(KEY_URI));
            uriField.set(operation, pluginUri);
        }
    } catch (Exception e) {
        return new ContentProviderResult[0];
    }

    if (operations.size() > 0) {
        ContentProvider provider = getContentProvider(operations.get(0).getUri());
        if (provider != null) {
            return provider.applyBatch(operations);
        }
    }

    return new ContentProviderResult[0];
}
 
Example 3
Project: healthvault-java-sdk   File: ContentProviderTest.java   View source code 6 votes vote down vote up
public void testBadViewUri() throws Exception {
	ContentProvider provider = this.getProvider();

	ContentValues values = new ContentValues();
	values.put("query", "this is a test");
	values.put("date_synced", "1/1/1970");

	try
	{
		provider.insert(
				Uri.withAppendedPath(HVContentContract.CONTENT_URI, "records/42/views/43/this-semgment-is-bad"),
				values);
		Assert.fail("Expected IllegalArgumentException");
	} catch (IllegalArgumentException e) {
		//ignore
	}
}
 
Example 4
Project: Spork   File: DefaultContextResolver.java   View source code 6 votes vote down vote up
@Override
@Nullable
public Context resolveContext(Object object) throws Exception {
	if (object instanceof View) {
		return ((View) object).getContext();
	} else if (object instanceof Fragment) {
		return ((Fragment) object).getActivity();
	} else if (object instanceof Context) {
		return (Context) object;
	} else if (object instanceof ContentProvider) {
		return ((ContentProvider) object).getContext();
	} else if (object instanceof ContextProvider) {
		return ((ContextProvider) object).getContext();
	} else if (object instanceof ViewProvider) {
		return ((ViewProvider) object).getView().getContext();
	} else {
		return null;
	}
}
 
Example 5
Project: dora   File: MetaDataParser.java   View source code 6 votes vote down vote up
public String getAuthority(Context context, Class<? extends ContentProvider> providerClass) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_PROVIDERS);
        ProviderInfo[] providers = packageInfo.providers;
        if (providers.length > 0) {
            for (int i = 0; i < providers.length; i++) {
                ProviderInfo provider = providers[i];
                if (provider.name.contains(providerClass.getSimpleName())) {
                    return provider.authority;
                }
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 6
Project: PluginM   File: PluginStubMainProvider.java   View source code 6 votes vote down vote up
private IContentProvider getIContentProvider(@NonNull PluginInfo pluginInfo, @NonNull ProviderInfo providerInfo) {
    if (pluginInfo != null && providerInfo != null) {
        ContentProvider contentProvider;
        synchronized (mContentProviderMap) {
            contentProvider = mContentProviderMap.get(providerInfo.name);
        }

        if (contentProvider == null) {
            contentProvider = installProvider(pluginInfo, providerInfo);
        }

        if (contentProvider != null) {
            return ContentProviderCompat.getIContentProvider(contentProvider);
        } else {
            throw new RuntimeException("CAN NOT get IContentProvider for " + providerInfo);
        }
    }

    return null;
}
 
Example 7
Project: PluginM   File: PluginStubMainProvider.java   View source code 6 votes vote down vote up
private ContentProvider installProvider(PluginInfo pluginInfo, ProviderInfo providerInfo) {
    try {
        Logger.d(TAG, "installProvider() providerInfo = " + providerInfo);
        Class clazz = pluginInfo.classLoader.loadClass(providerInfo.name);
        ContentProvider contentProvider = (ContentProvider) clazz.newInstance();
        contentProvider.attachInfo(pluginInfo.baseContext, providerInfo);

        synchronized (mContentProviderMap) {
            ContentProvider cp = mContentProviderMap.get(providerInfo.name);
            if (cp != null) {
                Logger.d(TAG, "installProvider() lose race!");
                contentProvider = cp;
            } else {
                mContentProviderMap.put(providerInfo.name, contentProvider);
                PluginManager.getInstance(getContext()).callProviderOnCreate(contentProvider, mStubInfo, providerInfo);
            }
        }

        Logger.d(TAG, "installProvider() providerInfo -> " + contentProvider);
        return contentProvider;
    } catch (Exception e) {
        Logger.e(TAG, "installProvider() error! providerInfo = " + providerInfo, e);
    }

    return null;
}
 
Example 8
Project: Multi-Mania-app   File: SyncUtils.java   View source code 6 votes vote down vote up
/**
 * Syncs a list of IData items with the ContentProvider
 * @param provider A ContentProvider for querying the database in an API like fashion
 * @param models The data items that need syncing
 * @throws RemoteException
 * @see be.ana.nmct.multimania.model.IData
 */
public static void syncData(ContentProvider provider,List<IData> models) throws RemoteException {
    for(IData model: models){
        boolean exists = false;
        if(model.getId()!=-1){
            exists = provider.query(model.getContentUriWithId(),null,null,null,null).getCount()>0;
        }
        if(exists){
            provider.update(model.getContentUri(),
                    model.getContentValues(),
                    BaseColumns._ID+"="+model.getId(),
                    null);
        }else{
            provider.insert(model.getContentUri(),
                    model.getContentValues());
        }
    }
}
 
Example 9
Project: android-project-template   File: ContentResolverRequestQueueTest.java   View source code 6 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();

    // Create test file
    final FileOutputStream os = getContext()
            .openFileOutput(TEST_FILE_NAME, Context.MODE_PRIVATE);
    os.write(TestFiles.TEST_DATA);
    os.close();

    // Replace IsolatedContext with the normal one
    try {
        final Field f = ContentProvider.class.getDeclaredField("mContext");
        f.setAccessible(true);
        f.set(getProvider(), getContext());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 10
Project: mc_backup   File: ContentProviderTest.java   View source code 6 votes vote down vote up
private void setUpContentProvider(ContentProvider targetProvider) throws Exception {
    mResolver = new ChangeRecordingMockContentResolver();

    final String filenamePrefix = this.getClass().getSimpleName() + ".";
    RenamingDelegatingContext targetContextWrapper =
            new RenamingDelegatingContext(
                new ContentProviderMockContext(),
                (Context)getActivity(),
                filenamePrefix);

    mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);

    targetProvider.attachInfo(mProviderContext, null);

    mProvider = new DelegatingTestContentProvider(targetProvider);
    mProvider.attachInfo(mProviderContext, null);

    mResolver.addProvider(mProviderAuthority, mProvider);
}
 
Example 11
Project: GenericDao   File: MetaDataParser.java   View source code 6 votes vote down vote up
public static String getAuthority(Context context, Class<? extends ContentProvider> providerClass) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_PROVIDERS);
        ProviderInfo[] providers = packageInfo.providers;
        if (providers.length > 0) {
            for (int i = 0; i < providers.length; i++) {
                ProviderInfo provider = providers[i];
                if (provider.name.contains(providerClass.getSimpleName())) {
                    return provider.authority;
                }
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 12
Project: FullRobolectricTestSample   File: ShadowContentResolver.java   View source code 6 votes vote down vote up
@Implementation
public 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 {
    TestCursor returnCursor = getCursor(uri);
    if (returnCursor == null) {
      return null;
    }

    returnCursor.setQuery(uri, projection, selection, selectionArgs,
        sortOrder);
    return returnCursor;
  }
}
 
Example 13
Project: droidel   File: ActivityThread.java   View source code 6 votes vote down vote up
private ProviderClientRecord installProviderAuthoritiesLocked(IContentProvider provider,
        ContentProvider localProvider, IActivityManager.ContentProviderHolder holder) {
    final String auths[] = PATTERN_SEMICOLON.split(holder.info.authority);
    final int userId = UserHandle.getUserId(holder.info.applicationInfo.uid);

    final ProviderClientRecord pcr = new ProviderClientRecord(
            auths, provider, localProvider, holder);
    for (String auth : auths) {
        final ProviderKey key = new ProviderKey(auth, userId);
        final ProviderClientRecord existing = mProviderMap.get(key);
        if (existing != null) {
            Slog.w(TAG, "Content provider " + pcr.mHolder.info.name
                    + " already published as " + auth);
        } else {
            mProviderMap.put(key, pcr);
        }
    }
    return pcr;
}
 
Example 14
Project: droidkit-v4x   File: SQLite.java   View source code 6 votes vote down vote up
public void clearDatabase() {
    final String authority = AUTHORITY_REF.get();
    if (!TextUtils.isEmpty(authority)) {
        final ContentProviderClient providerClient = mResolver.acquireContentProviderClient(authority);
        if (providerClient != null) {
            try {
                final ContentProvider provider = providerClient.getLocalContentProvider();
                if (provider instanceof SQLiteProvider) {
                    ((SQLiteProvider) provider).clearDatabase();
                }
            } finally {
                providerClient.release();
            }
        }
    }
}
 
Example 15
Project: EmopAndroid   File: DataUpdateService.java   View source code 6 votes vote down vote up
public DataUpdateService(Context context, ContentProvider provider){
	this.context = context;
	this.provider = provider;
	lockCache = new LruCache<String, Lock>(200) {
        @Override
        protected int sizeOf(String key, Lock obj) {
            return 1;
        }
    };
    dataPath = context.getDatabasePath("lastUpdate.db");
    
    loadLastUpadte();
    syncTimer = new Timer();
    syncTimer.scheduleAtFixedRate(new TimerTask(){

		@Override
		public void run() {
			saveLastUpdate();
		}}, 1000, 1000 * 60);
    
}
 
Example 16
Project: ipz2014   File: MetaDataParser.java   View source code 6 votes vote down vote up
public static String getAuthority(Context context, Class<? extends ContentProvider> providerClass) {
    try {
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_PROVIDERS);
        ProviderInfo[] providers = packageInfo.providers;
        if (providers.length > 0) {
            for (int i = 0; i < providers.length; i++) {
                ProviderInfo provider = providers[i];
                if (provider.name.contains(providerClass.getSimpleName())) {
                    return provider.authority;
                }
            }
        }
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 17
Project: VirtualAPK   File: RemoteContentProvider.java   View source code 5 votes vote down vote up
@Override
public String getType(Uri uri) {
    ContentProvider provider = getContentProvider(uri);
    Uri pluginUri = Uri.parse(uri.getQueryParameter(KEY_URI));
    if (provider != null) {
        return provider.getType(pluginUri);
    }

    return null;
}
 
Example 18
Project: VirtualAPK   File: RemoteContentProvider.java   View source code 5 votes vote down vote up
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    ContentProvider provider = getContentProvider(uri);
    Uri pluginUri = Uri.parse(uri.getQueryParameter(KEY_URI));
    if (provider != null) {
        return provider.query(pluginUri, projection, selection, selectionArgs, sortOrder);
    }

    return null;
}
 
Example 19
Project: VirtualAPK   File: RemoteContentProvider.java   View source code 5 votes vote down vote up
@Override
public Uri insert(Uri uri, ContentValues values) {
    ContentProvider provider = getContentProvider(uri);
    Uri pluginUri = Uri.parse(uri.getQueryParameter(KEY_URI));
    if (provider != null) {
        return provider.insert(pluginUri, values);
    }

    return uri;
}
 
Example 20
Project: VirtualAPK   File: RemoteContentProvider.java   View source code 5 votes vote down vote up
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    ContentProvider provider = getContentProvider(uri);
    Uri pluginUri = Uri.parse(uri.getQueryParameter(KEY_URI));
    if (provider != null) {
        return provider.delete(pluginUri, selection, selectionArgs);
    }

    return 0;
}
 
Example 21
Project: VirtualAPK   File: RemoteContentProvider.java   View source code 5 votes vote down vote up
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    ContentProvider provider = getContentProvider(uri);
    Uri pluginUri = Uri.parse(uri.getQueryParameter(KEY_URI));
    if (provider != null) {
        return provider.update(pluginUri, values, selection, selectionArgs);
    }

    return 0;
}
 
Example 22
Project: VirtualAPK   File: RemoteContentProvider.java   View source code 5 votes vote down vote up
@Override
public int bulkInsert(Uri uri, ContentValues[] values) {
    ContentProvider provider = getContentProvider(uri);
    Uri pluginUri = Uri.parse(uri.getQueryParameter(KEY_URI));
    if (provider != null) {
        return provider.bulkInsert(pluginUri, values);
    }

    return 0;
}
 
Example 23
Project: springreplugin   File: PluginProviderHelper.java   View source code 5 votes vote down vote up
public ContentProvider getProvider(PluginUri pu) {
    if (LogDebug.LOG) {
        Log.i(TAG, "getProvider(): Start... pu=" + pu);
    }

    String auth = pu.transferredUri.getAuthority();

    // 已有缓存?直接返回!
    ContentProvider cp = mProviderAuthorityMap.get(auth);
    if (cp != null) {
        if (LogDebug.LOG) {
            Log.i(TAG, "getProvider(): Exists! Return now. cp=" + cp);
        }
        return cp;
    }

    // 开始构建插件里的ContentProvider对象
    cp = installProvider(pu, auth);
    if (cp == null) {
        if (LogDebug.LOG) {
            Log.e(TAG, "getProvider(): Install fail!");
        }
        return null;
    }

    // 加入列表。下次直接读缓存
    mProviderAuthorityMap.put(auth, cp);

    if (LogDebug.LOG) {
        Log.i(TAG, "getProvider(): Okay! pu=" + pu + "; cp=" + cp);
    }
    return cp;
}
 
Example 24
Project: springreplugin   File: PluginPitProviderBase.java   View source code 5 votes vote down vote up
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    PluginProviderHelper.PluginUri pu = mHelper.toPluginUri(uri);
    if (pu == null) {
        return null;
    }
    ContentProvider cp = mHelper.getProvider(pu);
    if (cp == null) {
        return null;
    }
    return cp.query(pu.transferredUri, projection, selection, selectionArgs, sortOrder);
}
 
Example 25
Project: springreplugin   File: PluginPitProviderBase.java   View source code 5 votes vote down vote up
@Override
@TargetApi(16)
public Cursor query(Uri uri, String[] projection,
                    String selection, String[] selectionArgs, String sortOrder,
                    CancellationSignal cancellationSignal) {
    PluginProviderHelper.PluginUri pu = mHelper.toPluginUri(uri);
    if (pu == null) {
        return null;
    }
    ContentProvider cp = mHelper.getProvider(pu);
    if (cp == null) {
        return null;
    }
    return cp.query(pu.transferredUri, projection, selection, selectionArgs, sortOrder, cancellationSignal);
}
 
Example 26
Project: springreplugin   File: PluginPitProviderBase.java   View source code 5 votes vote down vote up
@Override
public String getType(Uri uri) {
    PluginProviderHelper.PluginUri pu = mHelper.toPluginUri(uri);
    if (pu == null) {
        return null;
    }
    ContentProvider cp = mHelper.getProvider(pu);
    if (cp == null) {
        return null;
    }
    return cp.getType(pu.transferredUri);
}
 
Example 27
Project: springreplugin   File: PluginPitProviderBase.java   View source code 5 votes vote down vote up
@Override
public Uri insert(Uri uri, ContentValues values) {
    PluginProviderHelper.PluginUri pu = mHelper.toPluginUri(uri);
    if (pu == null) {
        return null;
    }
    ContentProvider cp = mHelper.getProvider(pu);
    if (cp == null) {
        return null;
    }
    return cp.insert(pu.transferredUri, values);
}
 
Example 28
Project: springreplugin   File: PluginPitProviderBase.java   View source code 5 votes vote down vote up
@Override
public int bulkInsert(Uri uri, ContentValues[] values) {
    PluginProviderHelper.PluginUri pu = mHelper.toPluginUri(uri);
    if (pu == null) {
        return -1;
    }
    ContentProvider cp = mHelper.getProvider(pu);
    if (cp == null) {
        return -1;
    }
    return cp.bulkInsert(pu.transferredUri, values);
}
 
Example 29
Project: springreplugin   File: PluginPitProviderBase.java   View source code 5 votes vote down vote up
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    PluginProviderHelper.PluginUri pu = mHelper.toPluginUri(uri);
    if (pu == null) {
        return -1;
    }
    ContentProvider cp = mHelper.getProvider(pu);
    if (cp == null) {
        return -1;
    }
    return cp.delete(pu.transferredUri, selection, selectionArgs);
}
 
Example 30
Project: springreplugin   File: PluginPitProviderBase.java   View source code 5 votes vote down vote up
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
    PluginProviderHelper.PluginUri pu = mHelper.toPluginUri(uri);
    if (pu == null) {
        return -1;
    }
    ContentProvider cp = mHelper.getProvider(pu);
    if (cp == null) {
        return -1;
    }
    return cp.update(pu.transferredUri, values, selection, selectionArgs);
}
 
Example 31
Project: springreplugin   File: PluginPitProviderBase.java   View source code 5 votes vote down vote up
@Override
public void onLowMemory() {
    for (ContentProvider cp : mHelper.mProviderAuthorityMap.values()) {
        cp.onLowMemory();
    }
    super.onLowMemory();
}
 
Example 32
Project: springreplugin   File: PluginPitProviderBase.java   View source code 5 votes vote down vote up
@Override
@TargetApi(14)
public void onTrimMemory(int level) {
    for (ContentProvider cp : mHelper.mProviderAuthorityMap.values()) {
        cp.onTrimMemory(level);
    }
    super.onTrimMemory(level);
}
 
Example 33
Project: ACDD   File: ProviderProxy.java   View source code 5 votes vote down vote up
/*****验证ContentProvider实现类是否加载*****/
protected ContentProvider getContentProvider() {
    if (this.mContentProvider != null) {
        return this.mContentProvider;
    }
    try {
        Class<?> loadClass = Globals.getClassLoader().loadClass(mTargetProvider);
        if (loadClass != null) {
            Constructor<?> constructor = loadClass.getConstructor();
            constructor.setAccessible(true);
            this.mContentProvider = (ContentProvider) constructor.newInstance();
            Field declaredField = ContentProvider.class.getDeclaredField("mContext");
            declaredField.setAccessible(true);
            declaredField.set(this.mContentProvider, getContext());
            declaredField = ContentProvider.class.getDeclaredField("mReadPermission");
            declaredField.setAccessible(true);
            declaredField.set(this.mContentProvider, getReadPermission());
            declaredField = ContentProvider.class.getDeclaredField("mWritePermission");
            declaredField.setAccessible(true);
            declaredField.set(this.mContentProvider, getWritePermission());
            declaredField = ContentProvider.class.getDeclaredField("mPathPermissions");
            declaredField.setAccessible(true);
            declaredField.set(this.mContentProvider, getPathPermissions());
            this.mContentProvider.onCreate();
            return this.mContentProvider;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 34
Project: ACDD   File: ProviderProxy.java   View source code 5 votes vote down vote up
@Override
public Cursor query(Uri uri, String[] projection,
                    String selection, String[] selectionArgs, String sortOrder) {
    ContentProvider mContentProvider = getContentProvider();
    if (mContentProvider != null) {
        return mContentProvider.query(uri, projection, selection, selectionArgs, sortOrder);
    }
    return null;
}
 
Example 35
Project: ACDD   File: ProviderProxy.java   View source code 5 votes vote down vote up
@Override
public String getType(Uri uri) {
    ContentProvider mContentProvider = getContentProvider();
    if (mContentProvider != null) {
        return mContentProvider.getType(uri);
    }
    return null;
}
 
Example 36
Project: ACDD   File: ProviderProxy.java   View source code 5 votes vote down vote up
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
    ContentProvider mContentProvider = getContentProvider();
    if (mContentProvider != null) {
        return mContentProvider.insert(uri, contentValues);
    }
    return null;
}
 
Example 37
Project: ACDD   File: ProviderProxy.java   View source code 5 votes vote down vote up
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    ContentProvider mContentProvider = getContentProvider();
    if (mContentProvider != null) {
        return mContentProvider.delete(uri, selection, selectionArgs);
    }
    return 0;
}
 
Example 38
Project: ACDD   File: ProviderProxy.java   View source code 5 votes vote down vote up
@Override
public int update(Uri uri, ContentValues values, String selection,
                  String[] selectionArgs) {
    ContentProvider mContentProvider = getContentProvider();
    if (mContentProvider != null) {
        return mContentProvider.update(uri, values, selection, selectionArgs);
    }
    return 0;
}
 
Example 39
Project: dora   File: UriHelper.java   View source code 5 votes vote down vote up
protected String getAuthority(Context context, Class<? extends ContentProvider> providerClass) {
    if (providerClass == null) {
        return new MetaDataParser().getFirstAuthority(context);
    } else {
        return new MetaDataParser().getAuthority(context, providerClass);
    }
}
 
Example 40
Project: PluginM   File: CommonUtils.java   View source code 5 votes vote down vote up
public static ProviderInfo getProviderInfo(ContentProvider provider) {
    PackageManager pm = provider.getContext().getPackageManager();
    if (pm != null) {
        ComponentName componentName = new ComponentName(provider.getContext(), provider.getClass());
        try {
            return pm.getProviderInfo(componentName, 0);
        } catch (PackageManager.NameNotFoundException e) {
        }
    }

    return null;
}