Example usage for android.content ContentResolver SCHEME_CONTENT

List of usage examples for android.content ContentResolver SCHEME_CONTENT

Introduction

In this page you can find the example usage for android.content ContentResolver SCHEME_CONTENT.

Prototype

String SCHEME_CONTENT

To view the source code for android.content ContentResolver SCHEME_CONTENT.

Click Source Link

Usage

From source file:com.android.quicksearchbox.ShortcutRepositoryImplLog.java

private String getIconUriString(Source source, String drawableId) {
    // Fast path for empty icons
    if (TextUtils.isEmpty(drawableId) || "0".equals(drawableId)) {
        return null;
    }/*from w w  w. j  a va2 s.  co m*/
    // Fast path for icon URIs
    if (drawableId.startsWith(ContentResolver.SCHEME_ANDROID_RESOURCE)
            || drawableId.startsWith(ContentResolver.SCHEME_CONTENT)
            || drawableId.startsWith(ContentResolver.SCHEME_FILE)) {
        return drawableId;
    }
    Uri uri = source.getIconUri(drawableId);
    return uri == null ? null : uri.toString();
}

From source file:android.support.v7.widget.SuggestionsAdapter.java

/**
 * Import of hidden method: SearchManager.getSuggestions(SearchableInfo, String, int).
 */// w w w  .j ava2 s.  c o m
Cursor getSearchManagerSuggestions(SearchableInfo searchable, String query, int limit) {
    if (searchable == null) {
        return null;
    }

    String authority = searchable.getSuggestAuthority();
    if (authority == null) {
        return null;
    }

    Uri.Builder uriBuilder = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority)
            .query("") // TODO: Remove, workaround for a bug in Uri.writeToParcel()
            .fragment(""); // TODO: Remove, workaround for a bug in Uri.writeToParcel()

    // if content path provided, insert it now
    final String contentPath = searchable.getSuggestPath();
    if (contentPath != null) {
        uriBuilder.appendEncodedPath(contentPath);
    }

    // append standard suggestion query path
    uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY);

    // get the query selection, may be null
    String selection = searchable.getSuggestSelection();
    // inject query, either as selection args or inline
    String[] selArgs = null;
    if (selection != null) { // use selection if provided
        selArgs = new String[] { query };
    } else { // no selection, use REST pattern
        uriBuilder.appendPath(query);
    }

    if (limit > 0) {
        uriBuilder.appendQueryParameter("limit", String.valueOf(limit));
    }

    Uri uri = uriBuilder.build();

    // finally, make the query
    return mContext.getContentResolver().query(uri, null, selection, selArgs, null);
}

From source file:net.gsantner.opoc.util.ContextUtils.java

/**
 * Detect MimeType of given file/*from w w  w .ja  v  a2s. c om*/
 * Android/Java's own MimeType map is very very small and detection barely works at all
 * Hence use custom map for some file extensions
 */
public String getMimeType(Uri uri) {
    String mimeType = null;
    if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
        ContentResolver cr = _context.getContentResolver();
        mimeType = cr.getType(uri);
    } else {
        String ext = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
        mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext.toLowerCase());

        // Try to guess if the recommended methods fail
        if (TextUtils.isEmpty(mimeType)) {
            switch (ext) {
            case "md":
            case "markdown":
            case "mkd":
            case "mdown":
            case "mkdn":
            case "mdwn":
            case "rmd":
                mimeType = "text/markdown";
                break;
            case "yaml":
            case "yml":
                mimeType = "text/yaml";
                break;
            case "json":
                mimeType = "text/json";
                break;
            case "txt":
                mimeType = "text/plain";
                break;
            }
        }
    }

    if (TextUtils.isEmpty(mimeType)) {
        mimeType = "*/*";
    }
    return mimeType;
}

From source file:org.transdroid.core.gui.TorrentsActivity.java

/**
 * If required, add torrents from the supplied intent extras.
 *//*w ww . j  a  v  a2 s  .com*/
