mirror of
https://github.com/TwidereProject/Twidere-Android
synced 2025-01-30 16:35:00 +01:00
improving direct messages notification
This commit is contained in:
parent
acd084ab8a
commit
3a7b3e20ef
@ -198,6 +198,11 @@ public class TwidereApplication extends MultiDexApplication implements Constants
|
||||
mHandler = new Handler();
|
||||
mMessageBus = new Bus();
|
||||
mPreferences = getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE);
|
||||
if (!mPreferences.contains(KEY_USAGE_STATISTICS)) {
|
||||
final boolean prevUsageEnabled = mPreferences.getBoolean("ucd_data_profiling", false)
|
||||
|| mPreferences.getBoolean("spice_data_profiling", false);
|
||||
mPreferences.edit().putBoolean(KEY_USAGE_STATISTICS, prevUsageEnabled).apply();
|
||||
}
|
||||
mPreferences.registerOnSharedPreferenceChangeListener(this);
|
||||
initializeAsyncTask();
|
||||
initAccountColor(this);
|
||||
|
@ -123,6 +123,7 @@ import static org.mariotaku.twidere.util.Utils.isNotificationsSilent;
|
||||
public final class TwidereDataProvider extends ContentProvider implements Constants, OnSharedPreferenceChangeListener,
|
||||
LazyLoadCallback {
|
||||
|
||||
public static final String TAG_OLDEST_MESSAGES = "oldest_messages";
|
||||
private ContentResolver mContentResolver;
|
||||
private SQLiteDatabaseWrapper mDatabaseWrapper;
|
||||
private PermissionsManager mPermissionsManager;
|
||||
@ -206,6 +207,7 @@ public final class TwidereDataProvider extends ContentProvider implements Consta
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int delete(final Uri uri, final String selection, final String[] selectionArgs) {
|
||||
try {
|
||||
@ -840,7 +842,7 @@ public final class TwidereDataProvider extends ContentProvider implements Consta
|
||||
for (final AccountPreferences pref : prefs) {
|
||||
if (!pref.isDirectMessagesNotificationEnabled()) continue;
|
||||
final StringLongPair[] pairs = mReadStateManager.getPositionPairs(DirectMessagesFragment.KEY_READ_POSITION_TAG);
|
||||
showMessagesNotification(pref, pairs);
|
||||
showMessagesNotification(pref, pairs, valuesArray);
|
||||
}
|
||||
notifyUnreadCountChanged(NOTIFICATION_ID_DIRECT_MESSAGES);
|
||||
break;
|
||||
@ -1003,8 +1005,16 @@ public final class TwidereDataProvider extends ContentProvider implements Consta
|
||||
}
|
||||
}
|
||||
|
||||
private void showMessagesNotification(AccountPreferences pref, StringLongPair[] pairs) {
|
||||
private void showMessagesNotification(AccountPreferences pref, StringLongPair[] pairs, ContentValues[] valuesArray) {
|
||||
final long accountId = pref.getAccountId();
|
||||
final long prevOldestId = mReadStateManager.getPosition(TAG_OLDEST_MESSAGES, String.valueOf(accountId));
|
||||
long oldestId = -1;
|
||||
for (final ContentValues contentValues : valuesArray) {
|
||||
final long messageId = contentValues.getAsLong(DirectMessages.MESSAGE_ID);
|
||||
oldestId = oldestId < 0 ? messageId : Math.min(oldestId, messageId);
|
||||
if (messageId <= prevOldestId) return;
|
||||
}
|
||||
mReadStateManager.setPosition(TAG_OLDEST_MESSAGES, String.valueOf(accountId), oldestId, false);
|
||||
final Context context = getContext();
|
||||
final Resources resources = context.getResources();
|
||||
final NotificationManager nm = getNotificationManager();
|
||||
@ -1025,8 +1035,11 @@ public final class TwidereDataProvider extends ContentProvider implements Consta
|
||||
}
|
||||
}
|
||||
orExpressions.add(Expression.notIn(new Column(DirectMessages.SENDER_ID), new RawItemArray(senderIds.toArray())));
|
||||
final Expression selection = Expression.and(Expression.equals(Statuses.ACCOUNT_ID, accountId),
|
||||
Expression.or(orExpressions.toArray(new Expression[orExpressions.size()])));
|
||||
final Expression selection = Expression.and(
|
||||
Expression.equals(DirectMessages.ACCOUNT_ID, accountId),
|
||||
Expression.greaterThan(DirectMessages.MESSAGE_ID, prevOldestId),
|
||||
Expression.or(orExpressions.toArray(new Expression[orExpressions.size()]))
|
||||
);
|
||||
final String filteredSelection = selection.getSQL();
|
||||
final String[] userProjection = {DirectMessages.SENDER_ID, DirectMessages.SENDER_NAME,
|
||||
DirectMessages.SENDER_SCREEN_NAME};
|
||||
|
@ -5,90 +5,104 @@ import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
public class SQLiteDatabaseWrapper {
|
||||
private SQLiteDatabase mDatabase;
|
||||
private final SQLiteDatabaseWrapper.LazyLoadCallback mLazyLoadCallback;
|
||||
private SQLiteDatabase mDatabase;
|
||||
private final SQLiteDatabaseWrapper.LazyLoadCallback mLazyLoadCallback;
|
||||
|
||||
public SQLiteDatabaseWrapper(final LazyLoadCallback callback) {
|
||||
mLazyLoadCallback = callback;
|
||||
}
|
||||
public SQLiteDatabaseWrapper(final LazyLoadCallback callback) {
|
||||
mLazyLoadCallback = callback;
|
||||
}
|
||||
|
||||
public void beginTransaction() {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return;
|
||||
mDatabase.beginTransaction();
|
||||
}
|
||||
public void beginTransaction() {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return;
|
||||
mDatabase.beginTransaction();
|
||||
}
|
||||
|
||||
public int delete(final String table, final String whereClause, final String[] whereArgs) {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return 0;
|
||||
return mDatabase.delete(table, whereClause, whereArgs);
|
||||
}
|
||||
public int delete(final String table, final String whereClause, final String[] whereArgs) {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return 0;
|
||||
return mDatabase.delete(table, whereClause, whereArgs);
|
||||
}
|
||||
|
||||
public void endTransaction() {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return;
|
||||
mDatabase.endTransaction();
|
||||
}
|
||||
public void endTransaction() {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return;
|
||||
mDatabase.endTransaction();
|
||||
}
|
||||
|
||||
public SQLiteDatabase getSQLiteDatabase() {
|
||||
return mDatabase;
|
||||
}
|
||||
public SQLiteDatabase getSQLiteDatabase() {
|
||||
return mDatabase;
|
||||
}
|
||||
|
||||
public long insert(final String table, final String nullColumnHack, final ContentValues values) {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return -1;
|
||||
return mDatabase.insert(table, nullColumnHack, values);
|
||||
}
|
||||
public long insert(final String table, final String nullColumnHack, final ContentValues values) {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return -1;
|
||||
return mDatabase.insert(table, nullColumnHack, values);
|
||||
}
|
||||
|
||||
public long insertWithOnConflict(final String table, final String nullColumnHack,
|
||||
final ContentValues initialValues, final int conflictAlgorithm) {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return -1;
|
||||
return mDatabase.insertWithOnConflict(table, nullColumnHack, initialValues, conflictAlgorithm);
|
||||
}
|
||||
public long insertWithOnConflict(final String table, final String nullColumnHack,
|
||||
final ContentValues initialValues, final int conflictAlgorithm) {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return -1;
|
||||
return mDatabase.insertWithOnConflict(table, nullColumnHack, initialValues, conflictAlgorithm);
|
||||
}
|
||||
|
||||
public boolean isReady() {
|
||||
if (mLazyLoadCallback != null) return true;
|
||||
return mDatabase != null;
|
||||
}
|
||||
public boolean isReady() {
|
||||
if (mLazyLoadCallback != null) return true;
|
||||
return mDatabase != null;
|
||||
}
|
||||
|
||||
public Cursor query(final String table, final String[] columns, final String selection,
|
||||
final String[] selectionArgs, final String groupBy, final String having, final String orderBy) {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return null;
|
||||
return mDatabase.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
|
||||
}
|
||||
public Cursor query(final String table, final String[] columns, final String selection,
|
||||
final String[] selectionArgs, final String groupBy, final String having, final String orderBy) {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return null;
|
||||
return mDatabase.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
|
||||
}
|
||||
|
||||
public Cursor rawQuery(final String sql, final String[] selectionArgs) {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return null;
|
||||
return mDatabase.rawQuery(sql, selectionArgs);
|
||||
}
|
||||
public Cursor rawQuery(final String sql, final String[] selectionArgs) {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return null;
|
||||
return mDatabase.rawQuery(sql, selectionArgs);
|
||||
}
|
||||
|
||||
public void setSQLiteDatabase(final SQLiteDatabase database) {
|
||||
mDatabase = database;
|
||||
}
|
||||
public void setSQLiteDatabase(final SQLiteDatabase database) {
|
||||
mDatabase = database;
|
||||
}
|
||||
|
||||
public void setTransactionSuccessful() {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return;
|
||||
mDatabase.setTransactionSuccessful();
|
||||
}
|
||||
public void setTransactionSuccessful() {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return;
|
||||
mDatabase.setTransactionSuccessful();
|
||||
}
|
||||
|
||||
public int update(final String table, final ContentValues values, final String whereClause, final String[] whereArgs) {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return 0;
|
||||
return mDatabase.update(table, values, whereClause, whereArgs);
|
||||
}
|
||||
|
||||
private void tryCreateDatabase() {
|
||||
if (mLazyLoadCallback == null || mDatabase != null) return;
|
||||
mDatabase = mLazyLoadCallback.onCreateSQLiteDatabase();
|
||||
if (mDatabase == null) throw new IllegalStateException("Callback must not return null instance!");
|
||||
}
|
||||
public Cursor query(boolean distinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return null;
|
||||
return mDatabase.query(distinct, table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
|
||||
}
|
||||
|
||||
public static interface LazyLoadCallback {
|
||||
SQLiteDatabase onCreateSQLiteDatabase();
|
||||
}
|
||||
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return null;
|
||||
return mDatabase.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
|
||||
}
|
||||
|
||||
public int update(final String table, final ContentValues values, final String whereClause, final String[] whereArgs) {
|
||||
tryCreateDatabase();
|
||||
if (mDatabase == null) return 0;
|
||||
return mDatabase.update(table, values, whereClause, whereArgs);
|
||||
}
|
||||
|
||||
private void tryCreateDatabase() {
|
||||
if (mLazyLoadCallback == null || mDatabase != null) return;
|
||||
mDatabase = mLazyLoadCallback.onCreateSQLiteDatabase();
|
||||
if (mDatabase == null)
|
||||
throw new IllegalStateException("Callback must not return null instance!");
|
||||
}
|
||||
|
||||
public static interface LazyLoadCallback {
|
||||
SQLiteDatabase onCreateSQLiteDatabase();
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user