Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / base / test / android / java / src / org / chromium / base / ContentUriTestUtils.java
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.base;
6
7 import android.content.ContentResolver;
8 import android.content.ContentValues;
9 import android.content.Context;
10 import android.database.Cursor;
11 import android.net.Uri;
12 import android.provider.MediaStore;
13
14 import org.chromium.base.CalledByNative;
15
16 /**
17  * Utilities for testing operations on content URI.
18  */
19 public class ContentUriTestUtils {
20     /**
21      * Insert an image into the MediaStore, and return the content URI. If the
22      * image already exists in the MediaStore, just retrieve the URI.
23      *
24      * @param context Application context.
25      * @param path Path to the image file.
26      * @return Content URI of the image.
27      */
28     @CalledByNative
29     private static String insertImageIntoMediaStore(Context context, String path) {
30         // Check whether the content URI exists.
31         Cursor c = context.getContentResolver().query(
32                 MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
33                 new String[] { MediaStore.Video.VideoColumns._ID },
34                 MediaStore.Images.Media.DATA + " LIKE ?",
35                 new String[] { path },
36                 null);
37         if (c != null && c.getCount() > 0) {
38             c.moveToFirst();
39             int id = c.getInt(0);
40             return Uri.withAppendedPath(
41                     MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id).toString();
42         }
43
44         // Insert the content URI into MediaStore.
45         ContentValues values = new ContentValues();
46         values.put(MediaStore.MediaColumns.DATA, path);
47         Uri uri = context.getContentResolver().insert(
48                 MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
49         return uri.toString();
50     }
51 }