PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/frameworks/base/core/java/android/content/ContentProviderNative.java

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
Java | 648 lines | 521 code | 98 blank | 29 comment | 37 complexity | 7c08ed15199101c2673c723f0d493c48 MD5 | raw file
  1. /*
  2. * Copyright (C) 2006 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package android.content;
  17. import android.content.res.AssetFileDescriptor;
  18. import android.database.BulkCursorDescriptor;
  19. import android.database.BulkCursorNative;
  20. import android.database.BulkCursorToCursorAdaptor;
  21. import android.database.Cursor;
  22. import android.database.CursorToBulkCursorAdaptor;
  23. import android.database.DatabaseUtils;
  24. import android.database.IBulkCursor;
  25. import android.database.IContentObserver;
  26. import android.net.Uri;
  27. import android.os.Binder;
  28. import android.os.Bundle;
  29. import android.os.RemoteException;
  30. import android.os.IBinder;
  31. import android.os.ICancellationSignal;
  32. import android.os.Parcel;
  33. import android.os.ParcelFileDescriptor;
  34. import android.os.Parcelable;
  35. import java.io.FileNotFoundException;
  36. import java.util.ArrayList;
  37. /**
  38. * {@hide}
  39. */
  40. abstract public class ContentProviderNative extends Binder implements IContentProvider {
  41. public ContentProviderNative()
  42. {
  43. attachInterface(this, descriptor);
  44. }
  45. /**
  46. * Cast a Binder object into a content resolver interface, generating
  47. * a proxy if needed.
  48. */
  49. static public IContentProvider asInterface(IBinder obj)
  50. {
  51. if (obj == null) {
  52. return null;
  53. }
  54. IContentProvider in =
  55. (IContentProvider)obj.queryLocalInterface(descriptor);
  56. if (in != null) {
  57. return in;
  58. }
  59. return new ContentProviderProxy(obj);
  60. }
  61. /**
  62. * Gets the name of the content provider.
  63. * Should probably be part of the {@link IContentProvider} interface.
  64. * @return The content provider name.
  65. */
  66. public abstract String getProviderName();
  67. @Override
  68. public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
  69. throws RemoteException {
  70. try {
  71. switch (code) {
  72. case QUERY_TRANSACTION:
  73. {
  74. data.enforceInterface(IContentProvider.descriptor);
  75. Uri url = Uri.CREATOR.createFromParcel(data);
  76. // String[] projection
  77. int num = data.readInt();
  78. String[] projection = null;
  79. if (num > 0) {
  80. projection = new String[num];
  81. for (int i = 0; i < num; i++) {
  82. projection[i] = data.readString();
  83. }
  84. }
  85. // String selection, String[] selectionArgs...
  86. String selection = data.readString();
  87. num = data.readInt();
  88. String[] selectionArgs = null;
  89. if (num > 0) {
  90. selectionArgs = new String[num];
  91. for (int i = 0; i < num; i++) {
  92. selectionArgs[i] = data.readString();
  93. }
  94. }
  95. String sortOrder = data.readString();
  96. IContentObserver observer = IContentObserver.Stub.asInterface(
  97. data.readStrongBinder());
  98. ICancellationSignal cancellationSignal = ICancellationSignal.Stub.asInterface(
  99. data.readStrongBinder());
  100. Cursor cursor = query(url, projection, selection, selectionArgs, sortOrder,
  101. cancellationSignal);
  102. if (cursor != null) {
  103. CursorToBulkCursorAdaptor adaptor = new CursorToBulkCursorAdaptor(
  104. cursor, observer, getProviderName());
  105. BulkCursorDescriptor d = adaptor.getBulkCursorDescriptor();
  106. reply.writeNoException();
  107. reply.writeInt(1);
  108. d.writeToParcel(reply, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
  109. } else {
  110. reply.writeNoException();
  111. reply.writeInt(0);
  112. }
  113. return true;
  114. }
  115. case GET_TYPE_TRANSACTION:
  116. {
  117. data.enforceInterface(IContentProvider.descriptor);
  118. Uri url = Uri.CREATOR.createFromParcel(data);
  119. String type = getType(url);
  120. reply.writeNoException();
  121. reply.writeString(type);
  122. return true;
  123. }
  124. case INSERT_TRANSACTION:
  125. {
  126. data.enforceInterface(IContentProvider.descriptor);
  127. Uri url = Uri.CREATOR.createFromParcel(data);
  128. ContentValues values = ContentValues.CREATOR.createFromParcel(data);
  129. Uri out = insert(url, values);
  130. reply.writeNoException();
  131. Uri.writeToParcel(reply, out);
  132. return true;
  133. }
  134. case BULK_INSERT_TRANSACTION:
  135. {
  136. data.enforceInterface(IContentProvider.descriptor);
  137. Uri url = Uri.CREATOR.createFromParcel(data);
  138. ContentValues[] values = data.createTypedArray(ContentValues.CREATOR);
  139. int count = bulkInsert(url, values);
  140. reply.writeNoException();
  141. reply.writeInt(count);
  142. return true;
  143. }
  144. case APPLY_BATCH_TRANSACTION:
  145. {
  146. data.enforceInterface(IContentProvider.descriptor);
  147. final int numOperations = data.readInt();
  148. final ArrayList<ContentProviderOperation> operations =
  149. new ArrayList<ContentProviderOperation>(numOperations);
  150. for (int i = 0; i < numOperations; i++) {
  151. operations.add(i, ContentProviderOperation.CREATOR.createFromParcel(data));
  152. }
  153. final ContentProviderResult[] results = applyBatch(operations);
  154. reply.writeNoException();
  155. reply.writeTypedArray(results, 0);
  156. return true;
  157. }
  158. case DELETE_TRANSACTION:
  159. {
  160. data.enforceInterface(IContentProvider.descriptor);
  161. Uri url = Uri.CREATOR.createFromParcel(data);
  162. String selection = data.readString();
  163. String[] selectionArgs = data.readStringArray();
  164. int count = delete(url, selection, selectionArgs);
  165. reply.writeNoException();
  166. reply.writeInt(count);
  167. return true;
  168. }
  169. case UPDATE_TRANSACTION:
  170. {
  171. data.enforceInterface(IContentProvider.descriptor);
  172. Uri url = Uri.CREATOR.createFromParcel(data);
  173. ContentValues values = ContentValues.CREATOR.createFromParcel(data);
  174. String selection = data.readString();
  175. String[] selectionArgs = data.readStringArray();
  176. int count = update(url, values, selection, selectionArgs);
  177. reply.writeNoException();
  178. reply.writeInt(count);
  179. return true;
  180. }
  181. case OPEN_FILE_TRANSACTION:
  182. {
  183. data.enforceInterface(IContentProvider.descriptor);
  184. Uri url = Uri.CREATOR.createFromParcel(data);
  185. String mode = data.readString();
  186. ParcelFileDescriptor fd;
  187. fd = openFile(url, mode);
  188. reply.writeNoException();
  189. if (fd != null) {
  190. reply.writeInt(1);
  191. fd.writeToParcel(reply,
  192. Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
  193. } else {
  194. reply.writeInt(0);
  195. }
  196. return true;
  197. }
  198. case OPEN_ASSET_FILE_TRANSACTION:
  199. {
  200. data.enforceInterface(IContentProvider.descriptor);
  201. Uri url = Uri.CREATOR.createFromParcel(data);
  202. String mode = data.readString();
  203. AssetFileDescriptor fd;
  204. fd = openAssetFile(url, mode);
  205. reply.writeNoException();
  206. if (fd != null) {
  207. reply.writeInt(1);
  208. fd.writeToParcel(reply,
  209. Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
  210. } else {
  211. reply.writeInt(0);
  212. }
  213. return true;
  214. }
  215. case CALL_TRANSACTION:
  216. {
  217. data.enforceInterface(IContentProvider.descriptor);
  218. String method = data.readString();
  219. String stringArg = data.readString();
  220. Bundle args = data.readBundle();
  221. Bundle responseBundle = call(method, stringArg, args);
  222. reply.writeNoException();
  223. reply.writeBundle(responseBundle);
  224. return true;
  225. }
  226. case GET_STREAM_TYPES_TRANSACTION:
  227. {
  228. data.enforceInterface(IContentProvider.descriptor);
  229. Uri url = Uri.CREATOR.createFromParcel(data);
  230. String mimeTypeFilter = data.readString();
  231. String[] types = getStreamTypes(url, mimeTypeFilter);
  232. reply.writeNoException();
  233. reply.writeStringArray(types);
  234. return true;
  235. }
  236. case OPEN_TYPED_ASSET_FILE_TRANSACTION:
  237. {
  238. data.enforceInterface(IContentProvider.descriptor);
  239. Uri url = Uri.CREATOR.createFromParcel(data);
  240. String mimeType = data.readString();
  241. Bundle opts = data.readBundle();
  242. AssetFileDescriptor fd;
  243. fd = openTypedAssetFile(url, mimeType, opts);
  244. reply.writeNoException();
  245. if (fd != null) {
  246. reply.writeInt(1);
  247. fd.writeToParcel(reply,
  248. Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
  249. } else {
  250. reply.writeInt(0);
  251. }
  252. return true;
  253. }
  254. case CREATE_CANCELATION_SIGNAL_TRANSACTION:
  255. {
  256. data.enforceInterface(IContentProvider.descriptor);
  257. ICancellationSignal cancellationSignal = createCancellationSignal();
  258. reply.writeNoException();
  259. reply.writeStrongBinder(cancellationSignal.asBinder());
  260. return true;
  261. }
  262. }
  263. } catch (Exception e) {
  264. DatabaseUtils.writeExceptionToParcel(reply, e);
  265. return true;
  266. }
  267. return super.onTransact(code, data, reply, flags);
  268. }
  269. public IBinder asBinder()
  270. {
  271. return this;
  272. }
  273. }
  274. final class ContentProviderProxy implements IContentProvider
  275. {
  276. public ContentProviderProxy(IBinder remote)
  277. {
  278. mRemote = remote;
  279. }
  280. public IBinder asBinder()
  281. {
  282. return mRemote;
  283. }
  284. public Cursor query(Uri url, String[] projection, String selection,
  285. String[] selectionArgs, String sortOrder, ICancellationSignal cancellationSignal)
  286. throws RemoteException {
  287. BulkCursorToCursorAdaptor adaptor = new BulkCursorToCursorAdaptor();
  288. Parcel data = Parcel.obtain();
  289. Parcel reply = Parcel.obtain();
  290. try {
  291. data.writeInterfaceToken(IContentProvider.descriptor);
  292. url.writeToParcel(data, 0);
  293. int length = 0;
  294. if (projection != null) {
  295. length = projection.length;
  296. }
  297. data.writeInt(length);
  298. for (int i = 0; i < length; i++) {
  299. data.writeString(projection[i]);
  300. }
  301. data.writeString(selection);
  302. if (selectionArgs != null) {
  303. length = selectionArgs.length;
  304. } else {
  305. length = 0;
  306. }
  307. data.writeInt(length);
  308. for (int i = 0; i < length; i++) {
  309. data.writeString(selectionArgs[i]);
  310. }
  311. data.writeString(sortOrder);
  312. data.writeStrongBinder(adaptor.getObserver().asBinder());
  313. data.writeStrongBinder(cancellationSignal != null ? cancellationSignal.asBinder() : null);
  314. mRemote.transact(IContentProvider.QUERY_TRANSACTION, data, reply, 0);
  315. DatabaseUtils.readExceptionFromParcel(reply);
  316. if (reply.readInt() != 0) {
  317. BulkCursorDescriptor d = BulkCursorDescriptor.CREATOR.createFromParcel(reply);
  318. adaptor.initialize(d);
  319. } else {
  320. adaptor.close();
  321. adaptor = null;
  322. }
  323. return adaptor;
  324. } catch (RemoteException ex) {
  325. adaptor.close();
  326. throw ex;
  327. } catch (RuntimeException ex) {
  328. adaptor.close();
  329. throw ex;
  330. } finally {
  331. data.recycle();
  332. reply.recycle();
  333. }
  334. }
  335. public String getType(Uri url) throws RemoteException
  336. {
  337. Parcel data = Parcel.obtain();
  338. Parcel reply = Parcel.obtain();
  339. try {
  340. data.writeInterfaceToken(IContentProvider.descriptor);
  341. url.writeToParcel(data, 0);
  342. mRemote.transact(IContentProvider.GET_TYPE_TRANSACTION, data, reply, 0);
  343. DatabaseUtils.readExceptionFromParcel(reply);
  344. String out = reply.readString();
  345. return out;
  346. } finally {
  347. data.recycle();
  348. reply.recycle();
  349. }
  350. }
  351. public Uri insert(Uri url, ContentValues values) throws RemoteException
  352. {
  353. Parcel data = Parcel.obtain();
  354. Parcel reply = Parcel.obtain();
  355. try {
  356. data.writeInterfaceToken(IContentProvider.descriptor);
  357. url.writeToParcel(data, 0);
  358. values.writeToParcel(data, 0);
  359. mRemote.transact(IContentProvider.INSERT_TRANSACTION, data, reply, 0);
  360. DatabaseUtils.readExceptionFromParcel(reply);
  361. Uri out = Uri.CREATOR.createFromParcel(reply);
  362. return out;
  363. } finally {
  364. data.recycle();
  365. reply.recycle();
  366. }
  367. }
  368. public int bulkInsert(Uri url, ContentValues[] values) throws RemoteException {
  369. Parcel data = Parcel.obtain();
  370. Parcel reply = Parcel.obtain();
  371. try {
  372. data.writeInterfaceToken(IContentProvider.descriptor);
  373. url.writeToParcel(data, 0);
  374. data.writeTypedArray(values, 0);
  375. mRemote.transact(IContentProvider.BULK_INSERT_TRANSACTION, data, reply, 0);
  376. DatabaseUtils.readExceptionFromParcel(reply);
  377. int count = reply.readInt();
  378. return count;
  379. } finally {
  380. data.recycle();
  381. reply.recycle();
  382. }
  383. }
  384. public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
  385. throws RemoteException, OperationApplicationException {
  386. Parcel data = Parcel.obtain();
  387. Parcel reply = Parcel.obtain();
  388. try {
  389. data.writeInterfaceToken(IContentProvider.descriptor);
  390. data.writeInt(operations.size());
  391. for (ContentProviderOperation operation : operations) {
  392. operation.writeToParcel(data, 0);
  393. }
  394. mRemote.transact(IContentProvider.APPLY_BATCH_TRANSACTION, data, reply, 0);
  395. DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(reply);
  396. final ContentProviderResult[] results =
  397. reply.createTypedArray(ContentProviderResult.CREATOR);
  398. return results;
  399. } finally {
  400. data.recycle();
  401. reply.recycle();
  402. }
  403. }
  404. public int delete(Uri url, String selection, String[] selectionArgs)
  405. throws RemoteException {
  406. Parcel data = Parcel.obtain();
  407. Parcel reply = Parcel.obtain();
  408. try {
  409. data.writeInterfaceToken(IContentProvider.descriptor);
  410. url.writeToParcel(data, 0);
  411. data.writeString(selection);
  412. data.writeStringArray(selectionArgs);
  413. mRemote.transact(IContentProvider.DELETE_TRANSACTION, data, reply, 0);
  414. DatabaseUtils.readExceptionFromParcel(reply);
  415. int count = reply.readInt();
  416. return count;
  417. } finally {
  418. data.recycle();
  419. reply.recycle();
  420. }
  421. }
  422. public int update(Uri url, ContentValues values, String selection,
  423. String[] selectionArgs) throws RemoteException {
  424. Parcel data = Parcel.obtain();
  425. Parcel reply = Parcel.obtain();
  426. try {
  427. data.writeInterfaceToken(IContentProvider.descriptor);
  428. url.writeToParcel(data, 0);
  429. values.writeToParcel(data, 0);
  430. data.writeString(selection);
  431. data.writeStringArray(selectionArgs);
  432. mRemote.transact(IContentProvider.UPDATE_TRANSACTION, data, reply, 0);
  433. DatabaseUtils.readExceptionFromParcel(reply);
  434. int count = reply.readInt();
  435. return count;
  436. } finally {
  437. data.recycle();
  438. reply.recycle();
  439. }
  440. }
  441. public ParcelFileDescriptor openFile(Uri url, String mode)
  442. throws RemoteException, FileNotFoundException {
  443. Parcel data = Parcel.obtain();
  444. Parcel reply = Parcel.obtain();
  445. try {
  446. data.writeInterfaceToken(IContentProvider.descriptor);
  447. url.writeToParcel(data, 0);
  448. data.writeString(mode);
  449. mRemote.transact(IContentProvider.OPEN_FILE_TRANSACTION, data, reply, 0);
  450. DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(reply);
  451. int has = reply.readInt();
  452. ParcelFileDescriptor fd = has != 0 ? reply.readFileDescriptor() : null;
  453. return fd;
  454. } finally {
  455. data.recycle();
  456. reply.recycle();
  457. }
  458. }
  459. public AssetFileDescriptor openAssetFile(Uri url, String mode)
  460. throws RemoteException, FileNotFoundException {
  461. Parcel data = Parcel.obtain();
  462. Parcel reply = Parcel.obtain();
  463. try {
  464. data.writeInterfaceToken(IContentProvider.descriptor);
  465. url.writeToParcel(data, 0);
  466. data.writeString(mode);
  467. mRemote.transact(IContentProvider.OPEN_ASSET_FILE_TRANSACTION, data, reply, 0);
  468. DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(reply);
  469. int has = reply.readInt();
  470. AssetFileDescriptor fd = has != 0
  471. ? AssetFileDescriptor.CREATOR.createFromParcel(reply) : null;
  472. return fd;
  473. } finally {
  474. data.recycle();
  475. reply.recycle();
  476. }
  477. }
  478. public Bundle call(String method, String request, Bundle args)
  479. throws RemoteException {
  480. Parcel data = Parcel.obtain();
  481. Parcel reply = Parcel.obtain();
  482. try {
  483. data.writeInterfaceToken(IContentProvider.descriptor);
  484. data.writeString(method);
  485. data.writeString(request);
  486. data.writeBundle(args);
  487. mRemote.transact(IContentProvider.CALL_TRANSACTION, data, reply, 0);
  488. DatabaseUtils.readExceptionFromParcel(reply);
  489. Bundle bundle = reply.readBundle();
  490. return bundle;
  491. } finally {
  492. data.recycle();
  493. reply.recycle();
  494. }
  495. }
  496. public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException
  497. {
  498. Parcel data = Parcel.obtain();
  499. Parcel reply = Parcel.obtain();
  500. try {
  501. data.writeInterfaceToken(IContentProvider.descriptor);
  502. url.writeToParcel(data, 0);
  503. data.writeString(mimeTypeFilter);
  504. mRemote.transact(IContentProvider.GET_STREAM_TYPES_TRANSACTION, data, reply, 0);
  505. DatabaseUtils.readExceptionFromParcel(reply);
  506. String[] out = reply.createStringArray();
  507. return out;
  508. } finally {
  509. data.recycle();
  510. reply.recycle();
  511. }
  512. }
  513. public AssetFileDescriptor openTypedAssetFile(Uri url, String mimeType, Bundle opts)
  514. throws RemoteException, FileNotFoundException {
  515. Parcel data = Parcel.obtain();
  516. Parcel reply = Parcel.obtain();
  517. try {
  518. data.writeInterfaceToken(IContentProvider.descriptor);
  519. url.writeToParcel(data, 0);
  520. data.writeString(mimeType);
  521. data.writeBundle(opts);
  522. mRemote.transact(IContentProvider.OPEN_TYPED_ASSET_FILE_TRANSACTION, data, reply, 0);
  523. DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(reply);
  524. int has = reply.readInt();
  525. AssetFileDescriptor fd = has != 0
  526. ? AssetFileDescriptor.CREATOR.createFromParcel(reply) : null;
  527. return fd;
  528. } finally {
  529. data.recycle();
  530. reply.recycle();
  531. }
  532. }
  533. public ICancellationSignal createCancellationSignal() throws RemoteException {
  534. Parcel data = Parcel.obtain();
  535. Parcel reply = Parcel.obtain();
  536. try {
  537. data.writeInterfaceToken(IContentProvider.descriptor);
  538. mRemote.transact(IContentProvider.CREATE_CANCELATION_SIGNAL_TRANSACTION,
  539. data, reply, 0);
  540. DatabaseUtils.readExceptionFromParcel(reply);
  541. ICancellationSignal cancellationSignal = ICancellationSignal.Stub.asInterface(
  542. reply.readStrongBinder());
  543. return cancellationSignal;
  544. } finally {
  545. data.recycle();
  546. reply.recycle();
  547. }
  548. }
  549. private IBinder mRemote;
  550. }