package com.todoroo.andlib.utility; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import android.app.Activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.NetworkInfo.State; import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; import com.todoroo.andlib.service.ExceptionService; /** * Android Utility Classes * * @author Tim Su * */ public class AndroidUtilities { private static class ExceptionHelper { @Autowired public ExceptionService exceptionService; public ExceptionHelper() { DependencyInjectionService.getInstance().inject(this); } } /** * @return true if we're connected to the internet */ public static boolean isConnected(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); if (info == null) return false; if (info.getState() != State.CONNECTED) return false; return true; } /** Fetch the image specified by the given url */ public static Bitmap fetchImage(URL url) throws IOException { InputStream is = null; try { URLConnection conn = url.openConnection(); conn.connect(); is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is, 16384); try { Bitmap bitmap = BitmapFactory.decodeStream(bis); return bitmap; } finally { bis.close(); } } finally { if(is != null) is.close(); } } /** * Start the given intent, handling security exceptions if they arise * * @param context * @param intent */ public static void startExternalIntent(Context context, Intent intent) { try { context.startActivity(intent); } catch (SecurityException e) { ExceptionHelper helper = new ExceptionHelper(); helper.exceptionService.displayAndReportError(context, "start-external-intent-" + intent.toString(), //$NON-NLS-1$ e); } } /** * Start the given intent, handling security exceptions if they arise * * @param activity * @param intent * @param requestCode */ public static void startExternalIntentForResult( Activity activity, Intent intent, int requestCode) { try { activity.startActivityForResult(intent, requestCode); } catch (SecurityException e) { ExceptionHelper helper = new ExceptionHelper(); helper.exceptionService.displayAndReportError(activity, "start-external-intent-" + intent.toString(), //$NON-NLS-1$ e); } } }