protected void addFromIntent() {
    Intent intent = getIntent();
    Uri dataUri = intent.getData();
    String data = intent.getDataString();
    String action = intent.getAction();

    // Adding multiple torrents at the same time (as found in the Intent extras Bundle)
    if (action != null && action.equals("org.transdroid.ADD_MULTIPLE")) {
        // Intent should have some extras pointing to possibly multiple torrents
        String[] urls = intent.getStringArrayExtra("TORRENT_URLS");
        String[] titles = intent.getStringArrayExtra("TORRENT_TITLES");
        if (urls != null) {
            for (int i = 0; i < urls.length; i++) {
                String title = (titles != null && titles.length >= i ? titles[i]
                        : NavigationHelper.extractNameFromUri(Uri.parse(urls[i])));
                if (intent.hasExtra("PRIVATE_SOURCE")) {
                    // This is marked by the Search Module as being a private source site; get the url locally first
                    addTorrentFromPrivateSource(urls[i], title, intent.getStringExtra("PRIVATE_SOURCE"));
                } else {
                    addTorrentByUrl(urls[i], title);
                }
            }
        }
        return;
    }

    // Add a torrent from a local or remote data URI?
    if (dataUri == null) {
        return;
    }
    if (dataUri.getScheme() == null) {
        SnackbarManager
                .show(Snackbar.with(this).text(R.string.error_invalid_url_form).colorResource(R.color.red));
        return;
    }

    // Get torrent title
    String title = NavigationHelper.extractNameFromUri(dataUri);
    if (intent.hasExtra("TORRENT_TITLE")) {
        title = intent.getStringExtra("TORRENT_TITLE");
    }

    // Adding a torrent from the Android downloads manager
    if (dataUri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
        addTorrentFromDownloads(dataUri, title);
        return;
    }

    // Adding a torrent from http or https URL
    if (dataUri.getScheme().equals("http") || dataUri.getScheme().equals("https")) {

        String privateSource = getIntent().getStringExtra("PRIVATE_SOURCE");

        WebsearchSetting match = null;
        if (privateSource == null) {
            // Check if the target URL is also defined as a web search in the user's settings
            List<WebsearchSetting> websearches = applicationSettings.getWebsearchSettings();
            for (WebsearchSetting setting : websearches) {
                Uri uri = Uri.parse(setting.getBaseUrl());
                if (uri.getHost() != null && uri.getHost().equals(dataUri.getHost())) {
                    match = setting;
                    break;
                }
            }
        }

        // If the URL is also a web search and it defines cookies, use the cookies by downloading the targeted
        // torrent file (while supplies the cookies to the HTTP request) instead of sending the URL directly to the
        // torrent client. If instead it is marked (by the Torrent Search module) as being form a private site, use
        // the Search Module instead to download the url locally first.
        if (match != null && match.getCookies() != null) {
            addTorrentFromWeb(data, match, title);
        } else if (privateSource != null) {
            addTorrentFromPrivateSource(data, title, privateSource);
        } else {
            // Normally send the URL to the torrent client
            addTorrentByUrl(data, title);
        }
        return;
    }

    // Adding a torrent from magnet URL
    if (dataUri.getScheme().equals("magnet")) {
        addTorrentByMagnetUrl(data, title);
        return;
    }

    // Adding a local .torrent file; the title we show is just the file name
    if (dataUri.getScheme().equals("file")) {
        addTorrentByFile(data, title);
    }

}

From source file:org.transdroid.core.gui.TorrentsActivity.java

@Background
@OnActivityResult(FilePickerHelper.ACTIVITY_FILEPICKER)
public void onFilePicked(int resultCode, Intent data) {
    // We should have received an Intent with a local torrent's Uri as data from the file picker
    if (data != null && data.getData() != null && !data.getData().toString().equals("")) {
        Uri dataUri = data.getData();//from ww  w .  j  a v a  2  s .  c  o m

        // Get torrent title
        String title = NavigationHelper.extractNameFromUri(dataUri);

        // Adding a torrent from the via a content:// scheme (access through content provider stream)
        if (dataUri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
            addTorrentFromDownloads(dataUri, title);
            return;
        }

        // Adding a .torrent file directly via the file:// scheme (we can access it directly)
        if (dataUri.getScheme().equals("file")) {
            addTorrentByFile(data.getDataString(), title);
        }

    }
}

From source file:com.android.messaging.mmslib.pdu.PduPersister.java

