Skip to content

Commit a1bfbf1

Browse files
committed
Fix issue #41, can select multiple files, update dependencies and cleaned up code.
1 parent e819aaa commit a1bfbf1

File tree

16 files changed

+305
-106
lines changed

16 files changed

+305
-106
lines changed

.idea/compiler.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

+9-5
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ android {
1616
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
1717
}
1818
}
19+
compileOptions {
20+
sourceCompatibility JavaVersion.VERSION_1_8
21+
targetCompatibility JavaVersion.VERSION_1_8
22+
}
1923
}
2024

2125
dependencies {
2226
implementation fileTree(dir: 'libs', include: ['*.jar'])
23-
implementation 'androidx.appcompat:appcompat:1.1.0'
24-
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
25-
testImplementation 'junit:junit:4.12'
26-
androidTestImplementation 'androidx.test:runner:1.2.0'
27-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
27+
implementation 'androidx.appcompat:appcompat:1.3.1'
28+
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
29+
testImplementation 'junit:junit:4.13.2'
30+
androidTestImplementation 'androidx.test:runner:1.4.0'
31+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
2832
implementation project(path: ':pickit')
2933
}

app/src/main/java/com/hbisoft/pickitexample/MainActivity.java

+96-49
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
import android.Manifest;
44
import android.annotation.SuppressLint;
55
import android.app.ProgressDialog;
6+
import android.content.ClipData;
67
import android.content.Intent;
78
import android.content.pm.PackageManager;
89
import android.os.Build;
910
import android.os.Environment;
1011

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;
1116
import androidx.annotation.NonNull;
1217
import androidx.appcompat.app.AlertDialog;
1318
import androidx.appcompat.view.ContextThemeWrapper;
@@ -16,6 +21,7 @@
1621
import androidx.appcompat.app.AppCompatActivity;
1722

1823
import android.os.Bundle;
24+
import android.provider.MediaStore;
1925
import android.view.LayoutInflater;
2026
import android.view.View;
2127
import android.widget.Button;
@@ -26,9 +32,11 @@
2632
import com.hbisoft.pickit.PickiT;
2733
import com.hbisoft.pickit.PickiTCallbacks;
2834

35+
import java.util.ArrayList;
36+
import java.util.Objects;
37+
2938
public class MainActivity extends AppCompatActivity implements PickiTCallbacks {
3039
//Permissions
31-
private static final int SELECT_VIDEO_REQUEST = 777;
3240
private static final int PERMISSION_REQ_ID_RECORD_AUDIO = 22;
3341
private static final int PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE = PERMISSION_REQ_ID_RECORD_AUDIO + 1;
3442

@@ -66,17 +74,14 @@ private void init() {
6674
}
6775

6876
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);
8085
});
8186
}
8287

@@ -85,16 +90,19 @@ private void openGallery() {
8590
if (checkSelfPermission()) {
8691
Intent intent;
8792
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);
8994
} 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);
9196
}
9297
// In this example we will set the type to video
9398
intent.setType("video/*");
9499
intent.setAction(Intent.ACTION_GET_CONTENT);
95100
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+
}
96104
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
97-
startActivityForResult(intent, SELECT_VIDEO_REQUEST);
105+
activityResultLauncher.launch(intent);
98106
}
99107
}
100108

@@ -110,6 +118,7 @@ private boolean checkSelfPermission() {
110118
// Handle permissions
111119
@Override
112120
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
121+
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
113122
if (requestCode == PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE) {
114123
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
115124
// Permissions was granted, open the gallery
@@ -122,28 +131,45 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
122131
}
123132
}
124133

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+
}
141170

142-
}
143-
}
144-
}
171+
});
145172

146-
//
147173
// PickiT Listeners
148174
//
149175
// 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() {
180206

181207
@Override
182208
public void PickiTonStartListener() {
183-
if (progressBar.isShowing()){
209+
if (progressBar.isShowing()) {
184210
progressBar.cancel();
185211
}
186212
final AlertDialog.Builder mPro = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog));
187213
@SuppressLint("InflateParams") final View mPView = LayoutInflater.from(this).inflate(R.layout.dailog_layout, null);
188214
percentText = mPView.findViewById(R.id.percentText);
189215

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();
197220
}
198221
});
199222

@@ -212,39 +235,63 @@ public void PickiTonProgressUpdate(int progress) {
212235
mProgressBar.setProgress(progress);
213236
}
214237

238+
@SuppressLint("SetTextI18n")
215239
@Override
216240
public void PickiTonCompleteListener(String path, boolean wasDriveFile, boolean wasUnknownProvider, boolean wasSuccessful, String reason) {
217-
218241
if (mdialog != null && mdialog.isShowing()) {
219242
mdialog.cancel();
220243
}
221244

222245
// Check if it was a Drive/local/unknown provider file and display a Toast
223-
if (wasDriveFile){
246+
if (wasDriveFile) {
224247
showLongToast("Drive file was selected");
225-
}else if (wasUnknownProvider){
248+
} else if (wasUnknownProvider) {
226249
showLongToast("File was selected from unknown provider");
227-
}else {
250+
} else {
228251
showLongToast("Local file was selected");
229252
}
230253

231254
// Chick if it was successful
232255
if (wasSuccessful) {
233256
// 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+
}
235262

236263
// Make TextView's visible
237264
originalTitle.setVisibility(View.VISIBLE);
238265
originalTv.setVisibility(View.VISIBLE);
239266
pickitTitle.setVisibility(View.VISIBLE);
240267
pickitTv.setVisibility(View.VISIBLE);
241-
}else {
268+
} else {
242269
showLongToast("Error, please see the log..");
243270
pickitTv.setVisibility(View.VISIBLE);
244271
pickitTv.setText(reason);
245272
}
246273
}
247274

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+
248295

249296
//
250297
// Lifecycle methods

app/src/main/res/layout/activity_main.xml

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
android:textSize="18sp"
1414
android:textColor="@android:color/black"
1515
android:layout_marginTop="20dp"
16+
android:textStyle="bold"
1617
android:id="@+id/originalTitle"
1718
android:visibility="invisible"/>
1819

@@ -37,6 +38,7 @@
3738
android:textSize="18sp"
3839
android:textColor="@android:color/black"
3940
android:layout_marginTop="20dp"
41+
android:textStyle="bold"
4042
android:id="@+id/pickitTitle"
4143
android:visibility="invisible"/>
4244

app/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<string name="pick_file">Pick File</string>
44
<string name="original_path"><u>Original Path</u></string>
55
<string name="pickit_path"><u>PickiT Path</u></string>
6+
<string name="multiple_files_selected">Multiple files selected</string>
67
</resources>

build.gradle

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
buildscript {
44
repositories {
55
google()
6-
jcenter()
6+
mavenCentral()
77

88
}
99
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.4.1'
10+
classpath 'com.android.tools.build:gradle:4.1.3'
1111

1212
// NOTE: Do not place your application dependencies here; they belong
1313
// in the individual module build.gradle files
@@ -17,8 +17,8 @@ buildscript {
1717
allprojects {
1818
repositories {
1919
google()
20-
jcenter()
21-
20+
mavenCentral()
21+
2222
}
2323
}
2424

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Thu Aug 15 06:48:30 SAST 2019
1+
#Fri May 21 09:59:16 SAST 2021
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip

0 commit comments

Comments
 (0)