|
10 | 10 | import android.os.CountDownTimer; |
11 | 11 | import android.os.Handler; |
12 | 12 |
|
| 13 | +import androidx.activity.result.ActivityResultLauncher; |
| 14 | +import androidx.activity.result.contract.ActivityResultContracts; |
13 | 15 | import androidx.annotation.NonNull; |
14 | | -import androidx.annotation.Nullable; |
15 | 16 | import androidx.appcompat.app.AppCompatDelegate; |
16 | 17 | import androidx.core.app.ActivityCompat; |
17 | 18 | import androidx.core.app.ShareCompat; |
@@ -106,6 +107,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On |
106 | 107 | private CheckBox ChbCRC32; |
107 | 108 |
|
108 | 109 | private final String tmpFile = "tmpFile"; |
| 110 | + private ActivityResultLauncher<Intent> activityResultLauncher; |
109 | 111 |
|
110 | 112 | @Override |
111 | 113 | protected void onCreate(final Bundle savedInstanceState) { |
@@ -163,6 +165,35 @@ protected void onCreate(final Bundle savedInstanceState) { |
163 | 165 | loadSettingsContent(); |
164 | 166 |
|
165 | 167 | loadAlertContent(); |
| 168 | + |
| 169 | + this.activityResultLauncher = registerForActivityResult( |
| 170 | + new ActivityResultContracts.StartActivityForResult(), |
| 171 | + result -> { |
| 172 | + if (result.getData() != null) { |
| 173 | + final Uri selectedFileUri = result.getData().getData(); |
| 174 | + if (selectedFileUri != null) { |
| 175 | + try (final InputStream selectedFileStream = getContentResolver().openInputStream(selectedFileUri)) { |
| 176 | + final File outputFile = new File(getApplicationContext().getCacheDir(), tmpFile); |
| 177 | + |
| 178 | + try (final FileOutputStream outputStream = new FileOutputStream(outputFile, false)) { |
| 179 | + if (selectedFileStream != null) { |
| 180 | + StreamUtility.copyStream(selectedFileStream, outputStream); |
| 181 | + edtFilePath.setText(selectedFileUri.getPath()); |
| 182 | + } else { |
| 183 | + Toast.makeText(getApplicationContext(), R.string.error_open_file, Toast.LENGTH_SHORT).show(); |
| 184 | + } |
| 185 | + } catch (final IOException ex) { |
| 186 | + Toast.makeText(getApplicationContext() |
| 187 | + , R.string.error_copy_file, Toast.LENGTH_SHORT).show(); |
| 188 | + } |
| 189 | + } catch (final IOException ex) { |
| 190 | + Toast.makeText(getApplicationContext(), R.string.error_open_file, Toast.LENGTH_SHORT).show(); |
| 191 | + } |
| 192 | + } else { |
| 193 | + Toast.makeText(getApplicationContext(), R.string.error_open_file, Toast.LENGTH_SHORT).show(); |
| 194 | + } |
| 195 | + } |
| 196 | + }); |
166 | 197 | } |
167 | 198 |
|
168 | 199 | /** |
@@ -297,7 +328,7 @@ private void loadFileHashContent(final Bundle savedInstance) { |
297 | 328 | .setAction(Intent.ACTION_GET_CONTENT) |
298 | 329 | .addCategory(Intent.CATEGORY_OPENABLE); |
299 | 330 |
|
300 | | - startActivityForResult(Intent.createChooser(intent, getString(R.string.dialog_select_file)), 123); |
| 331 | + activityResultLauncher.launch(Intent.createChooser(intent, getString(R.string.dialog_select_file))); |
301 | 332 | } |
302 | 333 | }); |
303 | 334 |
|
@@ -455,7 +486,7 @@ private void loadHelpContent() { |
455 | 486 |
|
456 | 487 | btnWebsite.setOnClickListener(v -> IntentUtils.openSite(v.getContext(), "http://codedead.com/")); |
457 | 488 |
|
458 | | - btnSupport.setOnClickListener(v -> ShareCompat.IntentBuilder.from(MainActivity.this) |
| 489 | + btnSupport.setOnClickListener(v -> new ShareCompat.IntentBuilder(MainActivity.this) |
459 | 490 | .setType("message/rfc822") |
460 | 491 | .addEmailTo("admin@codedead.com") |
461 | 492 | .setSubject("DeadHash - Android") |
@@ -577,7 +608,6 @@ private void loadSettingsContent() { |
577 | 608 | lang = "ru"; |
578 | 609 | } |
579 | 610 |
|
580 | | - |
581 | 611 | final int checkedRadioButtonId = group.getCheckedRadioButtonId(); |
582 | 612 | int themeIndex = 0; |
583 | 613 | if (checkedRadioButtonId == R.id.RdbLightTheme) { |
@@ -683,34 +713,4 @@ public boolean onNavigationItemSelected(@NonNull final MenuItem item) { |
683 | 713 | drawer.closeDrawer(GravityCompat.START); |
684 | 714 | return true; |
685 | 715 | } |
686 | | - |
687 | | - @Override |
688 | | - protected void onActivityResult(final int requestCode, final int resultCode, @Nullable final Intent data) { |
689 | | - super.onActivityResult(requestCode, resultCode, data); |
690 | | - if (requestCode == 123 && resultCode == RESULT_OK) { |
691 | | - if (data != null) { |
692 | | - final Uri selectedFileUri = data.getData(); |
693 | | - if (selectedFileUri != null) { |
694 | | - try (final InputStream selectedFileStream = getContentResolver().openInputStream(selectedFileUri)) { |
695 | | - final File outputFile = new File(getApplicationContext().getCacheDir(), tmpFile); |
696 | | - |
697 | | - try (final FileOutputStream outputStream = new FileOutputStream(outputFile, false)) { |
698 | | - if (selectedFileStream != null) { |
699 | | - StreamUtility.copyStream(selectedFileStream, outputStream); |
700 | | - edtFilePath.setText(selectedFileUri.getPath()); |
701 | | - } else { |
702 | | - Toast.makeText(this, R.string.error_open_file, Toast.LENGTH_SHORT).show(); |
703 | | - } |
704 | | - } catch (final IOException ex) { |
705 | | - Toast.makeText(this, R.string.error_copy_file, Toast.LENGTH_SHORT).show(); |
706 | | - } |
707 | | - } catch (final IOException ex) { |
708 | | - Toast.makeText(this, R.string.error_open_file, Toast.LENGTH_SHORT).show(); |
709 | | - } |
710 | | - } else { |
711 | | - Toast.makeText(this, R.string.error_open_file, Toast.LENGTH_SHORT).show(); |
712 | | - } |
713 | | - } |
714 | | - } |
715 | | - } |
716 | 716 | } |
0 commit comments