Code Analysis


Source Code Analysis

Here, we will take a look at how to handle obstruction area with the sample application's source code.


Detecting Obstruction Area

To obtain the obstruction area information of the target device, import com.lge.wearable.ObstructionHelper. Then you can obtain the obstruction area information of RectF type by using the getObstructionRect() function.

//Import the com.lge.wearable.ObstructionHelper class. ①
import com.lge.wearable.ObstructionHelper;                           

public class MainActivity extends WearableActivity {

//Declare a variable of RecF type. ②
    private static RectF mObstructionRect;                            

    protected void onCreate(Bundle savedInstanceState) {

//Get the obstruction rectangle using ObstructionHelper. ③
        mObstructionRect = ObstructionHelper.getObstructionRect();

① Import com.lge.wearable.ObstructionHelper class.

② Declare a variable in order to obtain the obstruction area information of RectF type.

③ Save the obstruction area information obtained using getObstructionRect() function to the variable declared in ②.


You can know whether the obstruction area exists or not according to the obstruction area information (mObstructionRect value).

//If the obstruction rectangle is empty, no obstruction. ①
if (mObstructionRect.isEmpty()) {                                 
    TextView textView = findViewById(;
} else { //Otherwise, blink the obstruction rectangle. ②
    ViewGroup viewGroup = findViewById(;
    viewGroup.addView(new ObstructionView(this));

① If the mObstructionRect value is empty, this device has no obstruction area.

② If the mObstructionRect value is not empty, this device has an obstruction area. If the obstruction area exists, add ObstructionView and display the obstruction area through blinking.

class ObstructionView extends View implements Runnable {

        private Paint mPaint = new Paint();

        public ObstructionView(Context context) {

        protected void onDraw(Canvas canvas) {
           //Set the blink colors. ①
            int color = (mPaint.getColor() != Color.GRAY) ? Color.GRAY : Color.BLACK;
           //Draw the obstruction rectangle with the colors. ②
            canvas.drawRect(mObstructionRect, mPaint); 
           //Flashes at 0.5 second intervals. ③
            postDelayed(this, 500);                                     

        public void run() {

① For a blinking effect, save the gray and black colors alternately.

② Draw the obstruction area with the color saved in ①.

③ The color of obstruction area changes at intervals of 0.5 seconds and it blinks.


Adjustion Layout Against Obsturction

If there is a view object in the obstruction area, adjust the layout dynamically by adding a margin.

Layout is not changed if there is no obstruction area. You can adjust layout by adding a margin only when the obstruction area exists. So, it is possible to implement an application to be run on all smart watches.

//Move the imageView if there is an obstruction area. ①
ImageView imageView = findViewById(;
FrameLayout.LayoutParams layoutParams =
        (FrameLayout.LayoutParams) imageView.getLayoutParams();   
        layoutParams.topMargin + 2 * (int) mObstructionRect.height(), 
        layoutParams.rightMargin, layoutParams.bottomMargin);

① Obtain the current layout parameter information of the image in the obstruction area and add a margin of twice the height of obstruction area to topMargin in order to adjust the layout to display the image below the obstruction area.