/** Copyright (C) 2013 Louis Teboul (a.k.a Androguide)
*
* admin@pimpmyrom.org || louisteboul@gmail.com
* http://pimpmyrom.org || http://androguide.fr
* 71 quai Clémenceau, 69300 Caluire-et-Cuire, FRANCE.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
**/
package com.androguide.apkreator.helpers;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;
public class Helpers {
private static final String BUILD_PROP = "/system/build.prop";
// don't show unavoidable warnings
@SuppressWarnings({
"UnusedDeclaration",
"MethodWithMultipleReturnPoints",
"ReturnOfNull",
"NestedAssignment",
"DynamicRegexReplaceableByCompiledPattern",
"BreakStatement"})
// avoids hardcoding the tag
private static final String TAG = Thread.currentThread().getStackTrace()[1].getClassName();
public Helpers() {
// dummy constructor
}
public static String grabString(Context context, int resID) {
try {
return context.getResources().getString(resID);
} catch (NullPointerException e) {
Log.e("GRAB_STRING", "" + e);
return "";
}
}
public static String convertStreamToString(InputStream is) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
return sb.toString();
}
public static String getStringFromFile(String filePath) throws Exception {
File fl = new File(filePath);
FileInputStream fin = new FileInputStream(fl);
String ret = convertStreamToString(fin);
fin.close();
return ret;
}
public static boolean writeOneLine(String filename, String value) {
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter(filename);
fileWriter.write(value);
} catch (IOException e) {
String Error = "Error writing { " + value + " } to file: " + filename;
Log.e(TAG, Error, e);
return false;
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException ignored) {
// failed to close writer
}
}
}
return true;
}
/**
* Long toast message
*
* @param context Application Context
* @param msg Message to send
*/
public static void msgLong(Context context, String msg) {
if (context != null && msg != null) {
Toast.makeText(context, msg.trim(), Toast.LENGTH_LONG).show();
}
}
/**
* Short toast message
*
* @param context Application Context
* @param msg Message to send
*/
public static void msgShort(Context context, String msg) {
if (context != null && msg != null) {
Toast.makeText(context, msg.trim(), Toast.LENGTH_SHORT).show();
}
}
/**
* Long toast message
*
* @param context Application Context
* @param msg Message to send
*/
public static void sendMsg(Context context, String msg) {
if (context != null && msg != null) {
msgLong(context, msg);
}
}
/**
* Return a timestamp
*
* @param context Application Context
*/
@SuppressWarnings("UnnecessaryFullyQualifiedName")
public static String getTimestamp(Context context) {
String timestamp = "unknown";
Date now = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context);
java.text.DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(context);
timestamp = dateFormat.format(now) + ' ' + timeFormat.format(now);
return timestamp;
}
public static boolean isPackageInstalled(String packageName, PackageManager pm) {
try {
String mVersion = pm.getPackageInfo(packageName, 0).versionName;
if (mVersion == null) {
return false;
}
} catch (PackageManager.NameNotFoundException notFound) {
Log.e(TAG, "Package could not be found!", notFound);
return false;
}
return true;
}
public static String getSystemProp(Context context, String prop, String def) {
String result = null;
try {
result = SystemPropertiesReflection.get(context, prop);
} catch (IllegalArgumentException iae) {
Log.e(TAG, "Failed to get prop: " + prop);
}
return result == null ? def : result;
}
// If the value is empty or null, fallback to the second property if there's one
public static String getPropWithFallback(Context context, String[] props, String def) {
String value = "";
for (int i = 0; i < props.length; i++) {
value = Helpers.getSystemProp(context, props[i], def);
if (!value.equals(""))
return value;
}
return "";
}
/**
* This method takes a square bitmap and clips it into a circle
* @param bitmap : the image to clip
* @return the clipped bitmap
*/
public static Bitmap getCroppedBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
//Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
//return _bmp;
return output;
}
/**
* This method simply allows to open an url with a shorter syntax
*/
public static void openUrl(ActionBarActivity activity, String url) {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
}