3
3
import android .Manifest ;
4
4
import android .annotation .SuppressLint ;
5
5
import android .app .ProgressDialog ;
6
+ import android .content .ClipData ;
6
7
import android .content .Intent ;
7
8
import android .content .pm .PackageManager ;
8
9
import android .os .Build ;
9
10
import android .os .Environment ;
10
11
12
+ import androidx .activity .result .ActivityResult ;
13
+ import androidx .activity .result .ActivityResultCallback ;
14
+ import androidx .activity .result .ActivityResultLauncher ;
15
+ import androidx .activity .result .contract .ActivityResultContracts ;
11
16
import androidx .annotation .NonNull ;
12
17
import androidx .appcompat .app .AlertDialog ;
13
18
import androidx .appcompat .view .ContextThemeWrapper ;
16
21
import androidx .appcompat .app .AppCompatActivity ;
17
22
18
23
import android .os .Bundle ;
24
+ import android .provider .MediaStore ;
19
25
import android .view .LayoutInflater ;
20
26
import android .view .View ;
21
27
import android .widget .Button ;
26
32
import com .hbisoft .pickit .PickiT ;
27
33
import com .hbisoft .pickit .PickiTCallbacks ;
28
34
35
+ import java .util .ArrayList ;
36
+ import java .util .Objects ;
37
+
29
38
public class MainActivity extends AppCompatActivity implements PickiTCallbacks {
30
39
//Permissions
31
- private static final int SELECT_VIDEO_REQUEST = 777 ;
32
40
private static final int PERMISSION_REQ_ID_RECORD_AUDIO = 22 ;
33
41
private static final int PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE = PERMISSION_REQ_ID_RECORD_AUDIO + 1 ;
34
42
@@ -66,17 +74,14 @@ private void init() {
66
74
}
67
75
68
76
private void buttonClickEvent () {
69
- button_pick .setOnClickListener (new View .OnClickListener () {
70
- @ Override
71
- public void onClick (View view ) {
72
- openGallery ();
73
-
74
- // Make TextView's invisible
75
- originalTitle .setVisibility (View .INVISIBLE );
76
- originalTv .setVisibility (View .INVISIBLE );
77
- pickitTitle .setVisibility (View .INVISIBLE );
78
- pickitTv .setVisibility (View .INVISIBLE );
79
- }
77
+ button_pick .setOnClickListener (view -> {
78
+ openGallery ();
79
+
80
+ // Make TextView's invisible
81
+ originalTitle .setVisibility (View .INVISIBLE );
82
+ originalTv .setVisibility (View .INVISIBLE );
83
+ pickitTitle .setVisibility (View .INVISIBLE );
84
+ pickitTv .setVisibility (View .INVISIBLE );
80
85
});
81
86
}
82
87
@@ -85,16 +90,19 @@ private void openGallery() {
85
90
if (checkSelfPermission ()) {
86
91
Intent intent ;
87
92
if (Environment .getExternalStorageState ().equals (Environment .MEDIA_MOUNTED )) {
88
- intent = new Intent (Intent .ACTION_PICK , android . provider . MediaStore .Video .Media .EXTERNAL_CONTENT_URI );
93
+ intent = new Intent (Intent .ACTION_PICK , MediaStore .Video .Media .EXTERNAL_CONTENT_URI );
89
94
} else {
90
- intent = new Intent (Intent .ACTION_PICK , android . provider . MediaStore .Video .Media .INTERNAL_CONTENT_URI );
95
+ intent = new Intent (Intent .ACTION_PICK , MediaStore .Video .Media .INTERNAL_CONTENT_URI );
91
96
}
92
97
// In this example we will set the type to video
93
98
intent .setType ("video/*" );
94
99
intent .setAction (Intent .ACTION_GET_CONTENT );
95
100
intent .putExtra ("return-data" , true );
101
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .JELLY_BEAN_MR2 ) {
102
+ intent .putExtra (Intent .EXTRA_ALLOW_MULTIPLE , true );
103
+ }
96
104
intent .addFlags (Intent .FLAG_GRANT_READ_URI_PERMISSION );
97
- startActivityForResult (intent , SELECT_VIDEO_REQUEST );
105
+ activityResultLauncher . launch (intent );
98
106
}
99
107
}
100
108
@@ -110,6 +118,7 @@ private boolean checkSelfPermission() {
110
118
// Handle permissions
111
119
@ Override
112
120
public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
121
+ super .onRequestPermissionsResult (requestCode , permissions , grantResults );
113
122
if (requestCode == PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE ) {
114
123
if (grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
115
124
// Permissions was granted, open the gallery
@@ -122,28 +131,45 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
122
131
}
123
132
}
124
133
125
- @ Override
126
- protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
127
- super .onActivityResult (requestCode , resultCode , data );
128
- if (requestCode == SELECT_VIDEO_REQUEST ) {
129
- if (resultCode == RESULT_OK ) {
130
-
131
- // Get path from PickiT (The path will be returned in PickiTonCompleteListener)
132
- //
133
- // If the selected file is from Dropbox/Google Drive or OnDrive:
134
- // Then it will be "copied" to your app directory (see path example below) and when done the path will be returned in PickiTonCompleteListener
135
- // /storage/emulated/0/Android/data/your.package.name/files/Temp/tempDriveFile.mp4
136
- //
137
- // else the path will directly be returned in PickiTonCompleteListener
138
- pickiT .getPath (data .getData (), Build .VERSION .SDK_INT );
139
-
140
- originalTv .setText (String .valueOf (data .getData ()));
134
+ ActivityResultLauncher <Intent > activityResultLauncher = registerForActivityResult (
135
+ new ActivityResultContracts .StartActivityForResult (),
136
+ new ActivityResultCallback <ActivityResult >() {
137
+ @ Override
138
+ public void onActivityResult (ActivityResult result ) {
139
+ if (result .getResultCode () == RESULT_OK ) {
140
+ Intent data = result .getData ();
141
+ // Get path from PickiT (The path will be returned in PickiTonCompleteListener)
142
+ //
143
+ // If the selected file is from Dropbox/Google Drive or OnDrive:
144
+ // Then it will be "copied" to your app directory (see path example below) and when done the path will be returned in PickiTonCompleteListener
145
+ // /storage/emulated/0/Android/data/your.package.name/files/Temp/tempDriveFile.mp4
146
+ //
147
+ // else the path will directly be returned in PickiTonCompleteListener
148
+
149
+ ClipData clipData = Objects .requireNonNull (data ).getClipData ();
150
+ if (clipData != null ) {
151
+ int numberOfFilesSelected = clipData .getItemCount ();
152
+ if (numberOfFilesSelected > 1 ) {
153
+ pickiT .getMultiplePaths (clipData );
154
+ StringBuilder allPaths = new StringBuilder ("Multiple Files Selected:" + "\n " );
155
+ for (int i = 0 ; i < clipData .getItemCount (); i ++) {
156
+ allPaths .append ("\n \n " ).append (clipData .getItemAt (i ).getUri ());
157
+ }
158
+ originalTv .setText (allPaths .toString ());
159
+ }else {
160
+ pickiT .getPath (clipData .getItemAt (0 ).getUri (), Build .VERSION .SDK_INT );
161
+ originalTv .setText (String .valueOf (clipData .getItemAt (0 ).getUri ()));
162
+ }
163
+ } else {
164
+ pickiT .getPath (data .getData (), Build .VERSION .SDK_INT );
165
+ originalTv .setText (String .valueOf (data .getData ()));
166
+ }
167
+
168
+ }
169
+ }
141
170
142
- }
143
- }
144
- }
171
+ });
145
172
146
- //
147
173
// PickiT Listeners
148
174
//
149
175
// The listeners can be used to display a Dialog when a file is selected from Dropbox/Google Drive or OnDrive.
@@ -180,20 +206,17 @@ public void PickiTonUriReturned() {
180
206
181
207
@ Override
182
208
public void PickiTonStartListener () {
183
- if (progressBar .isShowing ()){
209
+ if (progressBar .isShowing ()) {
184
210
progressBar .cancel ();
185
211
}
186
212
final AlertDialog .Builder mPro = new AlertDialog .Builder (new ContextThemeWrapper (this , R .style .myDialog ));
187
213
@ SuppressLint ("InflateParams" ) final View mPView = LayoutInflater .from (this ).inflate (R .layout .dailog_layout , null );
188
214
percentText = mPView .findViewById (R .id .percentText );
189
215
190
- percentText .setOnClickListener (new View .OnClickListener () {
191
- @ Override
192
- public void onClick (View view ) {
193
- pickiT .cancelTask ();
194
- if (mdialog != null && mdialog .isShowing ()) {
195
- mdialog .cancel ();
196
- }
216
+ percentText .setOnClickListener (view -> {
217
+ pickiT .cancelTask ();
218
+ if (mdialog != null && mdialog .isShowing ()) {
219
+ mdialog .cancel ();
197
220
}
198
221
});
199
222
@@ -212,39 +235,63 @@ public void PickiTonProgressUpdate(int progress) {
212
235
mProgressBar .setProgress (progress );
213
236
}
214
237
238
+ @ SuppressLint ("SetTextI18n" )
215
239
@ Override
216
240
public void PickiTonCompleteListener (String path , boolean wasDriveFile , boolean wasUnknownProvider , boolean wasSuccessful , String reason ) {
217
-
218
241
if (mdialog != null && mdialog .isShowing ()) {
219
242
mdialog .cancel ();
220
243
}
221
244
222
245
// Check if it was a Drive/local/unknown provider file and display a Toast
223
- if (wasDriveFile ){
246
+ if (wasDriveFile ) {
224
247
showLongToast ("Drive file was selected" );
225
- }else if (wasUnknownProvider ){
248
+ } else if (wasUnknownProvider ) {
226
249
showLongToast ("File was selected from unknown provider" );
227
- }else {
250
+ } else {
228
251
showLongToast ("Local file was selected" );
229
252
}
230
253
231
254
// Chick if it was successful
232
255
if (wasSuccessful ) {
233
256
// Set returned path to TextView
234
- pickitTv .setText (path );
257
+ if (path .contains ("/proc/" )) {
258
+ pickitTv .setText ("Sub-directory inside Downloads was selected." + "\n " + " We will be making use of the /proc/ protocol." + "\n " + " You can use this path as you would normally." + "\n \n " + "PickiT path:" + "\n " + path );
259
+ } else {
260
+ pickitTv .setText (path );
261
+ }
235
262
236
263
// Make TextView's visible
237
264
originalTitle .setVisibility (View .VISIBLE );
238
265
originalTv .setVisibility (View .VISIBLE );
239
266
pickitTitle .setVisibility (View .VISIBLE );
240
267
pickitTv .setVisibility (View .VISIBLE );
241
- }else {
268
+ } else {
242
269
showLongToast ("Error, please see the log.." );
243
270
pickitTv .setVisibility (View .VISIBLE );
244
271
pickitTv .setText (reason );
245
272
}
246
273
}
247
274
275
+ @ Override
276
+ public void PickiTonMultipleCompleteListener (ArrayList <String > paths , boolean wasSuccessful , String Reason ) {
277
+ if (mdialog != null && mdialog .isShowing ()) {
278
+ mdialog .cancel ();
279
+ }
280
+ StringBuilder allPaths = new StringBuilder ();
281
+ for (int i = 0 ; i < paths .size (); i ++) {
282
+ allPaths .append ("\n " ).append (paths .get (i )).append ("\n " );
283
+ }
284
+
285
+ // Set returned path to TextView
286
+ pickitTv .setText (allPaths .toString ());
287
+
288
+ // Make TextView's visible
289
+ originalTitle .setVisibility (View .VISIBLE );
290
+ originalTv .setVisibility (View .VISIBLE );
291
+ pickitTitle .setVisibility (View .VISIBLE );
292
+ pickitTv .setVisibility (View .VISIBLE );
293
+ }
294
+
248
295
249
296
//
250
297
// Lifecycle methods
0 commit comments