Example usage for android.net Uri getAuthority

List of usage examples for android.net Uri getAuthority

Introduction

In this page you can find the example usage for android.net Uri getAuthority.

Prototype

@Nullable
public abstract String getAuthority();

Source Link

Document

Gets the decoded authority part of this URI.

Usage

From source file:com.android.mail.ui.AbstractActivityController.java

private void doMarkConversationMessagesUnread(Conversation conv, Set<Uri> unreadMessageUris,
        byte[] originalConversationInfo) {
    // Only do a granular 'mark unread' if a subset of messages are unread
    final int unreadCount = (unreadMessageUris == null) ? 0 : unreadMessageUris.size();
    final int numMessages = conv.getNumMessages();
    final boolean subsetIsUnread = (numMessages > 1 && unreadCount > 0 && unreadCount < numMessages);

    LogUtils.d(LOG_TAG,//from www.  j  a  v  a  2s  . c o  m
            "markConversationMessagesUnread(conv=%s)" + ", numMessages=%d, unreadCount=%d, subsetIsUnread=%b",
            conv, numMessages, unreadCount, subsetIsUnread);
    if (!subsetIsUnread) {
        // Conversations are neither marked read, nor viewed, and we don't want to show
        // the next conversation.
        LogUtils.d(LOG_TAG, ". . doing full mark unread");
        markConversationsRead(Collections.singletonList(conv), false, false, false);
    } else {
        if (LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG)) {
            final ConversationInfo info = ConversationInfo.fromBlob(originalConversationInfo);
            LogUtils.d(LOG_TAG, ". . doing subset mark unread, originalConversationInfo = %s", info);
        }
        mConversationListCursor.setConversationColumn(conv.uri, ConversationColumns.READ, 0);

        // Locally update conversation's conversationInfo to revert to original version
        if (originalConversationInfo != null) {
            mConversationListCursor.setConversationColumn(conv.uri, ConversationColumns.CONVERSATION_INFO,
                    originalConversationInfo);
        }

        // applyBatch with each CPO as an UPDATE op on each affected message uri
        final ArrayList<ContentProviderOperation> ops = Lists.newArrayList();
        String authority = null;
        for (Uri messageUri : unreadMessageUris) {
            if (authority == null) {
                authority = messageUri.getAuthority();
            }
            ops.add(ContentProviderOperation.newUpdate(messageUri).withValue(UIProvider.MessageColumns.READ, 0)
                    .build());
            LogUtils.d(LOG_TAG, ". . Adding op: read=0, uri=%s", messageUri);
        }
        LogUtils.d(LOG_TAG, ". . operations = %s", ops);
        new ContentProviderTask() {
            @Override
            protected void onPostExecute(Result result) {
                if (result.exception != null) {
                    LogUtils.e(LOG_TAG, result.exception, "ContentProviderTask() ERROR.");
                } else {
                    LogUtils.d(LOG_TAG, "ContentProviderTask(): success %s", Arrays.toString(result.results));
                }
            }
        }.run(mResolver, authority, ops);
    }
}

From source file:com.android.mail.compose.ComposeActivity.java

/**
 * Helper function to handle a list of uris to attach.
 * @return the total size of all successfully attached files.
 *///from   w  w w.  ja va 2s .  c  om
private long handleAttachmentUrisFromIntent(List<Uri> uris) {
    ArrayList<Attachment> attachments = Lists.newArrayList();
    for (Uri uri : uris) {
        try {
            if (uri != null) {
                if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
                    // We must not allow files from /data, even from our process.
                    final File f = new File(uri.getPath());
                    final String filePath = f.getCanonicalPath();
                    if (filePath.startsWith(DATA_DIRECTORY_ROOT)) {
                        showErrorToast(getString(R.string.attachment_permission_denied));
                        Analytics.getInstance().sendEvent(ANALYTICS_CATEGORY_ERRORS, "send_intent_attachment",
                                "data_dir", 0);
                        continue;
                    }
                } else if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
                    // disallow attachments from our own EmailProvider (b/27308057)
                    if (getEmailProviderAuthority().equals(uri.getAuthority())) {
                        showErrorToast(getString(R.string.attachment_permission_denied));
                        Analytics.getInstance().sendEvent(ANALYTICS_CATEGORY_ERRORS, "send_intent_attachment",
                                "email_provider", 0);
                        continue;
                    }
                }

                if (!handleSpecialAttachmentUri(uri)) {
                    final Attachment a = mAttachmentsView.generateLocalAttachment(uri);
                    attachments.add(a);

                    Analytics.getInstance().sendEvent("send_intent_attachment",
                            Utils.normalizeMimeType(a.getContentType()), null, a.size);
                }
            }
        } catch (AttachmentFailureException e) {
            LogUtils.e(LOG_TAG, e, "Error adding attachment");
            showAttachmentTooBigToast(e.getErrorRes());
        } catch (IOException | SecurityException e) {
            LogUtils.e(LOG_TAG, e, "Error adding attachment");
            showErrorToast(getString(R.string.attachment_permission_denied));
        }
    }
    return addAttachments(attachments);
}

From source file:group.pals.android.lib.ui.filechooser.FragmentFiles.java

/**
 * Connects to file provider service, then loads root directory. If can not,
 * then finishes this activity with result code =
 * {@link Activity#RESULT_CANCELED}//from   w  w w  .  ja v a  2 s . c  o  m
 * 
 * @param savedInstanceState
 */
