Skip to content

Commit

Permalink
Create placeholder ContentProviders for database observations.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Sep 8, 2018
1 parent 1c75f37 commit c3bdc48
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 10 deletions.
12 changes: 12 additions & 0 deletions AndroidManifest.xml
Expand Up @@ -557,6 +557,18 @@

</provider>

<provider android:name=".database.DatabaseContentProviders$Conversation"
android:authorities="org.thoughtcrime.securesms.database.conversation"
android:exported="false" />

<provider android:name=".database.DatabaseContentProviders$ConversationList"
android:authorities="org.thoughtcrime.securesms.database.conversationlist"
android:exported="false" />

<provider android:name=".database.DatabaseContentProviders$Attachment"
android:authorities="org.thoughtcrime.securesms.database.attachment"
android:exported="false" />

<receiver android:name=".service.BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
Expand Down
17 changes: 7 additions & 10 deletions src/org/thoughtcrime/securesms/database/Database.java
Expand Up @@ -28,10 +28,7 @@

public abstract class Database {

protected static final String ID_WHERE = "_id = ?";
private static final String CONVERSATION_URI = "content://textsecure/thread/";
private static final String CONVERSATION_LIST_URI = "content://textsecure/conversation-list";
private static final String ATTACHMENT_URI = "content://textsecure/attachment/";
protected static final String ID_WHERE = "_id = ?";

protected SQLCipherOpenHelper databaseHelper;
protected final Context context;
Expand All @@ -47,29 +44,29 @@ protected void notifyConversationListeners(Set<Long> threadIds) {
}

protected void notifyConversationListeners(long threadId) {
context.getContentResolver().notifyChange(Uri.parse(CONVERSATION_URI + threadId), null);
context.getContentResolver().notifyChange(DatabaseContentProviders.Conversation.getUriForThread(threadId), null);
}

protected void notifyConversationListListeners() {
context.getContentResolver().notifyChange(Uri.parse(CONVERSATION_LIST_URI), null);
context.getContentResolver().notifyChange(DatabaseContentProviders.ConversationList.CONTENT_URI, null);
}

protected void setNotifyConverationListeners(Cursor cursor, long threadId) {
cursor.setNotificationUri(context.getContentResolver(), Uri.parse(CONVERSATION_URI + threadId));
cursor.setNotificationUri(context.getContentResolver(), DatabaseContentProviders.Conversation.getUriForThread(threadId));
}

protected void setNotifyConverationListListeners(Cursor cursor) {
cursor.setNotificationUri(context.getContentResolver(), Uri.parse(CONVERSATION_LIST_URI));
cursor.setNotificationUri(context.getContentResolver(), DatabaseContentProviders.ConversationList.CONTENT_URI);
}

protected void registerAttachmentListeners(@NonNull ContentObserver observer) {
context.getContentResolver().registerContentObserver(Uri.parse(ATTACHMENT_URI),
context.getContentResolver().registerContentObserver(DatabaseContentProviders.Attachment.CONTENT_URI,
true,
observer);
}

protected void notifyAttachmentListeners() {
context.getContentResolver().notifyChange(Uri.parse(ATTACHMENT_URI), null);
context.getContentResolver().notifyChange(DatabaseContentProviders.Attachment.CONTENT_URI, null);
}

public void reset(SQLCipherOpenHelper databaseHelper) {
Expand Down
@@ -0,0 +1,67 @@
package org.thoughtcrime.securesms.database;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

/**
* Starting in API 26, a {@link ContentProvider} needs to be defined for each authority you wish to
* observe changes on. These classes essentially do nothing except exist so Android doesn't complain.
*/
public class DatabaseContentProviders {

public static class ConversationList extends NoopContentProvider {
public static final Uri CONTENT_URI = Uri.parse("content://org.thoughtcrime.securesms.database.conversationlist");
}

public static class Conversation extends NoopContentProvider {
private static final String CONTENT_URI_STRING = "content://org.thoughtcrime.securesms.database.conversation/";

public static Uri getUriForThread(long threadId) {
return Uri.parse(CONTENT_URI_STRING + threadId);
}
}

public static class Attachment extends NoopContentProvider {
public static final Uri CONTENT_URI = Uri.parse("content://org.thoughtcrime.securesms.database.attachment");
}

private static abstract class NoopContentProvider extends ContentProvider {

@Override
public boolean onCreate() {
return false;
}

@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
return null;
}

@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}

@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
return null;
}

@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
return 0;
}

@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
return 0;
}
}
}

0 comments on commit c3bdc48

Please sign in to comment.