Home | History | Annotate | Download | only in com.example.android.google.wearable.watchviewstub
      1 /*
      2  * Copyright (C) 2014 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.example.android.google.wearable.watchviewstub;
     18 
     19 import android.app.Activity;
     20 import android.os.Bundle;
     21 import android.support.v4.view.GestureDetectorCompat;
     22 import android.support.wearable.view.DismissOverlayView;
     23 import android.support.wearable.view.WatchViewStub;
     24 import android.view.GestureDetector;
     25 import android.view.MotionEvent;
     26 import android.view.View;
     27 import android.view.animation.Animation;
     28 import android.view.animation.ScaleAnimation;
     29 import android.widget.RelativeLayout;
     30 
     31 public class MainActivity extends Activity {
     32     private RelativeLayout mRectBackground;
     33     private RelativeLayout mRoundBackground;
     34 
     35     private GestureDetectorCompat mGestureDetector;
     36     private DismissOverlayView mDismissOverlayView;
     37 
     38     @Override
     39     public void onCreate(Bundle b) {
     40         super.onCreate(b);
     41         setContentView(R.layout.main_activity);
     42 
     43         WatchViewStub stub = (WatchViewStub) findViewById(R.id.stub);
     44         stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
     45             @Override
     46             public void onLayoutInflated(WatchViewStub stub) {
     47                 mRectBackground = (RelativeLayout) findViewById(R.id.rect_layout);
     48                 mRoundBackground = (RelativeLayout) findViewById(R.id.round_layout);
     49             }
     50         });
     51 
     52         mDismissOverlayView = (DismissOverlayView) findViewById(R.id.dismiss_overlay);
     53         mGestureDetector = new GestureDetectorCompat(this, new LongPressListener());
     54     }
     55 
     56     /**
     57      * Animates the layout when clicked. The animation used depends on whether the
     58      * device is round or rectangular.
     59      */
     60     public void onLayoutClicked(View view) {
     61         if (mRectBackground != null) {
     62             ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.7f, 1.0f, 0.7f,
     63                     Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
     64             scaleAnimation.setDuration(300);
     65             scaleAnimation.setRepeatCount(1);
     66             scaleAnimation.setRepeatMode(Animation.REVERSE);
     67             mRectBackground.startAnimation(scaleAnimation);
     68         }
     69         if (mRoundBackground != null) {
     70             mRoundBackground.animate().rotationBy(360).setDuration(300).start();
     71         }
     72     }
     73 
     74     @Override
     75     public boolean dispatchTouchEvent(MotionEvent event) {
     76         return mGestureDetector.onTouchEvent(event) || super.dispatchTouchEvent(event);
     77     }
     78 
     79     private class LongPressListener extends GestureDetector.SimpleOnGestureListener {
     80         @Override
     81         public void onLongPress(MotionEvent event) {
     82             mDismissOverlayView.show();
     83         }
     84     }
     85 }
     86