Skip to content

Commit

Permalink
Fix issue #41, can select multiple files, update dependencies and cle…
Browse files Browse the repository at this point in the history
…aned up code.
  • Loading branch information
HBiSoft committed Nov 10, 2021
1 parent e819aaa commit a1bfbf1
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 106 deletions.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation project(path: ':pickit')
}
145 changes: 96 additions & 49 deletions app/src/main/java/com/hbisoft/pickitexample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.ClipData;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.view.ContextThemeWrapper;
Expand All @@ -16,6 +21,7 @@
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
Expand All @@ -26,9 +32,11 @@
import com.hbisoft.pickit.PickiT;
import com.hbisoft.pickit.PickiTCallbacks;

import java.util.ArrayList;
import java.util.Objects;

public class MainActivity extends AppCompatActivity implements PickiTCallbacks {
//Permissions
private static final int SELECT_VIDEO_REQUEST = 777;
private static final int PERMISSION_REQ_ID_RECORD_AUDIO = 22;
private static final int PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE = PERMISSION_REQ_ID_RECORD_AUDIO + 1;

Expand Down Expand Up @@ -66,17 +74,14 @@ private void init() {
}

private void buttonClickEvent() {
button_pick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openGallery();

// Make TextView's invisible
originalTitle.setVisibility(View.INVISIBLE);
originalTv.setVisibility(View.INVISIBLE);
pickitTitle.setVisibility(View.INVISIBLE);
pickitTv.setVisibility(View.INVISIBLE);
}
button_pick.setOnClickListener(view -> {
openGallery();

// Make TextView's invisible
originalTitle.setVisibility(View.INVISIBLE);
originalTv.setVisibility(View.INVISIBLE);
pickitTitle.setVisibility(View.INVISIBLE);
pickitTv.setVisibility(View.INVISIBLE);
});
}

Expand All @@ -85,16 +90,19 @@ private void openGallery() {
if (checkSelfPermission()) {
Intent intent;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
} else {
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI);
intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.INTERNAL_CONTENT_URI);
}
// In this example we will set the type to video
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("return-data", true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(intent, SELECT_VIDEO_REQUEST);
activityResultLauncher.launch(intent);
}
}

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_VIDEO_REQUEST) {
if (resultCode == RESULT_OK) {

// Get path from PickiT (The path will be returned in PickiTonCompleteListener)
//
// If the selected file is from Dropbox/Google Drive or OnDrive:
// Then it will be "copied" to your app directory (see path example below) and when done the path will be returned in PickiTonCompleteListener
// /storage/emulated/0/Android/data/your.package.name/files/Temp/tempDriveFile.mp4
//
// else the path will directly be returned in PickiTonCompleteListener
pickiT.getPath(data.getData(), Build.VERSION.SDK_INT);

originalTv.setText(String.valueOf(data.getData()));
ActivityResultLauncher<Intent> activityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == RESULT_OK) {
Intent data = result.getData();
// Get path from PickiT (The path will be returned in PickiTonCompleteListener)
//
// If the selected file is from Dropbox/Google Drive or OnDrive:
// Then it will be "copied" to your app directory (see path example below) and when done the path will be returned in PickiTonCompleteListener
// /storage/emulated/0/Android/data/your.package.name/files/Temp/tempDriveFile.mp4
//
// else the path will directly be returned in PickiTonCompleteListener

ClipData clipData = Objects.requireNonNull(data).getClipData();
if (clipData != null) {
int numberOfFilesSelected = clipData.getItemCount();
if (numberOfFilesSelected > 1) {
pickiT.getMultiplePaths(clipData);
StringBuilder allPaths = new StringBuilder("Multiple Files Selected:" + "\n");
for(int i = 0; i < clipData.getItemCount(); i++) {
allPaths.append("\n\n").append(clipData.getItemAt(i).getUri());
}
originalTv.setText(allPaths.toString());
}else {
pickiT.getPath(clipData.getItemAt(0).getUri(), Build.VERSION.SDK_INT);
originalTv.setText(String.valueOf(clipData.getItemAt(0).getUri()));
}
} else {
pickiT.getPath(data.getData(), Build.VERSION.SDK_INT);
originalTv.setText(String.valueOf(data.getData()));
}

}
}

}
}
}
});

