/talkingdialer/src/com/google/marvin/talkingdialer/SlideDial.java

http://eyes-free.googlecode.com/ · Java · 235 lines · 179 code · 24 blank · 32 comment · 36 complexity · 1802dda335c362a1b5321e052af342d1 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 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.google.marvin.talkingdialer;
  17. import android.app.Activity;
  18. import android.content.Intent;
  19. import android.content.SharedPreferences;
  20. import android.content.SharedPreferences.Editor;
  21. import android.media.AudioManager;
  22. import android.os.Bundle;
  23. import android.speech.tts.TextToSpeech;
  24. import android.speech.tts.TextToSpeech.OnInitListener;
  25. import android.view.View;
  26. import android.view.View.OnClickListener;
  27. import android.widget.FrameLayout;
  28. import com.google.marvin.talkingdialer.RunButton.OnHoverListener;
  29. import com.google.marvin.talkingdialer.ScrollSidebarView.OnScrollDetectedListener;
  30. /**
  31. * Enables the user to dial without looking at the phone. The spot the user
  32. * touches down is "5". What the user actually dials depends on where they lift
  33. * up relative to where they touched down; this is based on the arrangement of
  34. * numbers on a standard touchtone phone dialpad: 1 2 3 4 5 6 7 8 9 * 0 # Thus,
  35. * sliding to the upperleft hand corner and lifting up will dial a "1". A
  36. * similar technique is used for dialing a contact. Stroking up will go to
  37. * previous contact; stroking down will go to the next contact.
  38. *
  39. * @author clchen@google.com (Charles L. Chen)
  40. */
  41. public class SlideDial extends Activity {
  42. private static final int DIALING_VIEW = 0;
  43. private static final int CONTACTS_VIEW = 1;
  44. private SlideDialView dialerView;
  45. private ContactsView contactsView;
  46. private SharedPreferences prefs;
  47. private int currentView = -1;
  48. public TextToSpeech tts;
  49. boolean contactsPickerMode = false;
  50. FrameLayout mFrameLayout;
  51. @Override
  52. public void onCreate(Bundle savedInstanceState) {
  53. super.onCreate(savedInstanceState);
  54. setContentView(R.layout.main);
  55. mFrameLayout = (FrameLayout) findViewById(R.id.frameLayout);
  56. RunButton callButton = (RunButton) findViewById(R.id.callButton);
  57. callButton.setOnClickListener(new OnClickListener() {
  58. @Override
  59. public void onClick(View v) {
  60. if (contactsView != null) {
  61. contactsView.dialActionHandler();
  62. } else if (dialerView != null) {
  63. dialerView.callCurrentNumber();
  64. }
  65. }
  66. });
  67. callButton.setOnHoverListener(new OnHoverListener() {
  68. @Override
  69. public void onHoverEnter() {
  70. if (contactsView != null) {
  71. contactsView.dialActionHandler();
  72. } else if (dialerView != null) {
  73. dialerView.callCurrentNumber();
  74. }
  75. }
  76. @Override
  77. public void onHoverExit() {
  78. }
  79. });
  80. }
  81. private OnScrollDetectedListener scrollListener = new OnScrollDetectedListener() {
  82. @Override
  83. public void onScrollDetected(int direction) {
  84. if (direction > 0) {
  85. contactsView.nextContact();
  86. } else {
  87. contactsView.prevContact();
  88. }
  89. }
  90. };
  91. @Override
  92. public void onResume() {
  93. super.onResume();
  94. setVolumeControlStream(AudioManager.STREAM_MUSIC);
  95. String action = getIntent().getAction();
  96. contactsPickerMode = action != null && action.equals(Intent.ACTION_PICK);
  97. if (tts == null) {
  98. tts = new TextToSpeech(this, ttsInitListener);
  99. } else {
  100. initView();
  101. }
  102. }
  103. /**
  104. * Load a main view, depending on preferences and whether this activity has
  105. * been launched as a contact chooser.
  106. */
  107. private void initView() {
  108. if (contactsPickerMode) {
  109. currentView = CONTACTS_VIEW;
  110. switchToContactsView();
  111. } else {
  112. prefs = getPreferences(MODE_PRIVATE);
  113. currentView = prefs.getInt(getString(R.string.view_mode_preference), DIALING_VIEW);
  114. if (currentView == DIALING_VIEW) {
  115. switchToDialingView();
  116. } else {
  117. switchToContactsView();
  118. }
  119. }
  120. }
  121. private OnInitListener ttsInitListener = new OnInitListener() {
  122. @Override
  123. public void onInit(int status) {
  124. String pkgName = SlideDial.class.getPackage().getName();
  125. tts.addEarcon(getString(R.string.earcon_tock), pkgName, R.raw.tock_snd);
  126. initView();
  127. }
  128. };
  129. public void returnResults(String dialedNumber) {
  130. dialedNumber = dialedNumber.replaceAll("[^0-9*#,;]", "");
  131. Intent dummyIntent = new Intent();
  132. dummyIntent.putExtra("number", dialedNumber);
  133. setResult(RESULT_OK, dummyIntent);
  134. finish();
  135. }
  136. public void returnResults(String dialedNumber, String contactName) {
  137. dialedNumber = dialedNumber.replaceAll("[^0-9*#,;]", "");
  138. Intent dummyIntent = new Intent();
  139. dummyIntent.putExtra("number", dialedNumber);
  140. dummyIntent.putExtra("label", contactName);
  141. setResult(RESULT_OK, dummyIntent);
  142. finish();
  143. }
  144. public void switchToContactsView() {
  145. removeViews();
  146. ScrollSidebarView lScroll = (ScrollSidebarView) findViewById(R.id.lScroll);
  147. lScroll.setVisibility(View.VISIBLE);
  148. lScroll.setOnScrollDetectedListener(scrollListener);
  149. ScrollSidebarView rScroll = (ScrollSidebarView) findViewById(R.id.rScroll);
  150. rScroll.setVisibility(View.VISIBLE);
  151. rScroll.setOnScrollDetectedListener(scrollListener);
  152. if (contactsView == null) {
  153. contactsView = new ContactsView(this);
  154. }
  155. if (mFrameLayout.getChildCount() > 1) {
  156. mFrameLayout.removeViewAt(0);
  157. }
  158. mFrameLayout.addView(contactsView, 0);
  159. // setContentView(contactsView);
  160. currentView = CONTACTS_VIEW;
  161. tts.speak(getString(R.string.phonebook), 0, null);
  162. }
  163. public void switchToDialingView() {
  164. removeViews();
  165. ScrollSidebarView lScroll = (ScrollSidebarView) findViewById(R.id.lScroll);
  166. lScroll.setVisibility(View.INVISIBLE);
  167. ScrollSidebarView rScroll = (ScrollSidebarView) findViewById(R.id.rScroll);
  168. rScroll.setVisibility(View.INVISIBLE);
  169. if (dialerView == null) {
  170. dialerView = new SlideDialView(this);
  171. }
  172. // setContentView(mView);
  173. if (mFrameLayout.getChildCount() > 1) {
  174. mFrameLayout.removeViewAt(0);
  175. }
  176. mFrameLayout.addView(dialerView, 0);
  177. currentView = DIALING_VIEW;
  178. tts.speak(getString(R.string.dialing_mode), 0, null);
  179. }
  180. public void removeViews() {
  181. if (contactsView != null) {
  182. contactsView.shutdown();
  183. contactsView.setVisibility(View.GONE);
  184. contactsView = null;
  185. }
  186. if (dialerView != null) {
  187. dialerView.shutdown();
  188. dialerView.setVisibility(View.GONE);
  189. dialerView = null;
  190. }
  191. }
  192. public void quit() {
  193. setResult(RESULT_CANCELED, null);
  194. finish();
  195. }
  196. @Override
  197. protected void onPause() {
  198. if (prefs != null && currentView != -1) {
  199. Editor editor = prefs.edit();
  200. editor.putInt(getString(R.string.view_mode_preference), currentView);
  201. editor.commit();
  202. }
  203. removeViews();
  204. super.onPause();
  205. }
  206. @Override
  207. protected void onDestroy() {
  208. tts.shutdown();
  209. super.onDestroy();
  210. }
  211. }