Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BookmarksDownloadManager.java « bookmarks « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aebea855e95ffe2c22004b5fef44902835d067a9 (plain)
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
package com.mapswithme.maps.bookmarks;

import android.app.DownloadManager;
import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Pair;

import com.mapswithme.maps.Framework;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import com.mapswithme.util.HttpClient;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URLEncoder;

public class BookmarksDownloadManager
{
  private static final String QUERY_PARAM_ID_KEY = "id";
  private static final String QUERY_PARAM_NAME_KEY = "name";
  private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.BILLING);
  private static final String TAG = BookmarksDownloadManager.class.getSimpleName();

  @NonNull
  private final Context mContext;

  private BookmarksDownloadManager(@NonNull Context context)
  {
    mContext = context.getApplicationContext();
  }

  @SuppressWarnings("UnusedReturnValue")
  public long enqueueRequest(@NonNull String url) throws MalformedURLException
  {
    Pair<Uri, Uri> uriPair = prepareUriPair(url);

    DownloadManager downloadManager =
        (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);

    if (downloadManager == null)
    {
      throw new NullPointerException(
          "Download manager is null, failed to download url = " + url);
    }

    Uri srcUri = uriPair.first;
    Uri dstUri = uriPair.second;

    LOGGER.d(TAG, "Bookmarks catalog url = " + dstUri);
    DownloadManager.Request request = new DownloadManager
        .Request(dstUri)
        .setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
        .addRequestHeader(HttpClient.HEADER_USER_AGENT, Framework.nativeGetUserAgent())
        .addRequestHeader(HttpClient.HEADER_DEVICE_ID, Framework.nativeGetDeviceId())
        .setDestinationInExternalFilesDir(mContext, null, dstUri.getLastPathSegment());

    String accessToken = Framework.nativeGetAccessToken();
    if (!TextUtils.isEmpty(accessToken))
    {
      LOGGER.d(TAG, "User authorized");
      String headerValue = HttpClient.HEADER_BEARER_PREFFIX + accessToken;
      request.addRequestHeader(HttpClient.HEADER_AUTHORIZATION, headerValue);
    }
    else
    {
      LOGGER.d(TAG, "User not authorized");
    }

    String title = makeTitle(srcUri);
    if (!TextUtils.isEmpty(title))
      request.setTitle(title);

    return downloadManager.enqueue(request);
  }

  @Nullable
  private static String makeTitle(@NonNull Uri srcUri)
  {
    String title = srcUri.getQueryParameter(QUERY_PARAM_NAME_KEY);
    return TextUtils.isEmpty(title) ? srcUri.getQueryParameter(QUERY_PARAM_ID_KEY) : title;
  }

  @NonNull
  private static Pair<Uri, Uri> prepareUriPair(@NonNull String url) throws MalformedURLException
  {
    Uri srcUri = Uri.parse(url);
    String fileId = srcUri.getQueryParameter(QUERY_PARAM_ID_KEY);
    if (TextUtils.isEmpty(fileId))
      throw new MalformedURLException("File id not found");

    String downloadUrl = BookmarkManager.INSTANCE.getCatalogDownloadUrl(fileId);
    Uri.Builder builder = Uri.parse(downloadUrl).buildUpon();

    for (String each : srcUri.getQueryParameterNames())
    {
      String queryParameter = srcUri.getQueryParameter(each);
      try
      {
        queryParameter = URLEncoder.encode(queryParameter, "UTF-8");
      }
      catch (UnsupportedEncodingException e)
      {
        queryParameter = srcUri.getQueryParameter(each);
      }

      builder.appendQueryParameter(each, queryParameter);
    }
    Uri dstUri = builder.build();
    return new Pair<>(srcUri, dstUri);
  }

  @NonNull
  public static BookmarksDownloadManager from(@NonNull Context context)
  {
    return new BookmarksDownloadManager(context);
  }
}