documentsUI下载列表点击事件流程

1.列表布局文件item_doc_list.xml

2.DirectoryFragment.java

private class DocumentsAdapter extends BaseAdapter { 

private View getDocumentView(){

.......

if (state.derivedMode == MODE_LIST) {
                    convertView = inflater.inflate(R.layout.item_doc_list, parent, false);
                } else if (state.derivedMode == MODE_GRID) {
                    convertView = inflater.inflate(R.layout.item_doc_grid, parent, false);}

.....

}

}

mListView.setAdapter(mAdapter);

public View onCreateView(){

mListView.setOnItemClickListener(mItemListener);

}

private OnItemClickListener mItemListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            final Cursor cursor = mAdapter.getItem(position);
Log.e("like","OnItemClickListener position="+position);
            if (cursor != null) {
                final String docMimeType = getCursorString(cursor, Document.COLUMN_MIME_TYPE);
                final int docFlags = getCursorInt(cursor, Document.COLUMN_FLAGS);
Log.e("like","docMimeType="+docMimeType+" docFlags="+docFlags);
                if (isDocumentEnabled(docMimeType, docFlags)) {
                    final DocumentInfo doc = DocumentInfo.fromDirectoryCursor(cursor);
Log.e("like","OnItemClickListener doc.displayName ="+doc.displayName);
                    ((DocumentsActivity) getActivity()).onDocumentPicked(doc);
                }
            }
        }
    };


3.DocumentsActivity.java 来处理事件

public void onDocumentPicked(DocumentInfo doc) {
        final FragmentManager fm = getFragmentManager();
Log.e("like","onDocumentPicked doc.displayName ="+doc.displayName+"   mState.action ="+mState.action);
        if (doc.isDirectory()) {
            mState.stack.push(doc);
            mState.stackTouched = true;
            onCurrentDirectoryChanged(ANIM_DOWN);
        } else if (mState.action == ACTION_OPEN || mState.action == ACTION_GET_CONTENT) {
            // Explicit file picked, return
            new ExistingFinishTask(doc.derivedUri).executeOnExecutor(getCurrentExecutor());
        } else if (mState.action == ACTION_CREATE) {
            // Replace selected file
            SaveFragment.get(fm).setReplaceTarget(doc);
        } else if (mState.action == ACTION_MANAGE) {
            // First try managing the document; we expect manager to filter
            // based on authority, so we don't grant.
            final Intent manage = new Intent(DocumentsContract.ACTION_MANAGE_DOCUMENT);
            manage.setData(doc.derivedUri);
            try {
                startActivity(manage);
Log.e("like","ACTION_MANAGE"+"  doc.derivedUri ="+doc.derivedUri);

//doc.derivedUri =content://com.android.providers.downloads.documents/document/2
            } catch (ActivityNotFoundException ex) {
                // Fall back to viewing
                final Intent view = new Intent(Intent.ACTION_VIEW);
                view.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                view.setData(doc.derivedUri);
                try {
Log.e("like","doc.derivedUri ="+doc.derivedUri+"ex = "+ex);
                    startActivity(view);
                } catch (ActivityNotFoundException ex2) {
                    /// M: Show toast with enhance way.
                    showToast(R.string.toast_no_application);
                }
            }
        }
    }

3.在packages\providers\DownloadProvider\ui\AndroidManifest.xml

            android:name=".TrampolineActivity"
            android:theme="@android:style/Theme.NoDisplay"
            android:permission="android.permission.MANAGE_DOCUMENTS">
           
               
               
                                    android:scheme="content"
                    android:host="com.android.providers.downloads.documents"
                    android:mimeType="*/*" />
           

       


4.TrampolineActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {

switch (status) {
            case DownloadManager.STATUS_PENDING:
            case DownloadManager.STATUS_RUNNING:
Log.d("like", "1");
                sendRunningDownloadClickedBroadcast(id);
                finish();
                break;


            case DownloadManager.STATUS_PAUSED:
Log.d("like", "2");
                if (reason == DownloadManager.PAUSED_QUEUED_FOR_WIFI) {
                    PausedDialogFragment.show(getFragmentManager(), id);
                } else {
                    sendRunningDownloadClickedBroadcast(id);
                    finish();
                }
                break;


            case DownloadManager.STATUS_SUCCESSFUL:
Log.d("like", "startViewIntent id = "+id);
                if (!OpenHelper.startViewIntent(this, id, 0)) {
                    Toast.makeText(this, R.string.download_no_application_title, Toast.LENGTH_SHORT)
                            .show();
                }
                finish();
                break;


            case DownloadManager.STATUS_FAILED:
Log.d("like", "4");
Log.e("like","DownloadManager.STATUS_FAILED");
                FailedDialogFragment.show(getFragmentManager(), id, reason);
                break;
        }

}


