Exemplo n.º 1
0
  /** Called when this application is started up */
  public synchronized void onStartupApplication(final Activity context) {
    if (hasStartedUp || context == null) {
      return;
    }

    // sets up context manager
    ContextManager.setContext(context);

    try {
      database.openForWriting();
      checkForMissingColumns();
    } catch (SQLiteException e) {
      handleSQLiteError(context, e);
      return;
    }

    // show notification if reminders are silenced
    if (context instanceof Activity) {
      AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
      if (!Preferences.getBoolean(R.string.p_rmd_enabled, true)) {
        Toast.makeText(context, R.string.TLA_notification_disabled, Toast.LENGTH_LONG).show();
      } else if (audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION) == 0) {
        Toast.makeText(context, R.string.TLA_notification_volume_low, Toast.LENGTH_LONG).show();
      }
    }

    // read current version
    int latestSetVersion = 0;
    try {
      latestSetVersion = AstridPreferences.getCurrentVersion();
    } catch (Exception e) {
      exceptionService.reportError("astrid-startup-version-read", e); // $NON-NLS-1$
    }

    if (latestSetVersion == 0) {
      if (Preferences.getLong(AstridPreferences.P_FIRST_LAUNCH, -1) < 0) {
        Preferences.setLong(AstridPreferences.P_FIRST_LAUNCH, DateUtilities.now());
      }
    }

    BeastModePreferences.assertHideUntilSectionExists(context, latestSetVersion);

    int version = 0;
    String versionName = "0"; // $NON-NLS-1$
    try {
      PackageManager pm = context.getPackageManager();
      PackageInfo pi = pm.getPackageInfo(Constants.PACKAGE, PackageManager.GET_META_DATA);
      version = pi.versionCode;
      versionName = pi.versionName;
    } catch (Exception e) {
      exceptionService.reportError("astrid-startup-package-read", e); // $NON-NLS-1$
    }

    Log.i(
        "astrid",
        "Astrid Startup. "
            + latestSetVersion
            + //$NON-NLS-1$ //$NON-NLS-2$
            " => "
            + version); //$NON-NLS-1$

    databaseRestoreIfEmpty(context);

    // invoke upgrade service
    boolean justUpgraded = latestSetVersion != version;
    if (justUpgraded && version > 0) {
      if (latestSetVersion > 0) {
        upgradeService.performUpgrade(context, latestSetVersion);
      }
      AstridPreferences.setCurrentVersion(version);
      AstridPreferences.setCurrentVersionName(versionName);
    }

    final int finalLatestVersion = latestSetVersion;

    initializeDatabaseListeners();

    // perform startup activities in a background thread
    new Thread(
            new Runnable() {
              @Override
              public void run() {
                taskService.cleanup();

                // if sync ongoing flag was set, clear it
                gtasksPreferenceService.stopOngoing();

                // perform initialization
                ReminderStartupReceiver.startReminderSchedulingService(context);
                BackupService.scheduleService(context);

                gtasksSyncService.initialize();

                // get and display update messages
                if (finalLatestVersion != 0) {
                  //                    new UpdateMessageService(context).processUpdates();
                }
              }
            })
        .start();

    AstridPreferences.setPreferenceDefaults();
    CalendarStartupReceiver.scheduleCalendarAlarms(
        context,
        false); // This needs to be after set preference defaults for the purposes of ab testing

    showTaskKillerHelp(context);

    hasStartedUp = true;
  }