Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

Added a way to provide the database files to the DatabasePeerManager module. #91

Merged
merged 1 commit into from Mar 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion stetho/src/main/java/com/facebook/stetho/Stetho.java
@@ -1,5 +1,6 @@
package com.facebook.stetho;

import com.facebook.stetho.inspector.database.DefaultDatabaseFilesProvider;
import javax.annotation.Nullable;

import java.io.IOException;
Expand Down Expand Up @@ -115,7 +116,7 @@ public Iterable<ChromeDevtoolsDomain> get() {
modules.add(new Runtime());
modules.add(new Worker());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
modules.add(new Database(context));
modules.add(new Database(context, new DefaultDatabaseFilesProvider(context)));
}
return modules;
}
Expand Down
@@ -0,0 +1,16 @@
package com.facebook.stetho.inspector.database;

import java.io.File;
import java.util.List;

/**
* Provides a {@link List} of database files.
*/
public interface DatabaseFilesProvider {

/**
* Returns a {@link List} of database files.
*/
List<File> getDatabaseFiles();

}
Expand Up @@ -2,14 +2,14 @@

package com.facebook.stetho.inspector.database;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.concurrent.ThreadSafe;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.content.Context;
import android.database.Cursor;
Expand All @@ -30,18 +30,39 @@ public class DatabasePeerManager extends ChromePeerManager {
};

private final Context mContext;
private final DatabaseFilesProvider mDatabaseFilesProvider;

/**
* Constructs the object with a {@link DatabaseFilesProvider} that supplies the database files
* from {@link Context#databaseList()}.
*
* @param context the context
* @deprecated use the other {@linkplain DatabasePeerManager#DatabasePeerManager(Context,
* DatabaseFilesProvider) constructor} and pass in the {@linkplain DefaultDatabaseFilesProvider
* default provider}.
*/
@Deprecated
public DatabasePeerManager(Context context) {
this(context, new DefaultDatabaseFilesProvider(context));
}

/**
* @param context the context
* @param databaseFilesProvider a database file name provider
*/
public DatabasePeerManager(Context context, DatabaseFilesProvider databaseFilesProvider) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: new line; mark databaseFilesProvider with @Nullable

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, mark mDatabaseFilesProvider with @Nullable and then make things not crash if it's null. Or drop this null support :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll drop the null support. Originally the DatabaseFilesProvider was optional and added to the context.databaseList(). I forgot to remove the comments once that interface became the only thing providing the file names.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: new line

I'm not sure where you want to the new line.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, misread :)

mContext = context;
mDatabaseFilesProvider = databaseFilesProvider;
setListener(mPeerRegistrationListener);
}

