|
13 | 13 | import android.content.pm.PackageManager; |
14 | 14 | import android.view.View; |
15 | 15 | import android.view.ViewGroup; |
| 16 | +import android.view.Window; |
16 | 17 | import android.view.WindowInsets; |
| 18 | +import android.view.WindowManager; |
17 | 19 | import android.widget.Button; |
18 | 20 | import android.widget.TextView; |
19 | 21 | import android.widget.Toast; |
@@ -48,6 +50,10 @@ public void onCreate(Bundle savedInstanceState) { |
48 | 50 | TextView hintTextView = (TextView) findViewById(R.id.hint); |
49 | 51 | hintTextView.setText(text); |
50 | 52 |
|
| 53 | + // secure this window |
| 54 | + boolean secure = !intent.getBooleanExtra("allow_screenshots", false); |
| 55 | + setSecureWindow(secure); |
| 56 | + |
51 | 57 | // bind "paste" button |
52 | 58 | Button btn = (Button) findViewById(R.id.paste_btn); |
53 | 59 | btn.setOnClickListener(new View.OnClickListener() { |
@@ -209,4 +215,43 @@ private void setupEdgeToEdge() { |
209 | 215 | return insets; |
210 | 216 | }); |
211 | 217 | } |
| 218 | + |
| 219 | + public void setSecureWindow(boolean secure) { |
| 220 | + runOnUiThread(new Runnable() { |
| 221 | + private Activity mActivity; |
| 222 | + private boolean mEnable; |
| 223 | + |
| 224 | + public Runnable _initialize(Activity activity, boolean enable) { |
| 225 | + this.mActivity = activity; |
| 226 | + this.mEnable = enable; |
| 227 | + return this; |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public void run() { |
| 232 | + Window window = this.mActivity.getWindow(); |
| 233 | + |
| 234 | + if ( !((window.getAttributes().flags & WindowManager.LayoutParams.FLAG_SECURE) != 0) ^ mEnable) |
| 235 | + return; // no change needed |
| 236 | + |
| 237 | + if (mEnable) { |
| 238 | + Log.v(TAG, "Setting Secure Window"); |
| 239 | + window.setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE); |
| 240 | + } else { |
| 241 | + Log.v(TAG, "UnSetting Secure Window"); |
| 242 | + window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE); |
| 243 | + // The below forces a redraw of the window/view, which is needed on some phones |
| 244 | + // to actually clear the flag. However on some other phones, it crashes the app... |
| 245 | + // see https://github.com/spesmilo/electrum/issues/8522 |
| 246 | + /*if (ViewCompat.isAttachedToWindow(window.getDecorView())) { |
| 247 | + WindowManager wm = this.mActivity.getWindowManager(); |
| 248 | + wm.removeViewImmediate(window.getDecorView()); |
| 249 | + wm.addView(window.getDecorView(), window.getAttributes()); |
| 250 | + }*/ |
| 251 | + } |
| 252 | + } |
| 253 | + }._initialize(this, secure)); |
| 254 | + } |
| 255 | + |
| 256 | + |
212 | 257 | } |
0 commit comments