Search the market for applications to handle various filetypes if they can't be handled natively

pull/14/head
Sam Bosley 13 years ago
parent 8e2302310c
commit 58c565cfd1

@ -25,6 +25,10 @@ import android.widget.Toast;
public class RecognizerApi implements RecognitionListener {
public static interface PlaybackExceptionHandler {
public void playbackFailed(String file);
}
private String aacFile;
private Context context;
@ -57,7 +61,7 @@ public class RecognizerApi implements RecognitionListener {
this.mListener = listener;
}
public static void play(Activity activity, String file, String type, int errId) {
public static void play(Activity activity, String file, PlaybackExceptionHandler handler) {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
@ -65,18 +69,8 @@ public class RecognizerApi implements RecognitionListener {
mediaPlayer.prepare();
mediaPlayer.start();
} catch (Exception e) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(file)), type);
activity.startActivity(intent);
} catch (Exception e2) {
Toast.makeText(activity, errId, Toast.LENGTH_LONG);
}
handler.playbackFailed(file);
}
//Toast.makeText(context, "Playing Audio", Toast.LENGTH_LONG).show();
}
private AACEncoder encoder = new AACEncoder();

@ -19,7 +19,7 @@ public class FileMetadata {
/** Constants for file types */
public static final String FILE_TYPE_AUDIO = "audio/"; //$NON-NLS-1$
public static final String FILE_TYPE_IMAGE = "image/"; //$NON-NLS-1$
public static final String FILE_TYPE_PDF = "application/pdf"; //$NON-NLS-1$
public static final String FILE_TYPE_OTHER = "application/octet-stream"; //$NON-NLS-1$

@ -10,8 +10,11 @@ import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
@ -25,6 +28,7 @@ import android.widget.Toast;
import com.timsu.astrid.R;
import com.todoroo.aacenc.RecognizerApi;
import com.todoroo.aacenc.RecognizerApi.PlaybackExceptionHandler;
import com.todoroo.andlib.data.TodorooCursor;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.DependencyInjectionService;
@ -40,6 +44,7 @@ import com.todoroo.astrid.data.Metadata;
import com.todoroo.astrid.data.Task;
import com.todoroo.astrid.service.MetadataService;
import com.todoroo.astrid.ui.PopupControlSet;
import com.todoroo.astrid.utility.Constants;
public class FilesControlSet extends PopupControlSet {
@ -207,12 +212,17 @@ public class FilesControlSet extends PopupControlSet {
}
}
private void showFile(Metadata m) {
String fileType = m.containsNonNullValue(FileMetadata.FILE_TYPE) ? m.getValue(FileMetadata.FILE_TYPE) : FileMetadata.FILE_TYPE_OTHER;
String filePath = m.getValue(FileMetadata.FILE_PATH);
private void showFile(final Metadata m) {
final String fileType = m.containsNonNullValue(FileMetadata.FILE_TYPE) ? m.getValue(FileMetadata.FILE_TYPE) : FileMetadata.FILE_TYPE_OTHER;
final String filePath = m.getValue(FileMetadata.FILE_PATH);
if (fileType.startsWith(FileMetadata.FILE_TYPE_AUDIO)) {
RecognizerApi.play(activity, m.getValue(FileMetadata.FILE_PATH), m.getValue(FileMetadata.FILE_TYPE), R.string.audio_err_playback);
RecognizerApi.play(activity, m.getValue(FileMetadata.FILE_PATH), new PlaybackExceptionHandler() {
@Override
public void playbackFailed(String file) {
showFromIntent(filePath, fileType);
}
});
} else if (fileType.startsWith(FileMetadata.FILE_TYPE_IMAGE)) {
AlertDialog image = new AlertDialog.Builder(activity).create();
ImageView imageView = new ImageView(activity);
@ -235,10 +245,47 @@ public class FilesControlSet extends PopupControlSet {
});
image.show();
} else {
Toast.makeText(activity, R.string.file_err_show, Toast.LENGTH_LONG);
showFromIntent(filePath, fileType);
}
}
private void showFromIntent(String file, String type) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(file)), type);
activity.startActivity(intent);
} catch (ActivityNotFoundException e) {
handleActivityNotFound(type);
}
}
private void handleActivityNotFound(String fileType) {
if (fileType.startsWith(FileMetadata.FILE_TYPE_AUDIO)) {
searchMarket("com.clov4r.android.nil", R.string.search_market_audio_title, R.string.search_market_audio); //$NON-NLS-1$
} else if (fileType.startsWith(FileMetadata.FILE_TYPE_PDF)) {
searchMarket("com.adobe.reader", R.string.search_market_pdf_title, R.string.search_market_pdf); //$NON-NLS-1$
} else {
//
}
}
private void searchMarket(final String packageName, int title, int body) {
DialogUtilities.okCancelDialog(activity, activity.getString(title),
activity.getString(body), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int which) {
Intent marketIntent = Constants.MARKET_STRATEGY.generateMarketLink(packageName);
try {
activity.startActivity(marketIntent);
} catch (ActivityNotFoundException ane) {
DialogUtilities.okDialog(activity,
activity.getString(R.string.EPr_marketUnavailable_dlg),
null);
}
}
}, null);
}
@SuppressWarnings("nls")
private void downloadFile(final Metadata m) {
final ProgressDialog pd = new ProgressDialog(activity);

@ -18,6 +18,12 @@
<string name="audio_err_encoding">Error encoding audio</string>
<string name="audio_err_playback">Sorry, the system does not support this type of audio file</string>
<string name="search_market_audio">No player found to handle that audio type. Would you like to download an audio player from the Android Market?</string>
<string name="search_market_audio_title">No audio player found</string>
<string name="search_market_pdf">No PDF reader was found. Would you like to download a PDF reader from the Android Market?</string>
<string name="search_market_pdf_title">No PDF reader found</string>
<string name="file_prefix_image">Image</string>
<string name="file_prefix_voice">Voice</string>
<string name="file_browser_up">Up</string>

Loading…
Cancel
Save