Update theme submodule
[platform/upstream/gstreamer.git] / examples / tutorials / android-tutorial-4 / src / com / gst_sdk_tutorials / tutorial_4 / GStreamerSurfaceView.java
1 package com.gst_sdk_tutorials.tutorial_4;
2
3 import android.content.Context;
4 import android.util.AttributeSet;
5 import android.util.Log;
6 import android.view.SurfaceView;
7 import android.view.View;
8
9 // A simple SurfaceView whose width and height can be set from the outside
10 public class GStreamerSurfaceView extends SurfaceView {
11     public int media_width = 320;
12     public int media_height = 240;
13
14     // Mandatory constructors, they do not do much
15     public GStreamerSurfaceView(Context context, AttributeSet attrs,
16             int defStyle) {
17         super(context, attrs, defStyle);
18     }
19
20     public GStreamerSurfaceView(Context context, AttributeSet attrs) {
21         super(context, attrs);
22     }
23
24     public GStreamerSurfaceView (Context context) {
25         super(context);
26     }
27
28     // Called by the layout manager to find out our size and give us some rules.
29     // We will try to maximize our size, and preserve the media's aspect ratio if
30     // we are given the freedom to do so.
31     @Override
32     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
33         int width = 0, height = 0;
34         int wmode = View.MeasureSpec.getMode(widthMeasureSpec);
35         int hmode = View.MeasureSpec.getMode(heightMeasureSpec);
36         int wsize = View.MeasureSpec.getSize(widthMeasureSpec);
37         int hsize = View.MeasureSpec.getSize(heightMeasureSpec);
38
39         Log.i ("GStreamer", "onMeasure called with " + media_width + "x" + media_height);
40         // Obey width rules
41         switch (wmode) {
42         case View.MeasureSpec.AT_MOST:
43             if (hmode == View.MeasureSpec.EXACTLY) {
44                 width = Math.min(hsize * media_width / media_height, wsize);
45                 break;
46             }
47         case View.MeasureSpec.EXACTLY:
48             width = wsize;
49             break;
50         case View.MeasureSpec.UNSPECIFIED:
51             width = media_width;
52         }
53
54         // Obey height rules
55         switch (hmode) {
56         case View.MeasureSpec.AT_MOST:
57             if (wmode == View.MeasureSpec.EXACTLY) {
58                 height = Math.min(wsize * media_height / media_width, hsize);
59                 break;
60             }
61         case View.MeasureSpec.EXACTLY:
62             height = hsize;
63             break;
64         case View.MeasureSpec.UNSPECIFIED:
65             height = media_height;
66         }
67
68         // Finally, calculate best size when both axis are free
69         if (hmode == View.MeasureSpec.AT_MOST && wmode == View.MeasureSpec.AT_MOST) {
70             int correct_height = width * media_height / media_width;
71             int correct_width = height * media_width / media_height;
72
73             if (correct_height < height)
74                 height = correct_height;
75             else
76                 width = correct_width;
77         }
78
79         // Obey minimum size
80         width = Math.max (getSuggestedMinimumWidth(), width);
81         height = Math.max (getSuggestedMinimumHeight(), height);
82         setMeasuredDimension(width, height);
83     }
84
85 }