3

What I want to do is add a view on top of my application which will be sort of a filter view (I want to manipulate the colors of the screen) and I also want to be able to change the brightness of the screen at the same time. Both of these things seem to work separately, but not together.

Here is my code:

Adding view:

colourView = new Layer(cordova.getActivity());

WindowManager localWindowManager = (WindowManager) cordova.getActivity().getWindowManager();
LayoutParams layoutParams = cordova.getActivity().getWindow().getAttributes();
layoutParams.format = PixelFormat.TRANSLUCENT;

layoutParams.type=LayoutParams.TYPE_SYSTEM_ALERT;
layoutParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCHABLE;
layoutParams.gravity=Gravity.LEFT|Gravity.TOP; 

localWindowManager.addView(colourView, layoutParams);

Layer class:

class Layer extends View
{
      private int a = 0;
      private int b = 0;
      private int g = 0;
      private int r = 0;

      public Layer(Context context){
        super(context);
      }

      @Override
      protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        canvas.drawARGB(this.a, this.r, this.g, this.b);
        Log.d("display", "rendering..");
      }

      @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
            int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
            this.setMeasuredDimension(parentWidth / 2, parentHeight);
            //Since you are attatching it to the window use window layout params.
            WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(parentWidth / 2,
                    parentHeight);
            this.setLayoutParams(layoutParams);

            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            Log.d("display", "filling...");
        }

      public void setColor(int a, int r, int g, int b){
        this.a = a;
        this.r = r;
        this.g = g;
        this.b = b;
        invalidate();
       }
}

Changing brightness:

WindowManager.LayoutParams layout = cordova.getActivity().getWindow().getAttributes();
try {
    layout.screenBrightness = (float) arg_object.getDouble("brightness");
    // ^ When I comment this line, it doesn't work either.
} catch (JSONException e) {
    e.printStackTrace();
}
cordova.getActivity().getWindow().setAttributes(layout);

When I add the view to the application and, after that, I want to change brightness of the screen - brightness is changing, but I can't click on anything on the screen. After few second I get an 'Application not responding message.

What causes my application to freeze?

Thanks in advance.

Joel
  • 4,732
  • 9
  • 39
  • 54
tomwesolowski
  • 956
  • 1
  • 11
  • 27

1 Answers1

3
LayoutParams layoutParams = cordova.getActivity().getWindow().getAttributes();

In this line you getting the Layoutparams object of the Activity. This is only a reference to the LayoutParams object in the Activity.

Now you are changing this object:

layoutParams.type=LayoutParams.TYPE_SYSTEM_ALERT;
layoutParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE |     LayoutParams.FLAG_NOT_TOUCHABLE;
layoutParams.gravity=Gravity.LEFT|Gravity.TOP; 

wehereby you are changing the attributes of the Activity and amongst other things, setting the FLAG_NOT_FOCUSABLE flag for the Activity, causing it to appear not responding, because it has been made not focusable.

Changing this line

LayoutParams layoutParams = cordova.getActivity().getWindow().getAttributes();

to

LayoutParams layoutParams = new LayoutParams();

should solve your problem.

ntv1000
  • 626
  • 6
  • 16
  • Thank you! I got one more problem: When I click 'Home button', exception is thrown: Activity has leaked window... from this line: localWindowManager.addView(colourView, layoutParams);. Do you know what can cause it? – tomwesolowski Feb 16 '14 at 22:26
  • 1
    Please create a separate question. – ntv1000 Feb 16 '14 at 22:28