private void bootstrapNewPeer(JsonRpcPeer peer) {
Iterable<String> tidiedList = tidyDatabaseList(mContext.databaseList());
for (String databaseName : tidiedList) {
List<File> potentialDatabaseFiles = mDatabaseFilesProvider.getDatabaseFiles();
Iterable<File> tidiedList = tidyDatabaseList(potentialDatabaseFiles);
for (File database : tidiedList) {
Database.DatabaseObject databaseParams = new Database.DatabaseObject();
databaseParams.id = databaseName;
databaseParams.name = databaseName;
databaseParams.id = database.getPath();
databaseParams.name = database.getName();
databaseParams.domain = mContext.getPackageName();
databaseParams.version = "N/A";
Database.AddDatabaseEvent eventParams = new Database.AddDatabaseEvent();
Expand All @@ -56,17 +77,18 @@ private void bootstrapNewPeer(JsonRpcPeer peer) {
* that this only removes the database if it is true that it shadows another database lacking
* the uninteresting suffix.
*
* @param databaseFilenames Raw list of database filenames.
* @param databaseFiles Raw list of database files.
* @return Tidied list with shadow databases removed.
*/
// @VisibleForTesting
static List<String> tidyDatabaseList(String[] databaseFilenames) {
Set<String> originalAsSet = new HashSet<String>(Arrays.asList(databaseFilenames));
List<String> tidiedList = new ArrayList<String>();
for (String databaseFilename : databaseFilenames) {
static List<File> tidyDatabaseList(List<File> databaseFiles) {
Set<File> originalAsSet = new HashSet<File>(databaseFiles);
List<File> tidiedList = new ArrayList<File>();
for (File databaseFile : databaseFiles) {
String databaseFilename = databaseFile.getPath();
String sansSuffix = removeSuffix(databaseFilename, UNINTERESTING_FILENAME_SUFFIXES);
if (sansSuffix.equals(databaseFilename) || !originalAsSet.contains(sansSuffix)) {
tidiedList.add(databaseFilename);
if (sansSuffix.equals(databaseFilename) || !originalAsSet.contains(new File(sansSuffix))) {
tidiedList.add(databaseFile);
}
}
return tidiedList;
Expand Down
@@ -0,0 +1,27 @@
package com.facebook.stetho.inspector.database;

import android.content.Context;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
* Provides the results of {@link Context#databaseList()}.
*/
public final class DefaultDatabaseFilesProvider implements DatabaseFilesProvider {

private final Context mContext;

public DefaultDatabaseFilesProvider(Context context) {
mContext = context;
}

@Override
public List<File> getDatabaseFiles() {
List<File> databaseFiles = new ArrayList<File>();
for (String filename : mContext.databaseList()) {
databaseFiles.add(new File(filename));
}
return databaseFiles;
}
}
Expand Up @@ -2,6 +2,7 @@

package com.facebook.stetho.inspector.protocol.module;

import com.facebook.stetho.inspector.database.DatabaseFilesProvider;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -38,11 +39,25 @@ public class Database implements ChromeDevtoolsDomain {
private final DatabasePeerManager mDatabasePeerManager;
private final ObjectMapper mObjectMapper;

/**
* Constructs the object with the default {@link DatabasePeerManager}.
* @param context the context
*/
@Deprecated
public Database(Context context) {
mDatabasePeerManager = new DatabasePeerManager(context);
mObjectMapper = new ObjectMapper();
}

/**
* @param context the context
* @param databaseFilesProvider a database files provider
*/
public Database(Context context, DatabaseFilesProvider databaseFilesProvider) {
mDatabasePeerManager = new DatabasePeerManager(context, databaseFilesProvider);
mObjectMapper = new ObjectMapper();
}

@ChromeDevtoolsMethod
public void enable(JsonRpcPeer peer, JSONObject params) {
mDatabasePeerManager.addPeer(peer);
Expand Down
Expand Up @@ -2,6 +2,8 @@

package com.facebook.stetho.inspector.database;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.junit.Test;
Expand All @@ -11,19 +13,19 @@
public class DatabasePeerManagerTest {
@Test
public void testTidyDatabaseList() {
String[] databases = {
"foo.db", "foo.db-journal",
"bar.db", "bar.db-journal", "bar.db-uid",
"baz.db", "baz.db-somethingelse",
"dangling.db-journal",
};
String[] expected = {
"foo.db",
"bar.db",
"baz.db", "baz.db-somethingelse",
"dangling.db-journal",
};
List<String> tidied = DatabasePeerManager.tidyDatabaseList(databases);
assertArrayEquals(expected, tidied.toArray());
File[] databases = {
new File("foo.db"), new File("foo.db-journal"),
new File("bar.db"), new File("bar.db-journal"), new File( "bar.db-uid"),
new File("baz.db"), new File("baz.db-somethingelse"),
new File("dangling.db-journal"),
};
File[] expected = {
new File( "foo.db"),
new File("bar.db"),
new File("baz.db"), new File("baz.db-somethingelse"),
new File("dangling.db-journal")
};
List<File> tidied = DatabasePeerManager.tidyDatabaseList(Arrays.asList(databases));
assertArrayEquals(expected, tidied.toArray());
}
}