-
Notifications
You must be signed in to change notification settings - Fork 4
/
IntentUtils.java
173 lines (159 loc) · 5.77 KB
/
IntentUtils.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.bytesizebit.androidutils;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import java.util.List;
/***********
* Android Utils
* Created by Shahar Barsheshet on 13/11/2015.
* bytesizebit@gmail.com
* www.bytesizebit.com
***********/
public class IntentUtils {
private IntentUtils() {
}
/**
* Makes a call
*
* @param context some context
* @param phoneNumber number to call
*/
public static void callNumber(Context context, String phoneNumber) {
openDialerActivityWithAction(context, phoneNumber, Intent.ACTION_CALL);
}
/**
* Dial a number in the phone's keypad
*
* @param context some context
* @param phoneNumber number to dial
*/
public static void dialNumber(Context context, String phoneNumber) {
openDialerActivityWithAction(context, phoneNumber, Intent.ACTION_DIAL);
}
/**
* Open dialer and perform action
* <p>
* Intent.ACTION_DIAL
* Intent.ACTION_CALL
* </p>
*
* @param context some context
* @param phoneNumber number to dial
* @param action action to perform
*/
public static void openDialerActivityWithAction(Context context, String phoneNumber, String action) {
Intent callIntent = new Intent(action);
callIntent.setData(Uri.parse("tel:" + phoneNumber));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(callIntent, 0);
ResolveInfo chosenRi = null;
for (ResolveInfo ri : resolveInfos) {
if (ri.activityInfo != null && ri.activityInfo.name.startsWith("com.android")) {
chosenRi = ri;
}
}
if (chosenRi != null) {
callIntent.setPackage(chosenRi.activityInfo.packageName);
context.startActivity(callIntent);
}
}
/**
* Open compose email activity
*
* @param context some context
* @param addresses email adress to send to
* @param subject email subject
*/
public static void openComposeEmailActivity(Context context, String[] addresses, String subject) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
startIntentIfPossible(context, intent);
}
/**
* Open SMS activity to send SMS
*
* @param context some context
* @param phoneNumber number to send to
* @param body the message
*/
public static void openSendSmsActivity(Context context, String phoneNumber, String body) {
Intent intent = new Intent(Intent.ACTION_VIEW);
String phone = phoneNumber != null ? phoneNumber : "";
intent.setData(Uri.parse("sms:" + phone));
if (StringUtils.isNotBlank(body)) {
intent.putExtra("sms_body", body);
}
startIntentIfPossible(context, intent);
}
/**
* Create a WhatsApp share intent
*
* @param body the text to send
* @return an Intent to share via whatsapp
*/
public static Intent createWhatsAppShareIntent(String body) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, body);
intent.setType("text/plain");
intent.setPackage("com.whatsapp");
return intent;
}
/**
* Starts an Activity if it is exists
*
* @param context the context
* @param intent the intent to start
*/
private static void startIntentIfPossible(Context context, Intent intent) {
if (isActivityAvailableForIntent(context, intent)) {
context.startActivity(intent);
}
}
/**
* Check if there is a valid Activity for the intent
*
* @param context the context
* @param intent the intent to test
* @return {@code true} if Activity exists for that Intent
*/
public static boolean isActivityAvailableForIntent(Context context, Intent intent) {
return intent.resolveActivity(context.getPackageManager()) != null;
}
/**
* Open the playstore page for the current app
*
* @param context the context
*/
public static void openPlayStoreAppPage(Context context) {
final String appPackageName = context.getPackageName(); // getPackageName() from Context or Activity object
try {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
/**
* Check if a service is running
*
* @param className service class name
* @param context some context
* @return {@code true} if service is running
*/
public static boolean isRunningService(String className, Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> runningServices = activityManager.getRunningServices(1000);
for (ActivityManager.RunningServiceInfo runningServiceInfo : runningServices) {
ComponentName service = runningServiceInfo.service;
if (className.equals(service.getClassName())) {
return true;
}
}
return false;
}
}