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
|
package com.mapswithme.maps.bookmarks;
import android.app.Application;
import android.app.DownloadManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.JobIntentService;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.text.TextUtils;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.bookmarks.data.Error;
import com.mapswithme.maps.bookmarks.data.Result;
import com.mapswithme.util.Utils;
import com.mapswithme.util.concurrency.UiThread;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import com.mapswithme.util.statistics.PushwooshHelper;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
public class SystemDownloadCompletedService extends JobIntentService
{
public final static String ACTION_DOWNLOAD_COMPLETED = "action_download_completed";
public final static String EXTRA_DOWNLOAD_STATUS = "extra_download_status";
@Override
public void onCreate()
{
super.onCreate();
MwmApplication app = (MwmApplication) getApplication();
if (app.arePlatformAndCoreInitialized())
return;
app.initCore();
}
@Override
protected void onHandleWork(@NonNull Intent intent)
{
DownloadManager manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
if (manager == null)
throw new IllegalStateException("Failed to get a download manager");
final OperationStatus status = calculateStatus(manager, intent);
Logger logger = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.BILLING);
String tag = SystemDownloadCompletedService.class.getSimpleName();
logger.i(tag, "Download status: " + status);
UiThread.run(new SendStatusTask(getApplicationContext(), status));
}
@NonNull
private OperationStatus calculateStatus(@NonNull DownloadManager manager, @NonNull Intent intent)
{
try
{
return calculateStatusInternal(manager, intent);
}
catch (Exception e)
{
return new OperationStatus(null, new Error(e.getMessage()));
}
}
@NonNull
private OperationStatus calculateStatusInternal(
@NonNull DownloadManager manager, @NonNull Intent intent) throws IOException
{
Cursor cursor = null;
try
{
final long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager.Query query = new DownloadManager.Query().setFilterById(id);
cursor = manager.query(query);
if (cursor.moveToFirst())
{
if (isDownloadFailed(cursor))
{
Error error = new Error(getHttpStatus(cursor), getErrorMessage(cursor));
return new OperationStatus(null, error);
}
logToPushWoosh((Application) getApplicationContext(), cursor);
Result result = new Result(getFilePath(cursor), getArchiveId(cursor));
return new OperationStatus(result, null);
}
throw new IOException("Failed to move the cursor at first row");
}
finally
{
Utils.closeSafely(cursor);
}
}
private static boolean isDownloadFailed(@NonNull Cursor cursor)
{
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
return status != DownloadManager.STATUS_SUCCESSFUL;
}
@Nullable
private static String getFilePath(@NonNull Cursor cursor)
{
String localUri = getColumnValue(cursor, DownloadManager.COLUMN_LOCAL_URI);
return localUri == null ? null : Uri.parse(localUri).getPath();
}
@Nullable
private static String getArchiveId(@NonNull Cursor cursor)
{
return Uri.parse(getColumnValue(cursor, DownloadManager.COLUMN_URI)).getLastPathSegment();
}
@Nullable
private static String getColumnValue(@NonNull Cursor cursor, @NonNull String columnName)
{
return cursor.getString(cursor.getColumnIndex(columnName));
}
private static int getHttpStatus(@NonNull Cursor cursor)
{
String rawStatus = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
return Integer.parseInt(rawStatus);
}
@Nullable
private static String getErrorMessage(@NonNull Cursor cursor)
{
return cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
}
private static void logToPushWoosh(@NonNull Application application, @NonNull Cursor cursor)
{
String url = getColumnValue(cursor, DownloadManager.COLUMN_URI);
if (TextUtils.isEmpty(url))
return;
String decodedUrl;
try
{
decodedUrl = URLDecoder.decode(url, "UTF-8");
}
catch (UnsupportedEncodingException exception)
{
decodedUrl = "";
}
BookmarkPaymentDataParser p = new BookmarkPaymentDataParser();
String productId = p.getParameter(decodedUrl, BookmarkPaymentDataParser.PRODUCT_ID);
String name = p.getParameter(decodedUrl, BookmarkPaymentDataParser.NAME);
MwmApplication app = (MwmApplication) application;
if (TextUtils.isEmpty(productId))
{
app.sendPushWooshTags("Bookmarks_Guides_free_title", new String[] {name});
}
else
{
app.sendPushWooshTags("Bookmarks_Guides_paid_tier", new String[] {productId});
app.sendPushWooshTags("Bookmarks_Guides_paid_title", new String[] {name});
app.sendPushWooshTags("Bookmarks_Guides_paid_date",
new String[] {PushwooshHelper.nativeGetFormattedTimestamp()});
}
}
private static class SendStatusTask implements Runnable
{
@NonNull
private final Context mAppContext;
@NonNull
private final OperationStatus mStatus;
private SendStatusTask(@NonNull Context applicationContext,
@NonNull OperationStatus status)
{
mAppContext = applicationContext;
mStatus = status;
}
@Override
public void run()
{
Intent intent = new Intent(ACTION_DOWNLOAD_COMPLETED);
intent.putExtra(EXTRA_DOWNLOAD_STATUS, mStatus);
LocalBroadcastManager.getInstance(mAppContext).sendBroadcast(intent);
}
}
}
|