package info.papdt.express.helper.support;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
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.graphics.RectF;
import android.net.ConnectivityManager;
import android.os.Build;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import info.papdt.express.helper.api.KuaiDi100Helper;
public class Utility {
public static boolean isChrome() {
return Build.BRAND.equals("chromium") || Build.BRAND.equals("chrome");
}
public static int getStatusBarHeight(Context context) {
int result = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = context.getResources().getDimensionPixelSize(resourceId);
}
return result;
}
public static Bitmap toRoundBitmap(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2;
top = 0;
bottom = width;
left = 0;
right = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width,
height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, src, dst, paint);
return output;
}
public static boolean isApplicationAvailable(Context context, String packageName) {
if (packageName == null || "".equals(packageName))
return false;
try {
ApplicationInfo info = context.getPackageManager()
.getApplicationInfo(
packageName,
PackageManager.GET_UNINSTALLED_PACKAGES
);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
public static void saveFile(Context context, String name, String text) throws IOException {
FileOutputStream fos = context.openFileOutput(name, Context.MODE_PRIVATE);
fos.write(text.getBytes());
fos.close();
}
public static String readFile(Context context, String name) throws IOException{
File file = context.getFileStreamPath(name);
InputStream is = new FileInputStream(file);
byte b[] = new byte[(int) file.length()];
is.read(b);
is.close();
String string = new String(b);
return string;
}
public static void startServiceAlarm(Context context, Class<?> service, long interval) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, service);
PendingIntent p = PendingIntent.getService(context, 10000, i, PendingIntent.FLAG_CANCEL_CURRENT);
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 0, interval, p);
}
public static void stopServiceAlarm(Context context, Class<?> service) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, service);
PendingIntent p = PendingIntent.getService(context, 10000, i, PendingIntent.FLAG_CANCEL_CURRENT);
am.cancel(p);
}
public static void startServices(Context context) {
Settings settings = Settings.getInstance(context);
int interval = getIntervalTime(settings.getInt(Settings.KEY_NOTIFICATION_INTERVAL, 0));
if (interval > -1) {
Log.i("Utils", "Interval : " + interval);
startServiceAlarm(context, ReminderService.class, interval);
}
}
public static void stopServices(Context context) {
stopServiceAlarm(context, ReminderService.class);
}
public static void restartServices(Context context) {
stopServices(context);
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) {
startServices(context);
}
}
public static int getIntervalTime(int id) {
switch (id){
case 0:
return 1 * 30 * 60 * 1000;
case 1:
return 1 * 60 * 60 * 1000;
case 2:
return 3 * 30 * 60 * 1000;
case 3:
return 3 * 60 * 60 * 1000;
case 4:
return -1;
}
return -1;
}
public static boolean isDisturbTime(Calendar c) {
int hours = c.get(Calendar.HOUR_OF_DAY);
return hours >= 23 | hours < 6;
}
}