PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/documentation/tutorial/MusicFileBrowser/MusicFileBrowser_Tutorial1/src/com/google/marvin/tutorials/musicFileBrowser1/MusicFileBrowser.java

http://eyes-free.googlecode.com/
Java | 222 lines | 184 code | 23 blank | 15 comment | 36 complexity | 50de681669c86300767f99dbeb0b7920 MD5 | raw file
  1package com.google.marvin.tutorials.musicFileBrowser1;
  2
  3/**
  4 * Music File Browser with speech added; no gestures yet.
  5 * 
  6 * File browsing code is based on a tutorial from anddev.org by Nicolas Gramlich
  7 * http://www.anddev.org/building_an_android_filebrowser_list-based_ -t67.html
  8 * 
  9 * @author clchen@google.com (Charles L. Chen)
 10 */
 11
 12import com.google.tts.TTS;
 13
 14import java.io.File;
 15import java.util.ArrayList;
 16import java.util.Collections;
 17import java.util.List;
 18
 19import android.app.Activity;
 20import android.content.Context;
 21import android.media.MediaPlayer;
 22import android.net.Uri;
 23import android.os.Bundle;
 24import android.os.Vibrator;
 25import android.view.KeyEvent;
 26import android.view.MotionEvent;
 27import android.view.View;
 28import android.widget.AdapterView;
 29import android.widget.ArrayAdapter;
 30import android.widget.ListView;
 31import android.widget.Toast;
 32import android.widget.AdapterView.OnItemClickListener;
 33
 34public class MusicFileBrowser extends Activity {
 35  private static final long[] VIBE_PATTERN = {0, 10, 70, 80};
 36  private Vibrator vibe;
 37  private List<String> directoryEntries = new ArrayList<String>();
 38  private File currentDirectory = new File("/sdcard/");
 39  private MediaPlayer mp = new MediaPlayer();
 40  private int lastPosition;
 41  private String lastPlayedFilename = "";
 42  private TTS tts;
 43  private ListView myList;
 44
 45  private TTS.InitListener ttsInitListener = new TTS.InitListener() {
 46    public void onInit(int version) {
 47      lastPosition = 0;
 48      vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
 49      browseTo(new File("/sdcard/"));
 50      tts.speak("Music File Browser started.", 0, null);
 51    }
 52  };
 53
 54  /** Called when the activity is first created. */
 55  @Override
 56  public void onCreate(Bundle icicle) {
 57    super.onCreate(icicle);
 58
 59    myList = new ListView(this);
 60    myList.setOnItemClickListener(new OnItemClickListener() {
 61      public void onItemClick(AdapterView<?> l, View v, int position, long id) {
 62        if (position == 0) {
 63          playNext();
 64        } else if (position == 1) {
 65          upOneLevel();
 66        } else {
 67          lastPosition = position;
 68          File clickedFile =
 69              new File(currentDirectory.getAbsolutePath() + directoryEntries.get(position));
 70          if (clickedFile != null) {
 71            browseTo(clickedFile);
 72          }
 73        }
 74      }
 75    });
 76    setContentView(myList);
 77
 78    tts = new TTS(this, ttsInitListener, true);
 79  }
 80
 81  /**
 82   * This function browses up one level according to the field: currentDirectory
 83   * Returns whether or not navigation occurred.
 84   */
 85  private boolean upOneLevel() {
 86    String parent = this.currentDirectory.getParent();
 87    if (parent.equals("/")) {
 88      browseTo(new File("/sdcard/"));
 89      return false;
 90    } else if (this.currentDirectory.getParent() != null) {
 91      this.browseTo(this.currentDirectory.getParentFile());
 92    }
 93    return true;
 94  }
 95
 96  private void browseTo(final File aDirectory) {
 97    if (aDirectory.isDirectory()) {
 98      this.currentDirectory = aDirectory;
 99      fill(aDirectory.listFiles());
100      lastPosition = 0;
101      myList.setSelection(0);
102    } else {
103      togglePlaying(aDirectory);
104    }
105  }
106
107  private void togglePlaying(final File aFile) {
108    if (mp.isPlaying()) {
109      mp.stop();
110    } else {
111      lastPlayedFilename = aFile.getAbsolutePath();
112      mp = MediaPlayer.create(this, Uri.parse(lastPlayedFilename));
113      mp.start();
114    }
115  }
116
117  private void playNext() {
118    if (mp.isPlaying()) {
119      mp.stop();
120    }
121    int i = lastPosition;
122    i++;
123    String path = this.currentDirectory.getAbsolutePath();
124    while (i < this.directoryEntries.size()) {
125      String filename = this.directoryEntries.get(i);
126      File currentFile = new File(path + filename);
127      if (currentFile.isFile()) {
128        lastPosition = i;
129        togglePlaying(currentFile);
130        return;
131      }
132      i++;
133    }
134    i = 0;
135    while (i <= lastPosition) {
136      String filename = this.directoryEntries.get(i);
137      File currentFile = new File(path + filename);
138      if (currentFile.isFile()) {
139        lastPosition = i;
140        togglePlaying(currentFile);
141        return;
142      }
143      i++;
144    }
145    Toast.makeText(this, R.string.no_files, 1).show();
146    tts.speak(getString(R.string.no_files), 0, null);
147  }
148
149  private void fill(File[] files) {
150    this.directoryEntries.clear();
151
152    // Add the top two choices
153    this.directoryEntries.add(currentDirectory.getAbsolutePath());
154    if (this.currentDirectory.getParent() != null) {
155      this.directoryEntries.add(" up one level");
156    }
157
158    int currentPathStringLength = this.currentDirectory.getAbsolutePath().length();
159
160    List<String> directories = new ArrayList<String>();
161    for (File file : files) {
162      if (file.isDirectory()) {
163        String filename = file.getAbsolutePath().substring(currentPathStringLength);
164        if (!filename.startsWith("/.")) {
165          directories.add(filename);
166        }
167      }
168    }
169    Collections.sort(directories);
170    this.directoryEntries.addAll(directories);
171
172    List<String> audioFiles = new ArrayList<String>();
173    for (File file : files) {
174      if (file.getPath().endsWith(".mp3")) {
175        String filename = file.getAbsolutePath().substring(currentPathStringLength);
176        if (!filename.startsWith("/.")) {
177          audioFiles.add(filename);
178        }
179      }
180    }
181    Collections.sort(audioFiles);
182    this.directoryEntries.addAll(audioFiles);
183
184    ArrayAdapter<String> directoryList =
185        new ArrayAdapter<String>(this, R.layout.file_row, this.directoryEntries);
186
187    myList.setAdapter(directoryList);
188  }
189
190  @Override
191  public boolean onTrackballEvent(MotionEvent event) {
192    int selectedId = myList.getSelectedItemPosition();
193    if (selectedId == -1) {
194      selectedId = 0;
195    }
196    if (lastPosition != selectedId) {
197      lastPosition = selectedId;
198      vibe.vibrate(VIBE_PATTERN, -1);
199      String filename = directoryEntries.get(selectedId);
200      tts.speak(filename.substring(1), 0, null);
201    }
202    return super.onTrackballEvent(event);
203  }
204
205  @Override
206  public boolean onKeyDown(int keyCode, KeyEvent event) {
207    if (keyCode == KeyEvent.KEYCODE_BACK) {
208      // Pressing back should go up a level, not quit the app
209      if (upOneLevel()) {
210        return true;
211      }
212    }
213    return super.onKeyDown(keyCode, event);
214  }
215  
216  @Override
217  public void onDestroy(){
218    tts.shutdown();
219    mp.stop();
220    super.onDestroy();
221  }
222}