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.wearable.findphone; 18 19 import android.app.Activity; 20 import android.app.Notification; 21 import android.app.Notification.Action; 22 import android.app.NotificationManager; 23 import android.app.PendingIntent; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.os.Bundle; 27 import android.text.Spannable; 28 import android.text.SpannableString; 29 import android.text.style.RelativeSizeSpan; 30 31 32 public class FindPhoneActivity extends Activity { 33 34 private static final int FIND_PHONE_NOTIFICATION_ID = 2; 35 private static Notification.Builder notification; 36 37 @Override onCreate(Bundle savedInstanceState)38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 41 // Create a notification with an action to toggle an alarm on the phone. 42 Intent toggleAlarmOperation = new Intent(this, FindPhoneService.class); 43 toggleAlarmOperation.setAction(FindPhoneService.ACTION_TOGGLE_ALARM); 44 PendingIntent toggleAlarmIntent = PendingIntent.getService(this, 0, toggleAlarmOperation, 45 PendingIntent.FLAG_CANCEL_CURRENT); 46 Action alarmAction = new Action(R.drawable.alarm_action_icon, "", toggleAlarmIntent); 47 // This intent turns off the alarm if the user dismisses the card from the wearable. 48 Intent cancelAlarmOperation = new Intent(this, FindPhoneService.class); 49 cancelAlarmOperation.setAction(FindPhoneService.ACTION_CANCEL_ALARM); 50 PendingIntent cancelAlarmIntent = PendingIntent.getService(this, 0, cancelAlarmOperation, 51 PendingIntent.FLAG_CANCEL_CURRENT); 52 // Use a spannable string for the notification title to resize it. 53 SpannableString title = new SpannableString(getString(R.string.app_name)); 54 title.setSpan(new RelativeSizeSpan(0.85f), 0, title.length(), Spannable.SPAN_POINT_MARK); 55 notification = new Notification.Builder(this) 56 .setContentTitle(title) 57 .setContentText(getString(R.string.turn_alarm_on)) 58 .setSmallIcon(R.drawable.ic_launcher) 59 .setVibrate(new long[] {0, 50}) // Vibrate to bring card to top of stream. 60 .setDeleteIntent(cancelAlarmIntent) 61 .extend(new Notification.WearableExtender() 62 .addAction(alarmAction) 63 .setContentAction(0) 64 .setHintHideIcon(true)) 65 .setLocalOnly(true) 66 .setPriority(Notification.PRIORITY_MAX); 67 ((NotificationManager) getSystemService(NOTIFICATION_SERVICE)) 68 .notify(FIND_PHONE_NOTIFICATION_ID, notification.build()); 69 70 finish(); 71 } 72 73 /** 74 * Updates the text on the wearable notification. This is used so the notification reflects the 75 * current state of the alarm on the phone. For instance, if the alarm is turned on, the 76 * notification text indicates that the user can tap it to turn it off, and vice-versa. 77 * 78 * @param context 79 * @param notificationText The new text to display on the wearable notification. 80 */ updateNotification(Context context, String notificationText)81 public static void updateNotification(Context context, String notificationText) { 82 notification.setContentText(notificationText); 83 ((NotificationManager) context.getSystemService(NOTIFICATION_SERVICE)) 84 .notify(FIND_PHONE_NOTIFICATION_ID, notification.build()); 85 } 86 87 } 88