From 7934a39f8904da575322c3c7c2bbab4ad5617750 Mon Sep 17 00:00:00 2001 From: Punpun <78902475+Jerson2000@users.noreply.github.com> Date: Sat, 30 Dec 2023 11:46:03 +0800 Subject: [PATCH] fix storage permission > api31 & some --- app/src/main/AndroidManifest.xml | 2 + .../umlclasseditor/controller/IOUtils.java | 2 +- .../controller/MainActivity.java | 207 ++++++++++-------- app/src/main/res/layout/fragment_graph.xml | 5 +- 4 files changed, 118 insertions(+), 98 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 294ecb9..f55230b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,8 +3,10 @@ + { - final EditText editText = new EditText(this); - String oldName = mProject.getName(); - editText.setText(mProject.getName()); - dialog = new MaterialAlertDialogBuilder(this) - .setTitle("Rename") - .setMessage("Enter new name :") - .setView(editText) - .setNegativeButton("CANCEL", (d, i) -> dialog.dismiss()) - .setPositiveButton("OK", (d, i) -> { - renameProject(oldName,editText.getText().toString()); - updateNavigationView(); - }) - .show(); - }); + mMenuHeaderProjectNameText.setOnClickListener(v -> { + final EditText editText = new EditText(this); + String oldName = mProject.getName(); + editText.setText(mProject.getName()); + dialog = new MaterialAlertDialogBuilder(this) + .setTitle("Rename") + .setMessage("Enter new name :") + .setView(editText) + .setNegativeButton("CANCEL", (d, i) -> dialog.dismiss()) + .setPositiveButton("OK", (d, i) -> { + renameProject(oldName, editText.getText().toString()); + updateNavigationView(); + }) + .show(); + }); } private void updateNavigationView() { - mMenuHeaderProjectNameText.setText(mProject.getName()); + mMenuHeaderProjectNameText.setText(mProject.getName() + ""); } private void savePreferences() { @@ -490,7 +492,7 @@ private void drawerMenuLoadProject() { spinner.setAdapter(projectDirectoryAdapter()); dialog = new MaterialAlertDialogBuilder(this) - .setTitle("Load project") + .setTitle("Load project") .setMessage("Choose project to load :") .setView(spinner) .setNegativeButton("CANCEL", (d, i) -> dialog.dismiss()) @@ -511,7 +513,7 @@ private void drawerMenuDeleteProject() { final Spinner spinner = new Spinner(this); spinner.setAdapter(projectDirectoryAdapter()); dialog = new MaterialAlertDialogBuilder(this) - .setTitle("Delete project") + .setTitle("Delete project") .setMessage("Choose project to delete :") .setView(spinner) .setNegativeButton("Cancel", (dialogInterface, i) -> { @@ -522,12 +524,12 @@ private void drawerMenuDeleteProject() { if (fileName != null) { File pathName = new File(getFilesDir(), UmlProject.PROJECT_DIRECTORY); final File file = new File(pathName, fileName); - dialog = new MaterialAlertDialogBuilder(this) - .setTitle("Delete Project") + dialog = new MaterialAlertDialogBuilder(this) + .setTitle("Delete Project") .setMessage("Are you sure you want to delete " + fileName + " ?") .setNegativeButton("NO", (d, i12) -> dialog.dismiss()) .setPositiveButton("YES", (d, i1) -> { - if(file.delete()) dialog.dismiss(); + if (file.delete()) dialog.dismiss(); }) .show(); } @@ -580,8 +582,7 @@ public boolean onOptionsItemSelected(MenuItem menuItem) { } else if (itemId == R.id.toolbar_menu_export2java) { if (sWriteExternalStoragePermission) menuExport2Java(); - } - else if (itemId == R.id.toolbar_menu_import) { + } else if (itemId == R.id.toolbar_menu_import) { if (sReadExternalStoragePermission) menuItemImport(); } else if (itemId == R.id.toolbar_menu_create_custom_type) { @@ -606,18 +607,20 @@ else if (itemId == R.id.toolbar_menu_import) { private void menuItemExport() { Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); - intent.setType("text/*"); + intent.setType("application/json"); + intent.putExtra(Intent.EXTRA_TITLE, mProject.getName() + ".json"); exportProjectLauncher.launch(intent); } - private void menuExportPdf(){ + + private void menuExportPdf() { Bitmap bitmap = IOUtils.getBitmapFromView(this.mGraphView); ImageView prev = new ImageView(this); prev.setImageBitmap(bitmap); new MaterialAlertDialogBuilder(this) .setTitle("Preview") .setView(prev) - .setNegativeButton("Cancel",(d,i)->d.dismiss()) - .setPositiveButton("Export",(d,i)->{ + .setNegativeButton("Cancel", (d, i) -> d.dismiss()) + .setPositiveButton("Export", (d, i) -> { Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); intent.setType("application/pdf"); exportPDFLauncher.launch(intent); @@ -628,6 +631,7 @@ private void menuExportPdf(){ private void menuItemImport() { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.setType("*/*"); + intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"application/json"}); importProjectLauncher.launch(intent); } /* export2 java */ @@ -641,18 +645,18 @@ private void menuExport2Java() { dialog.setCancelable(false); dialog.setCanceledOnTouchOutside(false); new Handler().postDelayed(() -> { - if(mProject.export2Java(getApplicationContext())) { + if (mProject.export2Java(getApplicationContext())) { dialog.dismiss(); Toast.makeText(this, "Generated! See the files in the app folder in your storage.", Toast.LENGTH_LONG).show(); - } - },2000); + } else dialog.dismiss(); + }, 2000); } private void menuCreateCustomType() { final EditText editText = new EditText(this); final Context context = getApplicationContext(); dialog = new MaterialAlertDialogBuilder(this) - .setTitle("Create custom type") + .setTitle("Create custom type") .setMessage("Enter custom type name :") .setView(editText) .setNegativeButton("Cancel", (d, i) -> dialog.dismiss()) @@ -682,8 +686,8 @@ private void menuDeleteCustomTypes() { listView.setAdapter(adapter); dialog = new MaterialAlertDialogBuilder(this) - .setTitle("Delete custom types") - .setMessage("Check custom types to delete\nCustom Types: "+listArray.size()+"\n") + .setTitle("Delete custom types") + .setMessage("Check custom types to delete\nCustom Types: " + listArray.size() + "\n") .setView(listView) .setNegativeButton("CANCEL", (d, i) -> dialog.dismiss()) .setPositiveButton("OK", (d, i) -> { @@ -717,7 +721,7 @@ private void menuImportCustomTypes() { } private void menuHelp() { - new MaterialAlertDialogBuilder(this) + new MaterialAlertDialogBuilder(this) .setTitle("Help") .setMessage(Html.fromHtml(IOUtils.readRawHtmlFile(this, R.raw.help_html))) .setPositiveButton("OK", (dialog, which) -> { @@ -726,7 +730,7 @@ private void menuHelp() { .show(); } -// ********************************************************************************************** + // ********************************************************************************************** // Intents // ********************************************************************************************** ActivityResultLauncher exportProjectLauncher = registerForActivityResult( @@ -740,68 +744,74 @@ private void menuHelp() { } } ), - importProjectLauncher = registerForActivityResult( - new ActivityResultContracts.StartActivityForResult(), - result -> { - if (result.getResultCode() == RESULT_OK) { - Intent data = result.getData(); - assert data != null; - Uri fileNameUri = data.getData(); - UmlType.clearProjectUmlTypes(); - mProject = UmlProject.importProject(this, fileNameUri); - mMenuHeaderProjectNameText.setText(mProject.getName()); - mGraphView.setUmlProject(mProject); - } - } - ), - exportCustomTypeLauncher = registerForActivityResult( - new ActivityResultContracts.StartActivityForResult(), - result -> { - if (result.getResultCode() == RESULT_OK) { - Intent data = result.getData(); - assert data != null; - Uri fileNameUri = data.getData(); - UmlType.exportCustomUmlTypes(this, fileNameUri); - } - } - ), - importCustomTypeLauncher = registerForActivityResult( - new ActivityResultContracts.StartActivityForResult(), - result -> { - if (result.getResultCode() == RESULT_OK) { - Intent data = result.getData(); - assert data != null; - Uri fileNameUri = data.getData(); - UmlType.importCustomUmlTypes(this, fileNameUri); - } - } - ), - exportPDFLauncher = registerForActivityResult( - new ActivityResultContracts.StartActivityForResult(), - result -> { - if (result.getResultCode() == RESULT_OK) { - Intent data = result.getData(); - assert data != null; - Uri fileNameUri = data.getData(); - mProject.exportProjectPDF(this, this.mGraphView, fileNameUri); - Toast.makeText(getApplicationContext(), "PDF exported!", Toast.LENGTH_SHORT).show(); + importProjectLauncher = registerForActivityResult( + new ActivityResultContracts.StartActivityForResult(), + result -> { + if (result.getResultCode() == RESULT_OK) { + Intent data = result.getData(); + assert data != null; + Uri fileNameUri = data.getData(); + UmlType.clearProjectUmlTypes(); + mProject = UmlProject.importProject(this, fileNameUri); + mMenuHeaderProjectNameText.setText(mProject.getName()); + mGraphView.setUmlProject(mProject); + } } - } - ); + ), + exportCustomTypeLauncher = registerForActivityResult( + new ActivityResultContracts.StartActivityForResult(), + result -> { + if (result.getResultCode() == RESULT_OK) { + Intent data = result.getData(); + assert data != null; + Uri fileNameUri = data.getData(); + UmlType.exportCustomUmlTypes(this, fileNameUri); + } + } + ), + importCustomTypeLauncher = registerForActivityResult( + new ActivityResultContracts.StartActivityForResult(), + result -> { + if (result.getResultCode() == RESULT_OK) { + Intent data = result.getData(); + assert data != null; + Uri fileNameUri = data.getData(); + UmlType.importCustomUmlTypes(this, fileNameUri); + } + } + ), + exportPDFLauncher = registerForActivityResult( + new ActivityResultContracts.StartActivityForResult(), + result -> { + if (result.getResultCode() == RESULT_OK) { + Intent data = result.getData(); + assert data != null; + Uri fileNameUri = data.getData(); + mProject.exportProjectPDF(this, this.mGraphView, fileNameUri); + Toast.makeText(getApplicationContext(), "PDF exported!", Toast.LENGTH_SHORT).show(); + } + } + ),storagePermissionLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult() + ,result ->{ + }) ; + @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); - sWriteExternalStoragePermission = requestCode == REQUEST_PERMISSION && grantResults[WRITE_EXTERNAL_STORAGE_INDEX] == PackageManager.PERMISSION_GRANTED; - sReadExternalStoragePermission = requestCode == REQUEST_PERMISSION && grantResults[READ_EXTERNAL_STORAGE_INDEX] == PackageManager.PERMISSION_GRANTED; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2) { + sWriteExternalStoragePermission = requestCode == REQUEST_PERMISSION && grantResults[WRITE_EXTERNAL_STORAGE_INDEX] == PackageManager.PERMISSION_GRANTED; + sReadExternalStoragePermission = requestCode == REQUEST_PERMISSION && grantResults[READ_EXTERNAL_STORAGE_INDEX] == PackageManager.PERMISSION_GRANTED; + } else + sManageExternalStoragePermission = requestCode == REQUEST_PERMISSION && grantResults[MANAGE_EXTERNAL_STORAGE_INDEX] == PackageManager.PERMISSION_GRANTED; } // ********************************************************************************************** // Project management methods // ********************************************************************************************** - private void renameProject(String oldName,String newName){ + private void renameProject(String oldName, String newName) { mProject.setName(newName); - mProject.rename(getApplicationContext(),oldName); + mProject.rename(getApplicationContext(), oldName); } private void saveAs() { @@ -817,18 +827,25 @@ private void saveAs() { @RequiresApi(api = Build.VERSION_CODES.M) private void checkPermissions() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) { String[] permissionString = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}; if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) requestPermissions(permissionString, REQUEST_PERMISSION); + } else { + if (!Environment.isExternalStorageManager()){ + Intent intent = new Intent(ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, Uri.parse("package:" + BuildConfig.APPLICATION_ID)); + storagePermissionLauncher.launch(intent); + } + + } } - public static void hideToolBar(boolean is_locate){ - staticToolbar.setVisibility(is_locate?View.GONE:View.VISIBLE); + public static void hideToolBar(boolean is_locate) { + staticToolbar.setVisibility(is_locate ? View.GONE : View.VISIBLE); } diff --git a/app/src/main/res/layout/fragment_graph.xml b/app/src/main/res/layout/fragment_graph.xml index 3a4ca72..e4a1923 100644 --- a/app/src/main/res/layout/fragment_graph.xml +++ b/app/src/main/res/layout/fragment_graph.xml @@ -91,6 +91,7 @@ android:layout_height="50dp" android:layout_weight="25" android:backgroundTint="?colorSurfaceInverse" + android:textAppearance="?attr/textAppearanceLabelSmall" android:text="ESC" /> @@ -132,8 +133,8 @@ android:ellipsize="marquee" android:singleLine="true" android:text="+ Class" - app:autoSizeMaxTextSize="14sp" - app:autoSizeTextType="uniform" /> + android:textAppearance="?attr/textAppearanceLabelSmall" + />