1package eu.siacs.conversations.ui.util;
2
3import android.app.AlertDialog;
4import android.app.Dialog;
5import android.content.Context;
6import android.content.DialogInterface;
7import android.content.Intent;
8import android.net.Uri;
9import android.os.SystemClock;
10import android.support.annotation.StringRes;
11
12import eu.siacs.conversations.R;
13import eu.siacs.conversations.services.QuickConversationsService;
14import eu.siacs.conversations.utils.TimeframeUtils;
15
16public class ApiDialogHelper {
17
18 public static Dialog createError(final Context context, final int code) {
19 @StringRes final int res;
20 switch (code) {
21 case QuickConversationsService.API_ERROR_AIRPLANE_MODE:
22 res = R.string.no_network_connection;
23 break;
24 case QuickConversationsService.API_ERROR_OTHER:
25 res = R.string.unknown_api_error_network;
26 break;
27 case QuickConversationsService.API_ERROR_CONNECT:
28 res = R.string.unable_to_connect_to_server;
29 break;
30 case QuickConversationsService.API_ERROR_SSL_HANDSHAKE:
31 res = R.string.unable_to_establish_secure_connection;
32 break;
33 case QuickConversationsService.API_ERROR_UNKNOWN_HOST:
34 res = R.string.unable_to_find_server;
35 break;
36 case 400:
37 res = R.string.invalid_user_input;
38 break;
39 case 403:
40 res = R.string.the_app_is_out_of_date;
41 break;
42 case 409:
43 res = R.string.logged_in_with_another_device;
44 break;
45 case 500:
46 res = R.string.something_went_wrong_processing_your_request;
47 break;
48 case 502:
49 case 503:
50 case 504:
51 res = R.string.temporarily_unavailable;
52 break;
53 default:
54 res = R.string.unknown_api_error_response;
55 }
56 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
57 builder.setMessage(res);
58 if (code == 403 && resolvable(context, getMarketViewIntent(context))) {
59 builder.setNegativeButton(R.string.cancel, null);
60 builder.setPositiveButton(R.string.update, (dialog, which) -> context.startActivity(getMarketViewIntent(context)));
61 } else {
62 builder.setPositiveButton(R.string.ok, null);
63 }
64 return builder.create();
65 }
66
67 public static Dialog createRateLimited(final Context context, final long timestamp) {
68 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
69 builder.setTitle(R.string.rate_limited);
70 builder.setMessage(context.getString(R.string.try_again_in_x, TimeframeUtils.resolve(context, timestamp - SystemClock.elapsedRealtime())));
71 builder.setPositiveButton(R.string.ok, null);
72 return builder.create();
73 }
74
75 public static Dialog createTooManyAttempts(final Context context) {
76 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
77 builder.setMessage(R.string.too_many_attempts);
78 builder.setPositiveButton(R.string.ok, null);
79 return builder.create();
80 }
81
82 private static Intent getMarketViewIntent(Context context) {
83 return new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + context.getPackageName()));
84 }
85
86 private static boolean resolvable(Context context, Intent intent) {
87 return context.getPackageManager().queryIntentActivities(intent, 0).size() > 0;
88 }
89}