/walkytalky/src/com/googlecode/eyesfree/walkytalky/WalkyTalky.java
Java | 137 lines | 89 code | 19 blank | 29 comment | 9 complexity | 29ab5081989222a8088acd8e46696ea4 MD5 | raw file
1/* 2 * Copyright (C) 2010 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.googlecode.eyesfree.walkytalky; 18 19import android.app.Activity; 20import android.app.AlertDialog; 21import android.content.ActivityNotFoundException; 22import android.content.BroadcastReceiver; 23import android.content.Context; 24import android.content.DialogInterface; 25import android.content.Intent; 26import android.content.IntentFilter; 27import android.net.Uri; 28import android.os.Bundle; 29import android.util.Log; 30 31import java.io.UnsupportedEncodingException; 32import java.net.URLEncoder; 33 34/** 35 * Generates status notifications of the user's position as they walk. 36 * 37 * @author clchen@google.com (Charles L. Chen) 38 * @author hiteshk@google.com (Hitesh Khandelwal) 39 */ 40 41public class WalkyTalky extends Activity { 42 /** Request code for NewLocationActivity. */ 43 private static final int NEW_LOCATION_REQUEST = 0; 44 45 /** Tag used for logging. */ 46 public static final String TAG = "WalkyTalky"; 47 48 private WalkyTalky self; 49 50 private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() { 51 @Override 52 public void onReceive(Context arg0, Intent arg1) { 53 startNotifications(); 54 } 55 }; 56 57 @Override 58 /** Called when the activity is first created. */ 59 public void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 self = this; 62 63 registerReceiver(mScreenOffReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF)); 64 } 65 66 private void startNotifications() { 67 // Start Position Notification service 68 Intent iPSNS = new Intent(self, PositionStatusNotificationService.class); 69 iPSNS.putExtra("ACTION", "Start"); 70 iPSNS.putExtra("SPEAK", false); 71 iPSNS.putExtra("POST_MESSAGE", true); 72 self.startService(iPSNS); 73 } 74 75 @Override 76 public void onResume() { 77 super.onResume(); 78 // Start New location activity 79 Intent newLoc = new Intent(self, NewLocationActivity.class); 80 newLoc.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 81 startActivityIfNeeded(newLoc, NEW_LOCATION_REQUEST); 82 } 83 84 @Override 85 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 86 if (requestCode == NEW_LOCATION_REQUEST && (resultCode == RESULT_OK)) { 87 launchDriveAbout(data.getStringExtra("LOC")); 88 } else if (requestCode == NEW_LOCATION_REQUEST && (resultCode == RESULT_FIRST_USER)) { 89 // Back button pressed in NewLocationActivity 90 finish(); 91 } 92 } 93 94 private void launchDriveAbout(String destination) { 95 String dest = destination; 96 try { 97 dest = URLEncoder.encode(destination, "UTF-8"); 98 } catch (UnsupportedEncodingException e) { 99 e.printStackTrace(); 100 } 101 102 Intent iNavigate = new Intent("android.intent.action.VIEW"); 103 iNavigate.setData(Uri.parse("http://maps.google.com/maps?myl=saddr&daddr=" + dest 104 + "&dirflg=w&nav=1")); 105 iNavigate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 106 iNavigate.setClassName("com.google.android.apps.maps", 107 "com.google.android.maps.driveabout.app.NavigationActivity"); 108 109 // Make DriveAbout do a warning chime before it speaks a direction 110 iNavigate.putExtra("CHIME_BEFORE_SPEECH", true); 111 try { 112 startActivity(iNavigate); 113 } catch (ActivityNotFoundException e) { 114 // you need to install driveabout 115 AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 116 alertDialog.setTitle("Google Maps required"); 117 alertDialog.setMessage("Google Maps navigation is required to use this functionality."); 118 alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 119 @Override 120 public void onClick(DialogInterface dialog, int which) { 121 122 } 123 }); 124 } 125 126 startNotifications(); 127 } 128 129 @Override 130 public void onDestroy() { 131 Intent i = new Intent(self, PositionStatusNotificationService.class); 132 i.putExtra("ACTION", "STOP"); 133 self.startService(i); 134 unregisterReceiver(mScreenOffReceiver); 135 super.onDestroy(); 136 } 137}