/*
   * converts a date to a relative time span ("8h", "3d", etc.) - similar to
   * DateUtils.getRelativeTimeSpanString but returns shorter result
   */
  public static String javaDateToTimeSpan(final Date date) {
    if (date == null) return "";

    long passedTime = date.getTime();
    long currentTime = System.currentTimeMillis();

    // return "now" if less than a minute has elapsed
    long secondsSince = (currentTime - passedTime) / 1000;
    if (secondsSince < 60) return WordPress.getContext().getString(R.string.reader_timespan_now);

    // less than an hour (ex: 12m)
    long minutesSince = secondsSince / 60;
    if (minutesSince < 60) return Long.toString(minutesSince) + "m";

    // less than a day (ex: 17h)
    long hoursSince = minutesSince / 60;
    if (hoursSince < 24) return Long.toString(hoursSince) + "h";

    // less than a week (ex: 5d)
    long daysSince = hoursSince / 24;
    if (daysSince < 7) return Long.toString(daysSince) + "d";

    // less than a year old, so return day/month without year (ex: Jan 30)
    if (daysSince < 365)
      return DateUtils.formatDateTime(
          WordPress.getContext(),
          passedTime,
          DateUtils.FORMAT_NO_YEAR | DateUtils.FORMAT_ABBREV_ALL);

    // date is older, so include year (ex: Jan 30, 2013)
    return DateUtils.formatDateTime(
        WordPress.getContext(), passedTime, DateUtils.FORMAT_ABBREV_ALL);
  }
 /**
  * Utility method to refresh mixpanel metadata.
  *
  * @param username WordPress.com username
  * @param email WordPress.com email address
  */
 public static void refreshMetadata(String username, String email) {
   SharedPreferences preferences =
       PreferenceManager.getDefaultSharedPreferences(WordPress.getContext());
   int sessionCount = preferences.getInt(AnalyticsTrackerMixpanel.SESSION_COUNT, 0);
   boolean isUserConnected = AccountHelper.isSignedIn();
   boolean isWordPressComUser = AccountHelper.isSignedInWordPressDotCom();
   boolean isJetpackUser = AccountHelper.isJetPackUser();
   int numBlogs = WordPress.wpDB.getVisibleBlogs().size();
   int versionCode = PackageUtils.getVersionCode(WordPress.getContext());
   AnalyticsTracker.refreshMetadata(
       isUserConnected,
       isWordPressComUser,
       isJetpackUser,
       sessionCount,
       numBlogs,
       versionCode,
       username,
       email);
 }
 public static ReaderDatabase getDatabase() {
   if (mReaderDb == null) {
     synchronized (mDbLock) {
       if (mReaderDb == null) {
         mReaderDb = new ReaderDatabase(WordPress.getContext());
         // this ensures that onOpen() is called with a writable database (open will fail if app
         // calls getReadableDb() first)
         mReaderDb.getWritableDatabase();
       }
     }
   }
   return mReaderDb;
 }
  /*
   * used during development to copy database to external storage so we can access it via DDMS
   */
  private void copyDatabase(SQLiteDatabase db) {
    String copyFrom = db.getPath();
    String copyTo =
        WordPress.getContext().getExternalFilesDir(null).getAbsolutePath() + "/" + DB_NAME;

    try {
      InputStream input = new FileInputStream(copyFrom);
      OutputStream output = new FileOutputStream(copyTo);

      byte[] buffer = new byte[1024];
      int length;
      while ((length = input.read(buffer)) > 0) {
        output.write(buffer, 0, length);
      }

      output.flush();
      output.close();
      input.close();
    } catch (IOException e) {
      AppLog.e(T.DB, "failed to copy reader database", e);
    }
  }