2014-07-03 07:48:39 +02:00
|
|
|
/*
|
|
|
|
* Twidere - Twitter client for Android
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.mariotaku.twidere.provider;
|
|
|
|
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.ContentProvider;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.MatrixCursor;
|
|
|
|
import android.database.SQLException;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2015-03-24 16:40:47 +01:00
|
|
|
import android.graphics.Typeface;
|
2015-04-01 12:28:26 +02:00
|
|
|
import android.media.AudioManager;
|
2014-07-03 07:48:39 +02:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Binder;
|
2014-12-07 16:13:07 +01:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2014-07-03 07:48:39 +02:00
|
|
|
import android.os.ParcelFileDescriptor;
|
2014-12-07 16:13:07 +01:00
|
|
|
import android.support.annotation.NonNull;
|
2014-07-03 07:48:39 +02:00
|
|
|
import android.support.v4.app.NotificationCompat;
|
2015-03-24 16:40:47 +01:00
|
|
|
import android.support.v4.app.NotificationCompat.InboxStyle;
|
2015-04-04 16:31:37 +02:00
|
|
|
import android.support.v4.util.LongSparseArray;
|
2015-03-24 16:40:47 +01:00
|
|
|
import android.text.Spannable;
|
|
|
|
import android.text.SpannableStringBuilder;
|
|
|
|
import android.text.style.StyleSpan;
|
2014-07-03 07:48:39 +02:00
|
|
|
import android.util.Log;
|
|
|
|
|
2015-05-08 21:46:58 +02:00
|
|
|
import com.squareup.okhttp.internal.Network;
|
2014-12-07 16:13:07 +01:00
|
|
|
import com.squareup.otto.Bus;
|
|
|
|
|
2015-01-11 10:28:45 +01:00
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
2015-01-14 17:35:37 +01:00
|
|
|
import org.mariotaku.querybuilder.Columns.Column;
|
2015-01-10 16:17:02 +01:00
|
|
|
import org.mariotaku.querybuilder.Expression;
|
2015-01-14 17:35:37 +01:00
|
|
|
import org.mariotaku.querybuilder.RawItemArray;
|
2015-01-14 09:47:51 +01:00
|
|
|
import org.mariotaku.querybuilder.query.SQLSelectQuery;
|
2014-07-03 07:48:39 +02:00
|
|
|
import org.mariotaku.twidere.Constants;
|
|
|
|
import org.mariotaku.twidere.R;
|
|
|
|
import org.mariotaku.twidere.activity.support.HomeActivity;
|
|
|
|
import org.mariotaku.twidere.app.TwidereApplication;
|
|
|
|
import org.mariotaku.twidere.model.AccountPreferences;
|
|
|
|
import org.mariotaku.twidere.model.ParcelableStatus;
|
2015-03-25 14:20:51 +01:00
|
|
|
import org.mariotaku.twidere.model.StringLongPair;
|
2014-07-03 07:48:39 +02:00
|
|
|
import org.mariotaku.twidere.model.UnreadItem;
|
2015-01-11 10:28:45 +01:00
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.Accounts;
|
2015-01-14 09:47:51 +01:00
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.CachedRelationships;
|
2015-01-11 10:28:45 +01:00
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.CachedUsers;
|
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.DirectMessages;
|
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.Drafts;
|
2015-03-24 16:40:47 +01:00
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.Mentions;
|
2015-01-11 10:28:45 +01:00
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.Preferences;
|
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.SearchHistory;
|
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.Statuses;
|
|
|
|
import org.mariotaku.twidere.provider.TwidereDataStore.UnreadCounts;
|
2015-04-04 16:31:37 +02:00
|
|
|
import org.mariotaku.twidere.receiver.NotificationReceiver;
|
2015-01-14 17:35:37 +01:00
|
|
|
import org.mariotaku.twidere.util.AsyncTwitterWrapper;
|
2014-07-03 07:48:39 +02:00
|
|
|
import org.mariotaku.twidere.util.ImagePreloader;
|
|
|
|
import org.mariotaku.twidere.util.MediaPreviewUtils;
|
|
|
|
import org.mariotaku.twidere.util.ParseUtils;
|
|
|
|
import org.mariotaku.twidere.util.PermissionsManager;
|
2015-03-25 14:20:51 +01:00
|
|
|
import org.mariotaku.twidere.util.ReadStateManager;
|
2014-07-03 07:48:39 +02:00
|
|
|
import org.mariotaku.twidere.util.SQLiteDatabaseWrapper;
|
|
|
|
import org.mariotaku.twidere.util.SQLiteDatabaseWrapper.LazyLoadCallback;
|
|
|
|
import org.mariotaku.twidere.util.SharedPreferencesWrapper;
|
2015-01-11 10:28:45 +01:00
|
|
|
import org.mariotaku.twidere.util.TwidereArrayUtils;
|
2015-01-14 09:47:51 +01:00
|
|
|
import org.mariotaku.twidere.util.TwidereQueryBuilder.CachedUsersQueryBuilder;
|
|
|
|
import org.mariotaku.twidere.util.TwidereQueryBuilder.ConversationQueryBuilder;
|
2015-04-24 08:14:33 +02:00
|
|
|
import org.mariotaku.twidere.util.UserColorNameManager;
|
2014-07-03 07:48:39 +02:00
|
|
|
import org.mariotaku.twidere.util.Utils;
|
2015-03-25 14:20:51 +01:00
|
|
|
import org.mariotaku.twidere.util.collection.CompactHashSet;
|
2014-12-07 16:13:07 +01:00
|
|
|
import org.mariotaku.twidere.util.message.UnreadCountUpdatedEvent;
|
2014-07-03 07:48:39 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
2015-03-14 16:11:20 +01:00
|
|
|
import java.net.InetAddress;
|
2015-03-25 14:20:51 +01:00
|
|
|
import java.util.ArrayList;
|
2014-07-03 07:48:39 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2014-11-26 08:30:05 +01:00
|
|
|
import static org.mariotaku.twidere.util.Utils.clearAccountColor;
|
|
|
|
import static org.mariotaku.twidere.util.Utils.clearAccountName;
|
|
|
|
import static org.mariotaku.twidere.util.Utils.getAccountIds;
|
|
|
|
import static org.mariotaku.twidere.util.Utils.getNotificationUri;
|
|
|
|
import static org.mariotaku.twidere.util.Utils.getTableId;
|
|
|
|
import static org.mariotaku.twidere.util.Utils.getTableNameById;
|
|
|
|
import static org.mariotaku.twidere.util.Utils.isNotificationsSilent;
|
|
|
|
|
2014-07-03 07:48:39 +02:00
|
|
|
public final class TwidereDataProvider extends ContentProvider implements Constants, OnSharedPreferenceChangeListener,
|
2014-11-26 08:30:05 +01:00
|
|
|
LazyLoadCallback {
|
|
|
|
|
2015-03-25 16:04:25 +01:00
|
|
|
public static final String TAG_OLDEST_MESSAGES = "oldest_messages";
|
2014-11-26 08:30:05 +01:00
|
|
|
private ContentResolver mContentResolver;
|
|
|
|
private SQLiteDatabaseWrapper mDatabaseWrapper;
|
|
|
|
private PermissionsManager mPermissionsManager;
|
|
|
|
private NotificationManager mNotificationManager;
|
2015-03-25 14:20:51 +01:00
|
|
|
private ReadStateManager mReadStateManager;
|
2014-11-26 08:30:05 +01:00
|
|
|
private SharedPreferencesWrapper mPreferences;
|
|
|
|
private ImagePreloader mImagePreloader;
|
2015-05-08 21:46:58 +02:00
|
|
|
private Network mNetwork;
|
2014-12-07 16:13:07 +01:00
|
|
|
private Handler mHandler;
|
2014-11-26 08:30:05 +01:00
|
|
|
|
|
|
|
private boolean mHomeActivityInBackground;
|
|
|
|
|
2015-03-24 16:40:47 +01:00
|
|
|
private boolean mNameFirst;
|
2014-11-26 08:30:05 +01:00
|
|
|
|
|
|
|
private final BroadcastReceiver mHomeActivityStateReceiver = new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(final Context context, final Intent intent) {
|
|
|
|
final String action = intent.getAction();
|
|
|
|
if (BROADCAST_HOME_ACTIVITY_ONSTART.equals(action)) {
|
|
|
|
mHomeActivityInBackground = false;
|
|
|
|
} else if (BROADCAST_HOME_ACTIVITY_ONSTOP.equals(action)) {
|
|
|
|
mHomeActivityInBackground = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
2015-01-10 16:17:02 +01:00
|
|
|
public int bulkInsert(final Uri uri, @NonNull final ContentValues[] valuesArray) {
|
2014-11-26 08:30:05 +01:00
|
|
|
try {
|
|
|
|
final int tableId = getTableId(uri);
|
|
|
|
final String table = getTableNameById(tableId);
|
|
|
|
checkWritePermission(tableId, table);
|
|
|
|
switch (tableId) {
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATION:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATIONS_ENTRIES:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int result = 0;
|
2015-01-14 17:35:37 +01:00
|
|
|
final long[] newIds = new long[valuesArray.length];
|
2014-12-07 16:13:07 +01:00
|
|
|
if (table != null) {
|
2014-11-26 08:30:05 +01:00
|
|
|
mDatabaseWrapper.beginTransaction();
|
2015-01-10 16:17:02 +01:00
|
|
|
if (tableId == TABLE_ID_CACHED_USERS) {
|
|
|
|
for (final ContentValues values : valuesArray) {
|
|
|
|
final Expression where = Expression.equals(CachedUsers.USER_ID,
|
|
|
|
values.getAsLong(CachedUsers.USER_ID));
|
|
|
|
mDatabaseWrapper.update(table, values, where.getSQL(), null);
|
2015-01-14 17:35:37 +01:00
|
|
|
newIds[result++] = mDatabaseWrapper.insertWithOnConflict(table, null,
|
2015-04-01 18:09:19 +02:00
|
|
|
values, SQLiteDatabase.CONFLICT_REPLACE);
|
2015-01-10 16:17:02 +01:00
|
|
|
}
|
|
|
|
} else if (tableId == TABLE_ID_SEARCH_HISTORY) {
|
|
|
|
for (final ContentValues values : valuesArray) {
|
|
|
|
values.put(SearchHistory.RECENT_QUERY, System.currentTimeMillis());
|
|
|
|
final Expression where = Expression.equalsArgs(SearchHistory.QUERY);
|
|
|
|
final String[] args = {values.getAsString(SearchHistory.QUERY)};
|
|
|
|
mDatabaseWrapper.update(table, values, where.getSQL(), args);
|
2015-01-14 17:35:37 +01:00
|
|
|
newIds[result++] = mDatabaseWrapper.insertWithOnConflict(table, null,
|
|
|
|
values, SQLiteDatabase.CONFLICT_IGNORE);
|
2015-01-10 16:17:02 +01:00
|
|
|
}
|
|
|
|
} else if (shouldReplaceOnConflict(tableId)) {
|
|
|
|
for (final ContentValues values : valuesArray) {
|
2015-01-14 17:35:37 +01:00
|
|
|
newIds[result++] = mDatabaseWrapper.insertWithOnConflict(table, null,
|
|
|
|
values, SQLiteDatabase.CONFLICT_REPLACE);
|
2015-01-10 16:17:02 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (final ContentValues values : valuesArray) {
|
2015-01-14 17:35:37 +01:00
|
|
|
newIds[result++] = mDatabaseWrapper.insert(table, null, values);
|
2014-11-26 08:30:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mDatabaseWrapper.setTransactionSuccessful();
|
|
|
|
mDatabaseWrapper.endTransaction();
|
|
|
|
}
|
|
|
|
if (result > 0) {
|
|
|
|
onDatabaseUpdated(tableId, uri);
|
|
|
|
}
|
2015-01-14 17:35:37 +01:00
|
|
|
onNewItemsInserted(uri, tableId, valuesArray, newIds);
|
2014-11-26 08:30:05 +01:00
|
|
|
return result;
|
|
|
|
} catch (final SQLException e) {
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 16:04:25 +01:00
|
|
|
|
2014-11-26 08:30:05 +01:00
|
|
|
@Override
|
|
|
|
public int delete(final Uri uri, final String selection, final String[] selectionArgs) {
|
|
|
|
try {
|
|
|
|
final int tableId = getTableId(uri);
|
|
|
|
final String table = getTableNameById(tableId);
|
|
|
|
checkWritePermission(tableId, table);
|
|
|
|
switch (tableId) {
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATION:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATIONS_ENTRIES:
|
|
|
|
return 0;
|
|
|
|
case VIRTUAL_TABLE_ID_NOTIFICATIONS: {
|
|
|
|
final List<String> segments = uri.getPathSegments();
|
|
|
|
if (segments.size() == 1) {
|
|
|
|
clearNotification();
|
|
|
|
} else if (segments.size() == 2) {
|
|
|
|
final int notificationType = ParseUtils.parseInt(segments.get(1));
|
|
|
|
clearNotification(notificationType, 0);
|
|
|
|
} else if (segments.size() == 3) {
|
|
|
|
final int notificationType = ParseUtils.parseInt(segments.get(1));
|
|
|
|
final long accountId = ParseUtils.parseLong(segments.get(2));
|
|
|
|
clearNotification(notificationType, accountId);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
case VIRTUAL_TABLE_ID_UNREAD_COUNTS: {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (table == null) return 0;
|
|
|
|
final int result = mDatabaseWrapper.delete(table, selection, selectionArgs);
|
|
|
|
if (result > 0) {
|
|
|
|
onDatabaseUpdated(tableId, uri);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} catch (final SQLException e) {
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getType(final Uri uri) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Uri insert(final Uri uri, final ContentValues values) {
|
|
|
|
try {
|
|
|
|
final int tableId = getTableId(uri);
|
|
|
|
final String table = getTableNameById(tableId);
|
|
|
|
checkWritePermission(tableId, table);
|
|
|
|
switch (tableId) {
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATION:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATIONS_ENTRIES:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (table == null) return null;
|
|
|
|
final long rowId;
|
2015-01-10 16:17:02 +01:00
|
|
|
if (tableId == TABLE_ID_CACHED_USERS) {
|
|
|
|
final Expression where = Expression.equals(CachedUsers.USER_ID,
|
|
|
|
values.getAsLong(CachedUsers.USER_ID));
|
|
|
|
mDatabaseWrapper.update(table, values, where.getSQL(), null);
|
|
|
|
rowId = mDatabaseWrapper.insertWithOnConflict(table, null, values,
|
|
|
|
SQLiteDatabase.CONFLICT_IGNORE);
|
|
|
|
} else if (tableId == TABLE_ID_SEARCH_HISTORY) {
|
|
|
|
values.put(SearchHistory.RECENT_QUERY, System.currentTimeMillis());
|
|
|
|
final Expression where = Expression.equalsArgs(SearchHistory.QUERY);
|
|
|
|
final String[] args = {values.getAsString(SearchHistory.QUERY)};
|
|
|
|
mDatabaseWrapper.update(table, values, where.getSQL(), args);
|
|
|
|
rowId = mDatabaseWrapper.insertWithOnConflict(table, null, values,
|
|
|
|
SQLiteDatabase.CONFLICT_IGNORE);
|
2015-01-14 09:47:51 +01:00
|
|
|
} else if (tableId == TABLE_ID_CACHED_RELATIONSHIPS) {
|
|
|
|
final long accountId = values.getAsLong(CachedRelationships.ACCOUNT_ID);
|
|
|
|
final long userId = values.getAsLong(CachedRelationships.USER_ID);
|
|
|
|
final Expression where = Expression.and(
|
|
|
|
Expression.equals(CachedRelationships.ACCOUNT_ID, accountId),
|
|
|
|
Expression.equals(CachedRelationships.USER_ID, userId)
|
|
|
|
);
|
|
|
|
if (mDatabaseWrapper.update(table, values, where.getSQL(), null) > 0) {
|
|
|
|
final String[] projection = {CachedRelationships._ID};
|
|
|
|
final Cursor c = mDatabaseWrapper.query(table, projection, where.getSQL(), null,
|
|
|
|
null, null, null);
|
|
|
|
if (c.moveToFirst()) {
|
|
|
|
rowId = c.getLong(0);
|
|
|
|
} else {
|
|
|
|
rowId = 0;
|
|
|
|
}
|
|
|
|
c.close();
|
|
|
|
} else {
|
|
|
|
rowId = mDatabaseWrapper.insertWithOnConflict(table, null, values,
|
|
|
|
SQLiteDatabase.CONFLICT_IGNORE);
|
|
|
|
}
|
2015-01-10 16:17:02 +01:00
|
|
|
} else if (shouldReplaceOnConflict(tableId)) {
|
|
|
|
rowId = mDatabaseWrapper.insertWithOnConflict(table, null, values,
|
|
|
|
SQLiteDatabase.CONFLICT_REPLACE);
|
2014-11-26 08:30:05 +01:00
|
|
|
} else {
|
|
|
|
rowId = mDatabaseWrapper.insert(table, null, values);
|
|
|
|
}
|
|
|
|
onDatabaseUpdated(tableId, uri);
|
2015-01-14 17:35:37 +01:00
|
|
|
onNewItemsInserted(uri, tableId, values, rowId);
|
2014-11-26 08:30:05 +01:00
|
|
|
return Uri.withAppendedPath(uri, String.valueOf(rowId));
|
|
|
|
} catch (final SQLException e) {
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
|
|
|
final Context context = getContext();
|
|
|
|
final TwidereApplication app = TwidereApplication.getInstance(context);
|
2014-12-07 16:13:07 +01:00
|
|
|
mHandler = new Handler(Looper.getMainLooper());
|
2014-11-26 08:30:05 +01:00
|
|
|
mDatabaseWrapper = new SQLiteDatabaseWrapper(this);
|
2015-05-08 21:46:58 +02:00
|
|
|
mNetwork = app.getNetwork();
|
2014-11-26 08:30:05 +01:00
|
|
|
mPreferences = SharedPreferencesWrapper.getInstance(context, SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
|
|
|
|
mPreferences.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
updatePreferences();
|
|
|
|
mPermissionsManager = new PermissionsManager(context);
|
2015-03-27 17:50:18 +01:00
|
|
|
mReadStateManager = app.getReadStateManager();
|
2014-11-26 08:30:05 +01:00
|
|
|
mImagePreloader = new ImagePreloader(context, app.getImageLoader());
|
|
|
|
final IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction(BROADCAST_HOME_ACTIVITY_ONSTART);
|
|
|
|
filter.addAction(BROADCAST_HOME_ACTIVITY_ONSTOP);
|
|
|
|
context.registerReceiver(mHomeActivityStateReceiver, filter);
|
|
|
|
// final GetWritableDatabaseTask task = new
|
|
|
|
// GetWritableDatabaseTask(context, helper, mDatabaseWrapper);
|
2014-12-05 14:25:27 +01:00
|
|
|
// task.executeTask();
|
2014-11-26 08:30:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SQLiteDatabase onCreateSQLiteDatabase() {
|
|
|
|
final TwidereApplication app = TwidereApplication.getInstance(getContext());
|
|
|
|
final SQLiteOpenHelper helper = app.getSQLiteOpenHelper();
|
|
|
|
return helper.getWritableDatabase();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
|
|
|
|
updatePreferences();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ParcelFileDescriptor openFile(final Uri uri, final String mode) throws FileNotFoundException {
|
|
|
|
if (uri == null || mode == null) throw new IllegalArgumentException();
|
|
|
|
final int table_id = getTableId(uri);
|
|
|
|
final String table = getTableNameById(table_id);
|
|
|
|
final int mode_code;
|
|
|
|
if ("r".equals(mode)) {
|
|
|
|
mode_code = ParcelFileDescriptor.MODE_READ_ONLY;
|
|
|
|
} else if ("rw".equals(mode)) {
|
|
|
|
mode_code = ParcelFileDescriptor.MODE_READ_WRITE;
|
|
|
|
} else if ("rwt".equals(mode)) {
|
|
|
|
mode_code = ParcelFileDescriptor.MODE_READ_WRITE | ParcelFileDescriptor.MODE_TRUNCATE;
|
|
|
|
} else
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
if (mode_code == ParcelFileDescriptor.MODE_READ_ONLY) {
|
|
|
|
checkReadPermission(table_id, table, null);
|
|
|
|
} else if ((mode_code & ParcelFileDescriptor.MODE_READ_WRITE) != 0) {
|
|
|
|
checkReadPermission(table_id, table, null);
|
|
|
|
checkWritePermission(table_id, table);
|
|
|
|
}
|
|
|
|
switch (table_id) {
|
|
|
|
case VIRTUAL_TABLE_ID_CACHED_IMAGES: {
|
|
|
|
return getCachedImageFd(uri.getQueryParameter(QUERY_PARAM_URL));
|
|
|
|
}
|
|
|
|
case VIRTUAL_TABLE_ID_CACHE_FILES: {
|
|
|
|
return getCacheFileFd(uri.getLastPathSegment());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Cursor query(final Uri uri, final String[] projection, final String selection, final String[] selectionArgs,
|
|
|
|
final String sortOrder) {
|
|
|
|
try {
|
|
|
|
final int tableId = getTableId(uri);
|
|
|
|
final String table = getTableNameById(tableId);
|
|
|
|
checkReadPermission(tableId, table, projection);
|
|
|
|
switch (tableId) {
|
|
|
|
case VIRTUAL_TABLE_ID_DATABASE_READY: {
|
|
|
|
if (mDatabaseWrapper.isReady())
|
|
|
|
return new MatrixCursor(projection != null ? projection : new String[0]);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
case VIRTUAL_TABLE_ID_PERMISSIONS: {
|
2015-01-11 10:28:45 +01:00
|
|
|
final MatrixCursor c = new MatrixCursor(TwidereDataStore.Permissions.MATRIX_COLUMNS);
|
2014-11-26 08:30:05 +01:00
|
|
|
final Map<String, String> map = mPermissionsManager.getAll();
|
|
|
|
for (final Map.Entry<String, String> item : map.entrySet()) {
|
|
|
|
c.addRow(new Object[]{item.getKey(), item.getValue()});
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
case VIRTUAL_TABLE_ID_ALL_PREFERENCES: {
|
|
|
|
return getPreferencesCursor(mPreferences, null);
|
|
|
|
}
|
|
|
|
case VIRTUAL_TABLE_ID_PREFERENCES: {
|
|
|
|
return getPreferencesCursor(mPreferences, uri.getLastPathSegment());
|
|
|
|
}
|
|
|
|
case VIRTUAL_TABLE_ID_DNS: {
|
|
|
|
return getDNSCursor(uri.getLastPathSegment());
|
|
|
|
}
|
|
|
|
case VIRTUAL_TABLE_ID_CACHED_IMAGES: {
|
|
|
|
return getCachedImageCursor(uri.getQueryParameter(QUERY_PARAM_URL));
|
|
|
|
}
|
|
|
|
case VIRTUAL_TABLE_ID_NOTIFICATIONS: {
|
|
|
|
final List<String> segments = uri.getPathSegments();
|
|
|
|
if (segments.size() == 2)
|
|
|
|
return getNotificationsCursor(ParseUtils.parseInt(segments.get(1), -1));
|
|
|
|
else
|
|
|
|
return getNotificationsCursor();
|
|
|
|
}
|
|
|
|
case VIRTUAL_TABLE_ID_UNREAD_COUNTS: {
|
|
|
|
final List<String> segments = uri.getPathSegments();
|
|
|
|
if (segments.size() == 2)
|
|
|
|
return getUnreadCountsCursor(ParseUtils.parseInt(segments.get(1), -1));
|
|
|
|
else
|
|
|
|
return getUnreadCountsCursor();
|
|
|
|
}
|
|
|
|
case VIRTUAL_TABLE_ID_UNREAD_COUNTS_BY_TYPE: {
|
|
|
|
final List<String> segments = uri.getPathSegments();
|
|
|
|
if (segments.size() != 3) return null;
|
|
|
|
return getUnreadCountsCursorByType(segments.get(2));
|
|
|
|
}
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATION: {
|
|
|
|
final List<String> segments = uri.getPathSegments();
|
|
|
|
if (segments.size() != 4) return null;
|
|
|
|
final long accountId = ParseUtils.parseLong(segments.get(2));
|
|
|
|
final long conversationId = ParseUtils.parseLong(segments.get(3));
|
2015-01-14 09:47:51 +01:00
|
|
|
final SQLSelectQuery query = ConversationQueryBuilder.buildByConversationId(projection,
|
2014-11-26 08:30:05 +01:00
|
|
|
accountId, conversationId, selection, sortOrder);
|
2015-01-14 09:47:51 +01:00
|
|
|
final Cursor c = mDatabaseWrapper.rawQuery(query.getSQL(), selectionArgs);
|
2014-11-26 08:30:05 +01:00
|
|
|
setNotificationUri(c, DirectMessages.CONTENT_URI);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATION_SCREEN_NAME: {
|
|
|
|
final List<String> segments = uri.getPathSegments();
|
|
|
|
if (segments.size() != 4) return null;
|
|
|
|
final long accountId = ParseUtils.parseLong(segments.get(2));
|
|
|
|
final String screenName = segments.get(3);
|
2015-01-14 09:47:51 +01:00
|
|
|
final SQLSelectQuery query = ConversationQueryBuilder.buildByScreenName(projection,
|
2014-11-26 08:30:05 +01:00
|
|
|
accountId, screenName, selection, sortOrder);
|
2015-01-14 09:47:51 +01:00
|
|
|
final Cursor c = mDatabaseWrapper.rawQuery(query.getSQL(), selectionArgs);
|
2014-11-26 08:30:05 +01:00
|
|
|
setNotificationUri(c, DirectMessages.CONTENT_URI);
|
|
|
|
return c;
|
|
|
|
}
|
2015-01-14 09:47:51 +01:00
|
|
|
case VIRTUAL_TABLE_ID_CACHED_USERS_WITH_RELATIONSHIP: {
|
|
|
|
final long accountId = ParseUtils.parseLong(uri.getLastPathSegment(), -1);
|
2015-04-03 23:12:33 +02:00
|
|
|
final SQLSelectQuery query = CachedUsersQueryBuilder.withRelationship(projection,
|
2015-01-14 09:47:51 +01:00
|
|
|
selection, sortOrder, accountId);
|
|
|
|
final Cursor c = mDatabaseWrapper.rawQuery(query.getSQL(), selectionArgs);
|
|
|
|
setNotificationUri(c, CachedUsers.CONTENT_URI);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
case VIRTUAL_TABLE_ID_CACHED_USERS_WITH_SCORE: {
|
|
|
|
final long accountId = ParseUtils.parseLong(uri.getLastPathSegment(), -1);
|
2015-04-03 23:12:33 +02:00
|
|
|
final SQLSelectQuery query = CachedUsersQueryBuilder.withScore(projection,
|
2015-01-14 09:47:51 +01:00
|
|
|
selection, sortOrder, accountId);
|
|
|
|
final Cursor c = mDatabaseWrapper.rawQuery(query.getSQL(), selectionArgs);
|
|
|
|
setNotificationUri(c, CachedUsers.CONTENT_URI);
|
|
|
|
return c;
|
|
|
|
}
|
2015-01-14 17:35:37 +01:00
|
|
|
case VIRTUAL_TABLE_ID_DRAFTS_UNSENT: {
|
|
|
|
final TwidereApplication app = TwidereApplication.getInstance(getContext());
|
|
|
|
final AsyncTwitterWrapper twitter = app.getTwitterWrapper();
|
|
|
|
final RawItemArray sendingIds = new RawItemArray(twitter.getSendingDraftIds());
|
|
|
|
final Expression where;
|
|
|
|
if (selection != null) {
|
|
|
|
where = Expression.and(new Expression(selection),
|
|
|
|
Expression.notIn(new Column(Drafts._ID), sendingIds));
|
|
|
|
} else {
|
|
|
|
where = Expression.and(Expression.notIn(new Column(Drafts._ID), sendingIds));
|
|
|
|
}
|
|
|
|
final Cursor c = mDatabaseWrapper.query(Drafts.TABLE_NAME, projection,
|
|
|
|
where.getSQL(), selectionArgs, null, null, sortOrder);
|
|
|
|
setNotificationUri(c, getNotificationUri(tableId, uri));
|
|
|
|
return c;
|
|
|
|
}
|
2014-11-26 08:30:05 +01:00
|
|
|
}
|
|
|
|
if (table == null) return null;
|
|
|
|
final Cursor c = mDatabaseWrapper.query(table, projection, selection, selectionArgs, null, null, sortOrder);
|
|
|
|
setNotificationUri(c, getNotificationUri(tableId, uri));
|
|
|
|
return c;
|
|
|
|
} catch (final SQLException e) {
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int update(final Uri uri, final ContentValues values, final String selection, final String[] selectionArgs) {
|
|
|
|
try {
|
|
|
|
final int tableId = getTableId(uri);
|
|
|
|
final String table = getTableNameById(tableId);
|
2015-01-10 16:17:02 +01:00
|
|
|
checkWritePermission(tableId, table);
|
2014-11-26 08:30:05 +01:00
|
|
|
int result = 0;
|
|
|
|
if (table != null) {
|
|
|
|
switch (tableId) {
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATION:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATIONS_ENTRIES:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
result = mDatabaseWrapper.update(table, values, selection, selectionArgs);
|
|
|
|
}
|
|
|
|
if (result > 0) {
|
|
|
|
onDatabaseUpdated(tableId, uri);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} catch (final SQLException e) {
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean checkPermission(final String... permissions) {
|
|
|
|
return mPermissionsManager.checkCallingPermission(permissions);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkReadPermission(final int id, final String table, final String[] projection) {
|
|
|
|
switch (id) {
|
|
|
|
case VIRTUAL_TABLE_ID_PREFERENCES:
|
|
|
|
case VIRTUAL_TABLE_ID_DNS: {
|
|
|
|
if (!checkPermission(PERMISSION_PREFERENCES))
|
|
|
|
throw new SecurityException("Access preferences requires level PERMISSION_LEVEL_PREFERENCES");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TABLE_ID_ACCOUNTS: {
|
|
|
|
// Reading some infomation like user_id, screen_name etc is
|
|
|
|
// okay, but reading columns like password requires higher
|
|
|
|
// permission level.
|
|
|
|
final String[] credentialsCols = {Accounts.BASIC_AUTH_PASSWORD, Accounts.OAUTH_TOKEN,
|
|
|
|
Accounts.OAUTH_TOKEN_SECRET, Accounts.CONSUMER_KEY, Accounts.CONSUMER_SECRET};
|
2015-01-11 10:28:45 +01:00
|
|
|
if (projection == null || TwidereArrayUtils.contains(projection, credentialsCols)
|
2015-04-13 18:34:10 +02:00
|
|
|
&& !checkPermission(PERMISSION_ACCOUNTS)) {
|
|
|
|
final String pkgName = mPermissionsManager.getPackageNameByUid(Binder.getCallingUid());
|
2015-01-11 10:28:45 +01:00
|
|
|
throw new SecurityException("Access column " + TwidereArrayUtils.toString(projection, ',', true)
|
2015-04-13 18:34:10 +02:00
|
|
|
+ " in database accounts requires level PERMISSION_LEVEL_ACCOUNTS, package: " + pkgName);
|
|
|
|
}
|
|
|
|
if (!checkPermission(PERMISSION_READ)) {
|
|
|
|
final String pkgName = mPermissionsManager.getPackageNameByUid(Binder.getCallingUid());
|
|
|
|
throw new SecurityException("Access database " + table + " requires level PERMISSION_LEVEL_READ, package: " + pkgName);
|
|
|
|
}
|
2014-11-26 08:30:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_INBOX:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_OUTBOX:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATION:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATION_SCREEN_NAME:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATIONS_ENTRIES: {
|
|
|
|
if (!checkPermission(PERMISSION_DIRECT_MESSAGES))
|
|
|
|
throw new SecurityException("Access database " + table
|
|
|
|
+ " requires level PERMISSION_LEVEL_DIRECT_MESSAGES");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TABLE_ID_STATUSES:
|
|
|
|
case TABLE_ID_MENTIONS:
|
|
|
|
case TABLE_ID_TABS:
|
|
|
|
case TABLE_ID_DRAFTS:
|
|
|
|
case TABLE_ID_CACHED_USERS:
|
|
|
|
case TABLE_ID_FILTERED_USERS:
|
|
|
|
case TABLE_ID_FILTERED_KEYWORDS:
|
|
|
|
case TABLE_ID_FILTERED_SOURCES:
|
|
|
|
case TABLE_ID_FILTERED_LINKS:
|
|
|
|
case TABLE_ID_TRENDS_LOCAL:
|
|
|
|
case TABLE_ID_CACHED_STATUSES:
|
|
|
|
case TABLE_ID_CACHED_HASHTAGS: {
|
|
|
|
if (!checkPermission(PERMISSION_READ))
|
|
|
|
throw new SecurityException("Access database " + table + " requires level PERMISSION_LEVEL_READ");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkWritePermission(final int id, final String table) {
|
|
|
|
switch (id) {
|
|
|
|
case TABLE_ID_ACCOUNTS: {
|
|
|
|
// Writing to accounts database is not allowed for third-party
|
|
|
|
// applications.
|
|
|
|
if (!mPermissionsManager.checkSignature(Binder.getCallingUid()))
|
|
|
|
throw new SecurityException(
|
|
|
|
"Writing to accounts database is not allowed for third-party applications");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_INBOX:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_OUTBOX:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATION:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATION_SCREEN_NAME:
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_CONVERSATIONS_ENTRIES: {
|
|
|
|
if (!checkPermission(PERMISSION_DIRECT_MESSAGES))
|
|
|
|
throw new SecurityException("Access database " + table
|
|
|
|
+ " requires level PERMISSION_LEVEL_DIRECT_MESSAGES");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TABLE_ID_STATUSES:
|
|
|
|
case TABLE_ID_MENTIONS:
|
|
|
|
case TABLE_ID_TABS:
|
|
|
|
case TABLE_ID_DRAFTS:
|
|
|
|
case TABLE_ID_CACHED_USERS:
|
|
|
|
case TABLE_ID_FILTERED_USERS:
|
|
|
|
case TABLE_ID_FILTERED_KEYWORDS:
|
|
|
|
case TABLE_ID_FILTERED_SOURCES:
|
|
|
|
case TABLE_ID_FILTERED_LINKS:
|
|
|
|
case TABLE_ID_TRENDS_LOCAL:
|
|
|
|
case TABLE_ID_CACHED_STATUSES:
|
|
|
|
case TABLE_ID_CACHED_HASHTAGS: {
|
|
|
|
if (!checkPermission(PERMISSION_WRITE))
|
|
|
|
throw new SecurityException("Access database " + table + " requires level PERMISSION_LEVEL_WRITE");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void clearNotification() {
|
|
|
|
getNotificationManager().cancelAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void clearNotification(final int notificationType, final long accountId) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private Cursor getCachedImageCursor(final String url) {
|
|
|
|
if (Utils.isDebugBuild()) {
|
|
|
|
Log.d(LOGTAG, String.format("getCachedImageCursor(%s)", url));
|
|
|
|
}
|
2015-01-11 10:28:45 +01:00
|
|
|
final MatrixCursor c = new MatrixCursor(TwidereDataStore.CachedImages.MATRIX_COLUMNS);
|
2014-11-26 08:30:05 +01:00
|
|
|
final File file = mImagePreloader.getCachedImageFile(url);
|
|
|
|
if (url != null && file != null) {
|
|
|
|
c.addRow(new String[]{url, file.getPath()});
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ParcelFileDescriptor getCachedImageFd(final String url) throws FileNotFoundException {
|
|
|
|
if (Utils.isDebugBuild()) {
|
|
|
|
Log.d(LOGTAG, String.format("getCachedImageFd(%s)", url));
|
|
|
|
}
|
|
|
|
final File file = mImagePreloader.getCachedImageFile(url);
|
|
|
|
if (file == null) return null;
|
|
|
|
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ParcelFileDescriptor getCacheFileFd(final String name) throws FileNotFoundException {
|
|
|
|
if (name == null) return null;
|
|
|
|
final Context mContext = getContext();
|
|
|
|
final File cacheDir = mContext.getCacheDir();
|
|
|
|
final File file = new File(cacheDir, name);
|
|
|
|
if (!file.exists()) return null;
|
|
|
|
return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ContentResolver getContentResolver() {
|
|
|
|
if (mContentResolver != null) return mContentResolver;
|
|
|
|
final Context context = getContext();
|
|
|
|
return mContentResolver = context.getContentResolver();
|
|
|
|
}
|
|
|
|
|
|
|
|
private Cursor getDNSCursor(final String host) {
|
2015-01-11 10:28:45 +01:00
|
|
|
final MatrixCursor c = new MatrixCursor(TwidereDataStore.DNS.MATRIX_COLUMNS);
|
2014-11-26 08:30:05 +01:00
|
|
|
try {
|
2015-05-08 21:46:58 +02:00
|
|
|
final InetAddress[] addresses = mNetwork.resolveInetAddresses(host);
|
2015-03-14 16:11:20 +01:00
|
|
|
for (InetAddress address : addresses) {
|
|
|
|
c.addRow(new String[]{host, address.getHostAddress()});
|
|
|
|
}
|
|
|
|
} catch (final IOException ignore) {
|
|
|
|
if (Utils.isDebugBuild()) {
|
|
|
|
Log.w(LOGTAG, ignore);
|
2014-11-26 08:30:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
private NotificationManager getNotificationManager() {
|
|
|
|
if (mNotificationManager != null) return mNotificationManager;
|
|
|
|
final Context context = getContext();
|
|
|
|
return mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Cursor getNotificationsCursor() {
|
2015-01-11 10:28:45 +01:00
|
|
|
final MatrixCursor c = new MatrixCursor(TwidereDataStore.Notifications.MATRIX_COLUMNS);
|
2014-11-26 08:30:05 +01:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Cursor getNotificationsCursor(final int id) {
|
2015-01-11 10:28:45 +01:00
|
|
|
final MatrixCursor c = new MatrixCursor(TwidereDataStore.Notifications.MATRIX_COLUMNS);
|
2014-11-26 08:30:05 +01:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Bitmap getProfileImageForNotification(final String profile_image_url) {
|
|
|
|
final Context context = getContext();
|
|
|
|
final Resources res = context.getResources();
|
|
|
|
final int w = res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
|
|
|
|
final int h = res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
|
|
|
|
final File profile_image_file = mImagePreloader.getCachedImageFile(profile_image_url);
|
|
|
|
final Bitmap profile_image = profile_image_file != null && profile_image_file.isFile() ? BitmapFactory
|
|
|
|
.decodeFile(profile_image_file.getPath()) : null;
|
|
|
|
if (profile_image != null) return Bitmap.createScaledBitmap(profile_image, w, h, true);
|
2015-03-23 04:57:55 +01:00
|
|
|
return Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.mipmap.ic_launcher), w, h, true);
|
2014-11-26 08:30:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private Cursor getUnreadCountsCursor() {
|
2015-01-11 10:28:45 +01:00
|
|
|
final MatrixCursor c = new MatrixCursor(TwidereDataStore.UnreadCounts.MATRIX_COLUMNS);
|
2014-11-26 08:30:05 +01:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Cursor getUnreadCountsCursor(final int position) {
|
2015-01-11 10:28:45 +01:00
|
|
|
final MatrixCursor c = new MatrixCursor(TwidereDataStore.UnreadCounts.MATRIX_COLUMNS);
|
2015-03-24 16:40:47 +01:00
|
|
|
|
2014-11-26 08:30:05 +01:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Cursor getUnreadCountsCursorByType(final String type) {
|
2015-01-11 10:28:45 +01:00
|
|
|
final MatrixCursor c = new MatrixCursor(TwidereDataStore.UnreadCounts.MATRIX_COLUMNS);
|
2014-11-26 08:30:05 +01:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int getUsersCount(final List<ParcelableStatus> items) {
|
|
|
|
if (items == null || items.isEmpty()) return 0;
|
2015-01-11 10:28:45 +01:00
|
|
|
final Set<Long> ids = new HashSet<>();
|
2014-11-26 08:30:05 +01:00
|
|
|
for (final ParcelableStatus item : items.toArray(new ParcelableStatus[items.size()])) {
|
|
|
|
ids.add(item.user_id);
|
|
|
|
}
|
|
|
|
return ids.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isNotificationAudible() {
|
|
|
|
return mHomeActivityInBackground && !isNotificationsSilent(getContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void notifyContentObserver(final Uri uri) {
|
2015-04-13 18:34:10 +02:00
|
|
|
mHandler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
final ContentResolver cr = getContentResolver();
|
|
|
|
if (uri == null || cr == null) return;
|
|
|
|
cr.notifyChange(uri, null);
|
|
|
|
}
|
|
|
|
});
|
2014-11-26 08:30:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void notifyUnreadCountChanged(final int position) {
|
|
|
|
final Context context = getContext();
|
2014-12-07 16:13:07 +01:00
|
|
|
final Bus bus = TwidereApplication.getInstance(context).getMessageBus();
|
|
|
|
mHandler.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
bus.post(new UnreadCountUpdatedEvent(position));
|
|
|
|
}
|
|
|
|
});
|
2014-11-26 08:30:05 +01:00
|
|
|
notifyContentObserver(UnreadCounts.CONTENT_URI);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onDatabaseUpdated(final int tableId, final Uri uri) {
|
|
|
|
if (uri == null) return;
|
|
|
|
switch (tableId) {
|
|
|
|
case TABLE_ID_ACCOUNTS: {
|
|
|
|
clearAccountColor();
|
|
|
|
clearAccountName();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notifyContentObserver(getNotificationUri(tableId, uri));
|
|
|
|
}
|
|
|
|
|
2015-01-14 17:35:37 +01:00
|
|
|
|
|
|
|
private void onNewItemsInserted(final Uri uri, final int tableId, final ContentValues values, final long newId) {
|
|
|
|
onNewItemsInserted(uri, tableId, new ContentValues[]{values}, new long[]{newId});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onNewItemsInserted(final Uri uri, final int tableId, final ContentValues[] valuesArray, final long[] newIds) {
|
2015-01-01 11:38:34 +01:00
|
|
|
if (uri == null || valuesArray == null || valuesArray.length == 0) return;
|
|
|
|
preloadImages(valuesArray);
|
2014-11-26 08:30:05 +01:00
|
|
|
if (!uri.getBooleanQueryParameter(QUERY_PARAM_NOTIFY, true)) return;
|
2015-01-01 11:38:34 +01:00
|
|
|
switch (tableId) {
|
2014-11-26 08:30:05 +01:00
|
|
|
case TABLE_ID_STATUSES: {
|
|
|
|
final AccountPreferences[] prefs = AccountPreferences.getNotificationEnabledPreferences(getContext(),
|
|
|
|
getAccountIds(getContext()));
|
|
|
|
for (final AccountPreferences pref : prefs) {
|
2015-03-24 16:40:47 +01:00
|
|
|
if (!pref.isHomeTimelineNotificationEnabled()) continue;
|
2015-04-06 18:42:09 +02:00
|
|
|
showTimelineNotification(pref, getPositionTag(TAB_TYPE_HOME_TIMELINE, pref.getAccountId()));
|
2014-11-26 08:30:05 +01:00
|
|
|
}
|
|
|
|
notifyUnreadCountChanged(NOTIFICATION_ID_HOME_TIMELINE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TABLE_ID_MENTIONS: {
|
|
|
|
final AccountPreferences[] prefs = AccountPreferences.getNotificationEnabledPreferences(getContext(),
|
|
|
|
getAccountIds(getContext()));
|
|
|
|
for (final AccountPreferences pref : prefs) {
|
2015-03-24 16:40:47 +01:00
|
|
|
if (!pref.isMentionsNotificationEnabled()) continue;
|
2015-04-06 18:42:09 +02:00
|
|
|
showMentionsNotification(pref, getPositionTag(TAB_TYPE_MENTIONS_TIMELINE, pref.getAccountId()));
|
2014-11-26 08:30:05 +01:00
|
|
|
}
|
2014-12-04 15:31:29 +01:00
|
|
|
notifyUnreadCountChanged(NOTIFICATION_ID_MENTIONS_TIMELINE);
|
2014-11-26 08:30:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TABLE_ID_DIRECT_MESSAGES_INBOX: {
|
|
|
|
final AccountPreferences[] prefs = AccountPreferences.getNotificationEnabledPreferences(getContext(),
|
|
|
|
getAccountIds(getContext()));
|
|
|
|
for (final AccountPreferences pref : prefs) {
|
2015-03-25 14:20:51 +01:00
|
|
|
if (!pref.isDirectMessagesNotificationEnabled()) continue;
|
2015-04-06 18:42:09 +02:00
|
|
|
final StringLongPair[] pairs = mReadStateManager.getPositionPairs(TAB_TYPE_DIRECT_MESSAGES);
|
2015-03-25 16:04:25 +01:00
|
|
|
showMessagesNotification(pref, pairs, valuesArray);
|
2014-11-26 08:30:05 +01:00
|
|
|
}
|
|
|
|
notifyUnreadCountChanged(NOTIFICATION_ID_DIRECT_MESSAGES);
|
|
|
|
break;
|
|
|
|
}
|
2015-01-01 11:38:34 +01:00
|
|
|
case TABLE_ID_DRAFTS: {
|
|
|
|
break;
|
|
|
|
}
|
2014-11-26 08:30:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 17:05:41 +02:00
|
|
|
private long getPositionTag(String tag, long accountId) {
|
|
|
|
final long position = mReadStateManager.getPosition(Utils.getReadPositionTagWithAccounts(tag,
|
|
|
|
accountId));
|
|
|
|
if (position != -1) return position;
|
|
|
|
return mReadStateManager.getPosition(tag);
|
|
|
|
}
|
|
|
|
|
2015-03-24 16:40:47 +01:00
|
|
|
private void showTimelineNotification(AccountPreferences pref, long position) {
|
|
|
|
final long accountId = pref.getAccountId();
|
|
|
|
final Context context = getContext();
|
|
|
|
final Resources resources = context.getResources();
|
|
|
|
final NotificationManager nm = getNotificationManager();
|
|
|
|
final Expression selection = Expression.and(Expression.equals(Statuses.ACCOUNT_ID, accountId),
|
|
|
|
Expression.greaterThan(Statuses.STATUS_ID, position));
|
|
|
|
final String filteredSelection = Utils.buildStatusFilterWhereClause(Statuses.TABLE_NAME,
|
2015-04-09 13:50:32 +02:00
|
|
|
selection).getSQL();
|
2015-03-24 16:40:47 +01:00
|
|
|
final String[] userProjection = {Statuses.USER_ID, Statuses.USER_NAME, Statuses.USER_SCREEN_NAME};
|
2015-04-04 16:31:37 +02:00
|
|
|
final String[] statusProjection = {Statuses.STATUS_ID};
|
2015-03-24 16:40:47 +01:00
|
|
|
final Cursor statusCursor = mDatabaseWrapper.query(Statuses.TABLE_NAME, statusProjection,
|
|
|
|
filteredSelection, null, null, null, Statuses.SORT_ORDER_TIMESTAMP_DESC);
|
|
|
|
final Cursor userCursor = mDatabaseWrapper.query(Statuses.TABLE_NAME, userProjection,
|
|
|
|
filteredSelection, null, Statuses.USER_ID, null, Statuses.SORT_ORDER_TIMESTAMP_DESC);
|
2015-04-21 05:29:05 +02:00
|
|
|
//noinspection TryFinallyCanBeTryWithResources
|
2015-03-24 16:40:47 +01:00
|
|
|
try {
|
|
|
|
final int usersCount = userCursor.getCount();
|
|
|
|
final int statusesCount = statusCursor.getCount();
|
|
|
|
if (statusesCount == 0 || usersCount == 0) return;
|
2015-04-04 16:31:37 +02:00
|
|
|
final int idxStatusId = statusCursor.getColumnIndex(Statuses.STATUS_ID),
|
|
|
|
idxUserName = userCursor.getColumnIndex(Statuses.USER_NAME),
|
2015-03-24 16:40:47 +01:00
|
|
|
idxUserScreenName = userCursor.getColumnIndex(Statuses.USER_NAME),
|
|
|
|
idxUserId = userCursor.getColumnIndex(Statuses.USER_NAME);
|
2015-04-04 16:31:37 +02:00
|
|
|
final long statusId = statusCursor.moveToFirst() ? statusCursor.getLong(idxStatusId) : -1;
|
2015-03-24 16:40:47 +01:00
|
|
|
final String notificationTitle = resources.getQuantityString(R.plurals.N_new_statuses,
|
|
|
|
statusesCount, statusesCount);
|
|
|
|
final String notificationContent;
|
|
|
|
userCursor.moveToFirst();
|
2015-04-24 08:14:33 +02:00
|
|
|
final UserColorNameManager manager = UserColorNameManager.getInstance(context);
|
|
|
|
final String displayName = manager.getUserNickname(userCursor.getLong(idxUserId),
|
2015-03-24 16:40:47 +01:00
|
|
|
mNameFirst ? userCursor.getString(idxUserName) : userCursor.getString(idxUserScreenName));
|
|
|
|
if (usersCount == 1) {
|
|
|
|
notificationContent = context.getString(R.string.from_name, displayName);
|
|
|
|
} else if (usersCount == 2) {
|
|
|
|
userCursor.moveToPosition(1);
|
2015-04-24 08:14:33 +02:00
|
|
|
final String othersName = manager.getUserNickname(userCursor.getLong(idxUserId),
|
2015-03-24 16:40:47 +01:00
|
|
|
mNameFirst ? userCursor.getString(idxUserName) : userCursor.getString(idxUserScreenName));
|
|
|
|
notificationContent = resources.getQuantityString(R.plurals.from_name_and_N_others,
|
|
|
|
usersCount - 1, othersName, usersCount - 1);
|
|
|
|
} else {
|
|
|
|
userCursor.moveToPosition(1);
|
2015-04-24 08:14:33 +02:00
|
|
|
final String othersName = manager.getUserNickname(userCursor.getLong(idxUserId),
|
2015-03-24 16:40:47 +01:00
|
|
|
mNameFirst ? userCursor.getString(idxUserName) : userCursor.getString(idxUserScreenName));
|
|
|
|
notificationContent = resources.getString(R.string.from_name_and_N_others, othersName, usersCount - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup notification
|
|
|
|
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
|
2015-03-28 19:30:51 +01:00
|
|
|
builder.setAutoCancel(true);
|
2015-03-24 16:40:47 +01:00
|
|
|
builder.setSmallIcon(R.drawable.ic_stat_twitter);
|
|
|
|
builder.setTicker(notificationTitle);
|
|
|
|
builder.setContentTitle(notificationTitle);
|
|
|
|
builder.setContentText(notificationContent);
|
|
|
|
builder.setCategory(NotificationCompat.CATEGORY_SOCIAL);
|
2015-04-04 16:31:37 +02:00
|
|
|
builder.setContentIntent(getContentIntent(context, AUTHORITY_HOME, accountId));
|
|
|
|
builder.setDeleteIntent(getDeleteIntent(context, AUTHORITY_HOME, accountId, statusId));
|
2015-03-24 16:40:47 +01:00
|
|
|
builder.setNumber(statusesCount);
|
|
|
|
builder.setColor(pref.getNotificationLightColor());
|
2015-04-01 12:28:26 +02:00
|
|
|
setNotificationPreferences(builder, pref, pref.getHomeTimelineNotificationType());
|
2015-03-24 16:40:47 +01:00
|
|
|
nm.notify("home_" + accountId, NOTIFICATION_ID_HOME_TIMELINE, builder.build());
|
2015-04-07 13:39:57 +02:00
|
|
|
Utils.sendPebbleNotification(context, notificationContent);
|
2015-03-24 16:40:47 +01:00
|
|
|
} finally {
|
|
|
|
statusCursor.close();
|
|
|
|
userCursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showMentionsNotification(AccountPreferences pref, long position) {
|
|
|
|
final long accountId = pref.getAccountId();
|
|
|
|
final Context context = getContext();
|
|
|
|
final Resources resources = context.getResources();
|
|
|
|
final NotificationManager nm = getNotificationManager();
|
2015-04-01 12:28:26 +02:00
|
|
|
final Expression selection;
|
|
|
|
if (pref.isNotificationFollowingOnly()) {
|
|
|
|
selection = Expression.and(Expression.equals(Statuses.ACCOUNT_ID, accountId),
|
|
|
|
Expression.greaterThan(Statuses.STATUS_ID, position),
|
|
|
|
Expression.equals(Statuses.IS_FOLLOWING, 1));
|
|
|
|
} else {
|
|
|
|
selection = Expression.and(Expression.equals(Statuses.ACCOUNT_ID, accountId),
|
|
|
|
Expression.greaterThan(Statuses.STATUS_ID, position));
|
|
|
|
}
|
2015-03-24 16:40:47 +01:00
|
|
|
final String filteredSelection = Utils.buildStatusFilterWhereClause(Mentions.TABLE_NAME,
|
2015-04-09 13:50:32 +02:00
|
|
|
selection).getSQL();
|
2015-03-24 16:40:47 +01:00
|
|
|
final String[] userProjection = {Statuses.USER_ID, Statuses.USER_NAME, Statuses.USER_SCREEN_NAME};
|
2015-04-04 16:31:37 +02:00
|
|
|
final String[] statusProjection = {Statuses.STATUS_ID, Statuses.USER_ID, Statuses.USER_NAME, Statuses.USER_SCREEN_NAME,
|
2015-03-24 16:40:47 +01:00
|
|
|
Statuses.TEXT_UNESCAPED, Statuses.STATUS_TIMESTAMP};
|
|
|
|
final Cursor statusCursor = mDatabaseWrapper.query(Mentions.TABLE_NAME, statusProjection,
|
|
|
|
filteredSelection, null, null, null, Statuses.SORT_ORDER_TIMESTAMP_DESC);
|
|
|
|
final Cursor userCursor = mDatabaseWrapper.query(Mentions.TABLE_NAME, userProjection,
|
|
|
|
filteredSelection, null, Statuses.USER_ID, null, Statuses.SORT_ORDER_TIMESTAMP_DESC);
|
2015-04-21 05:29:05 +02:00
|
|
|
//noinspection TryFinallyCanBeTryWithResources
|
2015-03-24 16:40:47 +01:00
|
|
|
try {
|
|
|
|
final int usersCount = userCursor.getCount();
|
|
|
|
final int statusesCount = statusCursor.getCount();
|
|
|
|
if (statusesCount == 0 || usersCount == 0) return;
|
|
|
|
final String accountName = Utils.getAccountName(context, accountId);
|
|
|
|
final String accountScreenName = Utils.getAccountScreenName(context, accountId);
|
|
|
|
final int idxStatusText = statusCursor.getColumnIndex(Statuses.TEXT_UNESCAPED),
|
2015-04-04 16:31:37 +02:00
|
|
|
idxStatusId = statusCursor.getColumnIndex(Statuses.STATUS_ID),
|
2015-03-24 16:40:47 +01:00
|
|
|
idxStatusTimestamp = statusCursor.getColumnIndex(Statuses.STATUS_TIMESTAMP),
|
|
|
|
idxStatusUserName = statusCursor.getColumnIndex(Statuses.USER_NAME),
|
|
|
|
idxStatusUserScreenName = statusCursor.getColumnIndex(Statuses.USER_SCREEN_NAME),
|
|
|
|
idxUserName = userCursor.getColumnIndex(Statuses.USER_NAME),
|
|
|
|
idxUserScreenName = userCursor.getColumnIndex(Statuses.USER_NAME),
|
|
|
|
idxUserId = userCursor.getColumnIndex(Statuses.USER_NAME);
|
|
|
|
|
|
|
|
final CharSequence notificationTitle = resources.getQuantityString(R.plurals.N_new_mentions,
|
|
|
|
statusesCount, statusesCount);
|
|
|
|
final String notificationContent;
|
|
|
|
userCursor.moveToFirst();
|
2015-04-24 08:14:33 +02:00
|
|
|
final UserColorNameManager manager = UserColorNameManager.getInstance(context);
|
|
|
|
final String displayName = manager.getUserNickname(userCursor.getLong(idxUserId),
|
2015-03-24 16:40:47 +01:00
|
|
|
mNameFirst ? userCursor.getString(idxUserName) : userCursor.getString(idxUserScreenName));
|
|
|
|
if (usersCount == 1) {
|
|
|
|
notificationContent = context.getString(R.string.notification_mention, displayName);
|
|
|
|
} else {
|
|
|
|
notificationContent = context.getString(R.string.notification_mention_multiple,
|
|
|
|
displayName, usersCount - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add rich notification and get latest tweet timestamp
|
2015-04-04 16:31:37 +02:00
|
|
|
long when = -1, statusId = -1;
|
2015-03-24 16:40:47 +01:00
|
|
|
final InboxStyle style = new InboxStyle();
|
|
|
|
for (int i = 0, j = Math.min(statusesCount, 5); statusCursor.moveToPosition(i) && i < j; i++) {
|
2015-04-04 16:31:37 +02:00
|
|
|
if (when == -1) {
|
2015-03-24 16:40:47 +01:00
|
|
|
when = statusCursor.getLong(idxStatusTimestamp);
|
|
|
|
}
|
2015-04-04 16:31:37 +02:00
|
|
|
if (statusId == -1) {
|
|
|
|
statusId = statusCursor.getLong(idxStatusId);
|
|
|
|
}
|
2015-03-24 16:40:47 +01:00
|
|
|
final SpannableStringBuilder sb = new SpannableStringBuilder();
|
2015-04-24 08:14:33 +02:00
|
|
|
sb.append(manager.getUserNickname(statusCursor.getLong(idxUserId),
|
2015-03-24 16:40:47 +01:00
|
|
|
mNameFirst ? statusCursor.getString(idxStatusUserName) : statusCursor.getString(idxStatusUserScreenName)));
|
|
|
|
sb.setSpan(new StyleSpan(Typeface.BOLD), 0, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
sb.append(' ');
|
|
|
|
sb.append(statusCursor.getString(idxStatusText));
|
|
|
|
style.addLine(sb);
|
|
|
|
}
|
|
|
|
if (mNameFirst) {
|
|
|
|
style.setSummaryText(accountName);
|
|
|
|
} else {
|
|
|
|
style.setSummaryText("@" + accountScreenName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup notification
|
|
|
|
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
|
2015-03-28 19:30:51 +01:00
|
|
|
builder.setAutoCancel(true);
|
2015-03-24 16:40:47 +01:00
|
|
|
builder.setSmallIcon(R.drawable.ic_stat_mention);
|
|
|
|
builder.setTicker(notificationTitle);
|
|
|
|
builder.setContentTitle(notificationTitle);
|
|
|
|
builder.setContentText(notificationContent);
|
|
|
|
builder.setCategory(NotificationCompat.CATEGORY_SOCIAL);
|
2015-04-04 16:31:37 +02:00
|
|
|
builder.setContentIntent(getContentIntent(context, AUTHORITY_MENTIONS, accountId));
|
|
|
|
builder.setDeleteIntent(getDeleteIntent(context, AUTHORITY_MENTIONS, accountId, statusId));
|
2015-03-24 16:40:47 +01:00
|
|
|
builder.setNumber(statusesCount);
|
|
|
|
builder.setWhen(when);
|
|
|
|
builder.setStyle(style);
|
|
|
|
builder.setColor(pref.getNotificationLightColor());
|
2015-04-01 12:28:26 +02:00
|
|
|
setNotificationPreferences(builder, pref, pref.getMentionsNotificationType());
|
2015-03-24 16:40:47 +01:00
|
|
|
nm.notify("mentions_" + accountId, NOTIFICATION_ID_MENTIONS_TIMELINE,
|
|
|
|
builder.build());
|
2015-04-07 13:39:57 +02:00
|
|
|
Utils.sendPebbleNotification(context, notificationContent);
|
2015-03-24 16:40:47 +01:00
|
|
|
} finally {
|
|
|
|
statusCursor.close();
|
|
|
|
userCursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 16:31:37 +02:00
|
|
|
private PendingIntent getContentIntent(Context context, String type, long accountId) {
|
|
|
|
// Setup click intent
|
|
|
|
final Intent homeIntent = new Intent(context, HomeActivity.class);
|
|
|
|
final Uri.Builder homeLinkBuilder = new Uri.Builder();
|
|
|
|
homeLinkBuilder.scheme(SCHEME_TWIDERE);
|
|
|
|
homeLinkBuilder.authority(type);
|
|
|
|
homeLinkBuilder.appendQueryParameter(QUERY_PARAM_ACCOUNT_ID, String.valueOf(accountId));
|
|
|
|
homeIntent.setData(homeLinkBuilder.build());
|
|
|
|
return PendingIntent.getActivity(context, 0, homeIntent, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static PendingIntent getDeleteIntent(Context context, String type, long accountId, long position) {
|
|
|
|
// Setup delete intent
|
|
|
|
final Intent recvIntent = new Intent(context, NotificationReceiver.class);
|
2015-04-08 12:39:25 +02:00
|
|
|
recvIntent.setAction(BROADCAST_NOTIFICATION_DELETED);
|
2015-04-04 16:31:37 +02:00
|
|
|
final Uri.Builder recvLinkBuilder = new Uri.Builder();
|
|
|
|
recvLinkBuilder.scheme(SCHEME_TWIDERE);
|
|
|
|
recvLinkBuilder.authority(AUTHORITY_NOTIFICATIONS);
|
|
|
|
recvLinkBuilder.appendPath(type);
|
|
|
|
recvLinkBuilder.appendQueryParameter(QUERY_PARAM_ACCOUNT_ID, String.valueOf(accountId));
|
|
|
|
recvLinkBuilder.appendQueryParameter(QUERY_PARAM_READ_POSITION, String.valueOf(position));
|
|
|
|
recvIntent.setData(recvLinkBuilder.build());
|
|
|
|
return PendingIntent.getBroadcast(context, 0, recvIntent, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static PendingIntent getDeleteIntent(Context context, String type, long accountId, StringLongPair[] positions) {
|
|
|
|
// Setup delete intent
|
|
|
|
final Intent recvIntent = new Intent(context, NotificationReceiver.class);
|
|
|
|
final Uri.Builder recvLinkBuilder = new Uri.Builder();
|
|
|
|
recvLinkBuilder.scheme(SCHEME_TWIDERE);
|
|
|
|
recvLinkBuilder.authority(AUTHORITY_NOTIFICATIONS);
|
|
|
|
recvLinkBuilder.appendPath(type);
|
|
|
|
recvLinkBuilder.appendQueryParameter(QUERY_PARAM_ACCOUNT_ID, String.valueOf(accountId));
|
|
|
|
recvLinkBuilder.appendQueryParameter(QUERY_PARAM_READ_POSITIONS, StringLongPair.toString(positions));
|
|
|
|
recvIntent.setData(recvLinkBuilder.build());
|
|
|
|
return PendingIntent.getBroadcast(context, 0, recvIntent, 0);
|
|
|
|
}
|
|
|
|
|
2015-04-01 12:28:26 +02:00
|
|
|
private void setNotificationPreferences(NotificationCompat.Builder builder, AccountPreferences pref, int defaultFlags) {
|
|
|
|
int notificationDefaults = 0;
|
|
|
|
if (AccountPreferences.isNotificationHasLight(defaultFlags)) {
|
|
|
|
notificationDefaults |= NotificationCompat.DEFAULT_LIGHTS;
|
|
|
|
}
|
2015-04-08 12:39:25 +02:00
|
|
|
if (isNotificationAudible()) {
|
|
|
|
if (AccountPreferences.isNotificationHasVibration(defaultFlags)) {
|
|
|
|
notificationDefaults |= NotificationCompat.DEFAULT_VIBRATE;
|
|
|
|
} else {
|
|
|
|
notificationDefaults &= ~NotificationCompat.DEFAULT_VIBRATE;
|
|
|
|
}
|
|
|
|
if (AccountPreferences.isNotificationHasRingtone(defaultFlags)) {
|
|
|
|
notificationDefaults |= NotificationCompat.DEFAULT_SOUND;
|
|
|
|
builder.setSound(pref.getNotificationRingtone(), AudioManager.STREAM_NOTIFICATION);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
notificationDefaults &= ~(NotificationCompat.DEFAULT_VIBRATE | NotificationCompat.DEFAULT_SOUND);
|
2015-04-01 12:28:26 +02:00
|
|
|
}
|
|
|
|
builder.setDefaults(notificationDefaults);
|
|
|
|
}
|
|
|
|
|
2015-03-25 16:04:25 +01:00
|
|
|
private void showMessagesNotification(AccountPreferences pref, StringLongPair[] pairs, ContentValues[] valuesArray) {
|
2015-03-25 14:20:51 +01:00
|
|
|
final long accountId = pref.getAccountId();
|
2015-03-25 16:04:25 +01:00
|
|
|
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);
|
2015-03-25 14:20:51 +01:00
|
|
|
final Context context = getContext();
|
|
|
|
final Resources resources = context.getResources();
|
|
|
|
final NotificationManager nm = getNotificationManager();
|
|
|
|
final ArrayList<Expression> orExpressions = new ArrayList<>();
|
|
|
|
final String prefix = accountId + "-";
|
|
|
|
final int prefixLength = prefix.length();
|
|
|
|
final Set<Long> senderIds = new CompactHashSet<>();
|
|
|
|
for (StringLongPair pair : pairs) {
|
|
|
|
final String key = pair.getKey();
|
|
|
|
if (key.startsWith(prefix)) {
|
|
|
|
final long senderId = Long.parseLong(key.substring(prefixLength));
|
|
|
|
senderIds.add(senderId);
|
|
|
|
final Expression expression = Expression.and(
|
|
|
|
Expression.equals(DirectMessages.SENDER_ID, senderId),
|
|
|
|
Expression.greaterThan(DirectMessages.MESSAGE_ID, pair.getValue())
|
|
|
|
);
|
|
|
|
orExpressions.add(expression);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
orExpressions.add(Expression.notIn(new Column(DirectMessages.SENDER_ID), new RawItemArray(senderIds.toArray())));
|
2015-03-25 16:04:25 +01:00
|
|
|
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()]))
|
|
|
|
);
|
2015-03-25 14:20:51 +01:00
|
|
|
final String filteredSelection = selection.getSQL();
|
|
|
|
final String[] userProjection = {DirectMessages.SENDER_ID, DirectMessages.SENDER_NAME,
|
|
|
|
DirectMessages.SENDER_SCREEN_NAME};
|
2015-04-04 16:31:37 +02:00
|
|
|
final String[] messageProjection = {DirectMessages.MESSAGE_ID, DirectMessages.SENDER_ID,
|
|
|
|
DirectMessages.SENDER_NAME, DirectMessages.SENDER_SCREEN_NAME, DirectMessages.TEXT_UNESCAPED,
|
2015-03-25 14:20:51 +01:00
|
|
|
DirectMessages.MESSAGE_TIMESTAMP};
|
|
|
|
final Cursor messageCursor = mDatabaseWrapper.query(DirectMessages.Inbox.TABLE_NAME, messageProjection,
|
|
|
|
filteredSelection, null, null, null, DirectMessages.DEFAULT_SORT_ORDER);
|
|
|
|
final Cursor userCursor = mDatabaseWrapper.query(DirectMessages.Inbox.TABLE_NAME, userProjection,
|
|
|
|
filteredSelection, null, DirectMessages.SENDER_ID, null, DirectMessages.DEFAULT_SORT_ORDER);
|
2015-04-21 05:29:05 +02:00
|
|
|
//noinspection TryFinallyCanBeTryWithResources
|
2015-03-25 14:20:51 +01:00
|
|
|
try {
|
|
|
|
final int usersCount = userCursor.getCount();
|
|
|
|
final int messagesCount = messageCursor.getCount();
|
|
|
|
if (messagesCount == 0 || usersCount == 0) return;
|
|
|
|
final String accountName = Utils.getAccountName(context, accountId);
|
|
|
|
final String accountScreenName = Utils.getAccountScreenName(context, accountId);
|
|
|
|
final int idxMessageText = messageCursor.getColumnIndex(DirectMessages.TEXT_UNESCAPED),
|
|
|
|
idxMessageTimestamp = messageCursor.getColumnIndex(DirectMessages.MESSAGE_TIMESTAMP),
|
2015-04-04 16:31:37 +02:00
|
|
|
idxMessageId = messageCursor.getColumnIndex(DirectMessages.MESSAGE_ID),
|
|
|
|
idxMessageUserId = messageCursor.getColumnIndex(DirectMessages.SENDER_ID),
|
2015-03-25 14:20:51 +01:00
|
|
|
idxMessageUserName = messageCursor.getColumnIndex(DirectMessages.SENDER_NAME),
|
|
|
|
idxMessageUserScreenName = messageCursor.getColumnIndex(DirectMessages.SENDER_SCREEN_NAME),
|
|
|
|
idxUserName = userCursor.getColumnIndex(DirectMessages.SENDER_NAME),
|
|
|
|
idxUserScreenName = userCursor.getColumnIndex(DirectMessages.SENDER_NAME),
|
|
|
|
idxUserId = userCursor.getColumnIndex(DirectMessages.SENDER_NAME);
|
|
|
|
|
|
|
|
final CharSequence notificationTitle = resources.getQuantityString(R.plurals.N_new_messages,
|
|
|
|
messagesCount, messagesCount);
|
|
|
|
final String notificationContent;
|
|
|
|
userCursor.moveToFirst();
|
2015-04-24 08:14:33 +02:00
|
|
|
final UserColorNameManager manager = UserColorNameManager.getInstance(context);
|
|
|
|
final String displayName = manager.getUserNickname(userCursor.getLong(idxUserId),
|
2015-03-25 14:20:51 +01:00
|
|
|
mNameFirst ? userCursor.getString(idxUserName) : userCursor.getString(idxUserScreenName));
|
|
|
|
if (usersCount == 1) {
|
|
|
|
if (messagesCount == 1) {
|
|
|
|
notificationContent = context.getString(R.string.notification_direct_message, displayName);
|
|
|
|
} else {
|
|
|
|
notificationContent = context.getString(R.string.notification_direct_message_multiple_messages,
|
|
|
|
displayName, messagesCount);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
notificationContent = context.getString(R.string.notification_direct_message_multiple_users,
|
|
|
|
displayName, usersCount - 1, messagesCount);
|
|
|
|
}
|
|
|
|
|
2015-04-04 16:31:37 +02:00
|
|
|
final LongSparseArray<Long> idsMap = new LongSparseArray<>();
|
2015-03-25 14:20:51 +01:00
|
|
|
// Add rich notification and get latest tweet timestamp
|
|
|
|
long when = -1;
|
|
|
|
final InboxStyle style = new InboxStyle();
|
2015-04-04 16:31:37 +02:00
|
|
|
for (int i = 0; messageCursor.moveToPosition(i) && i < messagesCount; i++) {
|
2015-03-25 14:20:51 +01:00
|
|
|
if (when < 0) {
|
|
|
|
when = messageCursor.getLong(idxMessageTimestamp);
|
|
|
|
}
|
2015-04-04 16:31:37 +02:00
|
|
|
if (i < 5) {
|
|
|
|
final SpannableStringBuilder sb = new SpannableStringBuilder();
|
2015-04-24 08:14:33 +02:00
|
|
|
sb.append(manager.getUserNickname(messageCursor.getLong(idxUserId),
|
2015-04-04 16:31:37 +02:00
|
|
|
mNameFirst ? messageCursor.getString(idxMessageUserName) : messageCursor.getString(idxMessageUserScreenName)));
|
|
|
|
sb.setSpan(new StyleSpan(Typeface.BOLD), 0, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
|
|
|
sb.append(' ');
|
|
|
|
sb.append(messageCursor.getString(idxMessageText));
|
|
|
|
style.addLine(sb);
|
|
|
|
}
|
|
|
|
final long userId = messageCursor.getLong(idxMessageUserId);
|
|
|
|
final long messageId = messageCursor.getLong(idxMessageId);
|
|
|
|
idsMap.put(userId, Math.max(idsMap.get(userId, -1L), messageId));
|
2015-03-25 14:20:51 +01:00
|
|
|
}
|
|
|
|
if (mNameFirst) {
|
|
|
|
style.setSummaryText(accountName);
|
|
|
|
} else {
|
|
|
|
style.setSummaryText("@" + accountScreenName);
|
|
|
|
}
|
2015-04-04 16:31:37 +02:00
|
|
|
final StringLongPair[] positions = new StringLongPair[idsMap.size()];
|
|
|
|
for (int i = 0, j = idsMap.size(); i < j; i++) {
|
|
|
|
positions[i] = new StringLongPair(String.valueOf(idsMap.keyAt(i)), idsMap.valueAt(i));
|
|
|
|
}
|
2015-03-25 14:20:51 +01:00
|
|
|
|
|
|
|
// Setup notification
|
|
|
|
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
|
2015-03-28 19:30:51 +01:00
|
|
|
builder.setAutoCancel(true);
|
2015-05-04 17:53:52 +02:00
|
|
|
builder.setSmallIcon(R.drawable.ic_stat_message);
|
2015-03-25 14:20:51 +01:00
|
|
|
builder.setTicker(notificationTitle);
|
|
|
|
builder.setContentTitle(notificationTitle);
|
|
|
|
builder.setContentText(notificationContent);
|
|
|
|
builder.setCategory(NotificationCompat.CATEGORY_SOCIAL);
|
2015-04-04 16:31:37 +02:00
|
|
|
builder.setContentIntent(getContentIntent(context, AUTHORITY_DIRECT_MESSAGES, accountId));
|
2015-04-21 05:38:09 +02:00
|
|
|
builder.setDeleteIntent(getDeleteIntent(context, AUTHORITY_DIRECT_MESSAGES, accountId, positions));
|
2015-03-25 14:20:51 +01:00
|
|
|
builder.setNumber(messagesCount);
|
|
|
|
builder.setWhen(when);
|
|
|
|
builder.setStyle(style);
|
|
|
|
builder.setColor(pref.getNotificationLightColor());
|
2015-04-01 12:28:26 +02:00
|
|
|
setNotificationPreferences(builder, pref, pref.getDirectMessagesNotificationType());
|
2015-03-25 14:20:51 +01:00
|
|
|
nm.notify("messages_" + accountId, NOTIFICATION_ID_DIRECT_MESSAGES, builder.build());
|
2015-04-07 13:39:57 +02:00
|
|
|
Utils.sendPebbleNotification(context, notificationContent);
|
2015-03-25 14:20:51 +01:00
|
|
|
} finally {
|
|
|
|
messageCursor.close();
|
|
|
|
userCursor.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 08:30:05 +01:00
|
|
|
private void preloadImages(final ContentValues... values) {
|
|
|
|
if (values == null) return;
|
|
|
|
for (final ContentValues v : values) {
|
|
|
|
if (mPreferences.getBoolean(KEY_PRELOAD_PROFILE_IMAGES, false)) {
|
|
|
|
mImagePreloader.preloadImage(v.getAsString(Statuses.USER_PROFILE_IMAGE_URL));
|
|
|
|
mImagePreloader.preloadImage(v.getAsString(DirectMessages.SENDER_PROFILE_IMAGE_URL));
|
|
|
|
mImagePreloader.preloadImage(v.getAsString(DirectMessages.RECIPIENT_PROFILE_IMAGE_URL));
|
|
|
|
}
|
|
|
|
if (mPreferences.getBoolean(KEY_PRELOAD_PREVIEW_IMAGES, false)) {
|
|
|
|
final String textHtml = v.getAsString(Statuses.TEXT_HTML);
|
|
|
|
for (final String link : MediaPreviewUtils.getSupportedLinksInStatus(textHtml)) {
|
|
|
|
mImagePreloader.preloadImage(link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setNotificationUri(final Cursor c, final Uri uri) {
|
|
|
|
final ContentResolver cr = getContentResolver();
|
|
|
|
if (cr == null || c == null || uri == null) return;
|
|
|
|
c.setNotificationUri(cr, uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updatePreferences() {
|
|
|
|
mNameFirst = mPreferences.getBoolean(KEY_NAME_FIRST, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static Cursor getPreferencesCursor(final SharedPreferencesWrapper preferences, final String key) {
|
2015-01-11 10:28:45 +01:00
|
|
|
final MatrixCursor c = new MatrixCursor(TwidereDataStore.Preferences.MATRIX_COLUMNS);
|
|
|
|
final Map<String, Object> map = new HashMap<>();
|
2014-11-26 08:30:05 +01:00
|
|
|
final Map<String, ?> all = preferences.getAll();
|
|
|
|
if (key == null) {
|
|
|
|
map.putAll(all);
|
|
|
|
} else {
|
|
|
|
map.put(key, all.get(key));
|
|
|
|
}
|
|
|
|
for (final Map.Entry<String, ?> item : map.entrySet()) {
|
|
|
|
final Object value = item.getValue();
|
|
|
|
final int type = getPreferenceType(value);
|
|
|
|
c.addRow(new Object[]{item.getKey(), ParseUtils.parseString(value), type});
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int getPreferenceType(final Object object) {
|
|
|
|
if (object == null)
|
|
|
|
return Preferences.TYPE_NULL;
|
|
|
|
else if (object instanceof Boolean)
|
|
|
|
return Preferences.TYPE_BOOLEAN;
|
|
|
|
else if (object instanceof Integer)
|
|
|
|
return Preferences.TYPE_INTEGER;
|
|
|
|
else if (object instanceof Long)
|
|
|
|
return Preferences.TYPE_LONG;
|
|
|
|
else if (object instanceof Float)
|
|
|
|
return Preferences.TYPE_FLOAT;
|
|
|
|
else if (object instanceof String) return Preferences.TYPE_STRING;
|
|
|
|
return Preferences.TYPE_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static int getUnreadCount(final List<UnreadItem> set, final long... accountIds) {
|
|
|
|
if (set == null || set.isEmpty()) return 0;
|
|
|
|
int count = 0;
|
|
|
|
for (final UnreadItem item : set.toArray(new UnreadItem[set.size()])) {
|
|
|
|
if (item != null && ArrayUtils.contains(accountIds, item.account_id)) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean shouldReplaceOnConflict(final int table_id) {
|
|
|
|
switch (table_id) {
|
|
|
|
case TABLE_ID_CACHED_HASHTAGS:
|
|
|
|
case TABLE_ID_CACHED_STATUSES:
|
|
|
|
case TABLE_ID_CACHED_USERS:
|
2015-01-10 16:17:02 +01:00
|
|
|
case TABLE_ID_CACHED_RELATIONSHIPS:
|
|
|
|
case TABLE_ID_SEARCH_HISTORY:
|
2014-11-26 08:30:05 +01:00
|
|
|
case TABLE_ID_FILTERED_USERS:
|
|
|
|
case TABLE_ID_FILTERED_KEYWORDS:
|
|
|
|
case TABLE_ID_FILTERED_SOURCES:
|
|
|
|
case TABLE_ID_FILTERED_LINKS:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("unused")
|
2015-04-09 13:50:32 +02:00
|
|
|
private static class GetWritableDatabaseTask extends AsyncTask<Object, Object, SQLiteDatabase> {
|
2014-11-26 08:30:05 +01:00
|
|
|
private final Context mContext;
|
|
|
|
private final SQLiteOpenHelper mHelper;
|
|
|
|
private final SQLiteDatabaseWrapper mWrapper;
|
|
|
|
|
|
|
|
GetWritableDatabaseTask(final Context context, final SQLiteOpenHelper helper,
|
|
|
|
final SQLiteDatabaseWrapper wrapper) {
|
|
|
|
mContext = context;
|
|
|
|
mHelper = helper;
|
|
|
|
mWrapper = wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-09 13:50:32 +02:00
|
|
|
protected SQLiteDatabase doInBackground(final Object... params) {
|
2014-11-26 08:30:05 +01:00
|
|
|
return mHelper.getWritableDatabase();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(final SQLiteDatabase result) {
|
|
|
|
mWrapper.setSQLiteDatabase(result);
|
|
|
|
if (result != null) {
|
|
|
|
mContext.sendBroadcast(new Intent(BROADCAST_DATABASE_READY));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-03 07:48:39 +02:00
|
|
|
|
|
|
|
}
|