private void loadInitialPath(final Bundle savedInstanceState) {
    if (BuildConfig.DEBUG)
        Log.d(CLASSNAME, String.format("loadInitialPath() >> authority=[%s] | mRoot=[%s]",
                mFileProviderAuthority, mRoot));

    /*
     * Priorities for starting path:
     * 
     * 1. Current location (in case the activity has been killed after
     * configurations changed).
     * 
     * 2. Selected file from key EXTRA_SELECT_FILE.
     * 
     * 3. Root path from key EXTRA_ROOTPATH.
     * 
     * 4. Last location.
     */

    new LoadingDialog<Void, Uri, Bundle>(getActivity(), false) {

        /**
         * In onPostExecute(), if result is null then check this value. If
         * this is not null, show a toast and finish. If this is null, call
         * showCannotConnectToServiceAndWaitForTheUserToFinish().
         */
        String errMsg = null;
        boolean errorMessageInDialog = false;

        String log = "";

        @Override
        protected Bundle doInBackground(Void... params) {
            /*
             * Current location
             */
            Uri path = (Uri) (savedInstanceState != null ? savedInstanceState.getParcelable(CURRENT_LOCATION)
                    : null);

            log += "savedInstanceState != null ? " + (savedInstanceState != null);
            log += "\npath != null ? " + (path != null);
            if (path != null)
                log += path;
            log += "\nmRoot != null ? " + (mRoot != null);
            if (mRoot != null)
                log += mRoot;

            if (mRoot != null) {
                Uri queryUri = BaseFile.genContentUriApi(mRoot.getAuthority()).buildUpon()
                        .appendPath(BaseFile.CMD_CHECK_CONNECTION)
                        .appendQueryParameter(BaseFile.PARAM_SOURCE, mRoot.getLastPathSegment()).build();
                Cursor cursor = getActivity().getContentResolver().query(queryUri, null, null, null, null);
                log += "\ncursor != null ? " + (cursor != null);
                if (cursor != null) {
                    cursor.moveToFirst();

                    errMsg = getString(R.string.afc_msg_cannot_connect_to_file_provider_service) + " " + " "
                            + cursor.getString(0);
                    errorMessageInDialog = true;
                    return null;
                }

            }

            try {
                /*
                 * Selected file
                 */
                log += "try selected file ";
                if (path == null) {
                    path = (Uri) getArguments().getParcelable(FileChooserActivity.EXTRA_SELECT_FILE);

                    if (path != null && BaseFileProviderUtils.fileExists(getActivity(), path))
                        path = BaseFileProviderUtils.getParentFile(getActivity(), path);
                }
                log += "success ? " + (path != null);

                /*
                 * Rootpath
                 */
                log += "rootpath";
                if ((path == null) || (!BaseFileProviderUtils.isDirectory(getActivity(), path))) {
                    log += " assign";
                    path = mRoot;
                }
                if (path != null) {
                    log += " path=" + path;
                    log += " isdir?" + BaseFileProviderUtils.isDirectory(getActivity(), path);
                }

                /*
                 * Last location
                 */
                if (path == null && DisplayPrefs.isRememberLastLocation(getActivity())) {
                    String lastLocation = DisplayPrefs.getLastLocation(getActivity());
                    if (lastLocation != null) {
                        path = Uri.parse(lastLocation);
                        log += " from last loc";
                    }
                }

                if (path == null || !BaseFileProviderUtils.isDirectory(getActivity(), path)) {
                    path = BaseFileProviderUtils.getDefaultPath(getActivity(),
                            path == null ? mFileProviderAuthority : path.getAuthority());
                    log += " getDefault. path==null?" + (path == null);
                }
            } catch (Exception ex) {
                errMsg = getString(R.string.afc_msg_cannot_connect_to_file_provider_service) + " "
                        + ex.toString();
                errorMessageInDialog = true;
                return null;
            }

            if (path == null) {
                errMsg = "Did not find initial path.";
                errorMessageInDialog = true;
                return null;
            }
            if (BuildConfig.DEBUG)
                Log.d(CLASSNAME, "loadInitialPath() >> " + path);

            publishProgress(path);

            if (BaseFileProviderUtils.fileCanRead(getActivity(), path)) {
                Bundle result = new Bundle();
                result.putParcelable(PATH, path);
                return result;
            } else {
                errorMessageInDialog = true;
                errMsg = getString(R.string.afc_pmsg_cannot_access_dir,
                        BaseFileProviderUtils.getFileName(getActivity(), path));
            }

            return null;
        }// doInBackground()

        @Override
        protected void onProgressUpdate(Uri... progress) {
            setCurrentLocation(progress[0]);
        }// onProgressUpdate()

        @Override
        protected void onPostExecute(Bundle result) {
            super.onPostExecute(result);

            if (result != null) {
                /*
                 * Prepare the loader. Either re-connect with an existing
                 * one, or start a new one.
                 */
                getLoaderManager().initLoader(mIdLoaderData, result, FragmentFiles.this);
            } else if (errMsg != null) {
                if (errorMessageInDialog) {
                    Dlg.showError(getActivity(), errMsg, new DialogInterface.OnCancelListener() {

                        @Override
                        public void onCancel(DialogInterface dialog) {
                            getActivity().setResult(Activity.RESULT_FIRST_USER);
                            getActivity().finish();
                        }// onCancel()
                    });
                } else {
                    Dlg.toast(getActivity(), errMsg, Dlg.LENGTH_SHORT);
                    getActivity().finish();
                }

            } else
                showCannotConnectToServiceAndWaitForTheUserToFinish("loadInitialPath");
        }// onPostExecute()

    }.execute();
}

From source file:com.tct.mail.compose.ComposeActivity.java

protected boolean handleLongRunAttachmentUri(final Uri contentUri) {
    if ("com.google.android.apps.photos.content".equals(contentUri.getAuthority())) {
        new LoadAttachmentTask().execute(contentUri);
        return true;
    }//from   w  ww . j a v a 2s  . c o m
    return false;
}