package etelligens.com.foodsafety.utils; import static etelligens.com.foodsafety.utils.Keyword.DB_LOGIN_USER_DATA; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Build; import android.view.View; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; import etelligens.com.foodsafety.activities.SplashScreen; public class Tools { public static Tools tools = new Tools(); /*to clear all data of user*/ public static void clearData(Activity activity) { SharedPreferences userData = activity.getSharedPreferences(DB_LOGIN_USER_DATA, Context.MODE_PRIVATE); SharedPreferences.Editor spEdit; spEdit = userData.edit(); spEdit.clear(); spEdit.apply(); spEdit.commit(); Intent intent = new Intent(activity, SplashScreen.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); activity.startActivity(intent); activity.finish(); } private String decodeString(String decodeValue) { String value= URLDecoder.decode(decodeValue); return value; } private Object changeString(String language) throws UnsupportedEncodingException { String out = URLEncoder.encode(language, "UTF-8"); return out; } public static boolean hasPermission(Context context, String... permissions) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 && context != null && permissions != null) { for (String permission : permissions) { if (ContextCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { return false; } } } return true; } public static Bitmap drawableToBitmap (Drawable drawable) { Bitmap bitmap = null; if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; if(bitmapDrawable.getBitmap() != null) { return bitmapDrawable.getBitmap(); } } if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) { bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel } else { bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } public static Bitmap viewToBitmap(View view){ Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; } public static Bitmap resizeBitmap(Bitmap bitmap, int dstWidth, int dstHeight) { if (bitmap == null) { return null; } if (bitmap.getWidth() == dstWidth && bitmap.getHeight() == dstHeight) { return bitmap; } if (dstWidth < 1 || dstHeight < 1) { throw new IllegalArgumentException("Bitmap output width and height must greater than 1"); } float scaleX = ((float) dstWidth) / bitmap.getWidth(); float scaleY = ((float) dstHeight) / bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.postScale(scaleX, scaleY); return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); } }