From 0aab37a14ad4454e0be408b333d4cc8052c362bf Mon Sep 17 00:00:00 2001 From: Pinkolik Date: Fri, 7 Oct 2022 10:17:50 +0500 Subject: [PATCH 1/3] issue-1054 handle all deprecation warnings --- .../CardsOnPowerScreenService.java | 2 +- .../card_locker/CatimaAppCompatActivity.java | 5 +- .../card_locker/ImportExportActivity.java | 16 ++-- .../protect/card_locker/ImportExportTask.java | 74 ++++++++++++++----- .../card_locker/LoyaltyCardViewActivity.java | 69 +++++++++++++---- .../protect/card_locker/UCropWrapper.java | 5 +- .../main/java/protect/card_locker/Utils.java | 18 ++++- .../preferences/SettingsActivity.java | 7 ++ .../protect/card_locker/ImportExportTest.java | 25 ++++--- 9 files changed, 165 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/protect/card_locker/CardsOnPowerScreenService.java b/app/src/main/java/protect/card_locker/CardsOnPowerScreenService.java index c86c62d251..9cce5a1e25 100644 --- a/app/src/main/java/protect/card_locker/CardsOnPowerScreenService.java +++ b/app/src/main/java/protect/card_locker/CardsOnPowerScreenService.java @@ -137,7 +137,7 @@ public void performControlAction(@NonNull String controlId, @NonNull ControlActi closePowerScreenOnAndroid11(); } - @SuppressLint({"MissingPermission", "deprecation"}) + @SuppressWarnings({"MissingPermission", "deprecation"}) private void closePowerScreenOnAndroid11() { // Android 12 will auto-close the power screen, but earlier versions won't // Lint complains about this but on Android 11 the permission is not needed diff --git a/app/src/main/java/protect/card_locker/CatimaAppCompatActivity.java b/app/src/main/java/protect/card_locker/CatimaAppCompatActivity.java index 8fc5d9409e..37cd5671ac 100644 --- a/app/src/main/java/protect/card_locker/CatimaAppCompatActivity.java +++ b/app/src/main/java/protect/card_locker/CatimaAppCompatActivity.java @@ -8,6 +8,7 @@ import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; +import androidx.core.view.WindowInsetsControllerCompat; public class CatimaAppCompatActivity extends AppCompatActivity { @Override @@ -29,8 +30,10 @@ protected void onPostCreate(@Nullable Bundle savedInstanceState) { // XXX changing this in onCreate causes issues with the splash screen activity, so doing this here boolean darkMode = Utils.isDarkModeEnabled(this); if (Build.VERSION.SDK_INT >= 23) { + View decorView = getWindow().getDecorView(); + WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(getWindow(), decorView); + wic.setAppearanceLightStatusBars(!darkMode); getWindow().setStatusBarColor(Color.TRANSPARENT); - getWindow().getDecorView().setSystemUiVisibility(darkMode ? 0 : View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); } else { // icons are always white back then getWindow().setStatusBarColor(darkMode ? Color.TRANSPARENT : Color.argb(127, 0, 0, 0)); diff --git a/app/src/main/java/protect/card_locker/ImportExportActivity.java b/app/src/main/java/protect/card_locker/ImportExportActivity.java index 4cea860135..0599f03899 100644 --- a/app/src/main/java/protect/card_locker/ImportExportActivity.java +++ b/app/src/main/java/protect/card_locker/ImportExportActivity.java @@ -16,12 +16,6 @@ import android.widget.FrameLayout; import android.widget.Toast; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.List; - import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.ActionBar; @@ -32,6 +26,12 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; + import protect.card_locker.async.TaskHandler; import protect.card_locker.databinding.ImportExportActivityBinding; import protect.card_locker.importexport.DataFormat; @@ -277,7 +277,7 @@ public void onTaskComplete(ImportExportResult result, DataFormat dataFormat) { }; importExporter = new ImportExportTask(ImportExportActivity.this, - dataFormat, target, password, listener); + dataFormat, target, password, listener, this); mTasks.executeTask(TaskHandler.TYPE.IMPORT, importExporter); } @@ -298,7 +298,7 @@ public void onTaskComplete(ImportExportResult result, DataFormat dataFormat) { }; importExporter = new ImportExportTask(ImportExportActivity.this, - DataFormat.Catima, target, password, listener); + DataFormat.Catima, target, password, listener, this); mTasks.executeTask(TaskHandler.TYPE.EXPORT, importExporter); } diff --git a/app/src/main/java/protect/card_locker/ImportExportTask.java b/app/src/main/java/protect/card_locker/ImportExportTask.java index 8284edff72..b86bfdbeca 100644 --- a/app/src/main/java/protect/card_locker/ImportExportTask.java +++ b/app/src/main/java/protect/card_locker/ImportExportTask.java @@ -1,11 +1,19 @@ package protect.card_locker; import android.app.Activity; -import android.app.ProgressDialog; import android.content.Context; -import android.content.DialogInterface; import android.database.sqlite.SQLiteDatabase; +import android.graphics.Color; import android.util.Log; +import android.view.Gravity; +import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.ProgressBar; +import android.widget.TextView; + +import androidx.appcompat.app.AlertDialog; + +import com.google.android.material.dialog.MaterialAlertDialogBuilder; import java.io.IOException; import java.io.InputStream; @@ -24,6 +32,7 @@ public class ImportExportTask implements CompatCallable { private static final String TAG = "Catima"; private Activity activity; + private Context context; private boolean doImport; private DataFormat format; private OutputStream outputStream; @@ -31,15 +40,16 @@ public class ImportExportTask implements CompatCallable { private char[] password; private TaskCompleteListener listener; - private ProgressDialog progress; + private AlertDialog dialog; /** * Constructor which will setup a task for exporting to the given file */ ImportExportTask(Activity activity, DataFormat format, OutputStream output, char[] password, - TaskCompleteListener listener) { + TaskCompleteListener listener, Context context) { super(); this.activity = activity; + this.context = context; this.doImport = false; this.format = format; this.outputStream = output; @@ -51,9 +61,10 @@ public class ImportExportTask implements CompatCallable { * Constructor which will setup a task for importing from the given InputStream. */ ImportExportTask(Activity activity, DataFormat format, InputStream input, char[] password, - TaskCompleteListener listener) { + TaskCompleteListener listener, Context context) { super(); this.activity = activity; + this.context = context; this.doImport = true; this.format = format; this.inputStream = input; @@ -63,7 +74,6 @@ public class ImportExportTask implements CompatCallable { private ImportExportResult performImport(Context context, InputStream stream, SQLiteDatabase database, char[] password) { ImportExportResult importResult = MultiFormatImporter.importData(context, database, stream, format, password); - Log.i(TAG, "Import result: " + importResult); return importResult; @@ -87,17 +97,47 @@ private ImportExportResult performExport(Context context, OutputStream stream, S } public void onPreExecute() { - progress = new ProgressDialog(activity); - progress.setTitle(doImport ? R.string.importing : R.string.exporting); + int llPadding = 30; + LinearLayout ll = new LinearLayout(context); + ll.setOrientation(LinearLayout.HORIZONTAL); + ll.setPadding(llPadding, llPadding, llPadding, llPadding); + ll.setGravity(Gravity.CENTER); + LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, + LinearLayout.LayoutParams.WRAP_CONTENT); + llParam.gravity = Gravity.CENTER; + ll.setLayoutParams(llParam); + + ProgressBar progressBar = new ProgressBar(context); + progressBar.setIndeterminate(true); + progressBar.setPadding(0, 0, llPadding, 0); + progressBar.setLayoutParams(llParam); + + llParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + llParam.gravity = Gravity.CENTER; + TextView tvText = new TextView(context); + tvText.setText(doImport ? R.string.importing : R.string.exporting); + + int textColor; + if (Utils.isDarkModeEnabled(context)) { + textColor = Color.WHITE; + } else { + textColor = Color.BLACK; + } + tvText.setTextColor(textColor); + tvText.setTextSize(20); + tvText.setLayoutParams(llParam); + + ll.addView(progressBar); + ll.addView(tvText); - progress.setOnDismissListener(new DialogInterface.OnDismissListener() { - @Override - public void onDismiss(DialogInterface dialog) { - ImportExportTask.this.stop(); - } - }); + AlertDialog.Builder builder = new MaterialAlertDialogBuilder(context); + builder.setCancelable(true); + builder.setView(ll); - progress.show(); + dialog = builder.create(); + dialog.show(); } protected ImportExportResult doInBackground(Void... nothing) { @@ -118,12 +158,12 @@ protected ImportExportResult doInBackground(Void... nothing) { public void onPostExecute(Object castResult) { listener.onTaskComplete((ImportExportResult) castResult, format); - progress.dismiss(); + dialog.dismiss(); Log.i(TAG, (doImport ? "Import" : "Export") + " Complete"); } protected void onCancelled() { - progress.dismiss(); + dialog.dismiss(); Log.i(TAG, (doImport ? "Import" : "Export") + " Cancelled"); } diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java index 7ad403bea2..9192d4416f 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java @@ -28,6 +28,8 @@ import android.view.ViewOutlineProvider; import android.view.ViewTreeObserver; import android.view.Window; +import android.view.WindowInsets; +import android.view.WindowInsetsController; import android.view.WindowManager; import android.widget.ImageButton; import android.widget.ImageView; @@ -49,6 +51,8 @@ import androidx.core.graphics.BlendModeCompat; import androidx.core.graphics.ColorUtils; import androidx.core.graphics.drawable.DrawableCompat; +import androidx.core.view.WindowCompat; +import androidx.core.view.WindowInsetsControllerCompat; import androidx.core.widget.TextViewCompat; import com.google.android.material.appbar.AppBarLayout; @@ -189,8 +193,7 @@ private void openCurrentMainImageInGallery() { .setDataAndType(FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, file), "image/*") .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(intent); - } - catch (ActivityNotFoundException e) { + } catch (ActivityNotFoundException e) { // Display a toast message if an image viewer is not installed on device Toast.makeText(this, R.string.failedLaunchingPhotoPicker, Toast.LENGTH_SHORT).show(); e.printStackTrace(); @@ -610,8 +613,11 @@ public void onResume() { } if (settings.getDisableLockscreenWhileViewingCard()) { - window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | - WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + setShowWhenLocked(true); + } else { + showWhenLockedSdkLessThan27(window); + } } window.setAttributes(attributes); @@ -707,7 +713,9 @@ public void onResume() { // Make notification area light if dark icons are needed if (Build.VERSION.SDK_INT >= 23) { - window.getDecorView().setSystemUiVisibility(backgroundNeedsDarkIcons ? View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR : 0); + View decorView = getWindow().getDecorView(); + WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(getWindow(), decorView); + wic.setAppearanceLightStatusBars(backgroundNeedsDarkIcons); window.setStatusBarColor(Color.TRANSPARENT); } else { // Darken statusbar if icons won't be visible otherwise @@ -748,6 +756,12 @@ public void onResume() { DBHelper.updateLoyaltyCardLastUsed(database, loyaltyCard.id); } + @SuppressWarnings("deprecation") + private void showWhenLockedSdkLessThan27(Window window) { + window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | + WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); + } + private void fixImageButtonColor(ImageButton imageButton) { imageButton.setColorFilter(BlendModeColorFilterCompat.createBlendModeColorFilterCompat(backgroundNeedsDarkIcons ? Color.BLACK : Color.WHITE, BlendModeCompat.SRC_ATOP)); } @@ -1047,11 +1061,15 @@ private void setFullscreen(boolean enabled) { editButton.setVisibility(View.GONE); // Set Android to fullscreen mode - getWindow().getDecorView().setSystemUiVisibility( - getWindow().getDecorView().getSystemUiVisibility() - | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY - | View.SYSTEM_UI_FLAG_FULLSCREEN - ); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + getWindow().setDecorFitsSystemWindows(false); + if (getWindow().getInsetsController() != null) { + getWindow().getInsetsController().hide(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars()); + getWindow().getInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE); + } + } else { + setFullscreenModeSdkLessThan30(); + } } else { Log.d(TAG, "Move out of fullscreen"); @@ -1082,13 +1100,34 @@ private void setFullscreen(boolean enabled) { bottomAppBar.setVisibility(View.VISIBLE); // Unset fullscreen mode - getWindow().getDecorView().setSystemUiVisibility( - getWindow().getDecorView().getSystemUiVisibility() - & ~View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY - & ~View.SYSTEM_UI_FLAG_FULLSCREEN - ); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + getWindow().setDecorFitsSystemWindows(true); + if (getWindow().getInsetsController() != null) { + getWindow().getInsetsController().show(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars()); + } + } else { + unsetFullscreenModeSdkLessThan30(); + } } Log.d("setFullScreen", "Is full screen enabled? " + enabled + " Zoom Level = " + barcodeScaler.getProgress()); } + + @SuppressWarnings("deprecation") + private void unsetFullscreenModeSdkLessThan30() { + getWindow().getDecorView().setSystemUiVisibility( + getWindow().getDecorView().getSystemUiVisibility() + & ~View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY + & ~View.SYSTEM_UI_FLAG_FULLSCREEN + ); + } + + @SuppressWarnings("deprecation") + private void setFullscreenModeSdkLessThan30() { + getWindow().getDecorView().setSystemUiVisibility( + getWindow().getDecorView().getSystemUiVisibility() + | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY + | View.SYSTEM_UI_FLAG_FULLSCREEN + ); + } } diff --git a/app/src/main/java/protect/card_locker/UCropWrapper.java b/app/src/main/java/protect/card_locker/UCropWrapper.java index af5f3c07ce..3c5fea3a5f 100644 --- a/app/src/main/java/protect/card_locker/UCropWrapper.java +++ b/app/src/main/java/protect/card_locker/UCropWrapper.java @@ -18,6 +18,7 @@ import androidx.appcompat.widget.AppCompatImageView; import androidx.core.content.ContextCompat; import androidx.core.graphics.ColorUtils; +import androidx.core.view.WindowInsetsControllerCompat; public class UCropWrapper extends UCropActivity { public static final String UCROP_TOOLBAR_TYPEFACE_STYLE = "ucop_toolbar_typeface_style"; @@ -28,7 +29,9 @@ protected void onPostCreate(@Nullable Bundle savedInstanceState) { boolean darkMode = Utils.isDarkModeEnabled(this); // setup status bar to look like the rest of the app if (Build.VERSION.SDK_INT >= 23) { - getWindow().getDecorView().setSystemUiVisibility(darkMode ? 0 : View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR); + View decorView = getWindow().getDecorView(); + WindowInsetsControllerCompat wic = new WindowInsetsControllerCompat(getWindow(), decorView); + wic.setAppearanceLightStatusBars(!darkMode); } else { // icons are always white back then if (!darkMode) { diff --git a/app/src/main/java/protect/card_locker/Utils.java b/app/src/main/java/protect/card_locker/Utils.java index 5db5f5214a..fe98efa7cd 100644 --- a/app/src/main/java/protect/card_locker/Utils.java +++ b/app/src/main/java/protect/card_locker/Utils.java @@ -122,7 +122,7 @@ static public BarcodeValues parseSetBarcodeActivityResult(int requestCode, int r ImageDecoder.Source image_source = ImageDecoder.createSource(context.getContentResolver(), intent.getData()); bitmap = ImageDecoder.decodeBitmap(image_source, (decoder, info, source) -> decoder.setMutableRequired(true)); } else { - bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), intent.getData()); + bitmap = getBitmapSdkLessThan29(intent, context); } } catch (IOException e) { Log.e(TAG, "Error getting data from image file"); @@ -163,6 +163,11 @@ static public BarcodeValues parseSetBarcodeActivityResult(int requestCode, int r throw new UnsupportedOperationException("Unknown request code for parseSetBarcodeActivityResult"); } + @SuppressWarnings("deprecation") + private static Bitmap getBitmapSdkLessThan29(Intent intent, Context context) throws IOException { + return MediaStore.Images.Media.getBitmap(context.getContentResolver(), intent.getData()); + } + static public BarcodeValues getBarcodeFromBitmap(Bitmap bitmap) { // This function is vulnerable to OOM, so we try again with a smaller bitmap is we get OOM for (int i = 0; i < 10; i++) { @@ -396,8 +401,7 @@ static public Context updateBaseContextLocale(Context context) { Resources res = context.getResources(); Configuration configuration = res.getConfiguration(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { - configuration.locale = chosenLocale != null ? chosenLocale : Locale.getDefault(); - res.updateConfiguration(configuration, res.getDisplayMetrics()); + setLocalesSdkLessThan24(chosenLocale, configuration, res); return context; } @@ -407,6 +411,12 @@ static public Context updateBaseContextLocale(Context context) { return context.createConfigurationContext(configuration); } + @SuppressWarnings("deprecation") + private static void setLocalesSdkLessThan24(Locale chosenLocale, Configuration configuration, Resources res) { + configuration.locale = chosenLocale != null ? chosenLocale : Locale.getDefault(); + res.updateConfiguration(configuration, res.getDisplayMetrics()); + } + static public long getUnixTime() { return System.currentTimeMillis() / 1000; } @@ -488,7 +498,7 @@ public static void patchColors(AppCompatActivity activity) { } else { // final catch all in case of invalid theme value from older versions // also handles R.string.settings_key_system_theme - DynamicColors.applyIfAvailable(activity); + DynamicColors.applyToActivityIfAvailable(activity); } if (isDarkModeEnabled(activity) && settings.getOledDark()) { diff --git a/app/src/main/java/protect/card_locker/preferences/SettingsActivity.java b/app/src/main/java/protect/card_locker/preferences/SettingsActivity.java index 4cc47c1a36..2a5b62cceb 100644 --- a/app/src/main/java/protect/card_locker/preferences/SettingsActivity.java +++ b/app/src/main/java/protect/card_locker/preferences/SettingsActivity.java @@ -17,9 +17,12 @@ import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.widget.Toolbar; import androidx.fragment.app.DialogFragment; +import androidx.fragment.app.FragmentResultListener; import androidx.preference.ListPreference; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; + +import kotlin.Suppress; import nl.invissvenska.numberpickerpreference.NumberDialogPreference; import nl.invissvenska.numberpickerpreference.NumberPickerPreferenceDialogFragment; import protect.card_locker.CatimaAppCompatActivity; @@ -166,6 +169,10 @@ private void refreshActivity(boolean reloadMain) { } @Override + @SuppressWarnings("deprecation") // setTargetFragment + // androidx.preference.PreferenceDialogFragmentCompat uses the deprecated method + // `getTargetFragment()`, which throws if `setTargetFragment()` isn't used before. + // While this isn't fixed on upstream, suppress the deprecation warning public void onDisplayPreferenceDialog(Preference preference) { if (preference instanceof NumberDialogPreference) { NumberDialogPreference dialogPreference = (NumberDialogPreference) preference; diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index b8e5a20a0d..f440d1c2a1 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -1,6 +1,14 @@ package protect.card_locker; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.robolectric.Shadows.shadowOf; + import android.app.Activity; +import android.content.Context; +import android.content.res.Resources; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; @@ -11,6 +19,9 @@ import android.os.Looper; import android.util.DisplayMetrics; +import androidx.core.content.res.ResourcesCompat; +import androidx.test.core.app.ApplicationProvider; + import com.google.zxing.BarcodeFormat; import org.json.JSONException; @@ -44,7 +55,6 @@ import java.util.HashMap; import java.util.List; -import androidx.core.content.res.ResourcesCompat; import protect.card_locker.async.TaskHandler; import protect.card_locker.importexport.DataFormat; import protect.card_locker.importexport.ImportExportResult; @@ -52,16 +62,11 @@ import protect.card_locker.importexport.MultiFormatExporter; import protect.card_locker.importexport.MultiFormatImporter; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.robolectric.Shadows.shadowOf; - @RunWith(RobolectricTestRunner.class) @Config(sdk = 23) public class ImportExportTest { private Activity activity; + private Context context; private SQLiteDatabase mDatabase; private final String BARCODE_DATA = "428311627547"; @@ -72,6 +77,8 @@ public void setUp() { ShadowLog.stream = System.out; activity = Robolectric.setupActivity(MainActivity.class); + context = ApplicationProvider.getApplicationContext(); + context.setTheme(R.style.AppTheme); mDatabase = TestHelpers.getEmptyDb(activity).getWritableDatabase(); } @@ -566,7 +573,7 @@ public void useImportExportTask() throws FileNotFoundException { // Export to the file final String password = "123456789"; FileOutputStream fileOutputStream = new FileOutputStream(exportFile); - ImportExportTask task = new ImportExportTask(activity, DataFormat.Catima, fileOutputStream, password.toCharArray(), listener); + ImportExportTask task = new ImportExportTask(activity, DataFormat.Catima, fileOutputStream, password.toCharArray(), listener, context); TaskHandler mTasks = new TaskHandler(); mTasks.executeTask(TaskHandler.TYPE.EXPORT, task); @@ -588,7 +595,7 @@ public void useImportExportTask() throws FileNotFoundException { FileInputStream fileStream = new FileInputStream(exportFile); - task = new ImportExportTask(activity, DataFormat.Catima, fileStream, password.toCharArray(), listener); + task = new ImportExportTask(activity, DataFormat.Catima, fileStream, password.toCharArray(), listener, context); mTasks.executeTask(TaskHandler.TYPE.IMPORT, task); // Actually run the task to completion From 561662acd4726c0f7272d445ef0e2fa2c6840510 Mon Sep 17 00:00:00 2001 From: Pinkolik Date: Sun, 9 Oct 2022 17:22:25 +0500 Subject: [PATCH 2/3] issue-1054 revert ProgressDialog changes --- .../card_locker/ImportExportActivity.java | 16 ++-- .../protect/card_locker/ImportExportTask.java | 74 +++++-------------- .../protect/card_locker/ImportExportTest.java | 25 +++---- 3 files changed, 34 insertions(+), 81 deletions(-) diff --git a/app/src/main/java/protect/card_locker/ImportExportActivity.java b/app/src/main/java/protect/card_locker/ImportExportActivity.java index 0599f03899..4cea860135 100644 --- a/app/src/main/java/protect/card_locker/ImportExportActivity.java +++ b/app/src/main/java/protect/card_locker/ImportExportActivity.java @@ -16,6 +16,12 @@ import android.widget.FrameLayout; import android.widget.Toast; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.ArrayList; +import java.util.List; + import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.ActionBar; @@ -26,12 +32,6 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.util.ArrayList; -import java.util.List; - import protect.card_locker.async.TaskHandler; import protect.card_locker.databinding.ImportExportActivityBinding; import protect.card_locker.importexport.DataFormat; @@ -277,7 +277,7 @@ public void onTaskComplete(ImportExportResult result, DataFormat dataFormat) { }; importExporter = new ImportExportTask(ImportExportActivity.this, - dataFormat, target, password, listener, this); + dataFormat, target, password, listener); mTasks.executeTask(TaskHandler.TYPE.IMPORT, importExporter); } @@ -298,7 +298,7 @@ public void onTaskComplete(ImportExportResult result, DataFormat dataFormat) { }; importExporter = new ImportExportTask(ImportExportActivity.this, - DataFormat.Catima, target, password, listener, this); + DataFormat.Catima, target, password, listener); mTasks.executeTask(TaskHandler.TYPE.EXPORT, importExporter); } diff --git a/app/src/main/java/protect/card_locker/ImportExportTask.java b/app/src/main/java/protect/card_locker/ImportExportTask.java index b86bfdbeca..8284edff72 100644 --- a/app/src/main/java/protect/card_locker/ImportExportTask.java +++ b/app/src/main/java/protect/card_locker/ImportExportTask.java @@ -1,19 +1,11 @@ package protect.card_locker; import android.app.Activity; +import android.app.ProgressDialog; import android.content.Context; +import android.content.DialogInterface; import android.database.sqlite.SQLiteDatabase; -import android.graphics.Color; import android.util.Log; -import android.view.Gravity; -import android.view.ViewGroup; -import android.widget.LinearLayout; -import android.widget.ProgressBar; -import android.widget.TextView; - -import androidx.appcompat.app.AlertDialog; - -import com.google.android.material.dialog.MaterialAlertDialogBuilder; import java.io.IOException; import java.io.InputStream; @@ -32,7 +24,6 @@ public class ImportExportTask implements CompatCallable { private static final String TAG = "Catima"; private Activity activity; - private Context context; private boolean doImport; private DataFormat format; private OutputStream outputStream; @@ -40,16 +31,15 @@ public class ImportExportTask implements CompatCallable { private char[] password; private TaskCompleteListener listener; - private AlertDialog dialog; + private ProgressDialog progress; /** * Constructor which will setup a task for exporting to the given file */ ImportExportTask(Activity activity, DataFormat format, OutputStream output, char[] password, - TaskCompleteListener listener, Context context) { + TaskCompleteListener listener) { super(); this.activity = activity; - this.context = context; this.doImport = false; this.format = format; this.outputStream = output; @@ -61,10 +51,9 @@ public class ImportExportTask implements CompatCallable { * Constructor which will setup a task for importing from the given InputStream. */ ImportExportTask(Activity activity, DataFormat format, InputStream input, char[] password, - TaskCompleteListener listener, Context context) { + TaskCompleteListener listener) { super(); this.activity = activity; - this.context = context; this.doImport = true; this.format = format; this.inputStream = input; @@ -74,6 +63,7 @@ public class ImportExportTask implements CompatCallable { private ImportExportResult performImport(Context context, InputStream stream, SQLiteDatabase database, char[] password) { ImportExportResult importResult = MultiFormatImporter.importData(context, database, stream, format, password); + Log.i(TAG, "Import result: " + importResult); return importResult; @@ -97,47 +87,17 @@ private ImportExportResult performExport(Context context, OutputStream stream, S } public void onPreExecute() { - int llPadding = 30; - LinearLayout ll = new LinearLayout(context); - ll.setOrientation(LinearLayout.HORIZONTAL); - ll.setPadding(llPadding, llPadding, llPadding, llPadding); - ll.setGravity(Gravity.CENTER); - LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams( - LinearLayout.LayoutParams.WRAP_CONTENT, - LinearLayout.LayoutParams.WRAP_CONTENT); - llParam.gravity = Gravity.CENTER; - ll.setLayoutParams(llParam); - - ProgressBar progressBar = new ProgressBar(context); - progressBar.setIndeterminate(true); - progressBar.setPadding(0, 0, llPadding, 0); - progressBar.setLayoutParams(llParam); - - llParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, - ViewGroup.LayoutParams.WRAP_CONTENT); - llParam.gravity = Gravity.CENTER; - TextView tvText = new TextView(context); - tvText.setText(doImport ? R.string.importing : R.string.exporting); - - int textColor; - if (Utils.isDarkModeEnabled(context)) { - textColor = Color.WHITE; - } else { - textColor = Color.BLACK; - } - tvText.setTextColor(textColor); - tvText.setTextSize(20); - tvText.setLayoutParams(llParam); - - ll.addView(progressBar); - ll.addView(tvText); + progress = new ProgressDialog(activity); + progress.setTitle(doImport ? R.string.importing : R.string.exporting); - AlertDialog.Builder builder = new MaterialAlertDialogBuilder(context); - builder.setCancelable(true); - builder.setView(ll); + progress.setOnDismissListener(new DialogInterface.OnDismissListener() { + @Override + public void onDismiss(DialogInterface dialog) { + ImportExportTask.this.stop(); + } + }); - dialog = builder.create(); - dialog.show(); + progress.show(); } protected ImportExportResult doInBackground(Void... nothing) { @@ -158,12 +118,12 @@ protected ImportExportResult doInBackground(Void... nothing) { public void onPostExecute(Object castResult) { listener.onTaskComplete((ImportExportResult) castResult, format); - dialog.dismiss(); + progress.dismiss(); Log.i(TAG, (doImport ? "Import" : "Export") + " Complete"); } protected void onCancelled() { - dialog.dismiss(); + progress.dismiss(); Log.i(TAG, (doImport ? "Import" : "Export") + " Cancelled"); } diff --git a/app/src/test/java/protect/card_locker/ImportExportTest.java b/app/src/test/java/protect/card_locker/ImportExportTest.java index f440d1c2a1..b8e5a20a0d 100644 --- a/app/src/test/java/protect/card_locker/ImportExportTest.java +++ b/app/src/test/java/protect/card_locker/ImportExportTest.java @@ -1,14 +1,6 @@ package protect.card_locker; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.robolectric.Shadows.shadowOf; - import android.app.Activity; -import android.content.Context; -import android.content.res.Resources; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; @@ -19,9 +11,6 @@ import android.os.Looper; import android.util.DisplayMetrics; -import androidx.core.content.res.ResourcesCompat; -import androidx.test.core.app.ApplicationProvider; - import com.google.zxing.BarcodeFormat; import org.json.JSONException; @@ -55,6 +44,7 @@ import java.util.HashMap; import java.util.List; +import androidx.core.content.res.ResourcesCompat; import protect.card_locker.async.TaskHandler; import protect.card_locker.importexport.DataFormat; import protect.card_locker.importexport.ImportExportResult; @@ -62,11 +52,16 @@ import protect.card_locker.importexport.MultiFormatExporter; import protect.card_locker.importexport.MultiFormatImporter; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.robolectric.Shadows.shadowOf; + @RunWith(RobolectricTestRunner.class) @Config(sdk = 23) public class ImportExportTest { private Activity activity; - private Context context; private SQLiteDatabase mDatabase; private final String BARCODE_DATA = "428311627547"; @@ -77,8 +72,6 @@ public void setUp() { ShadowLog.stream = System.out; activity = Robolectric.setupActivity(MainActivity.class); - context = ApplicationProvider.getApplicationContext(); - context.setTheme(R.style.AppTheme); mDatabase = TestHelpers.getEmptyDb(activity).getWritableDatabase(); } @@ -573,7 +566,7 @@ public void useImportExportTask() throws FileNotFoundException { // Export to the file final String password = "123456789"; FileOutputStream fileOutputStream = new FileOutputStream(exportFile); - ImportExportTask task = new ImportExportTask(activity, DataFormat.Catima, fileOutputStream, password.toCharArray(), listener, context); + ImportExportTask task = new ImportExportTask(activity, DataFormat.Catima, fileOutputStream, password.toCharArray(), listener); TaskHandler mTasks = new TaskHandler(); mTasks.executeTask(TaskHandler.TYPE.EXPORT, task); @@ -595,7 +588,7 @@ public void useImportExportTask() throws FileNotFoundException { FileInputStream fileStream = new FileInputStream(exportFile); - task = new ImportExportTask(activity, DataFormat.Catima, fileStream, password.toCharArray(), listener, context); + task = new ImportExportTask(activity, DataFormat.Catima, fileStream, password.toCharArray(), listener); mTasks.executeTask(TaskHandler.TYPE.IMPORT, task); // Actually run the task to completion From 932a8c4df899edda90e51ef5b231a46a7201c9b5 Mon Sep 17 00:00:00 2001 From: Pinkolik Date: Sun, 9 Oct 2022 17:28:18 +0500 Subject: [PATCH 3/3] issue-1054 add link to issue --- .../java/protect/card_locker/preferences/SettingsActivity.java | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/protect/card_locker/preferences/SettingsActivity.java b/app/src/main/java/protect/card_locker/preferences/SettingsActivity.java index 2a5b62cceb..25e0226fd5 100644 --- a/app/src/main/java/protect/card_locker/preferences/SettingsActivity.java +++ b/app/src/main/java/protect/card_locker/preferences/SettingsActivity.java @@ -173,6 +173,7 @@ private void refreshActivity(boolean reloadMain) { // androidx.preference.PreferenceDialogFragmentCompat uses the deprecated method // `getTargetFragment()`, which throws if `setTargetFragment()` isn't used before. // While this isn't fixed on upstream, suppress the deprecation warning + // https://issuetracker.google.com/issues/181793702 public void onDisplayPreferenceDialog(Preference preference) { if (preference instanceof NumberDialogPreference) { NumberDialogPreference dialogPreference = (NumberDialogPreference) preference;