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