PageRenderTime 23ms CodeModel.GetById 2ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

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