static { // getBestTimePattern() is only available in API 18 and up (Android 4.3 and better) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { DATE_SHORT_FORMAT = new SimpleDateFormat( android.text.format.DateFormat.getBestDateTimePattern( MainApplication.getContext().getResources().getConfiguration().locale, "d MMM")); } else { DATE_SHORT_FORMAT = android.text.format.DateFormat.getDateFormat(MainApplication.getContext()); } }
public static void unregisterOnPrefChangeListener(OnSharedPreferenceChangeListener listener) { try { PreferenceManager.getDefaultSharedPreferences(MainApplication.getContext()) .unregisterOnSharedPreferenceChangeListener(listener); } catch (Exception ignored) { // Seems to be possible to have a NPE here... Why?? } }
public void setData(Uri uri) { mCurrentPagerPos = -1; mBaseUri = FeedData.EntryColumns.PARENT_URI(uri.getPath()); try { mInitialEntryId = Long.parseLong(uri.getLastPathSegment()); } catch (Exception unused) { mInitialEntryId = -1; } if (mBaseUri != null) { Bundle b = getActivity().getIntent().getExtras(); String whereClause = FeedData.shouldShowReadEntries(mBaseUri) || (b != null && b.getBoolean(Constants.INTENT_FROM_WIDGET, false)) ? null : EntryColumns.WHERE_UNREAD; // Load the entriesIds list. Should be in a loader... but I was too lazy to do so Cursor entriesCursor = MainApplication.getContext() .getContentResolver() .query( mBaseUri, EntryColumns.PROJECTION_ID, whereClause, null, EntryColumns.DATE + Constants.DB_DESC); if (entriesCursor != null && entriesCursor.getCount() > 0) { mEntriesIds = new long[entriesCursor.getCount()]; int i = 0; while (entriesCursor.moveToNext()) { mEntriesIds[i] = entriesCursor.getLong(0); if (mEntriesIds[i] == mInitialEntryId) { mCurrentPagerPos = i; // To immediately display the good entry } i++; } entriesCursor.close(); } } else { mEntriesIds = null; } mEntryPagerAdapter.notifyDataSetChanged(); if (mCurrentPagerPos != -1) { mEntryPager.setCurrentItem(mCurrentPagerPos); } }
public class StringUtils { public static DateFormat DATE_SHORT_FORMAT = null; static { // getBestTimePattern() is only available in API 18 and up (Android 4.3 and better) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { DATE_SHORT_FORMAT = new SimpleDateFormat( android.text.format.DateFormat.getBestDateTimePattern( MainApplication.getContext().getResources().getConfiguration().locale, "d MMM")); } else { DATE_SHORT_FORMAT = android.text.format.DateFormat.getDateFormat(MainApplication.getContext()); } } public static final DateFormat TIME_FORMAT = android.text.format.DateFormat.getTimeFormat(MainApplication.getContext()); public static final int SIX_HOURS = 21600000; // six hours in milliseconds public static String getDateTimeString(long timestamp) { String outString; Date date = new Date(timestamp); Calendar calTimestamp = Calendar.getInstance(); calTimestamp.setTimeInMillis(timestamp); Calendar calCurrent = Calendar.getInstance(); if (calCurrent.getTimeInMillis() - timestamp < SIX_HOURS || calCurrent.get(Calendar.DAY_OF_MONTH) == calTimestamp.get(Calendar.DAY_OF_MONTH)) { outString = TIME_FORMAT.format(date); } else { outString = DATE_SHORT_FORMAT.format(date) + ' ' + TIME_FORMAT.format(date); } return outString; } public static String getMd5(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] messageDigest = md.digest(input.getBytes()); BigInteger number = new BigInteger(1, messageDigest); return number.toString(16); } catch (NoSuchAlgorithmException e) { return null; } } }
public static String getString(String key, String defValue) { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApplication.getContext()); return settings.getString(key, defValue); }
public static void putLong(String key, long value) { SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(MainApplication.getContext()).edit(); editor.putLong(key, value); editor.apply(); }
public static boolean getBoolean(String key, boolean defValue) { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainApplication.getContext()); return settings.getBoolean(key, defValue); }
public static void remove(String key) { SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(MainApplication.getContext()).edit(); editor.remove(key); editor.apply(); }