2022-04-29 15:18:33 +00:00
|
|
|
package com.blankie.fileify;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2022-04-29 16:13:06 +00:00
|
|
|
import android.widget.Toast;
|
2022-04-29 15:18:33 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.ActivityNotFoundException;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.blankie.fileify.R;
|
|
|
|
|
|
|
|
public class PickActivity extends Activity {
|
|
|
|
public static final String TAG = "fileify";
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
final Intent intent = getIntent();
|
|
|
|
Intent newIntent = new Intent(Intent.ACTION_GET_CONTENT);
|
|
|
|
|
|
|
|
newIntent.addCategory(Intent.CATEGORY_OPENABLE)
|
|
|
|
// from Telegram ax, why not?
|
|
|
|
.putExtra("android.content.extra.SHOW_ADVANCED", true);
|
|
|
|
if (intent.hasExtra(Intent.EXTRA_ALLOW_MULTIPLE)) {
|
|
|
|
newIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,
|
|
|
|
intent.getBooleanExtra(Intent.EXTRA_ALLOW_MULTIPLE, false));
|
2022-04-29 16:03:03 +00:00
|
|
|
}
|
2022-04-29 15:18:33 +00:00
|
|
|
if (intent.getType() == "vnd.android.cursor.dir/image") {
|
|
|
|
newIntent.setType("image/*");
|
|
|
|
} else if (intent.getType() == "vnd.android.cursor.dir/video") {
|
|
|
|
newIntent.setType("video/*");
|
|
|
|
} else {
|
|
|
|
newIntent.setType(intent.getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
startActivityForResult(newIntent, 0);
|
|
|
|
} catch (ActivityNotFoundException e) {
|
2022-04-29 16:13:06 +00:00
|
|
|
Log.e(TAG, getString(R.string.no_activity));
|
|
|
|
Toast.makeText(getApplicationContext(), msg, Toast.LENGH_SHORT)
|
|
|
|
.show();
|
|
|
|
setResult(Activity.RESULT_CANCELLED);
|
2022-04-29 15:18:33 +00:00
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode,
|
|
|
|
Intent data) {
|
|
|
|
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
|
|
|
|
if (resultCode != RESULT_OK) {
|
|
|
|
Log.d(TAG, "Received resultCode = " + resultCode);
|
2022-04-29 16:13:06 +00:00
|
|
|
setResult(Activity.RESULT_CANCELLED);
|
2022-04-29 15:18:33 +00:00
|
|
|
finish();
|
|
|
|
// probably not necessary but just in case or something
|
|
|
|
return;
|
|
|
|
} else if (data == null) {
|
|
|
|
Log.d(TAG, "Received data = null");
|
2022-04-29 16:13:06 +00:00
|
|
|
setResult(Activity.RESULT_CANCELLED);
|
2022-04-29 15:18:33 +00:00
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Intent intent = new Intent();
|
|
|
|
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
|
if (data.getData() != null) {
|
|
|
|
intent.setData(data.getData());
|
2022-04-29 16:13:06 +00:00
|
|
|
setResult(Activity.RESULT_OK, intent);
|
2022-04-29 15:18:33 +00:00
|
|
|
} else if (data.getClipData() != null) {
|
|
|
|
// unsafe?
|
|
|
|
intent.setClipData(data.getClipData());
|
2022-04-29 16:13:06 +00:00
|
|
|
setResult(Activity.RESULT_OK, intent);
|
2022-04-29 15:18:33 +00:00
|
|
|
} else {
|
|
|
|
Log.d(TAG, "Received data.getData() = null and data.getClipData() = null");
|
2022-04-29 16:13:06 +00:00
|
|
|
setResult(Activity.RESULT_CANCELLED);
|
2022-04-29 15:18:33 +00:00
|
|
|
}
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|