//
// PickiT Listeners
//
// The listeners can be used to display a Dialog when a file is selected from Dropbox/Google Drive or OnDrive.
Expand Down Expand Up @@ -180,20 +206,17 @@ public void PickiTonUriReturned() {

@Override
public void PickiTonStartListener() {
if (progressBar.isShowing()){
if (progressBar.isShowing()) {
progressBar.cancel();
}
final AlertDialog.Builder mPro = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog));
@SuppressLint("InflateParams") final View mPView = LayoutInflater.from(this).inflate(R.layout.dailog_layout, null);
percentText = mPView.findViewById(R.id.percentText);

percentText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pickiT.cancelTask();
if (mdialog != null && mdialog.isShowing()) {
mdialog.cancel();
}
percentText.setOnClickListener(view -> {
pickiT.cancelTask();
if (mdialog != null && mdialog.isShowing()) {
mdialog.cancel();
}
});

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

@SuppressLint("SetTextI18n")
@Override
public void PickiTonCompleteListener(String path, boolean wasDriveFile, boolean wasUnknownProvider, boolean wasSuccessful, String reason) {

if (mdialog != null && mdialog.isShowing()) {
mdialog.cancel();
}

// Check if it was a Drive/local/unknown provider file and display a Toast
if (wasDriveFile){
if (wasDriveFile) {
showLongToast("Drive file was selected");
}else if (wasUnknownProvider){
} else if (wasUnknownProvider) {
showLongToast("File was selected from unknown provider");
}else {
} else {
showLongToast("Local file was selected");
}

// Chick if it was successful
if (wasSuccessful) {
// Set returned path to TextView
pickitTv.setText(path);
if (path.contains("/proc/")) {
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);
} else {
pickitTv.setText(path);
}

// Make TextView's visible
originalTitle.setVisibility(View.VISIBLE);
originalTv.setVisibility(View.VISIBLE);
pickitTitle.setVisibility(View.VISIBLE);
pickitTv.setVisibility(View.VISIBLE);
}else {
} else {
showLongToast("Error, please see the log..");
pickitTv.setVisibility(View.VISIBLE);
pickitTv.setText(reason);
}
}

@Override
public void PickiTonMultipleCompleteListener(ArrayList<String> paths, boolean wasSuccessful, String Reason) {
if (mdialog != null && mdialog.isShowing()) {
mdialog.cancel();
}
StringBuilder allPaths = new StringBuilder();
for (int i = 0; i < paths.size(); i++) {
allPaths.append("\n").append(paths.get(i)).append("\n");
}

// Set returned path to TextView
pickitTv.setText(allPaths.toString());

// Make TextView's visible
originalTitle.setVisibility(View.VISIBLE);
originalTv.setVisibility(View.VISIBLE);
pickitTitle.setVisibility(View.VISIBLE);
pickitTv.setVisibility(View.VISIBLE);
}


//
// Lifecycle methods
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
android:textSize="18sp"
android:textColor="@android:color/black"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="@+id/originalTitle"
android:visibility="invisible"/>

Expand All @@ -37,6 +38,7 @@
android:textSize="18sp"
android:textColor="@android:color/black"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="@+id/pickitTitle"
android:visibility="invisible"/>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<string name="pick_file">Pick File</string>
<string name="original_path"><u>Original Path</u></string>
<string name="pickit_path"><u>PickiT Path</u></string>
<string name="multiple_files_selected">Multiple files selected</string>
</resources>
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
buildscript {
repositories {
google()
jcenter()
mavenCentral()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.android.tools.build:gradle:4.1.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -17,8 +17,8 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
mavenCentral()

}
}

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Aug 15 06:48:30 SAST 2019
#Fri May 21 09:59:16 SAST 2021
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
Loading

0 comments on commit a1bfbf1

Please sign in to comment.