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

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

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.mapswithme.maps.MapStorage.Index;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import com.mapswithme.maps.location.LocationService;
import com.mapswithme.maps.state.AppStateManager;
import com.mapswithme.maps.state.SuppotedState;
import com.mapswithme.util.Utils;


public class MWMApplication extends android.app.Application implements MapStorage.Listener
{
  private final static String TAG = "MWMApplication";

  private static MWMApplication mSelf;


  private LocationService m_location = null;
  private LocationState m_locationState = null;
  private MapStorage m_storage = null;
  private int m_slotID = 0;

  private AppStateManager mAppStateManager = new AppStateManager();

  private boolean m_isProVersion = false;

  // Set default string to Google Play page.
  private final static String m_defaultProURL = "http://play.google.com/store/apps/details?id=com.mapswithme.maps.pro";
  private String m_proVersionURL = m_defaultProURL;


  public MWMApplication()
  {
    super();
    mSelf = this;
  }

  /**
   * Just for convenience.
   *
   * @return global MWMApp
   */
  public static MWMApplication get()
  {
    return mSelf;
  }

  private void showDownloadToast(int resID, Index idx)
  {
    final String msg = String.format(getString(resID), m_storage.countryName(idx));
    Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
  }

  @Override
  public void onCountryStatusChanged(Index idx)
  {
    switch (m_storage.countryStatus(idx))
    {
    case MapStorage.ON_DISK:
      showDownloadToast(R.string.download_country_success, idx);
      break;

    case MapStorage.DOWNLOAD_FAILED:
      showDownloadToast(R.string.download_country_failed, idx);
      break;
    }
  }

  @Override
  public void onCountryProgress(Index idx, long current, long total)
  {
  }

  @Override
  public void onCreate()
  {
    super.onCreate();

    m_isProVersion = getPackageName().endsWith(".pro");

    // http://stackoverflow.com/questions/1440957/httpurlconnection-getresponsecode-returns-1-on-second-invocation
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.ECLAIR_MR1)
      System.setProperty("http.keepAlive", "false");

    // get url for PRO version
    if (!m_isProVersion)
    {
      AssetManager assets = getAssets();
      InputStream stream = null;
      try
      {
        stream = assets.open("app_info.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

        final String s = reader.readLine();
        if (s.length() > 0)
          m_proVersionURL = s;

        Log.i(TAG, "Pro version url: " + m_proVersionURL);
      }
      catch (IOException ex)
      {
        // suppress exceptions - pro version doesn't need app_info.txt
      }
      Utils.closeStream(stream);
    }

    final String extStoragePath = getDataStoragePath();
    final String extTmpPath = getTempPath();

    // Create folders if they don't exist
    new File(extStoragePath).mkdirs();
    new File(extTmpPath).mkdirs();

    // init native framework
    nativeInit(getApkPath(), extStoragePath, extTmpPath,
               getOBBGooglePath(), m_isProVersion);

    m_slotID = getMapStorage().subscribe(this);

    // init cross-platform strings bundle
    nativeAddLocalization("country_status_added_to_queue", getString(R.string.country_status_added_to_queue));
    nativeAddLocalization("country_status_downloading", getString(R.string.country_status_downloading));
    nativeAddLocalization("country_status_download", getString(R.string.country_status_download));
    nativeAddLocalization("country_status_download_failed", getString(R.string.country_status_download_failed));
    nativeAddLocalization("try_again", getString(R.string.try_again));
    nativeAddLocalization("not_enough_free_space_on_sdcard", getString(R.string.not_enough_free_space_on_sdcard));
    nativeAddLocalization("dropped_pin", getString(R.string.dropped_pin));
    nativeAddLocalization("my_places", getString(R.string.my_places));
    nativeAddLocalization("my_position", getString(R.string.my_position));

    // init BookmarkManager (automatically loads bookmarks)
    if (m_isProVersion)
      BookmarkManager.getBookmarkManager(getApplicationContext());
  }

  public LocationService getLocationService()
  {
    if (m_location == null)
      m_location = new LocationService(this);

    return m_location;
  }

  public LocationState getLocationState()
  {
    if (m_locationState == null)
      m_locationState = new LocationState();

    return m_locationState;
  }

  public MapStorage getMapStorage()
  {
    if (m_storage == null)
      m_storage = new MapStorage();

    return m_storage;
  }

  public String getApkPath()
  {
    try
    {
      return getPackageManager().getApplicationInfo(getPackageName(), 0).sourceDir;
    }
    catch (NameNotFoundException e)
    {
      Log.e(TAG, "Can't get apk path from PackageManager");
      return "";
    }
  }

  public String getDataStoragePath()
  {
    return Environment.getExternalStorageDirectory().getAbsolutePath() + "/MapsWithMe/";
  }

  public String getTempPath()
  {
    // Can't use getExternalCacheDir() here because of API level = 7.
    return getExtAppDirectoryPath("cache");
  }

  public String getExtAppDirectoryPath(String folder)
  {
    final String storagePath = Environment.getExternalStorageDirectory().getAbsolutePath();
    return storagePath.concat(String.format("/Android/data/%s/%s/", getPackageName(), folder));
  }

  private String getOBBGooglePath()
  {
    final String storagePath = Environment.getExternalStorageDirectory().getAbsolutePath();
    return storagePath.concat(String.format("/Android/obb/%s/", getPackageName()));
  }

  /// Check if we have free space on storage (writable path).
  public native boolean hasFreeSpace(long size);

  public double getForegroundTime()
  {
    return nativeGetDouble("AllForegroundTime", 0);
  }

  public boolean isProVersion()
  {
    return m_isProVersion;
  }

  public String getProVersionURL()
  {
    return m_proVersionURL;
  }

  public String getDefaultProVersionURL()
  {
    return m_defaultProURL;
  }

  public AppStateManager getAppStateManager()
  {
    return mAppStateManager;
  }

  public SuppotedState getAppState()
  {
    return mAppStateManager.getCurrentState();
  }

  static
  {
    System.loadLibrary("mapswithme");
  }

  private native void nativeInit(String apkPath, String storagePath,
                                 String tmpPath, String obbGooglePath,
                                 boolean isPro);

  public native boolean nativeIsBenchmarking();

  /// @name Dealing with dialogs.
  /// @note Constants should be equal with map/dialog_settings.hpp
  /// @{
  static public final int FACEBOOK = 0;
  static public final int BUYPRO = 1;
  public native boolean shouldShowDialog(int dlg);

  static public final int OK = 0;
  static public final int LATER = 1;
  static public final int NEVER = 2;
  public native void submitDialogResult(int dlg, int res);
  /// @}

  private native void nativeAddLocalization(String name, String value);

  /// Dealing with Settings
  public native boolean nativeGetBoolean(String name, boolean defaultValue);
  public native void nativeSetBoolean(String name, boolean value);
  public native int nativeGetInt(String name, int defaultValue);
  public native void nativeSetInt(String name, int value);
  public native long nativeGetLong(String name, long defaultValue);
  public native void nativeSetLong(String name, long value);

  public native double nativeGetDouble(String name, double defaultValue);
  public native void nativeSetDouble(String name, double value);
}