/walkytalky/src/com/googlecode/eyesfree/walkytalky/WalkyTalky.java

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