/**
 * This method expects uri in the following format
 *     content://media/<table_name>/<row_index> (or)
 *     file://sdcard/test.mp4//  w  ww .ja  v  a 2 s .  c  o  m
 *     http://test.com/test.mp4
 *
 * Here <table_name> shall be "video" or "audio" or "images"
 * <row_index> the index of the content in given table
 */
public static String convertUriToPath(final Context context, final Uri uri) {
    String path = null;
    if (null != uri) {
        final String scheme = uri.getScheme();
        if (null == scheme || scheme.equals("") || scheme.equals(ContentResolver.SCHEME_FILE)) {
            path = uri.getPath();

        } else if (scheme.equals("http")) {
            path = uri.toString();

        } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
            final String[] projection = new String[] { MediaStore.MediaColumns.DATA };
            Cursor cursor = null;
            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                if (null == cursor || 0 == cursor.getCount() || !cursor.moveToFirst()) {
                    throw new IllegalArgumentException("Given Uri could not be found" + " in media store");
                }
                final int pathIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                path = cursor.getString(pathIndex);
            } catch (final SQLiteException e) {
                throw new IllegalArgumentException(
                        "Given Uri is not formatted in a way " + "so that it can be found in media store.");
            } finally {
                if (null != cursor) {
                    cursor.close();
                }
            }
        } else {
            throw new IllegalArgumentException("Given Uri scheme is not supported");
        }
    }
    return path;
}

From source file:com.ubuntuone.android.files.activity.FilesActivity.java

private void buildFileContextMenu(Menu menu, FileViewHolder holder) {
    if (ResourceState.STATE_GETTING.equals(holder.resourceState)) {
        menu.add(Menu.NONE, R.id.context_cancel_download, 1, R.string.context_cancel_download);
    } else {//from   w w  w .java 2s  . com
        menu.add(Menu.NONE, R.id.context_download, 1, R.string.context_download);
    }
    if (holder.isPublic) {
        menu.add(Menu.NONE, R.id.context_share_link, 1, R.string.context_share_link);
        menu.add(Menu.NONE, R.id.context_disable_link, 2, R.string.context_disable_link);
    } else {
        menu.add(Menu.NONE, R.id.context_create_link, 1, R.string.context_create_link);
    }
    menu.add(Menu.NONE, R.id.context_rename, 10, R.string.context_rename);
    menu.add(Menu.NONE, R.id.context_delete, 11, R.string.context_delete);
    if (!TextUtils.isEmpty(holder.data) && !holder.data.startsWith(ContentResolver.SCHEME_CONTENT)) {
        menu.add(Menu.NONE, R.id.context_delete_from_device, 12, R.string.context_delete_from_device);
    }
}

From source file:com.ubuntuone.android.files.activity.FilesActivity.java

