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.app; 18 19 import android.app.Activity; 20 import android.app.Notification; 21 import android.app.PendingIntent; 22 import android.content.Intent; 23 import android.os.Bundle; 24 import android.support.v4.app.NotificationCompat; 25 import android.support.v4.app.NotificationManagerCompat; 26 import android.support.v4.view.GestureDetectorCompat; 27 import android.support.wearable.view.DelayedConfirmationView; 28 import android.support.wearable.view.DismissOverlayView; 29 import android.util.Log; 30 import android.view.GestureDetector; 31 import android.view.MotionEvent; 32 import android.view.View; 33 import android.widget.ScrollView; 34 35 public class MainActivity extends Activity 36 implements DelayedConfirmationView.DelayedConfirmationListener { 37 private static final String TAG = "MainActivity"; 38 39 private static final int NOTIFICATION_ID = 1; 40 private static final int NOTIFICATION_REQUEST_CODE = 1; 41 private static final int NUM_SECONDS = 5; 42 43 private GestureDetectorCompat mGestureDetector; 44 private DismissOverlayView mDismissOverlayView; 45 46 @Override onCreate(Bundle b)47 public void onCreate(Bundle b) { 48 super.onCreate(b); 49 setContentView(R.layout.main_activity); 50 51 mDismissOverlayView = (DismissOverlayView) findViewById(R.id.dismiss_overlay); 52 mDismissOverlayView.setIntroText(R.string.intro_text); 53 mDismissOverlayView.showIntroIfNecessary(); 54 mGestureDetector = new GestureDetectorCompat(this, new LongPressListener()); 55 } 56 57 @Override dispatchTouchEvent(MotionEvent event)58 public boolean dispatchTouchEvent(MotionEvent event) { 59 return mGestureDetector.onTouchEvent(event) || super.dispatchTouchEvent(event); 60 } 61 62 private class LongPressListener extends GestureDetector.SimpleOnGestureListener { 63 @Override onLongPress(MotionEvent event)64 public void onLongPress(MotionEvent event) { 65 mDismissOverlayView.show(); 66 } 67 } 68 69 /** 70 * Handles the button to launch a notification. 71 */ showNotification(View view)72 public void showNotification(View view) { 73 Notification notification = new NotificationCompat.Builder(this) 74 .setContentTitle(getString(R.string.notification_title)) 75 .setContentText(getString(R.string.notification_title)) 76 .setSmallIcon(R.drawable.ic_launcher) 77 .addAction(R.drawable.ic_launcher, 78 getText(R.string.action_launch_activity), 79 PendingIntent.getActivity(this, NOTIFICATION_REQUEST_CODE, 80 new Intent(this, GridExampleActivity.class), 81 PendingIntent.FLAG_UPDATE_CURRENT)) 82 .build(); 83 NotificationManagerCompat.from(this).notify(NOTIFICATION_ID, notification); 84 finish(); 85 } 86 87 88 /** 89 * Handles the button press to finish this activity and take the user back to the Home. 90 */ onFinishActivity(View view)91 public void onFinishActivity(View view) { 92 setResult(RESULT_OK); 93 finish(); 94 } 95 96 /** 97 * Handles the button to start a DelayedConfirmationView timer. 98 */ onStartTimer(View view)99 public void onStartTimer(View view) { 100 DelayedConfirmationView delayedConfirmationView = (DelayedConfirmationView) 101 findViewById(R.id.timer); 102 delayedConfirmationView.setTotalTimeMs(NUM_SECONDS * 1000); 103 delayedConfirmationView.setListener(this); 104 delayedConfirmationView.start(); 105 scroll(View.FOCUS_DOWN); 106 } 107 108 @Override onTimerFinished(View v)109 public void onTimerFinished(View v) { 110 Log.d(TAG, "onTimerFinished is called."); 111 scroll(View.FOCUS_UP); 112 } 113 114 @Override onTimerSelected(View v)115 public void onTimerSelected(View v) { 116 Log.d(TAG, "onTimerSelected is called."); 117 scroll(View.FOCUS_UP); 118 } 119 scroll(final int scrollDirection)120 private void scroll(final int scrollDirection) { 121 final ScrollView scrollView = (ScrollView) findViewById(R.id.scroll); 122 scrollView.post(new Runnable() { 123 @Override 124 public void run() { 125 scrollView.fullScroll(scrollDirection); 126 } 127 }); 128 } 129 } 130