1414import java .io .InputStream ;
1515import java .io .OutputStream ;
1616import java .nio .charset .StandardCharsets ;
17+ import android .provider .DocumentsContract ;
18+
1719
1820public class documentFile extends CordovaPlugin {
1921
@@ -41,6 +43,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
4143 case "canWrite" : return handleCanWrite (args , callbackContext );
4244 case "childByNameExists" : return handleChildByNameExists (args , callbackContext );
4345 case "getChildByName" : return handleGetChildByName (args , callbackContext );
46+ case "isMyChild" : return handleIsMyChild (args , callbackContext );
4447 case "toUri" : return handleToUri (args , callbackContext );
4548 default : return false ;
4649 }
@@ -50,6 +53,55 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
5053 }
5154 }
5255
56+ private boolean handleIsMyChild (JSONArray args , CallbackContext cb ) throws JSONException {
57+ String parentUriStr = args .getString (0 );
58+ String childUriStr = args .getString (1 );
59+
60+ try {
61+ Uri parentUri = Uri .parse (parentUriStr );
62+ Uri childUri = Uri .parse (childUriStr );
63+
64+ boolean result = false ;
65+
66+ // SAF-safe check
67+ if (DocumentsContract .isDocumentUri (getContext (), childUri )) {
68+ try {
69+ result = DocumentsContract .isChildDocument (
70+ getContext ().getContentResolver (),
71+ parentUri ,
72+ childUri
73+ );
74+ } catch (Exception e ) {
75+ // If that fails, fallback to ID-based check
76+ result = isChildByDocIdFallback (parentUri , childUri );
77+ }
78+ } else {
79+ // Non-SAF fallback: compare normalized paths
80+ String p = parentUri .getPath ();
81+ String c = childUri .getPath ();
82+ if (p != null && c != null && c .startsWith (p )) {
83+ result = true ;
84+ }
85+ }
86+
87+ cb .success (result ? 1 : 0 );
88+ } catch (Exception e ) {
89+ cb .error ("Error: " + e .getMessage ());
90+ }
91+ return true ;
92+ }
93+
94+ /**
95+ * Fallback if DocumentsContract.isChildDocument() fails.
96+ * Checks if the child’s document ID starts with parent’s document ID.
97+ */
98+ private boolean isChildByDocIdFallback (Uri parentUri , Uri childUri ) {
99+ String parentId = DocumentsContract .getDocumentId (parentUri );
100+ String childId = DocumentsContract .getDocumentId (childUri );
101+ return childId != null && parentId != null && childId .startsWith (parentId + "%2F" );
102+ }
103+
104+
53105 private DocumentFile fromUri (String uriStr ) {
54106 try {
55107 // Decode once if double-encoded
0 commit comments