private void onFileClicked(final String resourcePath, final String resourceState, final String filename,
        final String data) {
    Log.d(TAG, "onFileClicked() data=" + data);

    final boolean isStateIdle = (resourceState == null);
    boolean isDownloading, isFailedDownload;
    isDownloading = isFailedDownload = false;
    if (!isStateIdle) {
        isDownloading = ResourceState.STATE_GETTING.equals(resourceState);
        isFailedDownload = ResourceState.STATE_GETTING_FAILED.equals(resourceState);
    }//from w  ww .  j a v a  2s.  c  o m

    final boolean validTarget = MetaUtilities.isValidUriTarget(data);

    Log.d(TAG, String.format("isStateIdle=%s, validTarget=%s", String.valueOf(isStateIdle),
            String.valueOf(validTarget)));
    if (validTarget && isStateIdle) {
        // File exists and is not being downloaded.
        if (data.startsWith(ContentResolver.SCHEME_CONTENT)) {
            Log.d(TAG, "opening file from content uri");

            final Intent intent = new Intent(Intent.ACTION_VIEW);
            final Uri uri = Uri.parse(data);
            intent.setDataAndType(uri, FileUtilities.getMime(filename));

            final Intent chooser = new Intent(Intent.ACTION_VIEW);
            chooser.putExtra(Intent.EXTRA_INTENT, intent);
            chooser.putExtra(Intent.EXTRA_TITLE, getText(R.string.dialog_open_with_title));
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                UIUtil.showToast(this, R.string.toast_no_suitable_activity);
            }
        } else {
            Log.d(TAG, "opening file directly");
            File file = null;
            try {
                Log.d(TAG, "opening " + data);
                if (data.startsWith(ContentResolver.SCHEME_FILE)) {
                    file = new File(URI.create(data));
                } else {
                    file = new File(data);
                }
            } catch (Exception e) {
                Log.e(TAG, "file uri is empty", e);
            }
            if (file != null && file.exists()) {
                Log.d(TAG, "Resource exists, opening: " + file.getAbsolutePath());
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file), FileUtilities.getMime(data));

                Intent chooser = new Intent(Intent.ACTION_VIEW);
                chooser.putExtra(Intent.EXTRA_INTENT, intent);
                chooser.putExtra(Intent.EXTRA_TITLE, getText(R.string.dialog_open_with_title));
                try {
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    UIUtil.showToast(this, R.string.toast_no_suitable_activity);
                }
            } else {
                Log.d(TAG, "Setting resource as not cached: " + resourcePath);
                MetaUtilities.setIsCached(resourcePath, false);
                MetaUtilities.setStateAndData(resourcePath, null, null);
                UIUtil.showToast(this, R.string.toast_file_not_cached_anymore, false);
                mResolver.notifyChange(Nodes.CONTENT_URI, null);
            }
        }
    } else if (isFailedDownload) {
        // File does not exist or download failed.
        Log.d(TAG, "was: failed download or invalid");
        if (NetworkUtil.isConnected(this)) {
            downloadFile(resourcePath);
        } else {
            UIUtil.showToast(this, R.string.toast_no_network);
        }
    } else if (isStateIdle && !validTarget) {
        // File removed from device, need to download.
        downloadFile(resourcePath);
    } else if (isDownloading) {
        UIUtil.showToast(this, "Please wait while downloading...");
    } else {
        Log.e(TAG, "unhandled state: " + resourceState);
    }
}

From source file:me.ccrama.redditslide.Views.SubsamplingScaleImageView.java

/**
 * Helper method for load tasks. Examines the EXIF info on the image file to determine the orientation.
 * This will only work for external files, not assets, resources or other URIs.
 *//*from  w  w w  .  ja v  a 2 s . c om*/
private int getExifOrientation(String sourceUri) {
    int exifOrientation = ORIENTATION_0;
    if (sourceUri.startsWith(ContentResolver.SCHEME_CONTENT)) {
        try {
            final String[] columns = { MediaStore.Images.Media.ORIENTATION };
            final Cursor cursor = getContext().getContentResolver().query(Uri.parse(sourceUri), columns, null,
                    null, null);
            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    int orientation = cursor.getInt(0);
                    if (VALID_ORIENTATIONS.contains(orientation) && orientation != ORIENTATION_USE_EXIF) {
                        exifOrientation = orientation;
                    } else {
                        Log.w(TAG, "Unsupported orientation: " + orientation);
                    }
                }
                cursor.close();
            }
        } catch (Exception e) {
            Log.w(TAG, "Could not get orientation of image from media store");
        }
    } else if (sourceUri.startsWith(ImageSource.FILE_SCHEME)
            && !sourceUri.startsWith(ImageSource.ASSET_SCHEME)) {
        try {
            ExifInterface exifInterface = new ExifInterface(
                    sourceUri.substring(ImageSource.FILE_SCHEME.length() - 1));
            int orientationAttr = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);
            if (orientationAttr == ExifInterface.ORIENTATION_NORMAL
                    || orientationAttr == ExifInterface.ORIENTATION_UNDEFINED) {
                exifOrientation = ORIENTATION_0;
            } else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_90) {
                exifOrientation = ORIENTATION_90;
            } else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_180) {
                exifOrientation = ORIENTATION_180;
            } else if (orientationAttr == ExifInterface.ORIENTATION_ROTATE_270) {
                exifOrientation = ORIENTATION_270;
            } else {
                Log.w(TAG, "Unsupported EXIF orientation: " + orientationAttr);
            }
        } catch (Exception e) {
            Log.w(TAG, "Could not get EXIF orientation of image");
        }
    }
    return exifOrientation;
}