package com.newsblur.activity;

import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.newsblur.R;
import com.newsblur.domain.FeedResult;
import com.newsblur.fragment.AddFeedFragment;
import com.newsblur.network.SearchAsyncTaskLoader;
import com.newsblur.network.SearchLoaderResponse;

public class SearchForFeeds extends NbFragmentActivity implements LoaderCallbacks<SearchLoaderResponse>, OnItemClickListener {
	private ListView resultsList;
	private Loader<SearchLoaderResponse> searchLoader;
	private FeedSearchResultAdapter adapter;

	@Override
	protected void onCreate(Bundle arg0) {
		requestWindowFeature(Window.FEATURE_PROGRESS);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		super.onCreate(arg0);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		
		setTitle(R.string.title_feed_search);
		setContentView(R.layout.activity_feed_search);
		
		TextView emptyView = (TextView) findViewById(R.id.empty_view);
		resultsList = (ListView) findViewById(R.id.feed_result_list);
		resultsList.setEmptyView(emptyView);
		resultsList.setOnItemClickListener(this);
		resultsList.setItemsCanFocus(false);
		searchLoader = getSupportLoaderManager().initLoader(0, new Bundle(), this);
		
		onSearchRequested();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.search, menu);
		return true;
	}

	@Override
	protected void onNewIntent(Intent intent) {
		setIntent(intent);
		handleIntent(intent);
	}

	private void handleIntent(Intent intent) {
		if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
			String query = intent.getStringExtra(SearchManager.QUERY);
			setSupportProgressBarIndeterminateVisibility(true);
			
			Bundle bundle = new Bundle();
			bundle.putString(SearchAsyncTaskLoader.SEARCH_TERM, query);
			searchLoader = getSupportLoaderManager().restartLoader(0, bundle, this);
			
			searchLoader.forceLoad();
		}
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == R.id.menu_search) {
			onSearchRequested();
			return true;
		} else if (item.getItemId() == android.R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public Loader<SearchLoaderResponse> onCreateLoader(int loaderId, Bundle bundle) {
		String searchTerm = bundle.getString(SearchAsyncTaskLoader.SEARCH_TERM);
		return new SearchAsyncTaskLoader(this, searchTerm);
	}

	@Override
	public void onLoadFinished(Loader<SearchLoaderResponse> loader, SearchLoaderResponse results) {
		setSupportProgressBarIndeterminateVisibility(false);
		if(!results.hasError()) {
			adapter = new FeedSearchResultAdapter(this, 0, 0, results.getResults());
			resultsList.setAdapter(adapter);
		} else {
			String message = results.getErrorMessage() == null ? "Error" : results.getErrorMessage();
			Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onLoaderReset(Loader<SearchLoaderResponse> loader) {
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
		FeedResult result = adapter.getItem(position);
		DialogFragment addFeedFragment = AddFeedFragment.newInstance(result.url, result.label);
		addFeedFragment.show(getSupportFragmentManager(), "dialog");
	}

}

@@ -25,16 +25,77 @@
 ivity;%0A%0A
+import java.net.MalformedURLException;%0Aimport java.net.URL;%0A%0A
 import a
@@ -116,24 +116,24 @@
 rchManager;%0A
-
 import andro
@@ -1047,16 +1047,81 @@
 tener %7B%0A
+    %0A    private static String SUPPORTED_URL_PROTOCOL = %22http%22;%0A%0A
 %09private
@@ -2411,16 +2411,144 @@
 QUERY);%0A
+%0A            // test to see if a feed URL was passed rather than a search term%0A            if (tryAddByURL(query)) %7B return; %7D%0A%0A
 %09%09%09setSu
@@ -2811,16 +2811,810 @@
 %09%09%7D%0A%09%7D%0A%0A
+    /**%0A     * See if the text entered in the query field was actually a URL so we can skip the%0A     * search step and just let users who know feed URLs directly subscribe.%0A     */%0A    private boolean tryAddByURL(String s) %7B%0A        URL u = null;%0A        try %7B%0A            u = new URL(s);%0A        %7D catch (MalformedURLException mue) %7B%0A            ; // this just signals that the string wasn't a URL, we will return%0A        %7D%0A        if (u == null) %7B return false; %7D%0A        if (!u.getProtocol().equals(SUPPORTED_URL_PROTOCOL)) %7B return false; %7D;%0A        if ((u.getHost() == null) %7C%7C (u.getHost().trim().isEmpty())) %7B return false; %7D%0A%0A%09%09DialogFragment addFeedFragment = AddFeedFragment.newInstance(s, s);%0A%09%09addFeedFragment.show(getSupportFragmentManager(), %22dialog%22);%0A        return true;%0A    %7D%0A
 %0A%09@Overr

Support direct subscription to feeds via URL. (#448)