@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]; }
@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; }
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 } }
@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; } }
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; }
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; }
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; }
/** * 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()); } } }
@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); } }
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); }
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; }
@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; } }
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; }
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(); } } } }
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); }
@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; }
@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; }
@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; }
@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; }
@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; }
@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; }
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; }
@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); }
@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); }
@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); }
@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); }
@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); }
@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); }
@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); }
@Override public void onLowMemory() { for (ContentProvider cp : mHelper.mProviderAuthorityMap.values()) { cp.onLowMemory(); } super.onLowMemory(); }
@Override @TargetApi(14) public void onTrimMemory(int level) { for (ContentProvider cp : mHelper.mProviderAuthorityMap.values()) { cp.onTrimMemory(level); } super.onTrimMemory(level); }
/*****验证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; }
@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; }
@Override public String getType(Uri uri) { ContentProvider mContentProvider = getContentProvider(); if (mContentProvider != null) { return mContentProvider.getType(uri); } return null; }
@Override public Uri insert(Uri uri, ContentValues contentValues) { ContentProvider mContentProvider = getContentProvider(); if (mContentProvider != null) { return mContentProvider.insert(uri, contentValues); } return null; }
@Override public int delete(Uri uri, String selection, String[] selectionArgs) { ContentProvider mContentProvider = getContentProvider(); if (mContentProvider != null) { return mContentProvider.delete(uri, selection, selectionArgs); } return 0; }
@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; }
protected String getAuthority(Context context, Class<? extends ContentProvider> providerClass) { if (providerClass == null) { return new MetaDataParser().getFirstAuthority(context); } else { return new MetaDataParser().getAuthority(context, providerClass); } }
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; }