Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2014 Siarhei Luskanau * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package siarhei.luskanau.gps.tracker.free.utils; import android.app.Notification; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.BatteryManager; import android.provider.Settings; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import android.telephony.TelephonyManager; import android.util.Log; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.math.BigDecimal; import java.util.Locale; import siarhei.luskanau.gps.tracker.free.R; import siarhei.luskanau.gps.tracker.free.ui.app.AppActivity; public class Utils { private static final String TAG = "Utils"; public static double round(double value, int scale) { return new BigDecimal(value).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } public static String getDeviceId(final Context context) { String deviceId = null; TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { deviceId = telephonyManager.getDeviceId(); } if (deviceId == null) { deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); } if (deviceId != null) { deviceId = deviceId.toUpperCase(Locale.ENGLISH); deviceId = deviceId.replace(" ", ""); if (deviceId != null) { for (; deviceId.length() < 15;) { deviceId = "0" + deviceId; } } } return deviceId; } public static boolean isPowerConnected(Context context) { Intent intent = context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB; } public static Notification createNotification(Context context, String contentText) { NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_launcher).setContentTitle(context.getString(R.string.tracker_app_name)) .setContentText(contentText); TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context).addParentStack(AppActivity.class) .addNextIntent(new Intent(context, AppActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)); PendingIntent resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); notificationBuilder.setContentIntent(resultPendingIntent); return notificationBuilder.build(); } public static boolean isWifiNetworkType(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager != null) { NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { return networkInfo.getType() == ConnectivityManager.TYPE_WIFI; } } return false; } public static boolean isOnline(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivityManager == null) { return false; } NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); if (networkInfo == null) { return false; } return networkInfo.isConnected(); } public static int getVersionCode(Context context) { try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); return packageInfo.versionCode; } catch (PackageManager.NameNotFoundException e) { Log.e(TAG, e.toString(), e); } return 0; } public static byte[] getBytes(InputStream inputStream) throws Exception { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; for (;;) { int length = inputStream.read(buffer); if (length == -1) { break; } else { if (length > 0) { byteArrayOutputStream.write(buffer, 0, length); } else { try { Thread.sleep(300); } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } } } } inputStream.close(); return byteArrayOutputStream.toByteArray(); } }