Ejemplo n.º 1
0
 // /////////////////////////////////////////////////////////////
 // SYNC OBSERVER
 // ////////////////////////////////////////////////////////////
 private boolean isSyncActive() {
   try {
     return ContentResolver.isSyncActive(
         AlfrescoAccountManager.getInstance(getActivity())
             .getAndroidAccount(SessionUtils.getAccount(getActivity()).getId()),
         SyncContentProvider.AUTHORITY);
   } catch (Exception e) {
     return false;
   }
 }
  public static void onUpgrade(Context context, SQLiteDatabase db, int oldVersion, int newVersion) {
    // Update database to add the Paid Account flag. This was introduced in
    // DB version 3.
    if (oldVersion <= DatabaseVersionNumber.VERSION_1_0_0) {
      db.execSQL(QUERY_ADD_PAID_ACCOUNT_COLUM);
    }

    // Update database to create account content provider
    if (oldVersion <= DatabaseVersionNumber.VERSION_1_1_0) {
      // Rename old table
      db.execSQL(QUERY_RENAME_TABLE_OLD);
      db.execSQL(QUERY_TABLE_CREATE);
      db.execSQL(REPLICATE_ACCOUNT);
      db.execSQL(QUERY_DROP_TABLE_OLD);
    }

    // Migrate all accounts to Account Manager
    if (oldVersion < DatabaseVersionNumber.VERSION_1_5_0) {
      //
      List<Account> accounts = AccountProvider.retrieveAccounts(db);
      android.accounts.Account newAccount;
      String accountName;
      AccountManager mAccountManager = AccountManager.get(context);
      SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
      SyncContentManager syncManager = SyncContentManager.getInstance(context);
      for (Account account : accounts) {
        // Check Account Name
        accountName =
            AlfrescoAccountManager.getInstance(context)
                .createUniqueAccountName(account.getUsername());
        newAccount = new android.accounts.Account(accountName, AlfrescoAccount.ACCOUNT_TYPE);
        Bundle b = new Bundle();
        // Very important !
        // We keep the same Account Id from previous version.
        // Used by the SyncService
        b.putString(AlfrescoAccount.ACCOUNT_ID, Long.toString(account.getId()));
        BundleUtils.addIfNotEmpty(b, AlfrescoAccount.ACCOUNT_NAME, account.getDescription());
        BundleUtils.addIfNotEmpty(b, AlfrescoAccount.ACCOUNT_URL, account.getUrl());
        BundleUtils.addIfNotEmpty(b, AlfrescoAccount.ACCOUNT_USERNAME, account.getUsername());
        BundleUtils.addIfNotEmpty(
            b, AlfrescoAccount.ACCOUNT_REPOSITORY_ID, account.getRepositoryId());
        BundleUtils.addIfNotEmpty(
            b, AlfrescoAccount.ACCOUNT_REPOSITORY_TYPE_ID, String.valueOf(account.getTypeId()));
        BundleUtils.addIfNotEmpty(
            b,
            AlfrescoAccount.ACCOUNT_IS_PAID_ACCOUNT,
            Boolean.toString(account.getIsPaidAccount()));
        BundleUtils.addIfNotEmpty(b, AlfrescoAccount.ACCOUNT_ACTIVATION, account.getActivation());
        BundleUtils.addIfNotEmpty(
            b, AlfrescoAccount.ACCOUNT_ACCESS_TOKEN, account.getAccessToken());
        BundleUtils.addIfNotEmpty(
            b, AlfrescoAccount.ACCOUNT_REFRESH_TOKEN, account.getRefreshToken());

        // Time to create.
        if (mAccountManager.addAccountExplicitly(newAccount, account.getPassword(), b)) {
          // Let's define if sync automatically regarding previous
          // settings
          syncManager.setActivateSync(
              account.getId(), sharedPref.getBoolean(SYNCHRO_PREFIX + account.getId(), false));
          sharedPref.edit().remove(SYNCHRO_PREFIX + account.getId()).apply();
        }

        Log.i(
            "Migration",
            "Account " + account.getDescription() + "[" + account.getId() + "] has been migrated");
      }
      // Delete old table
      db.execSQL(QUERY_DROP_TABLE);
    }
  }