5.执行到OpenHelper.startViewIntent(this, id, 0) OpenHelper.java  通过setDataAndType来打开对应应用


public static boolean startViewIntent(Context context, long id, int intentFlags) {
        final Intent intent = OpenHelper.buildViewIntent(context, id);
        if (intent == null) {
            Log.w(TAG, "No intent built for " + id);
            return false;
        }


        intent.addFlags(intentFlags);
        try {
            context.startActivity(intent);
            return true;
        } catch (ActivityNotFoundException e) {
            Log.w(TAG, "Failed to start " + intent + ": " + e);
            return false;
        }
    }


    /**
     * Build an {@link Intent} to view the download with given ID, handling
     * subtleties around installing packages.
     */
    private static Intent buildViewIntent(Context context, long id) {
        final DownloadManager downManager = (DownloadManager) context.getSystemService(
                Context.DOWNLOAD_SERVICE);
        downManager.setAccessAllDownloads(true);


        final Cursor cursor = downManager.query(new DownloadManager.Query().setFilterById(id));
        try {
            if (!cursor.moveToFirst()) {
                return null;
            }


            final Uri localUri = getCursorUri(cursor, COLUMN_LOCAL_URI);
     
            // ckt: HePJ changed for bug 1065 @{
            if(null == localUri){
                return null;
            }
            /// @}


            final File file = getCursorFile(cursor, COLUMN_LOCAL_FILENAME);
            String mimeType = getCursorString(cursor, COLUMN_MEDIA_TYPE);
            if (mimeType == null && file.getName().endsWith(".vcf")) {
                mimeType = "text/x-vcard";
            }
            Log.d(TAG, "file: " + file.getName() + " mimeType: " + mimeType);


            final Intent intent = new Intent(Intent.ACTION_VIEW);


            /// M: Add to support MTK DRM @{
            if (Constants.MTK_DRM_ENABLED
                    && null != mimeType
                    && (mimeType.equalsIgnoreCase(OmaDrmStore.DrmObjectMime.MIME_DRM_MESSAGE) || mimeType
                            .equalsIgnoreCase(OmaDrmStore.DrmObjectMime.MIME_DRM_CONTENT))) {
                Xlog.i(Constants.DL_DRM, "will send DRM intent");
                //DrmManagerClient drmClient = new DrmManagerClient(context);
                //OmaDrmClient drmClient = new OmaDrmClient(context);
                mimeType = DownloadDrmHelper.getOriginalMimeType(context, file, mimeType);


                Xlog.d(Constants.DL_DRM, "Open DRM file:" + localUri + " MimeType is" + mimeType);
                intent.setDataAndType(localUri, mimeType);
            } else {
            /// @}
            if ("application/vnd.android.package-archive".equals(mimeType)) {
                // PackageInstaller doesn't like content URIs, so open file
                intent.setDataAndType(localUri, mimeType);


                // Also splice in details about where it came from
                final Uri remoteUri = getCursorUri(cursor, COLUMN_URI);
                intent.putExtra(Intent.EXTRA_ORIGINATING_URI, remoteUri);
                intent.putExtra(Intent.EXTRA_REFERRER, getRefererUri(context, id));
                intent.putExtra(Intent.EXTRA_ORIGINATING_UID, getOriginatingUid(context, id));
            } else if ("file".equals(localUri.getScheme())) {
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
                        | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                intent.setDataAndType(
                        ContentUris.withAppendedId(ALL_DOWNLOADS_CONTENT_URI, id), mimeType);
            } else {
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.setDataAndType(localUri, mimeType);
            }
            /// M: Add to support MTK DRM @{
            }
            /// @}
            return intent;
        } finally {
            cursor.close();
        }
    }



你可能感兴趣的:(Android系统研发)