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

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

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class MapStorage
{
  public static final int GROUP = -2;
  public static final int COUNTRY = -1;

  /// This constants should be equal with storage/storage.hpp, storage::TStatus
  public static final int ON_DISK = 0;
  public static final int NOT_DOWNLOADED = 1;
  public static final int DOWNLOAD_FAILED = 2;
  public static final int DOWNLOADING = 3;
  public static final int IN_QUEUE = 4;
  public static final int UNKNOWN = 5;
  public static final int ON_DISK_OUT_OF_DATE = 6;

  public interface Listener
  {
    public void onCountryStatusChanged(Index idx);
    public void onCountryProgress(Index idx, long current, long total);
  };

  public static class Index
  {
    int mGroup;
    int mCountry;
    int mRegion;

    public Index()
    {
      mGroup = -1;
      mCountry = -1;
      mRegion = -1;
    }

    public Index(int group, int country, int region)
    {
      mGroup = group;
      mCountry = country;
      mRegion = region;
    }

    public boolean isRoot()
    {
      return (mGroup == -1 && mCountry == -1 && mRegion == -1);
    }

    public boolean isValid()
    {
      return !isRoot();
    }

    public Index getChild(int position)
    {
      Index ret = new Index(mGroup, mCountry, mRegion);

      if (ret.mGroup == -1) ret.mGroup = position;
      else if (ret.mCountry == -1) ret.mCountry = position;
      else
      {
        assert(ret.mRegion == -1);
        ret.mRegion = position;
      }

      return ret;
    }

    public Index getParent()
    {
      Index ret = new Index(mGroup, mCountry, mRegion);

      if (ret.mRegion != -1) ret.mRegion = -1;
      else if (ret.mCountry != -1) ret.mCountry = -1;
      else
      {
        assert(ret.mGroup != -1);
        ret.mGroup = -1;
      }

      return ret;
    }

    public boolean isEqual(Index idx)
    {
      return (mGroup == idx.mGroup && mCountry == idx.mCountry && mRegion == idx.mRegion);
    }

    public boolean isChild(Index idx)
    {
      return (idx.getParent().isEqual(this));
    }

    public int getPosition()
    {
      if (mRegion != -1) return mRegion;
      else if (mCountry != -1) return mCountry;
      else return mGroup;
    }

    @Override
    public String toString()
    {
      return ("Index(" + mGroup + ", " + mCountry + ", " + mRegion + ")");
    }
  }

  public native int countriesCount(Index idx);
  public native int countryStatus(Index idx);
  public native long countryLocalSizeInBytes(Index idx);
  public native long countryRemoteSizeInBytes(Index idx);
  public native String countryName(Index idx);
  public native String countryFlag(Index idx);

  public native void downloadCountry(Index idx);
  public native void deleteCountry(Index idx);

  public native Index findIndexByFile(String name);

  public native void showCountry(Index idx);

  public native int subscribe(Listener l);
  public native void unsubscribe(int slotId);

  private native String[] nativeGetMapsWithoutSearch();


  public MapStorage()
  {
  }

  private void runDownloadCountries(Index[] indexes)
  {
    for (int i = 0; i < indexes.length; ++i)
    {
      if (indexes[i] != null)
        downloadCountry(indexes[i]);
    }
  }

  public interface UpdateFunctor
  {
    public void doUpdate();
    public void doCancel();
  }

  public boolean updateMaps(int msgID, Context context, final UpdateFunctor fn)
  {
    // get map names without search index
    final String[] maps = nativeGetMapsWithoutSearch();

    if (maps.length == 0)
      return false;

    // get indexes and filter out maps that already downloading
    int count = 0;
    final Index[] indexes = new Index[maps.length];
    for (int i = 0; i < maps.length; ++i)
    {
      indexes[i] = null;

      final Index idx = findIndexByFile(maps[i]);
      if (idx != null)
      {
        final int st = countryStatus(idx);
        if (st != DOWNLOADING && st != IN_QUEUE)
        {
          indexes[i] = idx;
          ++count;
        }
      }
    }

    // all maps are already downloading
    if (count == 0)
      return false;

    String msg = context.getString(msgID);
    for (int i = 0; i < maps.length; ++i)
    {
      if (indexes[i] != null)
        msg = msg + "\n" + maps[i];
    }

    new AlertDialog.Builder(context)
    .setMessage(msg)
    .setPositiveButton(context.getString(R.string.download), new DialogInterface.OnClickListener()
    {
      @Override
      public void onClick(DialogInterface dlg, int which)
      {
        dlg.dismiss();

        runDownloadCountries(indexes);

        fn.doUpdate();
      }
    })
    .setNegativeButton(context.getString(R.string.later), new DialogInterface.OnClickListener()
    {
      @Override
      public void onClick(DialogInterface dlg, int which)
      {
        dlg.dismiss();

        fn.doCancel();
      }
    })
    .create()
    .show();

    return true;
  }
}