-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppUtils.java
92 lines (76 loc) · 3.27 KB
/
AppUtils.java
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
package tools.amolood.qrpanda.utility;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Vibrator;
import android.widget.Toast;
import java.io.File;
import tools.amolood.qrpanda.R;
public class AppUtils {
private static long backPressed = 0;
public static void tapToExit(Activity activity) {
if (backPressed + 2500 > System.currentTimeMillis()) {
activity.finish();
} else {
showToast(activity.getApplicationContext(), activity.getResources().getString(R.string.tapAgain));
}
backPressed = System.currentTimeMillis();
}
public static void showToast(Context context, String msg) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager connectivityManager = ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE));
if (connectivityManager != null) {
return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
}
return false;
}
public static void shareApp(Activity activity) {
try {
final String appPackageName = activity.getPackageName();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, activity.getResources().getString(R.string.share) + " https://play.google.com/store/apps/details?id=" + appPackageName);
sendIntent.setType("text/plain");
activity.startActivity(sendIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void rateThisApp(Activity activity) {
try {
activity.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + activity.getPackageName())));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void vibrateDevice(Context context) {
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
if (v != null) {
v.vibrate(500);
}
}
public static void share(Activity activity, String fileUrl) {//, String text) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
//shareIntent.putExtra(Intent.EXTRA_TEXT, text);
Uri uri = Uri.fromFile(new File(fileUrl));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
activity.startActivity(Intent.createChooser(shareIntent, "Share via"));
}
public static void copyToClipboard(Context context, String text) {
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", text);
if (clipboard != null) {
clipboard.setPrimaryClip(clip);
}
showToast(context, context.getResources().getString(R.string.copied));
}
}