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

SplashActivity.java « maps « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 41f4a1a2ea07c9ba182d69916caa97dd4ce35171 (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package com.mapswithme.maps;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.mapswithme.maps.ads.Banner;
import com.mapswithme.maps.analytics.AdvertisingObserver;
import com.mapswithme.maps.analytics.ExternalLibrariesMediator;
import com.mapswithme.maps.base.BaseActivity;
import com.mapswithme.maps.base.BaseActivityDelegate;
import com.mapswithme.maps.base.Detachable;
import com.mapswithme.maps.downloader.UpdaterDialogFragment;
import com.mapswithme.maps.editor.ViralFragment;
import com.mapswithme.maps.location.LocationHelper;
import com.mapswithme.maps.news.BaseNewsFragment;
import com.mapswithme.maps.news.NewsFragment;
import com.mapswithme.maps.news.WelcomeDialogFragment;
import com.mapswithme.maps.permissions.PermissionsDialogFragment;
import com.mapswithme.maps.permissions.StoragePermissionsDialogFragment;
import com.mapswithme.util.Config;
import com.mapswithme.util.Counters;
import com.mapswithme.util.PermissionsUtils;
import com.mapswithme.util.ThemeUtils;
import com.mapswithme.util.UiUtils;
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 com.my.tracker.MyTracker;

public class SplashActivity extends AppCompatActivity
    implements BaseNewsFragment.NewsDialogListener, BaseActivity,
               WelcomeDialogFragment.PolicyAgreementListener
{
  private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
  private static final String TAG = SplashActivity.class.getSimpleName();
  private static final String EXTRA_ACTIVITY_TO_START = "extra_activity_to_start";
  public static final String EXTRA_INITIAL_INTENT = "extra_initial_intent";
  private static final int REQUEST_PERMISSIONS = 1;
  private static final long FIRST_START_DELAY = 300;
  private static final long DELAY = 100;

  private View mIvLogo;
  private View mAppName;

  private boolean mPermissionsGranted;
  private boolean mNeedStoragePermission;
  private boolean mCanceled;
  private boolean mWaitForAdvertisingInfo;

  @NonNull
  private final Runnable mWelcomeScreenDelayedTask = new Runnable()
  {
    @Override
    public void run()
    {
      WelcomeDialogFragment.show(SplashActivity.this);
    }
  };

  @NonNull
  private final Runnable mPermissionsDelayedTask = new Runnable()
  {
    @Override
    public void run()
    {
      PermissionsDialogFragment.show(SplashActivity.this, REQUEST_PERMISSIONS);
    }
  };

  @NonNull
  private final Runnable mInitCoreDelayedTask = new Runnable()
  {
    @Override
    public void run()
    {
      MwmApplication app = (MwmApplication) getApplication();
      if (app.arePlatformAndCoreInitialized())
      {
        UiThread.runLater(mFinalDelayedTask);
        return;
      }

      ExternalLibrariesMediator mediator = MwmApplication.from(getApplicationContext()).getMediator();
      if (!mediator.isAdvertisingInfoObtained())
      {
        LOGGER.i(TAG, "Advertising info not obtained yet, wait...");
        mWaitForAdvertisingInfo = true;
        return;
      }

      mWaitForAdvertisingInfo = false;

      if (!mediator.isLimitAdTrackingEnabled())
      {
        LOGGER.i(TAG, "Limit ad tracking disabled, sensitive tracking initialized");
        mediator.initSensitiveData();
        MyTracker.trackLaunchManually(SplashActivity.this);
      }
      else
      {
        LOGGER.i(TAG, "Limit ad tracking enabled, sensitive tracking not initialized");
      }

      init();
      LOGGER.i(TAG, "Core initialized: " + app.arePlatformAndCoreInitialized());
      if (app.arePlatformAndCoreInitialized() && mediator.isLimitAdTrackingEnabled())
      {
        LOGGER.i(TAG, "Limit ad tracking enabled, rb banners disabled.");
        mediator.disableAdProvider(Banner.Type.TYPE_RB);
      }

//    Run delayed task because resumeDialogs() must see the actual value of mCanceled flag,
//    since onPause() callback can be blocked because of UI thread is busy with framework
//    initialization.
      UiThread.runLater(mFinalDelayedTask);
    }
  };

  @NonNull
  private final Runnable mFinalDelayedTask = new Runnable()
  {
    @Override
    public void run()
    {
      resumeDialogs();
    }
  };

  @NonNull
  private final BaseActivityDelegate mBaseDelegate = new BaseActivityDelegate(this);

  @NonNull
  private final AdvertisingInfoObserver mAdvertisingObserver = new AdvertisingInfoObserver();

  public static void start(@NonNull Context context,
                           @Nullable Class<? extends Activity> activityToStart,
                           @Nullable Intent initialIntent)
  {
    Intent intent = new Intent(context, SplashActivity.class);
    if (activityToStart != null)
      intent.putExtra(EXTRA_ACTIVITY_TO_START, activityToStart);
    if (initialIntent != null)
      intent.putExtra(EXTRA_INITIAL_INTENT, initialIntent);
    context.startActivity(intent);
  }

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    mBaseDelegate.onCreate();
    handleUpdateMapsFragmentCorrectly(savedInstanceState);
    UiThread.cancelDelayedTasks(mWelcomeScreenDelayedTask);
    UiThread.cancelDelayedTasks(mPermissionsDelayedTask);
    UiThread.cancelDelayedTasks(mInitCoreDelayedTask);
    UiThread.cancelDelayedTasks(mFinalDelayedTask);
    Counters.initCounters(this);
    initView();
  }

  @Override
  protected void onNewIntent(Intent intent)
  {
    super.onNewIntent(intent);
    mBaseDelegate.onNewIntent(intent);
  }

  private void handleUpdateMapsFragmentCorrectly(@Nullable Bundle savedInstanceState)
  {
    if (savedInstanceState == null)
      return;

    FragmentManager fm = getSupportFragmentManager();
    DialogFragment updaterFragment = (DialogFragment) fm
        .findFragmentByTag(UpdaterDialogFragment.class.getName());

    if (updaterFragment == null)
      return;

    // If the user revoked the external storage permission while the app was killed
    // we can't update maps automatically during recovering process, so just dismiss updater fragment
    // and ask the user to grant the permission.
    if (!PermissionsUtils.isExternalStorageGranted())
    {
      fm.beginTransaction().remove(updaterFragment).commitAllowingStateLoss();
      fm.executePendingTransactions();
      StoragePermissionsDialogFragment.show(this);
    }
    else
    {
      // If external permissions are still granted we just need to check platform
      // and core initialization, because we may be in the recovering process,
      // i.e. method onResume() may not be invoked in that case.
      if (!MwmApplication.get().arePlatformAndCoreInitialized())
      {
        init();
      }
    }
  }

  @Override
  protected void onStart()
  {
    super.onStart();
    mBaseDelegate.onStart();
    mAdvertisingObserver.attach(this);
    ExternalLibrariesMediator mediator = MwmApplication.from(this).getMediator();
    LOGGER.d(TAG, "Add advertising observer");
    mediator.addAdvertisingObserver(mAdvertisingObserver);
  }

  @Override
  protected void onResume()
  {
    super.onResume();
    mBaseDelegate.onResume();
    mCanceled = false;

    if (Counters.isMigrationNeeded())
    {
      Config.migrateCountersToSharedPrefs();
      Counters.setMigrationExecuted();
    }
    
    boolean isFirstLaunch = WelcomeDialogFragment.isFirstLaunch(this);
    if (isFirstLaunch)
      MwmApplication.from(this).setFirstLaunch(true);

    if (isFirstLaunch || WelcomeDialogFragment.recreate(this))
    {
      UiThread.runLater(mWelcomeScreenDelayedTask, FIRST_START_DELAY);
      return;
    }

    processPermissionGranting();
  }

  private void processPermissionGranting()
  {
    mPermissionsGranted = PermissionsUtils.isExternalStorageGranted();
    DialogFragment storagePermissionsDialog = StoragePermissionsDialogFragment.find(this);
    DialogFragment permissionsDialog = PermissionsDialogFragment.find(this);
    if (!mPermissionsGranted)
    {
      if (mNeedStoragePermission || storagePermissionsDialog != null)
      {
        if (permissionsDialog != null)
          permissionsDialog.dismiss();
        if (storagePermissionsDialog == null)
          StoragePermissionsDialogFragment.show(this);
        return;
      }
      permissionsDialog = PermissionsDialogFragment.find(this);
      if (permissionsDialog == null)
        UiThread.runLater(mPermissionsDelayedTask, DELAY);

      return;
    }

    if (permissionsDialog != null)
      permissionsDialog.dismiss();

    if (storagePermissionsDialog != null)
      storagePermissionsDialog.dismiss();

    runInitCoreTask();
  }

  private void runInitCoreTask()
  {
    UiThread.runLater(mInitCoreDelayedTask, DELAY);
  }

  @Override
  protected void onPause()
  {
    super.onPause();
    mBaseDelegate.onPause();
    mCanceled = true;
    UiThread.cancelDelayedTasks(mWelcomeScreenDelayedTask);
    UiThread.cancelDelayedTasks(mPermissionsDelayedTask);
    UiThread.cancelDelayedTasks(mInitCoreDelayedTask);
    UiThread.cancelDelayedTasks(mFinalDelayedTask);
  }

  @Override
  protected void onStop()
  {
    super.onStop();
    mBaseDelegate.onStop();
    mAdvertisingObserver.detach();
    ExternalLibrariesMediator mediator = MwmApplication.from(this).getMediator();
    LOGGER.d(TAG, "Remove advertising observer");
    mediator.removeAdvertisingObserver(mAdvertisingObserver);
  }

  @Override
  protected void onDestroy()
  {
    super.onDestroy();
    mBaseDelegate.onDestroy();
  }

  private void resumeDialogs()
  {
    if (mCanceled)
      return;

    MwmApplication app = (MwmApplication) getApplication();
    if (!app.arePlatformAndCoreInitialized())
    {
      showExternalStorageErrorDialog();
      return;
    }

    boolean showNews = NewsFragment.showOn(this, this);
    if (!showNews)
    {
      if (ViralFragment.shouldDisplay())
      {
        UiUtils.hide(mIvLogo, mAppName);
        ViralFragment dialog = new ViralFragment();
        dialog.onDismissListener(new Runnable()
        {
          @Override
          public void run()
          {
            onDialogDone();
          }
        });
        dialog.show(getSupportFragmentManager(), "");
      }
      else
      {
        processNavigation();
      }
    }
    else
    {
      UiUtils.hide(mIvLogo, mAppName);
    }
  }

  private void showExternalStorageErrorDialog()
  {
    AlertDialog dialog = new AlertDialog.Builder(this)
        .setTitle(R.string.dialog_error_storage_title)
        .setMessage(R.string.dialog_error_storage_message)
        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
        {
          @Override
          public void onClick(DialogInterface dialog, int which)
          {
            SplashActivity.this.finish();
          }
        })
        .setCancelable(false)
        .create();
    dialog.show();
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                         @NonNull int[] grantResults)
  {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults.length == 0)
      return;

    mPermissionsGranted = PermissionsUtils.computePermissionsResult(permissions, grantResults)
                                          .isExternalStorageGranted();
    mNeedStoragePermission = !mPermissionsGranted;
  }

  @Override
  public void onDialogDone()
  {
    processNavigation();
  }

  @Override
  public void onPolicyAgreementApplied()
  {
    processPermissionGranting();
  }

  private void initView()
  {
    UiUtils.setupStatusBar(this);
    setContentView(R.layout.activity_splash);
    mIvLogo = findViewById(R.id.iv__logo);
    mAppName = findViewById(R.id.tv__app_name);
  }

  private void init()
  {
    MwmApplication app = MwmApplication.from(this);
    boolean success = app.initCore();
    if (!success || !app.isFirstLaunch())
      return;

    PushwooshHelper.nativeProcessFirstLaunch();
    LocationHelper.INSTANCE.onEnteredIntoFirstRun();
    if (!LocationHelper.INSTANCE.isActive())
      LocationHelper.INSTANCE.start();
  }

  @SuppressWarnings("unchecked")
  private void processNavigation()
  {
    Intent input = getIntent();
    Intent result = new Intent(this, DownloadResourcesLegacyActivity.class);
    if (input != null)
    {
      if (input.hasExtra(EXTRA_ACTIVITY_TO_START))
      {
        result = new Intent(this,
                            (Class<? extends Activity>) input.getSerializableExtra(EXTRA_ACTIVITY_TO_START));
      }

      Intent initialIntent = input.hasExtra(EXTRA_INITIAL_INTENT) ?
                           input.getParcelableExtra(EXTRA_INITIAL_INTENT) :
                           input;
      result.putExtra(EXTRA_INITIAL_INTENT, initialIntent);
    }
    startActivity(result);
    finish();
  }

  @Override
  @NonNull
  public Activity get()
  {
    return this;
  }

  @Override
  public int getThemeResourceId(@NonNull String theme)
  {
    if (ThemeUtils.isDefaultTheme(theme))
      return R.style.MwmTheme;

    if (ThemeUtils.isNightTheme(theme))
      return R.style.MwmTheme_Night;

    throw new IllegalArgumentException("Attempt to apply unsupported theme: " + theme);
  }

  boolean isWaitForAdvertisingInfo()
  {
    return mWaitForAdvertisingInfo;
  }

  private static class AdvertisingInfoObserver implements AdvertisingObserver,
                                                          Detachable<SplashActivity>
  {
    @Nullable
    private SplashActivity mActivity;

    @Override
    public void onAdvertisingInfoObtained()
    {
      LOGGER.i(TAG, "Advertising info obtained");
      if (mActivity == null)
        return;

      if (!mActivity.isWaitForAdvertisingInfo())
      {
        LOGGER.i(TAG, "Advertising info not waited");
        return;
      }

      mActivity.runInitCoreTask();
    }

    @Override
    public void attach(@NonNull SplashActivity object)
    {
      mActivity = object;
    }

    @Override
    public void detach()
    {
      mActivity = null;
    }
  }
}