-
Notifications
You must be signed in to change notification settings - Fork 138
/
ConvenienceBuilder.java
357 lines (320 loc) · 14.5 KB
/
ConvenienceBuilder.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package com.danielstone.materialaboutlibrary;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import android.webkit.WebView;
import android.widget.Toast;
import com.danielstone.materialaboutlibrary.items.MaterialAboutActionItem;
import com.danielstone.materialaboutlibrary.items.MaterialAboutItemOnClickAction;
import com.danielstone.materialaboutlibrary.items.MaterialAboutTitleItem;
import com.danielstone.materialaboutlibrary.model.MaterialAboutCard;
import com.danielstone.materialaboutlibrary.util.OpenSourceLicense;
@SuppressWarnings("JavaDoc")
public class ConvenienceBuilder {
public static MaterialAboutTitleItem createAppTitleItem(String appName, Drawable applicationIcon) {
return new MaterialAboutTitleItem(appName, null, applicationIcon);
}
public static MaterialAboutTitleItem createAppTitleItem(Context c) {
Context applicationContext = c.getApplicationContext();
PackageManager packageManager = applicationContext.getPackageManager();
ApplicationInfo applicationInfo = applicationContext.getApplicationInfo();
CharSequence appName = packageManager.getApplicationLabel(applicationInfo);
Drawable applicationIcon = packageManager.getApplicationIcon(applicationInfo);
return createAppTitleItem(appName == null ? "" : appName.toString(), applicationIcon);
}
/**
* Creates an item with version info read from the PackageManager for current application.
*
* @param c context
* @param icon
* @param text
* @param includeVersionCode
* @return Item to add to card.
*/
public static MaterialAboutActionItem createVersionActionItem(Context c, Drawable icon, CharSequence text, boolean includeVersionCode) {
String versionName = "";
int versionCode = 0;
try {
PackageInfo pInfo = c.getPackageManager().getPackageInfo(c.getPackageName(), 0);
versionName = pInfo.versionName;
versionCode = pInfo.versionCode;
} catch (PackageManager.NameNotFoundException ignored) {
// This shouldn't happen.
}
return new MaterialAboutActionItem.Builder()
.text(text)
.subText(versionName + (includeVersionCode ? " (" + versionCode + ")" : ""))
.icon(icon)
.build();
}
public static MaterialAboutItemOnClickAction createWebViewDialogOnClickAction(final Context c, final CharSequence dialogTitle, final String htmlString, final boolean isStringUrl, final boolean supportZoom) {
return createWebViewDialogOnClickAction(c, dialogTitle, c.getString(R.string.mal_close), htmlString, isStringUrl, supportZoom);
}
public static MaterialAboutItemOnClickAction createWebViewDialogOnClickAction(final Context c, final CharSequence dialogTitle, final CharSequence dialogNegativeButton, final String htmlString, final boolean isStringUrl, final boolean supportZoom) {
return new MaterialAboutItemOnClickAction() {
@Override
public void onClick() {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(c);
alertBuilder.setTitle(dialogTitle);
final WebView wv = new WebView(c);
wv.getSettings().setSupportZoom(supportZoom);
if (!supportZoom) {
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
}
if (isStringUrl) {
wv.loadUrl(htmlString);
} else {
wv.loadData(htmlString, "text/html; charset=utf-8", "UTF-8");
}
alertBuilder.setView(wv);
alertBuilder.setNegativeButton(dialogNegativeButton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
wv.destroy();
dialog.dismiss();
}
});
final AlertDialog dialog = alertBuilder.create();
dialog.show();
}
};
}
public static MaterialAboutActionItem createWebViewDialogItem(Context c, Drawable icon, CharSequence text, @Nullable CharSequence subText, CharSequence dialogTitle, String htmlString, boolean isStringUrl, boolean supportZoom) {
return createWebViewDialogItem(c, icon, text, subText, dialogTitle, c.getString(R.string.mal_close), htmlString, isStringUrl, supportZoom);
}
public static MaterialAboutActionItem createWebViewDialogItem(Context c, Drawable icon, CharSequence text, @Nullable CharSequence subText, CharSequence dialogTitle, CharSequence dialogNegativeButton, String htmlString, boolean isStringUrl, boolean supportZoom) {
return new MaterialAboutActionItem.Builder()
.text(text)
.subText(subText)
.icon(icon)
.setOnClickAction(createWebViewDialogOnClickAction(c, dialogTitle, dialogNegativeButton, htmlString, isStringUrl, supportZoom))
.build();
}
public static MaterialAboutItemOnClickAction createWebsiteOnClickAction(final Context c, final Uri websiteUrl) {
return new MaterialAboutItemOnClickAction() {
@Override
public void onClick() {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(websiteUrl);
try {
c.startActivity(i);
} catch (Exception e) {
// No activity to handle intent
Toast.makeText(c, R.string.mal_activity_exception, Toast.LENGTH_SHORT).show();
}
}
};
}
/**
* Creates an ActionItem which will open a url when tapped
*
* @param c context
* @param text
* @param icon
* @param websiteUrl (subText)
* @return Item to add to card.
*/
public static MaterialAboutActionItem createWebsiteActionItem(Context c, Drawable icon, CharSequence text, boolean showAddress, final Uri websiteUrl) {
return new MaterialAboutActionItem.Builder()
.text(text)
.subText((showAddress ? websiteUrl.toString() : null))
.icon(icon)
.setOnClickAction(createWebsiteOnClickAction(c, websiteUrl))
.build();
}
/**
* Creates a MaterialAboutItemOnClickAction that will open
* the Google Play store listing for the app.
*
* @param c context
* @return onClickAction
*/
public static MaterialAboutItemOnClickAction createRateOnClickAction(final Context c) {
Uri uri = Uri.parse("market://details?id=" + c.getPackageName());
final Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
} else {
goToMarket.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY |
Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
}
return new MaterialAboutItemOnClickAction() {
@Override
public void onClick() {
try {
c.startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
c.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + c.getPackageName())));
}
}
};
}
/**
* Creates an ActionItem which will open the play store when tapped
*
* @param c context
* @param text
* @param subText
* @param icon
* @return Item to add to card.
*/
public static MaterialAboutActionItem createRateActionItem(Context c, Drawable icon, CharSequence text, @Nullable CharSequence subText) {
return new MaterialAboutActionItem.Builder()
.text(text)
.subText(subText)
.icon(icon)
.setOnClickAction(createRateOnClickAction(c))
.build();
}
public static MaterialAboutItemOnClickAction createEmailOnClickAction(final Context c, String email, String emailSubject) {
return createEmailOnClickAction(c, email, emailSubject, c.getString(R.string.mal_send_email));
}
/**
* Creates a MaterialAboutItemOnClickAction that will open
* an email intent with specified address.
*
* @param c context
* @param email email address
* @return onClickAction
*/
public static MaterialAboutItemOnClickAction createEmailOnClickAction(final Context c, String email, String emailSubject, final CharSequence chooserTitle) {
final Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + email));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, emailSubject);
return new MaterialAboutItemOnClickAction() {
@Override
public void onClick() {
try {
c.startActivity(Intent.createChooser(emailIntent, chooserTitle));
} catch (Exception e) {
// No activity to handle intent
Toast.makeText(c, R.string.mal_activity_exception, Toast.LENGTH_SHORT).show();
}
}
};
}
/**
* Creates an ActionItem which will open the an email intent when tapped
*
* @param c context
* @param text
* @param icon
* @param email email address (also used as subText)
* @return Item to add to card.
*/
public static MaterialAboutActionItem createEmailItem(Context c, Drawable icon, CharSequence text, boolean showEmail, String email, String emailSubject, CharSequence chooserTitle) {
return new MaterialAboutActionItem.Builder()
.text(text)
.subText((showEmail ? email : null))
.icon(icon)
.setOnClickAction(createEmailOnClickAction(c, email, emailSubject, chooserTitle))
.build();
}
public static MaterialAboutActionItem createEmailItem(Context c, Drawable icon, CharSequence text, boolean showEmail, String email, String emailSubject) {
return createEmailItem(c, icon, text, showEmail, email, emailSubject, c.getString(R.string.mal_send_email));
}
/**
* Creates a MaterialAboutItemOnClickAction that will open
* the dialer with specified number.
*
* @param c context
* @param number phone number
* @return onClickAction
*/
public static MaterialAboutItemOnClickAction createPhoneOnClickAction(final Context c, String number) {
final Intent phoneIntent = new Intent(Intent.ACTION_DIAL);
phoneIntent.setData(Uri.parse("tel:" + number));
return new MaterialAboutItemOnClickAction() {
@Override
public void onClick() {
try {
c.startActivity(phoneIntent);
} catch (Exception e) {
// No activity to handle intent
Toast.makeText(c, R.string.mal_activity_exception, Toast.LENGTH_SHORT).show();
}
}
};
}
/**
* Creates an ActionItem which will open the dialer when tapped
*
* @param c context
* @param text
* @param icon
* @param number phone number (also used as subText)
* @return Item to add to card.
*/
public static MaterialAboutActionItem createPhoneItem(Context c, Drawable icon, CharSequence text, boolean showNumber, String number) {
return new MaterialAboutActionItem.Builder()
.text(text)
.subText((showNumber ? number : null))
.icon(icon)
.setOnClickAction(createPhoneOnClickAction(c, number))
.build();
}
/**
* Creates a MaterialAboutItemOnClickAction that will open
* maps with a query.
* Query can be either lat,lng(label) or written address
*
* @param c context
* @param addressQuery address query
* @return onClickAction
*/
public static MaterialAboutItemOnClickAction createMapOnClickAction(final Context c, String addressQuery) {
final Intent mapIntent = new Intent(Intent.ACTION_VIEW);
mapIntent.setData(Uri.parse("geo:0,0").buildUpon().appendQueryParameter("q", addressQuery).build());
return new MaterialAboutItemOnClickAction() {
@Override
public void onClick() {
try {
c.startActivity(mapIntent);
} catch (Exception e) {
// No activity to handle intent
Toast.makeText(c, R.string.mal_activity_exception, Toast.LENGTH_SHORT).show();
}
}
};
}
/**
* Creates an ActionItem which will open maps when tapped
* Query can be either lat,lng(label) or written address
*
* @param c context
* @param text
* @param subText can be set to null
* @param icon
* @param addressQuery addressQuery
* @return Item to add to card.
*/
public static MaterialAboutActionItem createMapItem(Context c, Drawable icon, CharSequence text, CharSequence subText, String addressQuery) {
return new MaterialAboutActionItem.Builder()
.text(text)
.subText(subText)
.icon(icon)
.setOnClickAction(createMapOnClickAction(c, addressQuery))
.build();
}
public static MaterialAboutCard createLicenseCard(Context c, Drawable icon, CharSequence libraryTitle, CharSequence year, CharSequence name, OpenSourceLicense license) {
MaterialAboutActionItem licenseItem = new MaterialAboutActionItem.Builder()
.icon(icon)
.setIconGravity(MaterialAboutActionItem.GRAVITY_TOP)
.text(libraryTitle)
.subText(String.format(c.getString(license.getResourceId()), year, name))
.build();
return new MaterialAboutCard.Builder().addItem(licenseItem).build();
}
}