55import  android .net .Uri ;
66import  android .util .Base64 ;
77
8+ import  com .google .gson .JsonArray ;
89import  com .google .gson .JsonObject ;
910
1011import  java .io .File ;
1112import  java .io .IOException ;
13+ import  java .util .ArrayList ;
1214
1315import  io .trigger .forge .android .core .ForgeActivity .EventAccessBlock ;
1416import  io .trigger .forge .android .core .ForgeApp ;
1517import  io .trigger .forge .android .core .ForgeFile ;
1618import  io .trigger .forge .android .core .ForgeIntentResultHandler ;
19+ import  io .trigger .forge .android .core .ForgeLog ;
1720import  io .trigger .forge .android .core .ForgeParam ;
1821import  io .trigger .forge .android .core .ForgeStorage ;
1922import  io .trigger .forge .android .core .ForgeTask ;
2023
2124import  static  android .app .Activity .RESULT_CANCELED ;
2225import  static  android .app .Activity .RESULT_OK ;
26+ import  static  io .trigger .forge .android .modules .file .Storage .writeImageUriToTemporaryFile ;
27+ import  static  io .trigger .forge .android .modules .file .Storage .writeVideoUriToTemporaryFile ;
2328
2429public  class  API  {
2530
2631    //region media picker 
2732
28-     public  static  void  getImage (final  ForgeTask  task ) { // deprecated in favour of pickMedia 
33+     public  static  void  getImages (final  ForgeTask  task ) { // deprecated in favour of pickMedia 
2934        API .pickMedia (task , "image/*" );
3035    }
3136
32-     public  static  void  getVideo (final  ForgeTask  task ) { // deprecated in favour of pickMedia 
37+     public  static  void  getVideos (final  ForgeTask  task ) { // deprecated in favour of pickMedia 
3338        API .pickMedia (task , "video/*" );
3439    }
3540
3641    public  static  void  pickMedia (final  ForgeTask  task , final  String  type ) {
3742        // parse options 
43+         final  int  selectionLimit  = task .params .has ("selectionLimit" ) ? task .params .get ("selectionLimit" ).getAsInt () : 1 ;
3844        final  int  maxWidth  = task .params .has ("width" ) ? task .params .get ("width" ).getAsInt () : 0 ;
3945        final  int  maxHeight  = task .params .has ("height" ) ? task .params .get ("height" ).getAsInt () : 0 ;
4046        final  String  videoQuality  = task .params .has ("videoQuality" ) ? task .params .get ("videoQuality" ).getAsString () : "default" ;
@@ -43,38 +49,55 @@ public static void pickMedia(final ForgeTask task, final String type) {
4349            @ Override 
4450            public  void  result (int  requestCode , int  resultCode , Intent  data ) {
4551                if  (resultCode  == RESULT_OK ) {
46- 
47-                     /* TODO if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { 
48-                         int itemCount = data.getClipData().getItemCount(); 
49-                         for (int index = 0; index < itemCount; index++) { 
50-                             Uri uri = data.getClipData().getItemAt(index).getUri(); 
51-                             ForgeLog.d(uri.toString()); 
52-                         } 
53-                     }*/ 
54- 
55-                     Uri  source  = data .getData ();
56-                     try  {
52+                     Storage .IOFunction <Uri , ForgeFile > write  = (Uri  source ) -> {
5753                        ForgeFile  forgeFile  = null ;
5854                        if  (type .startsWith ("image/" ) && (maxWidth  > 0  || maxHeight  > 0 )) {
59-                             forgeFile  = Storage .writeImageUriToTemporaryFile (source , maxWidth , maxHeight );
60- 
55+                             forgeFile  = writeImageUriToTemporaryFile (source , maxWidth , maxHeight );
6156                        } else  if  (type .startsWith ("video/" ) && !videoQuality .equalsIgnoreCase ("default" )) {
62-                             forgeFile  = Storage .writeVideoUriToTemporaryFile (source , videoQuality );
63- 
57+                             forgeFile  = writeVideoUriToTemporaryFile (source , videoQuality );
6458                        } else  {
6559                            forgeFile  = Storage .writeMediaUriToTemporaryFile (source );
6660                        }
61+                         return  forgeFile ;
62+                     };
63+ 
64+                     try  {
65+                         ArrayList <ForgeFile > forgeFiles  = new  ArrayList <>();
66+                         if  (android .os .Build .VERSION .SDK_INT  >= android .os .Build .VERSION_CODES .JELLY_BEAN ) {
67+                             int  itemCount  = data .getClipData ().getItemCount ();
68+                             for  (int  index  = 0 ; index  < itemCount ; index ++) {
69+                                 Uri  uri  = data .getClipData ().getItemAt (index ).getUri ();
70+                                 ForgeLog .d (uri .toString ());
71+                                 ForgeFile  forgeFile  = write .apply (uri );
72+                                 forgeFiles .add (forgeFile );
73+                             }
74+                         } else  {
75+                             Uri  uri  = data .getData ();
76+                             ForgeFile  forgeFile  = write .apply (uri );
77+                             forgeFiles .add (forgeFile );
78+                         }
6779
68-                         task .success (forgeFile .toScriptObject ());
80+                         if  (forgeFiles .size () == 0 ) {
81+                             task .error ("No valid items selected" , "EXPECTED_FAILURE" , null );
82+                         } else  if  (selectionLimit  == 1 ) {
83+                             task .success (forgeFiles .get (0 ).toScriptObject ());
84+                         } else  {
85+                             JsonArray  ret  = new  JsonArray ();
86+                             for  (ForgeFile  file : forgeFiles ) {
87+                                 ret .add (file .toScriptObject ());
88+                             }
89+                             task .success (ret );
90+                         }
6991
7092                    } catch  (IOException  e ) {
7193                        e .printStackTrace ();
72-                         task .error ("Error saving image  to app storage: "  + e .getLocalizedMessage (), "EXPECTED_FAILURE" , null );
94+                         task .error ("Error saving selection  to app storage: "  + e .getLocalizedMessage (), "EXPECTED_FAILURE" , null );
7395                    }
96+ 
7497                } else  if  (resultCode  == RESULT_CANCELED ) {
75-                     task .error ("User cancelled image capture " , "EXPECTED_FAILURE" , null );
98+                     task .error ("User cancelled selection " , "EXPECTED_FAILURE" , null );
7699                } else  {
77-                     task .error ("Unknown error capturing image " , "UNEXPECTED_FAILURE" , null );
100+                     task .error ("Unknown error during selection " , "UNEXPECTED_FAILURE" , null );
78101                }
79102            }
80103        };
@@ -88,7 +111,9 @@ public void run(boolean granted) {
88111                    public  void  run () {
89112                        Intent  intent  = new  Intent (Intent .ACTION_PICK );
90113                        intent .setType (type );
91-                         // TODO intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); 
114+                         if  (selectionLimit  != 1 ) {
115+                             intent .putExtra (Intent .EXTRA_ALLOW_MULTIPLE , true );
116+                         }
92117                        ForgeApp .intentWithHandler (intent , resultHandler );
93118                    }
94119                });
0 commit comments