permissions removed

android13 fix
This commit is contained in:
Epic Studios 2023-11-17 17:23:41 +02:00
parent 4dbf351b46
commit c0b13620f7
6 changed files with 123 additions and 194 deletions

View file

@ -16,8 +16,10 @@ import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.Html;
import android.util.Log;
@ -391,14 +393,17 @@ public class FirstActivity extends AppCompatActivity {
ad.setMessage("press import button and select "+selectedPath.replace(".IMG", ".vbi")+" file.");
ad.setButton(Dialog.BUTTON_POSITIVE, "IMPORT", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent chooseFile = new Intent(ACTION_OPEN_DOCUMENT);
// Ask specifically for something that can be opened:
chooseFile.addCategory(Intent.CATEGORY_OPENABLE);
chooseFile.setType("*/*");
startActivityForResult(
Intent.createChooser(chooseFile, "Choose a file"),
0
);
Intent intent = new Intent(ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
// Optionally, specify a URI for the file that should appear in the
// system file picker when it loads.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, Environment.DIRECTORY_DOWNLOADS);
}
startActivityForResult(intent, 0);
}
});
ad.setButton(Dialog.BUTTON_NEGATIVE, "DOWNLAOD "+selectedPath.replace(".IMG", ".vbi"), new DialogInterface.OnClickListener() {
@ -434,13 +439,72 @@ public class FirstActivity extends AppCompatActivity {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK){
Uri content_describer = data.getData();
File selectedFilePath = new File(getPath(content_describer));
if (selectedFilePath.getName().equals(selectedPath.replace(".IMG", ".vbi"))) {
try {
unzip(selectedFilePath.toString(), Config.maindirpath);
} catch (IOException e) {
progressDialog = new ProgressDialog(activity,
R.style.MainDialogTheme);
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.show(); // Showing Progress Dialog
Thread t = new Thread() {
public void run() {
FileInputStream zipFile = null;
try {
zipFile = (FileInputStream) getContentResolver().openInputStream(content_describer);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
File targetDirectory = new File(Config.maindirpath);
ZipInputStream zis = null;
zis = new ZipInputStream(
new BufferedInputStream(zipFile));
try {
ZipEntry ze;
int count;
byte[] buffer = new byte[8192];
while ((ze = zis.getNextEntry()) != null) {
File file = new File(targetDirectory, ze.getName());
File dir = ze.isDirectory() ? file : file.getParentFile();
if (!dir.isDirectory() && !dir.mkdirs())
throw new FileNotFoundException("Failed to ensure directory: " +
dir.getAbsolutePath());
if (ze.isDirectory())
continue;
FileOutputStream fout = new FileOutputStream(file);
try {
while ((count = zis.read(buffer)) != -1)
fout.write(buffer, 0, count);
} finally {
fout.close();
}
/* if time should be restored as well
long time = ze.getTime();
if (time > 0)
file.setLastModified(time);
*/
}
} catch (FileNotFoundException e) {
UIUtils.toastLong(activity, e.toString());
throw new RuntimeException(e);
} catch (IOException e) {
UIUtils.toastLong(activity, e.toString());
throw new RuntimeException(e);
} finally {
progressDialog.cancel(); // cancelling Dialog.
try {
zis.close();
} catch (IOException e) {
UIUtils.toastLong(activity, e.toString());
throw new RuntimeException(e);
}
}
}
};
t.start();
} catch (Exception e) {
progressDialog.dismiss(); // Close Progress Dialog
UIUtils.toastLong(activity, e.toString());
throw new RuntimeException(e);
@ -453,70 +517,6 @@ public class FirstActivity extends AppCompatActivity {
}
}
public void unzip(String _zipFile, String _location) throws IOException {
progressDialog = new ProgressDialog(activity,
R.style.MainDialogTheme);
progressDialog.setMessage("Please wait...");
progressDialog.setCancelable(false);
progressDialog.show(); // Showing Progress Dialog
Thread t = new Thread() {
public void run() {
File zipFile = new File(_zipFile);
File targetDirectory = new File(_location);
ZipInputStream zis = null;
try {
zis = new ZipInputStream(
new BufferedInputStream(new FileInputStream(zipFile)));
} catch (FileNotFoundException e) {
UIUtils.toastLong(activity, e.toString());
throw new RuntimeException(e);
}
try {
ZipEntry ze;
int count;
byte[] buffer = new byte[8192];
while ((ze = zis.getNextEntry()) != null) {
File file = new File(targetDirectory, ze.getName());
File dir = ze.isDirectory() ? file : file.getParentFile();
if (!dir.isDirectory() && !dir.mkdirs())
throw new FileNotFoundException("Failed to ensure directory: " +
dir.getAbsolutePath());
if (ze.isDirectory())
continue;
FileOutputStream fout = new FileOutputStream(file);
try {
while ((count = zis.read(buffer)) != -1)
fout.write(buffer, 0, count);
} finally {
fout.close();
}
/* if time should be restored as well
long time = ze.getTime();
if (time > 0)
file.setLastModified(time);
*/
}
} catch (FileNotFoundException e) {
UIUtils.toastLong(activity, e.toString());
throw new RuntimeException(e);
} catch (IOException e) {
UIUtils.toastLong(activity, e.toString());
throw new RuntimeException(e);
} finally {
progressDialog.cancel(); // cancelling Dialog.
try {
zis.close();
} catch (IOException e) {
UIUtils.toastLong(activity, e.toString());
throw new RuntimeException(e);
}
}
}
};
t.start();
}
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
class DownloadsImage extends AsyncTask<String, Void,Void>{
@ -575,6 +575,8 @@ public class FirstActivity extends AppCompatActivity {
@Override
public void onBackPressed() {
super.onBackPressed();
if (getParentActivityIntent()==MainActivity.activity.getIntent()
)
finish();
}

View file

@ -660,9 +660,9 @@ public class MainActivity extends AppCompatActivity {
ProgressBar progressBar = findViewById(R.id.progressBar);
progressBar.setMax((int) totalMemory);
if (SDK_INT >= Build.VERSION_CODES.N) {
progressBar.setProgress((int) freeMemory, true);
progressBar.setProgress((int) usedMemory, true);
} else {
progressBar.setProgress((int) freeMemory);
progressBar.setProgress((int) usedMemory);
}
}
});

View file

@ -13,6 +13,7 @@ import android.preference.PreferenceManager;
import android.provider.Settings;
import android.view.*;
import android.graphics.*;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
@ -26,113 +27,45 @@ import com.epicstudios.vectras.utils.UIUtils;
import java.io.File;
public class SplashActivity extends AppCompatActivity implements Runnable {
public AlertDialog ad;
public static SplashActivity activity;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
activity = this;
File baseDir = new File(Config.basefiledir);
public AlertDialog ad;
public static SplashActivity activity;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
activity = this;
File baseDir = new File(Config.basefiledir);
if (!baseDir.exists()) {
baseDir.mkdirs();
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
if (!checkPermission()) {
ad = new AlertDialog.Builder(this, R.style.MainDialogTheme).create();
ad.setTitle("permissions");
ad.setMessage("Vectras needs some permissions:\n-full storage access(shared folder - vectras bootable image '.vbi')");
ad.setCanceledOnTouchOutside(false);
ad.setButton(Dialog.BUTTON_POSITIVE, "Allow", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
try {
ActivityCompat.requestPermissions(SplashActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} catch (Exception e) {
UIUtils.toastLong(activity, e.toString());
throw new RuntimeException(e);
}
return;
}
});
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
ad.setButton(Dialog.BUTTON_NEGATIVE, "Learn More", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String gt = Config.vectrasRepo;
Intent g = new Intent(Intent.ACTION_VIEW);
g.setData(Uri.parse(gt));
startActivity(g);
finish();
return;
}
});
ad.show();
} else {
File sharedDir = new File(Config.sharedFolder);
if (!sharedDir.exists()) {
sharedDir.mkdirs();
}
new Handler().postDelayed(this, 2000);
}
new Handler().postDelayed(this, 3000);
File sharedDir = new File(Config.sharedFolder);
if (!sharedDir.exists()) {
sharedDir.mkdirs();
}
File mainDir = new File(Config.maindirpath);
if (!mainDir.exists()) {
mainDir.mkdirs();
}
File sharedDir = new File(Config.sharedFolder);
if (!sharedDir.exists()) {
sharedDir.mkdirs();
}
File mainDir = new File(Config.maindirpath);
if (!mainDir.exists()) {
mainDir.mkdirs();
}
}
}
private boolean checkPermission() {
if (SDK_INT >= Build.VERSION_CODES.R) {
return Environment.isExternalStorageManager();
} else {
int result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
if (android.os.Build.VERSION.SDK_INT >= 30) {
int result1 = ContextCompat.checkSelfPermission(this, Manifest.permission.MANAGE_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED && result1 == PackageManager.PERMISSION_GRANTED;
} else {
return result == PackageManager.PERMISSION_GRANTED;
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (android.os.Build.VERSION.SDK_INT >= 30) {
public static final String CREDENTIAL_SHARED_PREF = "settings_prefs";
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
Uri uri = Uri.fromParts("package", this.getPackageName(), null);
intent.setData(uri);
startActivity(intent);
ad.cancel();
finish();
} else {
new Handler().postDelayed(this, 2000);
@Override
public void run() {
SharedPreferences prefs = getSharedPreferences(CREDENTIAL_SHARED_PREF, Context.MODE_PRIVATE);
}
} else if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(SplashActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
}
public static final String CREDENTIAL_SHARED_PREF = "settings_prefs";
@Override
public void run() {
SharedPreferences prefs = getSharedPreferences(CREDENTIAL_SHARED_PREF, Context.MODE_PRIVATE);
boolean isAccessed = prefs.getBoolean("isFirstLaunch", false);
if (!isAccessed) {
startActivity(new Intent(this, FirstActivity.class));
} else {
startActivity(new Intent(this, MainActivity.class));
}
finish();
}
boolean isAccessed = prefs.getBoolean("isFirstLaunch", false);
if (!isAccessed) {
startActivity(new Intent(this, FirstActivity.class));
} else {
startActivity(new Intent(this, MainActivity.class));
}
finish();
}
}