77import android .os .Build ;
88import android .os .Parcel ;
99import android .os .Parcelable ;
10+
1011import androidx .annotation .NonNull ;
12+ import androidx .annotation .Nullable ;
1113
1214import com .cloudinary .android .UploadRequest ;
1315import com .cloudinary .android .uploadwidget .model .CropPoints ;
1416import com .cloudinary .android .uploadwidget .ui .UploadWidgetActivity ;
1517
1618import java .util .ArrayList ;
19+ import java .util .Collection ;
1720
1821/**
1922 * Helper class to start the UploadWidget and preprocess its results.
@@ -30,16 +33,47 @@ public class UploadWidget {
3033 */
3134 public static final String URIS_EXTRA = "uris_extra" ;
3235
36+ public static final String ACTION_EXTRA = "required_action_extra" ;
37+
3338 /**
34- * Start the {@link UploadWidgetActivity}. Please make sure that you have declared it your manifest.
39+ * Start the {@link UploadWidgetActivity} with a pre-populated list of files to upload, and return
40+ * a list of upload request to dispatch. This is equivalent to RequiredAction.NONE.
41+ * Deprecated - please use {@link #startActivity(Activity, int, Options)} directly.
3542 *
3643 * @param activity The activity which requested the upload widget.
3744 * @param requestCode A request code to start the upload widget with.
38- * @param uris Uris of the selected media files.
45+ * @param uris Uris of the selected media files.
3946 */
47+ @ Deprecated
4048 public static void startActivity (@ NonNull Activity activity , int requestCode , @ NonNull ArrayList <Uri > uris ) {
41- Intent intent = new Intent (activity , UploadWidgetActivity .class );
42- intent .putParcelableArrayListExtra (URIS_EXTRA , uris );
49+ startActivity (activity , requestCode , new Options (Action .NONE , uris ));
50+ }
51+
52+ /**
53+ * Start the {@link UploadWidgetActivity} configured for full process - Launch file selection UI
54+ * as well as dispatching the created upload request automatically.
55+ *
56+ * @param activity The activity which requested the upload widget.
57+ * @param requestCode A request code to start the upload widget with.
58+ */
59+ public static void startActivity (@ NonNull Activity activity , int requestCode ) {
60+ startActivity (activity , requestCode , new Options (Action .DISPATCH , null ));
61+ }
62+
63+ /**
64+ * Start the {@link UploadWidgetActivity} configured according to the supplied launch options.
65+ *
66+ * @param activity The activity which requested the upload widget.
67+ * @param requestCode A request code to start the upload widget with.
68+ * @param options The launch option to define the required upload widget behaviour
69+ */
70+ public static void startActivity (@ NonNull Activity activity , int requestCode , Options options ) {
71+ Intent intent = new Intent (activity , UploadWidgetActivity .class ).putExtra (ACTION_EXTRA , options .action );
72+
73+ if (options .uris != null && !options .uris .isEmpty ()) {
74+ intent .putParcelableArrayListExtra (URIS_EXTRA , new ArrayList <Parcelable >(options .uris ));
75+ }
76+
4377 activity .startActivityForResult (intent , requestCode );
4478 }
4579
@@ -76,9 +110,9 @@ public static UploadRequest preprocessResult(Context context, Result result) {
76110 * Preprocess the {@code uploadRequest}'s with the upload widget results.
77111 *
78112 * @param uploadRequest Already constructed upload request.
79- * @param result Result data from the upload widget.
113+ * @param result Result data from the upload widget.
80114 * @return Preprocessed {@link UploadRequest}
81- * @throws IllegalStateException if {@code uploadRequest} was already dispatched.
115+ * @throws IllegalStateException if {@code uploadRequest} was already dispatched.
82116 */
83117 public static UploadRequest preprocessResult (Context context , @ NonNull UploadRequest uploadRequest , Result result ) {
84118 return UploadWidgetResultProcessor .process (context , uploadRequest , result );
@@ -93,7 +127,9 @@ public static UploadRequest preprocessResult(Context context, @NonNull UploadReq
93127 public static void openMediaChooser (Activity activity , int requestCode ) {
94128 Intent intent = new Intent (Intent .ACTION_GET_CONTENT );
95129 intent .addCategory (Intent .CATEGORY_OPENABLE );
96- intent .putExtra (Intent .EXTRA_ALLOW_MULTIPLE , true );
130+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR2 ) {
131+ intent .putExtra (Intent .EXTRA_ALLOW_MULTIPLE , true );
132+ }
97133
98134 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
99135 intent .putExtra (Intent .EXTRA_MIME_TYPES , new String []{"image/jpeg" , "image/jpg" , "image/png" , "video/*" });
@@ -133,6 +169,13 @@ public static final class Result implements Parcelable {
133169 */
134170 public int rotationAngle ;
135171
172+ /**
173+ * The request id, in case the full flow was requested and an upload request was already
174+ * started or dispatched.
175+ */
176+ public String requestId ;
177+
178+
136179 public Result (Uri uri ) {
137180 this .uri = uri ;
138181 }
@@ -142,6 +185,7 @@ public void writeToParcel(Parcel dest, int flags) {
142185 dest .writeParcelable (uri , flags );
143186 dest .writeParcelable (cropPoints , flags );
144187 dest .writeInt (rotationAngle );
188+ dest .writeString (requestId );
145189 }
146190
147191 public static final Creator <Result > CREATOR = new Creator <Result >() {
@@ -160,6 +204,7 @@ protected Result(Parcel in) {
160204 uri = in .readParcelable (Uri .class .getClassLoader ());
161205 cropPoints = in .readParcelable (CropPoints .class .getClassLoader ());
162206 rotationAngle = in .readInt ();
207+ requestId = in .readString ();
163208 }
164209
165210 @ Override
@@ -168,4 +213,47 @@ public int describeContents() {
168213 }
169214 }
170215
216+ /**
217+ * This class is used to define the required launch behaviour of the upload widget.
218+ */
219+ public static class Options {
220+ final Action action ;
221+ final Collection <Uri > uris ;
222+
223+ /**
224+ * Construct a new instance to use when launching the upload widget activity.
225+ *
226+ * @param action Indicates the widget how to handle the selected files. This also
227+ * affects the result received later in onActivityResult. When the action
228+ * used is DISPATCH or START_NOW the widget returns a list of request IDs.
229+ * When the action is NONE, the widget returns results that needs to be
230+ * processed into UploadRequest, allowing customization before dispatching/starting.
231+ * @param uris A list of Uris of files to display and upload.
232+ */
233+ public Options (@ NonNull Action action , @ Nullable Collection <Uri > uris ) {
234+ this .action = action ;
235+ this .uris = uris ;
236+ }
237+ }
238+
239+ /**
240+ * Define how the upload widget handles the selected files to upload
241+ */
242+ public enum Action {
243+ /**
244+ * Dispatch the selected files within the upload widget, and return request IDs.
245+ */
246+ DISPATCH ,
247+
248+ /**
249+ * Immediately start the selected files within the upload widget, and return request IDs.
250+ */
251+ START_NOW ,
252+
253+ /**
254+ * Create the request data and preprocess configuration without starting any request.
255+ */
256+ NONE ,
257+ }
258+
171259}
0 commit comments