android5.0下载管理器研究(二)

DownloadProvider:

    下载管理器的数据库主文件,包括了创建数据库、插入数据、更新数据等各种方法。

    1.常量和全局变量:

    /** Database filename */
    private static final String DB_NAME = "downloads.db";
    /** Current database version */
    private static final int DB_VERSION = 109;
    /** Name of table in the database */
    private static final String DB_TABLE = "downloads";

    /** MIME type for the entire download list */
    private static final String DOWNLOAD_LIST_TYPE = "vnd.android.cursor.dir/download";
    /** MIME type for an individual download */
    private static final String DOWNLOAD_TYPE = "vnd.android.cursor.item/download";

    /** URI matcher used to recognize URIs sent by applications */
    private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    /** URI matcher constant for the URI of all downloads belonging to the calling UID */
    private static final int MY_DOWNLOADS = 1;
    /** URI matcher constant for the URI of an individual download belonging to the calling UID */
    private static final int MY_DOWNLOADS_ID = 2;
    /** URI matcher constant for the URI of all downloads in the system */
    private static final int ALL_DOWNLOADS = 3;
    /** URI matcher constant for the URI of an individual download */
    private static final int ALL_DOWNLOADS_ID = 4;
    /** URI matcher constant for the URI of a download's request headers */
    private static final int REQUEST_HEADERS_URI = 5;
    /** URI matcher constant for the public URI returned by
     * {@link DownloadManager#getUriForDownloadedFile(long)} if the given downloaded file
     * is publicly accessible.
     */
    private static final int PUBLIC_DOWNLOAD_ID = 6;


    DB_NAME:生成的db文件名称,可以用SQLite软件查看其中的表。
    DB_VERSION:数据库版本,数据库升版时用到,可以将低版本数据库的数据无损的导入到新版中。
    DB_TABLE:数据库中的表名。
    DOWNLOAD_LIST_TYPE、DOWNLOAD_TYPE:用于区分是查一个下载,还是所有下载。
    sURIMatcher以及后续的几个DOWNLOADS常量:插入下载管理器数据库时,使用的url一般都是类似content://downloads/my_downloads这种形式,使用matcher可以方便的分别出来这几种下载,例如,我要查询下载管理器所有的下载记录,那么URL就可以写成cursor.query("content://downloads/all_downloads"·····),这样就会把下载的记录全部返回到cursor中,遍历cursor即可拿出数据。如果要查询单个应用的记录,可以写成cursor.query("content://downloads/my_downloads/27"),这样返回的cursor大小就只有1,moveToFirst之后直接用即可,无需遍历。源码如下:
static {
        sURIMatcher.addURI("downloads", "my_downloads", MY_DOWNLOADS);
        sURIMatcher.addURI("downloads", "my_downloads/#", MY_DOWNLOADS_ID);
        sURIMatcher.addURI("downloads", "all_downloads", ALL_DOWNLOADS);
        sURIMatcher.addURI("downloads", "all_downloads/#", ALL_DOWNLOADS_ID);
        sURIMatcher.addURI("downloads",
                "my_downloads/#/" + Downloads.Impl.RequestHeaders.URI_SEGMENT,
                REQUEST_HEADERS_URI);
        sURIMatcher.addURI("downloads",
                "all_downloads/#/" + Downloads.Impl.RequestHeaders.URI_SEGMENT,
                REQUEST_HEADERS_URI);
        // temporary, for backwards compatibility
        sURIMatcher.addURI("downloads", "download", MY_DOWNLOADS);
        sURIMatcher.addURI("downloads", "download/#", MY_DOWNLOADS_ID);
        sURIMatcher.addURI("downloads",
                "download/#/" + Downloads.Impl.RequestHeaders.URI_SEGMENT,
                REQUEST_HEADERS_URI);
        sURIMatcher.addURI("downloads",
                Downloads.Impl.PUBLICLY_ACCESSIBLE_DOWNLOADS_URI_SEGMENT + "/#",
                PUBLIC_DOWNLOAD_ID);
    }

    Downloads的impl内部类中的url名称(插入下载管理器数据库时用到的地址):

 /**
         * The content:// URI to access downloads owned by the caller's UID.
         */
        public static final Uri CONTENT_URI =
                Uri.parse("content://downloads/my_downloads");

        /**
         * The content URI for accessing all downloads across all UIDs (requires the
         * ACCESS_ALL_DOWNLOADS permission).
         */
        public static final Uri ALL_DOWNLOADS_CONTENT_URI =
                Uri.parse("content://downloads/all_downloads");

        /** URI segment to access a publicly accessible downloaded file */
        public static final String PUBLICLY_ACCESSIBLE_DOWNLOADS_URI_SEGMENT = "public_downloads";

        /**
         * The content URI for accessing publicly accessible downloads (i.e., it requires no
         * permissions to access this downloaded file)
         */
        public static final Uri PUBLICLY_ACCESSIBLE_DOWNLOADS_URI =
                Uri.parse("content://downloads/" + PUBLICLY_ACCESSIBLE_DOWNLOADS_URI_SEGMENT)
    sColumnsMap:这个哈希表中只有两个键值对,一个是单个下载项用来显示的默认名,一个是单个下载项的总大小。
    mSystemUid、mDefContainerUid:一个是Process.SYSTEM_UID,一个是com.android.defcontainer进程的uid,在下载管理器的权限控制里会用到。
    mOpenHelper:DatabaseHelper的一个全局实例,DatabaseHelper是用来创建、升版、降版数据库的一个SQLiteOpenHelper子类。


 2.具体方法分析:

to be continue.







你可能感兴趣的:(android5.0下载管理器研究(二))