Upstream version 10.39.225.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.ContentValues;
8 import android.content.Context;
9 import android.database.Cursor;
10 import android.net.Uri;
11 import android.provider.MediaStore;
12
13 /**
14  * Utilities for testing operations on content URI.
15  */
16 public class ContentUriTestUtils {
17     /**
18      * Insert an image into the MediaStore, and return the content URI. If the
19      * image already exists in the MediaStore, just retrieve the URI.
20      *
21      * @param context Application context.
22      * @param path Path to the image file.
23      * @return Content URI of the image.
24      */
25     @CalledByNative
26     private static String insertImageIntoMediaStore(Context context, String path) {
27         // Check whether the content URI exists.
28         Cursor c = context.getContentResolver().query(
29                 MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
30                 new String[] { MediaStore.Video.VideoColumns._ID },
31                 MediaStore.Images.Media.DATA + " LIKE ?",
32                 new String[] { path },
33                 null);
34         if (c != null && c.getCount() > 0) {
35             c.moveToFirst();
36             int id = c.getInt(0);
37             return Uri.withAppendedPath(
38                     MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id).toString();
39         }
40
41         // Insert the content URI into MediaStore.
42         ContentValues values = new ContentValues();
43         values.put(MediaStore.MediaColumns.DATA, path);
44         Uri uri = context.getContentResolver().insert(
45                 MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
46         return uri.toString();
47     }
48 }