}
@Override
protected void onActivityResult(int _requestCode, int _resultCode, Intent
_data) {
super.onActivityResult(_requestCode, _resultCode, _data);
if (_requestCode == new_folder){
if (_resultCode == Activity.RESULT_OK) {
if (_data != null) {
final Uri uri2 = _data.getData();
if (Uri.decode(uri2.toString()).endsWith(":")) {
SketchwareUtil.showMessage(getApplicationContext(), "Operação falhou");
askPermission(uri2.toString());
}
else {
final int takeFlags = i.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// [email protected]
getContentResolver().takePersistableUriPermission(uri2, takeFlags);
} else {
}
} else {
}
}
if (_requestCode == 2000) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
} else {
}
}
// SE FOR FAZER EM UMA NOVA TELA MUDE O CÓDIGO ABAIXO
public void RequestPermission_Dialog() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
try {
Intent intent = new
Intent(android.provider.Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package: ", new
Object[]{getApplicationContext().getPackageName()})));
startActivityForResult(intent, 2000);
} catch (Exception e) {
Intent obj = new Intent();
obj.setAction(android.provider.Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivityForResult(obj, 2000);
}
} else {
androidx.core.app.ActivityCompat.requestPermissions(MainActivity.this,
new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE);
}
}
public boolean permission() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) { //
R is Android 11
return Environment.isExternalStorageManager();
} else {
int write =
androidx.core.content.ContextCompat.checkSelfPermission(getApplicationContext(),
android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
int read =
androidx.core.content.ContextCompat.checkSelfPermission(getApplicationContext(),
android.Manifest.permission.READ_EXTERNAL_STORAGE);
return write == android.content.pm.PackageManager.PERMISSION_GRANTED
&& read == android.content.pm.PackageManager.PERMISSION_GRANTED;
}
}
// ask permissions request
public void askPermission(final String _uri) {
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
i.setAction(Intent.ACTION_OPEN_DOCUMENT_TREE);
i.putExtra(android.provider.DocumentsContract.EXTRA_INITIAL_URI, Uri.parse(_uri));
startActivityForResult(i, new_folder);
}
// check permissions of path if accepted
public boolean checkPermission(final String _uri) {
Uri muri = Uri.parse(_uri);
dFile =
androidx.documentfile.provider.DocumentFile.fromTreeUri(getApplicationContext(),
muri);
if (dFile.canRead() && dFile.canWrite()) {
return true ;
}
return false ;
}
// simple path to UriTree path
public String pathToRealUri( String _path) {
uriFor1 = "content://com.android.externalstorage.documents/tree/primary
%3A";
if ( _path.endsWith("/")) {
_path = _path.substring(0, _path.length()-1);
}
if (_path.contains("/sdcard/")) {
uriFor2 = _path.replace("/sdcard/", "").replace("/", "%2F");
if (uriFor2.substring(uriFor2.length()-1,
uriFor2.length()).equals("/")) {
uriFor2 = uriFor1.substring(0, uriFor1.length()-1);
}
else {
if (_path.contains("/storage/") && _path.contains("/emulated/"))
{
uriFor2 = _path.replace("/storage/emulated/0/",
"").replace("/", "%2F");
if (uriFor2.substring(uriFor2.length()-1,
uriFor2.length()).equals("/")) {
uriFor2 = uriFor1.substring(0, uriFor1.length()-1);
}
else {
}
}
return uriFor1 = uriFor1 + uriFor2;
}
// simple path to UriTree path 2
public String pathToUri( String _path) {
uriFor1 = "content://com.android.externalstorage.documents/tree/primary
%3AAndroid/document/primary%3A";
if ( _path.endsWith("/")) {
_path = _path.substring(0, _path.length()-1);
}
if (_path.contains("/sdcard/")) {
uriFor2 = _path.replace("/sdcard/", "").replace("/", "%2F");
if (uriFor2.substring(uriFor2.length()-1,
uriFor2.length()).equals("/")) {
uriFor2 = uriFor1.substring(0, uriFor1.length()-1);
}
else {
if (_path.contains("/storage/") && _path.contains("/emulated/"))
{
uriFor2 = _path.replace("/storage/emulated/0/",
"").replace("/", "%2F");
if (uriFor2.substring(uriFor2.length()-1,
uriFor2.length()).equals("/")) {
uriFor2 = uriFor1.substring(0, uriFor1.length()-1);
}
else {
}
}
return uriFor1 = uriFor1 + uriFor2;
}
// ccopy file from path to path
private boolean copyAsset(final String assetFilename, final Uri targetUri) {
try{
int count;
InputStream input = null;
OutputStream output = null;
ContentResolver content =
getApplicationContext().getContentResolver();
input = getApplicationContext().getAssets().open(assetFilename);
output = content.openOutputStream(targetUri);
byte data[] = new byte[1024];
while ((count = input.read(data))>0) {
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
SketchwareUtil.showMessage(getApplicationContext(), "50%");
}catch(Exception e){
FileUtil.writeFile("/sdcard/log.txt", "\n"+ "3 " +e.toString());
SketchwareUtil.showMessage(getApplicationContext(), e.toString());
return false;
}
return true;
}
private void copyAssetFolder(String _folder, String _out ) {
AssetManager assetManager = getAssets();
int sizeList = 0;
String[] files = null;
try {
files = assetManager.list(_folder);
} catch (java.io.IOException e) {
}
final ArrayList<String> str = new ArrayList<>(Arrays.asList(files));
int nn = 0;
for(int _repeat12 = 0; _repeat12 < (int)(str.size()); _repeat12++) {
try {
Uri mUri = Uri.parse(pathToRealUri(_out));
String fileName = str.get((int)nn);
sizeList = str.size()-1;
androidx.documentfile.provider.DocumentFile dFile =
androidx.documentfile.provider.DocumentFile.fromTreeUri(getApplicationContext(),
mUri);
Uri mUri2 = Uri.parse(mUri.toString()+ "%2" +
fileName);
androidx.documentfile.provider.DocumentFile dFile2 =
androidx.documentfile.provider.DocumentFile.fromTreeUri(getApplicationContext(),
mUri2);
try {
androidx.documentfile.provider.DocumentFile file =
dFile.findFile(fileName);
android.provider.DocumentsContract.deleteDocument(getApplicationContext().getConten
tResolver(), file.getUri());
android.provider.DocumentsContract.deleteDocument(getApplicationContext().getConten
tResolver(), mUri2);
} catch (FileNotFoundException e) {
} catch (Exception e2) {
}
dFile2 = dFile.createFile("*/*", fileName);
mUri = dFile2.getUri();
if (copyAsset(_folder+"/"+fileName, mUri)) {
if (nn >= sizeList){
SketchwareUtil.showMessage(getApplicationContext(), "️100% Activado");
}
} else {
SketchwareUtil.showMessage(getApplicationContext(), "Falha");
break;
}
} catch (Exception re){}
nn++;
}
//Create By Amir Sina IZ
public boolean copy(java.io.File copy, String directory, Context con) {
java.io.FileInputStream inStream = null;
java.io.OutputStream outStream = null;
androidx.documentfile.provider.DocumentFile dir=
androidx.documentfile.provider.DocumentFile.fromTreeUri(getApplicationContext(),
Uri.parse(pathToRealUri(directory)));
try {
String fileN = Uri.parse(copy.getPath()).getLastPathSegment();
androidx.documentfile.provider.DocumentFile file = dir.findFile(fileN);
android.provider.DocumentsContract.deleteDocument(getApplicationContext().getConten
tResolver(), file.getUri());
} catch (Exception e){
e.printStackTrace();
}
String mim = mime(copy.toURI().toString());
androidx.documentfile.provider.DocumentFile copy1= dir.createFile(mim,
copy.getName());
try {
inStream = new java.io.FileInputStream(copy);
outStream =
con.getContentResolver().openOutputStream(copy1.getUri());
byte[] buffer = new byte[16384];
int bytesRead;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
} catch (java.io.FileNotFoundException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
} finally {
try {
inStream.close();
outStream.close();
return true;
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
return false;
}
public String mime(String URI) {
String type = "";
String extention = android.webkit.MimeTypeMap.getFileExtensionFromUrl(URI);
if (extention != null) {
type =
android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extention);
}
return type;
}
//Create By Amir Sina IZ
private boolean fromStorage = false;
final static int REQUEST_CODE = 333;
final static int OLD_REQUEST = 2000;
private SharedPreferences sha;
private Intent i = new Intent();
private Uri muri;
private String uriFor1 = "";
private String uriFor2 = "";
private
androidx.documentfile.provider.DocumentFile dFile;
private double PermissionNumber;
private static final int new_folder = 43;
{