Skip to content

Instantly share code, notes, and snippets.

@davidtavarez
Created March 8, 2014 16:11
Show Gist options
  • Save davidtavarez/9434195 to your computer and use it in GitHub Desktop.
Save davidtavarez/9434195 to your computer and use it in GitHub Desktop.
Some java functions to help any Android developer.
package com.dt.memmo.tools;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.hardware.Camera;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.dt.memmo.R;
import java.io.*;
import java.util.UUID;
public class Helpers {
private Context _context;
public Helpers(Context context) {
this._context = context;
}
public boolean isConnectingToInternet() {
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
return false;
}
public String getIMEI() {
return ((TelephonyManager) _context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
}
public String getUUID() {
String android_id = Settings.Secure.getString(_context.getApplicationContext()
.getContentResolver(), Settings.Secure.ANDROID_ID);
final TelephonyManager tm = (TelephonyManager) _context
.getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = ""
+ android.provider.Settings.Secure.getString(
_context.getContentResolver(),
android.provider.Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice
.hashCode() << 32)
| tmSerial.hashCode());
return deviceUuid.toString();
}
public boolean isFacebookInstalled() {
try {
ApplicationInfo facebook = this._context.getPackageManager().
getApplicationInfo("com.facebook.katana", 0);
ApplicationInfo facebookMessenger = this._context.getPackageManager().
getApplicationInfo("com.facebook.orca", 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
public void displayMessage(Context c, String title, String msg) {
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(c);
alertDialogBuilder.setTitle(title);
alertDialogBuilder
.setMessage(msg)
.setCancelable(false)
.setPositiveButton(c.getString(R.string.Ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
dialog.dismiss();
}
});
alertDialogBuilder.create().show();
}
public boolean hasFrontFacingCamera() {
boolean cameraId = false;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
cameraId = true;
break;
}
}
return cameraId;
}
/**
* This method converts dp unit to equivalent pixels, depending on device density.
*
* @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
* @return A float value to represent px equivalent to dp depending on device density
*/
public float convertDpToPixel(float dp) {
Resources resources = this._context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}
/**
* This method converts device specific pixels to density independent pixels.
*
* @param px A value in px (pixels) unit. Which we need to convert into db
* @return A float value to represent dp equivalent to px value
*/
public float convertPixelsToDp(float px) {
Resources resources = this._context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return dp;
}
public void bigToast(Activity activity, String message) {
LayoutInflater inflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.toast_messages,
(ViewGroup) activity.findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.textToastMessage);
text.setText(message);
Toast toast = new Toast(this._context.getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
public byte[] getByteArrayFromImage(String filePath) throws FileNotFoundException, IOException {
File file = new File(filePath);
System.out.println(file.exists() + "!!");
FileInputStream fis = new FileInputStream(file);
//create FileInputStream which obtains input bytes from a file in a file system
//FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
//InputStream in = resource.openStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1; ) {
bos.write(buf, 0, readNum);
//no doubt here is 0
/*Writes len bytes from the specified byte array starting at offset
off to this byte array output stream.*/
System.out.println("read " + readNum + " bytes,");
}
} catch (IOException ex) {
Log.d("error", "error");
}
byte[] bytes = bos.toByteArray();
return bytes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment