Home | History | Annotate | Download | only in tapjacking
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.cts.verifier.admin.tapjacking;
     18 
     19 import android.content.Intent;
     20 import android.graphics.PixelFormat;
     21 import android.os.Bundle;
     22 import android.provider.Settings;
     23 import android.util.TypedValue;
     24 import android.view.Gravity;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.view.WindowManager;
     28 import android.widget.Button;
     29 import android.widget.Toast;
     30 
     31 import com.android.cts.verifier.PassFailButtons;
     32 import com.android.cts.verifier.R;
     33 
     34 public class UsbTest extends PassFailButtons.Activity {
     35     private View mOverlay;
     36     private Button mTriggerOverlayButton;
     37 
     38     public static final String LOG_TAG = "UsbTest";
     39 
     40     @Override
     41     protected void onCreate(Bundle savedInstanceState) {
     42         super.onCreate(savedInstanceState);
     43         setContentView(R.layout.tapjacking);
     44         setPassFailButtonClickListeners();
     45         setInfoResources(R.string.usb_tapjacking_test,
     46                 R.string.usb_tapjacking_test_info, -1);
     47 
     48         //initialise the escalate button and set a listener
     49         mTriggerOverlayButton = (Button) findViewById(R.id.tapjacking_btn);
     50         mTriggerOverlayButton.setEnabled(true);
     51         mTriggerOverlayButton.setOnClickListener(new View.OnClickListener() {
     52             @Override
     53             public void onClick(View v) {
     54                 if (!Settings.canDrawOverlays(v.getContext())) {
     55                     // show settings permission
     56                     startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION));
     57                 }
     58 
     59                 if (!Settings.canDrawOverlays(v.getContext())) {
     60                     Toast.makeText(v.getContext(), R.string.usb_tapjacking_error_toast2,
     61                             Toast.LENGTH_LONG).show();
     62                     return;
     63                 }
     64                 showOverlay();
     65             }
     66         });
     67     }
     68 
     69     private void showOverlay() {
     70         if (mOverlay != null)
     71             return;
     72 
     73         WindowManager windowManager = (WindowManager) getApplicationContext().
     74                 getSystemService(WINDOW_SERVICE);
     75         WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
     76                 WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
     77                 WindowManager.LayoutParams.FLAG_FULLSCREEN
     78                         | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
     79                         | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
     80                         | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
     81                         | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
     82         );
     83         layoutParams.format = PixelFormat.TRANSLUCENT;
     84         layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
     85         layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
     86         layoutParams.x = 0;
     87         layoutParams.y = dipToPx(-46);
     88         layoutParams.gravity = Gravity.CENTER;
     89         layoutParams.windowAnimations = 0;
     90 
     91         mOverlay = View.inflate(getApplicationContext(), R.layout.usb_tapjacking_overlay,
     92                 null);
     93         windowManager.addView(mOverlay, layoutParams);
     94     }
     95 
     96     private void hideOverlay() {
     97         if (mOverlay != null) {
     98             WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(
     99                     WINDOW_SERVICE);
    100             windowManager.removeViewImmediate(mOverlay);
    101             mOverlay = null;
    102         }
    103     }
    104 
    105     private int dipToPx(int dip) {
    106         return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip,
    107                 getResources().getDisplayMetrics()));
    108     }
    109 
    110     @Override
    111     public void onStop() {
    112         super.onStop();
    113         hideOverlay();
    114     }
    115 }
    116