Tools.java
3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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);
}
}