mirror of https://github.com/tasks/tasks
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
4.3 KiB
Java
144 lines
4.3 KiB
Java
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.content.ContentValues;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.BitmapFactory;
|
|
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 <tim@todoroo.com>
|
|
*
|
|
*/
|
|
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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Put an arbitrary object into a {@link ContentValues}
|
|
* @param target
|
|
* @param key
|
|
* @param value
|
|
*/
|
|
public static void putInto(ContentValues target, String key, Object value) {
|
|
if(value instanceof String)
|
|
target.put(key, (String) value);
|
|
else if(value instanceof Long)
|
|
target.put(key, (Long) value);
|
|
else if(value instanceof Integer)
|
|
target.put(key, (Integer) value);
|
|
else if(value instanceof Double)
|
|
target.put(key, (Double) value);
|
|
else
|
|
throw new UnsupportedOperationException("Could not handle type " + //$NON-NLS-1$
|
|
value.getClass());
|
|
}
|
|
|
|
/**
|
|
* Return index of value in array
|
|
* @param array array to search
|
|
* @param value value to look for
|
|
* @return
|
|
*/
|
|
public static <TYPE> int indexOf(TYPE[] array, TYPE value) {
|
|
for(int i = 0; i < array.length; i++)
|
|
if(array[i].equals(value))
|
|
return i;
|
|
return -1;
|
|
}
|
|
|
|
}
|