MtpDatabaseConstants.java revision dc47344660035b27a564ab6d9c9a9b58ec340347
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.mtp;
18
19import android.provider.DocumentsContract.Document;
20import android.provider.DocumentsContract.Root;
21
22/**
23 * Class containing MtpDatabase constants.
24 */
25class MtpDatabaseConstants {
26    static final int DATABASE_VERSION = 1;
27    static final String DATABASE_NAME = null;
28
29    /**
30     * Table representing documents including root documents.
31     */
32    static final String TABLE_DOCUMENTS = "Documents";
33
34    /**
35     * Table containing additional information only available for root documents.
36     * The table uses same primary keys with corresponding documents.
37     */
38    static final String TABLE_ROOT_EXTRA = "RootExtra";
39
40    /**
41     * View to join Documents and RootExtra tables to provide roots information.
42     */
43    static final String VIEW_ROOTS = "Roots";
44
45    static final String COLUMN_DEVICE_ID = "device_id";
46    static final String COLUMN_STORAGE_ID = "storage_id";
47    static final String COLUMN_OBJECT_HANDLE = "object_handle";
48    static final String COLUMN_PARENT_DOCUMENT_ID = "parent_document_id";
49    static final String COLUMN_ROW_STATE = "row_state";
50
51    /**
52     * The state represents that the row has a valid object handle.
53     */
54    static final int ROW_STATE_VALID = 0;
55
56    /**
57     * The state represents that the rows added at the previous cycle and need to be updated with
58     * fresh values.
59     * The row may not have valid object handle. External application can still fetch the documents.
60     * If the external application tries to fetch object handle, the provider resolves pending
61     * documents with invalidated documents ahead.
62     */
63    static final int ROW_STATE_INVALIDATED = 1;
64
65    /**
66     * The state represents the raw has a valid object handle but it may be going to be mapped with
67     * another rows invalidated. After fetching all documents under the parent, the database tries
68     * to map the pending documents and the invalidated documents in order to keep old document ID
69     * alive.
70     */
71    static final int ROW_STATE_PENDING = 2;
72
73    /**
74     * Mapping mode that uses MTP identifier to find corresponding rows.
75     */
76    static final int MAP_BY_MTP_IDENTIFIER = 0;
77
78    /**
79     * Mapping mode that uses name to find corresponding rows.
80     */
81    static final int MAP_BY_NAME = 1;
82
83    static final String SELECTION_DOCUMENT_ID = Document.COLUMN_DOCUMENT_ID + " = ?";
84    static final String SELECTION_ROOT_ID = Root.COLUMN_ROOT_ID + " = ?";
85    static final String SELECTION_ROOT_DOCUMENTS =
86            COLUMN_DEVICE_ID + " = ? AND " + COLUMN_PARENT_DOCUMENT_ID + " IS NULL";
87    static final String SELECTION_CHILD_DOCUMENTS = COLUMN_PARENT_DOCUMENT_ID + " = ?";
88
89    static final String QUERY_CREATE_DOCUMENTS =
90            "CREATE TABLE " + TABLE_DOCUMENTS + " (" +
91            Document.COLUMN_DOCUMENT_ID +
92                " INTEGER PRIMARY KEY AUTOINCREMENT," +
93            COLUMN_DEVICE_ID + " INTEGER NOT NULL," +
94            COLUMN_STORAGE_ID + " INTEGER," +
95            COLUMN_OBJECT_HANDLE + " INTEGER," +
96            COLUMN_PARENT_DOCUMENT_ID + " INTEGER," +
97            COLUMN_ROW_STATE + " INTEGER NOT NULL," +
98            Document.COLUMN_MIME_TYPE + " TEXT," +
99            Document.COLUMN_DISPLAY_NAME + " TEXT NOT NULL," +
100            Document.COLUMN_SUMMARY + " TEXT," +
101            Document.COLUMN_LAST_MODIFIED + " INTEGER," +
102            Document.COLUMN_ICON + " INTEGER," +
103            Document.COLUMN_FLAGS + " INTEGER NOT NULL," +
104            Document.COLUMN_SIZE + " INTEGER NOT NULL);";
105
106    static final String QUERY_CREATE_ROOT_EXTRA =
107            "CREATE TABLE " + TABLE_ROOT_EXTRA + " (" +
108            Root.COLUMN_ROOT_ID + " INTEGER PRIMARY KEY," +
109            Root.COLUMN_FLAGS + " INTEGER NOT NULL," +
110            Root.COLUMN_AVAILABLE_BYTES + " INTEGER NOT NULL," +
111            Root.COLUMN_CAPACITY_BYTES + " INTEGER NOT NULL," +
112            Root.COLUMN_MIME_TYPES + " TEXT NOT NULL);";
113
114    /**
115     * Creates a view to join Documents table and RootExtra table on their primary keys to
116     * provide DocumentContract.Root equivalent information.
117     */
118    static final String QUERY_CREATE_VIEW_ROOTS =
119            "CREATE VIEW " + VIEW_ROOTS + " AS SELECT " +
120                    TABLE_DOCUMENTS + "." + Document.COLUMN_DOCUMENT_ID + " AS " +
121                            Root.COLUMN_ROOT_ID + "," +
122                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_FLAGS + "," +
123                    TABLE_DOCUMENTS + "." + Document.COLUMN_ICON + " AS " +
124                            Root.COLUMN_ICON + "," +
125                    TABLE_DOCUMENTS + "." + Document.COLUMN_DISPLAY_NAME + " AS " +
126                            Root.COLUMN_TITLE + "," +
127                    TABLE_DOCUMENTS + "." + Document.COLUMN_SUMMARY + " AS " +
128                            Root.COLUMN_SUMMARY + "," +
129                    // Temporary replace COLUMN_DOCUMENT_ID with old format.
130                    TABLE_DOCUMENTS + "." + Document.COLUMN_DOCUMENT_ID + " AS " +
131                    Root.COLUMN_DOCUMENT_ID + "_," +
132                    TABLE_DOCUMENTS + "." + COLUMN_DEVICE_ID + "|| '_' ||" +
133                    TABLE_DOCUMENTS + "." + COLUMN_STORAGE_ID + "||'_0' AS " +
134                    Root.COLUMN_DOCUMENT_ID + "," +
135                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_AVAILABLE_BYTES + "," +
136                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_CAPACITY_BYTES + "," +
137                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_MIME_TYPES + "," +
138                    TABLE_DOCUMENTS + "." + COLUMN_ROW_STATE +
139            " FROM " + TABLE_DOCUMENTS + " INNER JOIN " + TABLE_ROOT_EXTRA +
140            " ON " +
141                    COLUMN_PARENT_DOCUMENT_ID + " IS NULL AND " +
142                    TABLE_DOCUMENTS + "." + Document.COLUMN_DOCUMENT_ID +
143                    "=" +
144                    TABLE_ROOT_EXTRA + "." + Root.COLUMN_ROOT_ID;
145}
146