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

<activity
            android:name=".TrampolineActivity"
            android:theme="@android:style/Theme.NoDisplay"
            android:permission="android.permission.MANAGE_DOCUMENTS">
            <intent-filter>
                <action android:name="android.provider.action.MANAGE_DOCUMENT" />
                <category android:name="android.intent.category.DEFAULT" />
                <data
                    android:scheme="content"
                    android:host="com.android.providers.downloads.documents"
                    android:mimeType="*/*" />
            </intent-filter>
        </activity>


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();
        }
    }



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值