Fix toast is covered by PIN code screen after wrong pin

The error toast is displayed when user input wrong PIN
code three times, but at that moment the SIM PUK code input
page in SystemUI pop up and covers the toast, so the user
can't seen the error message.

Adjust the toast window layer, let the the toast pop up
over the SIM pin code input page.

Bug: 74056527
Change-Id: I569a2fc2adac15c4713b4c95e9720335bd4df88a
diff --git a/src/com/android/settings/IccLockSettings.java b/src/com/android/settings/IccLockSettings.java
index 2cb4904..1de7a10 100644
--- a/src/com/android/settings/IccLockSettings.java
+++ b/src/com/android/settings/IccLockSettings.java
@@ -20,7 +20,9 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.res.Configuration;
 import android.content.res.Resources;
+import android.graphics.PixelFormat;
 import android.os.AsyncResult;
 import android.os.Bundle;
 import android.os.Handler;
@@ -30,9 +32,11 @@
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.util.Log;
+import android.view.Gravity;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.WindowManager;
 import android.widget.EditText;
 import android.widget.ListView;
 import android.widget.TabHost;
@@ -40,6 +44,7 @@
 import android.widget.TabHost.TabContentFactory;
 import android.widget.TabHost.TabSpec;
 import android.widget.TabWidget;
+import android.widget.TextView;
 import android.widget.Toast;
 
 import androidx.preference.Preference;
@@ -118,6 +123,9 @@
     private static final int MSG_CHANGE_ICC_PIN_COMPLETE = 101;
     private static final int MSG_SIM_STATE_CHANGED = 102;
 
+    // @see android.widget.Toast$TN
+    private static final long LONG_DURATION_TIMEOUT = 7000;
+
     // For replies from IccCard interface
     private Handler mHandler = new Handler() {
         public void handleMessage(Message msg) {
@@ -461,8 +469,7 @@
             if (exception instanceof CommandException) {
                 CommandException.Error err = ((CommandException)(exception)).getCommandError();
                 if (err == CommandException.Error.PASSWORD_INCORRECT) {
-                    Toast.makeText(getContext(), getPinPasswordErrorMessage(attemptsRemaining),
-                            Toast.LENGTH_LONG).show();
+                    createCustomTextToast(getPinPasswordErrorMessage(attemptsRemaining));
                 } else {
                     if (mToState) {
                         Toast.makeText(getContext(), mRes.getString
@@ -478,11 +485,56 @@
         resetDialogState();
     }
 
+    private void createCustomTextToast(CharSequence errorMessage) {
+        // Cannot overlay Toast on PUK unlock screen.
+        // The window type of Toast is set by NotificationManagerService.
+        // It can't be overwritten by LayoutParams.type.
+        // Ovarlay a custom window with LayoutParams (TYPE_STATUS_BAR_PANEL) on PUK unlock screen.
+        View v = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
+                .inflate(com.android.internal.R.layout.transient_notification, null);
+        TextView tv = (TextView) v.findViewById(com.android.internal.R.id.message);
+        tv.setText(errorMessage);
+
+        final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
+        final Configuration config = v.getContext().getResources().getConfiguration();
+        final int gravity = Gravity.getAbsoluteGravity(
+                getContext().getResources().getInteger(
+                        com.android.internal.R.integer.config_toastDefaultGravity),
+                config.getLayoutDirection());
+        params.gravity = gravity;
+        if ((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) == Gravity.FILL_HORIZONTAL) {
+            params.horizontalWeight = 1.0f;
+        }
+        if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.FILL_VERTICAL) {
+            params.verticalWeight = 1.0f;
+        }
+        params.y = getContext().getResources().getDimensionPixelSize(
+                com.android.internal.R.dimen.toast_y_offset);
+
+        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
+        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
+        params.format = PixelFormat.TRANSLUCENT;
+        params.windowAnimations = com.android.internal.R.style.Animation_Toast;
+        params.type = WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL;
+        params.setTitle(errorMessage);
+        params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
+                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
+                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
+
+        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
+        wm.addView(v, params);
+
+        mHandler.postDelayed(new Runnable() {
+            @Override
+            public void run() {
+                wm.removeViewImmediate(v);
+            }
+        }, LONG_DURATION_TIMEOUT);
+    }
+
     private void iccPinChanged(boolean success, int attemptsRemaining) {
         if (!success) {
-            Toast.makeText(getContext(), getPinPasswordErrorMessage(attemptsRemaining),
-                    Toast.LENGTH_LONG)
-                    .show();
+            createCustomTextToast(getPinPasswordErrorMessage(attemptsRemaining));
         } else {
             Toast.makeText(getContext(), mRes.getString(R.string.sim_change_succeeded),
                     Toast.LENGTH_SHORT)