1 package com.android.documentsui; 2 3 import static com.android.documentsui.base.DocumentInfo.getCursorString; 4 5 import android.database.Cursor; 6 import android.net.Uri; 7 import android.provider.DocumentsContract; 8 import android.util.Log; 9 10 import com.android.documentsui.base.DocumentInfo; 11 import com.android.documentsui.roots.RootCursorWrapper; 12 13 import java.util.ArrayList; 14 import java.util.List; 15 16 public class ModelId { 17 private final static String TAG = "ModelId"; 18 build(Uri uri)19 public static final String build(Uri uri) { 20 String documentId; 21 try { 22 documentId = DocumentsContract.getDocumentId(uri); 23 } catch (IllegalArgumentException e) { 24 Log.e(TAG, "Failed to get document id.", e); 25 return null; 26 } 27 String authority; 28 authority = uri.getAuthority(); 29 return ModelId.build(authority, documentId); 30 } 31 build(DocumentInfo docInfo)32 public static final String build(DocumentInfo docInfo) { 33 if (docInfo == null) { 34 return null; 35 } 36 return ModelId.build(docInfo.authority, docInfo.documentId); 37 } 38 build(Cursor cursor)39 public static final String build(Cursor cursor) { 40 if (cursor == null) { 41 return null; 42 } 43 return ModelId.build(getCursorString(cursor, RootCursorWrapper.COLUMN_AUTHORITY), 44 getCursorString(cursor, DocumentsContract.Document.COLUMN_DOCUMENT_ID)); 45 } 46 build(ArrayList<Uri> uris)47 public static final ArrayList<String> build(ArrayList<Uri> uris) { 48 if (uris == null || uris.isEmpty()) { 49 return null; 50 } 51 ArrayList<String> ids = new ArrayList<>(); 52 String id; 53 for (Uri uri : uris) { 54 id = ModelId.build(uri); 55 if (id != null) { 56 ids.add(id); 57 } 58 } 59 return ids; 60 } 61 build(String authority, String docId)62 public static final String build(String authority, String docId) { 63 if (authority == null || authority.isEmpty() || docId == null || docId.isEmpty()) { 64 return null; 65 } 66 return authority + "|" + docId; 67 } 68 } 69