private void initWpDb() {
   if (!createAndVerifyWpDb()) {
     AppLog.e(T.DB, "Invalid database, sign out user and delete database");
     currentBlog = null;
     if (wpDB != null) {
       wpDB.updateLastBlogId(-1);
     }
     // Force DB deletion
     WordPressDB.deleteDatabase(this);
     wpDB = new WordPressDB(this);
   }
 }
  public static void removeWpComUserRelatedData(Context context) {
    // cancel all Volley requests - do this before unregistering push since that uses
    // a Volley request
    VolleyUtils.cancelAllRequests(requestQueue);

    NotificationsUtils.unregisterDevicePushNotifications(context);
    try {
      GCMRegistrar.checkDevice(context);
      GCMRegistrar.unregister(context);
    } catch (Exception e) {
      AppLog.v(T.NOTIFS, "Could not unregister for GCM: " + e.getMessage());
    }

    // delete wpcom blogs
    wpDB.deleteWordPressComBlogs(context);

    // reset default account
    AccountHelper.getDefaultAccount().signout();

    // reset all reader-related prefs & data
    AppPrefs.reset();
    ReaderDatabase.reset();

    // Reset Stats Data
    StatsDatabaseHelper.getDatabase(context).reset();

    // Reset Simperium buckets (removes local data)
    SimperiumUtils.resetBucketsAndDeauthorize();
  }
  @SuppressWarnings("unused")
  public void onEventMainThread(UserSignedOutCompletely event) {
    try {
      SelfSignedSSLCertsManager.getInstance(getContext()).emptyLocalKeyStoreFile();
    } catch (GeneralSecurityException e) {
      AppLog.e(T.UTILS, "Error while cleaning the Local KeyStore File", e);
    } catch (IOException e) {
      AppLog.e(T.UTILS, "Error while cleaning the Local KeyStore File", e);
    }

    flushHttpCache();

    // Analytics resets
    AnalyticsTracker.endSession(false);
    AnalyticsTracker.clearAllData();

    // disable passcode lock
    AbstractAppLock appLock = AppLockManager.getInstance().getCurrentAppLock();
    if (appLock != null) {
      appLock.setPassword(null);
    }

    // dangerously delete all content!
    wpDB.dangerouslyDeleteAllContent();
  }
  public static void setCurrentBlogAndSetVisible(int id) {
    setCurrentBlog(id);

    if (currentBlog != null && currentBlog.isHidden()) {
      wpDB.setDotComBlogsVisibility(id, true);
    }
  }
 /**
  * Get the blog with the specified ID.
  *
  * @param id ID of the blog to retrieve.
  * @return the blog with the specified ID, or null if blog could not be retrieved.
  */
 public static Blog getBlog(int id) {
   try {
     return wpDB.instantiateBlogByLocalId(id);
   } catch (Exception e) {
     return null;
   }
 }
  /**
   * Get the currently active blog.
   *
   * <p>If the current blog is not already set, try and determine the last active blog from the last
   * time the application was used. If we're not able to determine the last active blog, just select
   * the first one.
   */
  public static Blog getCurrentBlog() {
    if (currentBlog == null || !wpDB.isDotComBlogVisible(currentBlog.getRemoteBlogId())) {
      attemptToRestoreLastActiveBlog();
    }

    return currentBlog;
  }
 private static void attemptToRestoreLastActiveBlog() {
   if (setCurrentBlogToLastActive() == null) {
     // fallback to just using the first blog
     List<Map<String, Object>> accounts = WordPress.wpDB.getVisibleBlogs();
     if (accounts.size() > 0) {
       int id = Integer.valueOf(accounts.get(0).get("id").toString());
       setCurrentBlog(id);
       wpDB.updateLastBlogId(id);
     }
   }
 }
 private boolean createAndVerifyWpDb() {
   try {
     wpDB = new WordPressDB(this);
     // verify account data - query will return 1 if any blog names or urls are null
     int result =
         SqlUtils.intForQuery(
             wpDB.getDatabase(),
             "SELECT 1 FROM accounts WHERE blogName IS NULL OR url IS NULL LIMIT 1",
             null);
     return result != 1;
   } catch (RuntimeException e) {
     AppLog.e(T.DB, e);
     return false;
   }
 }