From cddbe40d0f828c66a4ff5c3870572b0047c3b903 Mon Sep 17 00:00:00 2001 From: Nikolay Pultsin Date: Sun, 25 Sep 2011 17:47:24 +0100 Subject: [PATCH] open network catalogs from external applications --- AndroidManifest.xml | 6 +++ AndroidManifest.xml.pattern | 6 +++ .../network/AddCustomCatalogActivity.java | 53 +++++++------------ .../network/NetworkLibraryActivity.java | 24 ++++++--- .../android/fbreader/network/Util.java | 2 +- .../action/EditCustomCatalogAction.java | 4 +- 6 files changed, 51 insertions(+), 44 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 9c22573f3..b0e2a28fb 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -139,6 +139,12 @@ + + + + + + diff --git a/AndroidManifest.xml.pattern b/AndroidManifest.xml.pattern index 601deed94..03cc9a8d5 100644 --- a/AndroidManifest.xml.pattern +++ b/AndroidManifest.xml.pattern @@ -139,6 +139,12 @@ + + + + + + diff --git a/src/org/geometerplus/android/fbreader/network/AddCustomCatalogActivity.java b/src/org/geometerplus/android/fbreader/network/AddCustomCatalogActivity.java index 8a8f2462b..5b4e2628c 100644 --- a/src/org/geometerplus/android/fbreader/network/AddCustomCatalogActivity.java +++ b/src/org/geometerplus/android/fbreader/network/AddCustomCatalogActivity.java @@ -42,37 +42,11 @@ import org.geometerplus.fbreader.network.urlInfo.*; import org.geometerplus.android.util.UIUtil; public class AddCustomCatalogActivity extends Activity { - private static final String ADD_CATALOG_TITLE_KEY = "title"; - private static final String ADD_CATALOG_SUMMARY_KEY = "summary"; - private static final String ADD_CATALOG_ID_KEY = "id"; - private static final String ADD_CATALOG_URLS_MAP_KEY = "urls"; - - public static void addLinkToIntent(Intent intent, ICustomNetworkLink link) { - Util.intentByLink(intent, link) - .putExtra(ADD_CATALOG_TITLE_KEY, link.getTitle()) - .putExtra(ADD_CATALOG_SUMMARY_KEY, link.getSummary()) - .putExtra(ADD_CATALOG_ID_KEY, link.getId()) - .putExtra(ADD_CATALOG_URLS_MAP_KEY, link.urlInfoMap()); - } - - static ICustomNetworkLink getLinkFromIntent(Intent intent) { - final Uri uri = intent.getData(); - if (uri == null || !intent.hasExtra(ADD_CATALOG_ID_KEY)) { - return null; - } - - return new OPDSCustomNetworkLink( - intent.getIntExtra(ADD_CATALOG_ID_KEY, ICustomNetworkLink.INVALID_ID), - uri.getHost(), - intent.getStringExtra(ADD_CATALOG_TITLE_KEY), - intent.getStringExtra(ADD_CATALOG_SUMMARY_KEY), - null, - (UrlInfoCollection)intent.getSerializableExtra(ADD_CATALOG_URLS_MAP_KEY) - ); - } + public static String EDIT_KEY = "EditNotAdd"; private ZLResource myResource; private volatile ICustomNetworkLink myLink; + private boolean myEditNotAdd; @Override public void onCreate(Bundle icicle) { @@ -114,8 +88,18 @@ public class AddCustomCatalogActivity extends Activity { Util.initLibrary(this); final Intent intent = getIntent(); - myLink = getLinkFromIntent(intent); - final Uri uri = intent.getData(); + myLink = null; + Uri uri = intent.getData(); + if (uri != null) { + if ("opds".equals(uri.getScheme())) { + uri = Uri.parse("http" + uri.toString().substring(4)); + } + final INetworkLink link = NetworkLibrary.Instance().getLinkByUrl(uri.toString()); + if (link instanceof ICustomNetworkLink) { + myLink = (ICustomNetworkLink)link; + } + } + myEditNotAdd = intent.getBooleanExtra(EDIT_KEY, false); if (myLink != null) { setTextById(R.id.add_custom_catalog_url, myLink.getUrl(UrlInfo.Type.Catalog)); @@ -123,6 +107,9 @@ public class AddCustomCatalogActivity extends Activity { setTextById(R.id.add_custom_catalog_summary, myLink.getSummary()); setExtraFieldsVisibility(true); } else if (uri != null) { + if ("opds".equals(uri.getScheme())) { + uri = Uri.parse("http" + uri.toString().substring(4)); + } loadInfoByUri(uri); } else { setExtraFieldsVisibility(false); @@ -167,12 +154,10 @@ public class AddCustomCatalogActivity extends Activity { final Intent intent = new Intent( NetworkLibraryActivity.OPEN_CATALOG_ACTION, - uri, + myEditNotAdd ? null : uri, AddCustomCatalogActivity.this, NetworkLibraryActivity.class ).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); - - addLinkToIntent(intent, myLink); startActivity(intent); finish(); } @@ -265,8 +250,6 @@ public class AddCustomCatalogActivity extends Activity { if (isEmptyString(uri.getScheme())) { textUrl = "http://" + textUrl; uri = Uri.parse(textUrl); - } else if ("opds".equals(uri.getScheme())) { - textUrl = "http" + uri.toString().substring(4); } setTextById(R.id.add_custom_catalog_url, textUrl); diff --git a/src/org/geometerplus/android/fbreader/network/NetworkLibraryActivity.java b/src/org/geometerplus/android/fbreader/network/NetworkLibraryActivity.java index 341f30689..2156e4551 100644 --- a/src/org/geometerplus/android/fbreader/network/NetworkLibraryActivity.java +++ b/src/org/geometerplus/android/fbreader/network/NetworkLibraryActivity.java @@ -24,6 +24,7 @@ import java.util.*; import android.app.AlertDialog; import android.content.Intent; import android.content.DialogInterface; +import android.net.Uri; import android.os.Bundle; import android.view.*; import android.widget.AdapterView; @@ -55,6 +56,7 @@ public class NetworkLibraryActivity extends TreeActivity implements NetworkLibra final List myOptionsMenuActions = new ArrayList(); final List myContextMenuActions = new ArrayList(); final List myListClickActions = new ArrayList(); + private Intent myDeferredIntent; @Override public void onCreate(Bundle icicle) { @@ -71,18 +73,19 @@ public class NetworkLibraryActivity extends TreeActivity implements NetworkLibra setListAdapter(new NetworkLibraryAdapter(this)); init(getIntent()); + myDeferredIntent = null; setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL); if (getCurrentTree() instanceof RootTree) { if (!NetworkLibrary.Instance().isInitialized()) { Util.initLibrary(this); + myDeferredIntent = getIntent(); } else { NetworkLibrary.Instance().fireModelChangedEvent(NetworkLibrary.ChangeListener.Code.SomeCode); + openTreeByIntent(getIntent()); } } - - openTreeByIntent(getIntent()); } @Override @@ -124,11 +127,14 @@ public class NetworkLibraryActivity extends TreeActivity implements NetworkLibra private boolean openTreeByIntent(Intent intent) { if (OPEN_CATALOG_ACTION.equals(intent.getAction())) { - final NetworkTree tree = - NetworkLibrary.Instance().getCatalogTreeByUrl(intent.getData().toString()); - if (tree != null) { - new OpenCatalogAction(this).run(tree); - return true; + final Uri uri = intent.getData(); + if (uri != null) { + final NetworkTree tree = + NetworkLibrary.Instance().getCatalogTreeByUrl(uri.toString()); + if (tree != null) { + new OpenCatalogAction(this).run(tree); + return true; + } } } return false; @@ -361,6 +367,10 @@ public class NetworkLibraryActivity extends TreeActivity implements NetworkLibra break; case InitializationFinished: NetworkLibrary.Instance().runBackgroundUpdate(false); + if (myDeferredIntent != null) { + openTreeByIntent(myDeferredIntent); + myDeferredIntent = null; + } break; case Found: openTree((NetworkTree)params[0]); diff --git a/src/org/geometerplus/android/fbreader/network/Util.java b/src/org/geometerplus/android/fbreader/network/Util.java index 0c12b699f..bf7e69fba 100644 --- a/src/org/geometerplus/android/fbreader/network/Util.java +++ b/src/org/geometerplus/android/fbreader/network/Util.java @@ -47,7 +47,7 @@ public abstract class Util implements UserRegistrationConstants { return uri != null ? NetworkLibrary.Instance().getLinkByUrl(uri.toString()) : null; } - static Intent intentByLink(Intent intent, INetworkLink link) { + public static Intent intentByLink(Intent intent, INetworkLink link) { if (link != null) { intent.setData(Uri.parse(link.getUrl(UrlInfo.Type.Catalog))); } diff --git a/src/org/geometerplus/android/fbreader/network/action/EditCustomCatalogAction.java b/src/org/geometerplus/android/fbreader/network/action/EditCustomCatalogAction.java index 615077eb3..7555452f8 100644 --- a/src/org/geometerplus/android/fbreader/network/action/EditCustomCatalogAction.java +++ b/src/org/geometerplus/android/fbreader/network/action/EditCustomCatalogAction.java @@ -26,6 +26,7 @@ import org.geometerplus.fbreader.network.NetworkTree; import org.geometerplus.fbreader.network.ICustomNetworkLink; import org.geometerplus.fbreader.network.tree.NetworkCatalogRootTree; +import org.geometerplus.android.fbreader.network.Util; import org.geometerplus.android.fbreader.network.AddCustomCatalogActivity; public class EditCustomCatalogAction extends CatalogAction { @@ -43,7 +44,8 @@ public class EditCustomCatalogAction extends CatalogAction { @Override protected void run(NetworkTree tree) { final Intent intent = new Intent(myActivity, AddCustomCatalogActivity.class); - AddCustomCatalogActivity.addLinkToIntent(intent, (ICustomNetworkLink)tree.getLink()); + Util.intentByLink(intent, tree.getLink()); + intent.putExtra(AddCustomCatalogActivity.EDIT_KEY, true); myActivity.startActivity(intent); } }