Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-1054 handle all deprecation warnings #1072

Merged
merged 3 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.WindowInsetsControllerCompat;

public class CatimaAppCompatActivity extends AppCompatActivity {
@Override
Expand All @@ -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));
Expand Down
16 changes: 8 additions & 8 deletions app/src/main/java/protect/card_locker/ImportExportActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
74 changes: 57 additions & 17 deletions app/src/main/java/protect/card_locker/ImportExportTask.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,22 +32,24 @@ public class ImportExportTask implements CompatCallable<ImportExportResult> {
private static final String TAG = "Catima";

private Activity activity;
private Context context;
private boolean doImport;
private DataFormat format;
private OutputStream outputStream;
private InputStream inputStream;
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;
Expand All @@ -51,9 +61,10 @@ public class ImportExportTask implements CompatCallable<ImportExportResult> {
* 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;
Expand All @@ -63,7 +74,6 @@ public class ImportExportTask implements CompatCallable<ImportExportResult> {

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;
Expand All @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you're fixing the ProgressDialog being deprecated here by recreating the ProgressDialog in a MaterialAlertDialog?

While that does remove the deprecation warning, it doesn't solve the real issue: importing is done on the main thread, locking up the UI and breaking if the user navigates away and Android stops the activity.

Because this doesn't fix the real issue and just adds a lot of extra code, I'd rather you just leave the ProgressDialog here and cause the in this case very correct deprecation warning to remain shown. #513 exists to track this specific issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted changes

}

protected ImportExportResult doInBackground(Void... nothing) {
Expand All @@ -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");
}

Expand Down
69 changes: 54 additions & 15 deletions app/src/main/java/protect/card_locker/LoyaltyCardViewActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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
);
}
}
5 changes: 4 additions & 1 deletion app/src/main/java/protect/card_locker/UCropWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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) {
Expand Down
Loading