You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
293 lines
9.9 KiB
293 lines
9.9 KiB
11 years ago
|
/*
|
||
|
* Copyright 2010-2013 Eric Kok et al.
|
||
|
*
|
||
|
* Transdroid 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.
|
||
|
*
|
||
|
* Transdroid 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 Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
11 years ago
|
package org.transdroid.core.gui.search;
|
||
|
|
||
|
import java.util.List;
|
||
|
|
||
|
import org.androidannotations.annotations.AfterViews;
|
||
|
import org.androidannotations.annotations.Bean;
|
||
|
import org.androidannotations.annotations.EActivity;
|
||
|
import org.androidannotations.annotations.FragmentById;
|
||
|
import org.androidannotations.annotations.OptionsItem;
|
||
|
import org.androidannotations.annotations.OptionsMenu;
|
||
|
import org.androidannotations.annotations.SystemService;
|
||
|
import org.androidannotations.annotations.ViewById;
|
||
10 years ago
|
import org.transdroid.R;
|
||
11 years ago
|
import org.transdroid.core.app.search.SearchHelper;
|
||
|
import org.transdroid.core.app.search.SearchSite;
|
||
10 years ago
|
import org.transdroid.core.app.settings.ApplicationSettings;
|
||
|
import org.transdroid.core.app.settings.SystemSettings_;
|
||
|
import org.transdroid.core.app.settings.WebsearchSetting;
|
||
|
import org.transdroid.core.gui.TorrentsActivity_;
|
||
11 years ago
|
import org.transdroid.core.gui.navigation.NavigationHelper;
|
||
|
|
||
|
import android.annotation.TargetApi;
|
||
10 years ago
|
import android.app.ActionBar;
|
||
|
import android.app.ActionBar.OnNavigationListener;
|
||
|
import android.app.Activity;
|
||
11 years ago
|
import android.app.SearchManager;
|
||
|
import android.content.Intent;
|
||
|
import android.net.Uri;
|
||
|
import android.os.Build;
|
||
|
import android.os.Bundle;
|
||
|
import android.provider.SearchRecentSuggestions;
|
||
10 years ago
|
import android.view.Menu;
|
||
|
import android.view.MenuItem;
|
||
11 years ago
|
import android.view.View;
|
||
|
import android.widget.AdapterView;
|
||
|
import android.widget.AdapterView.OnItemClickListener;
|
||
10 years ago
|
import android.widget.ListView;
|
||
|
import android.widget.SearchView;
|
||
11 years ago
|
import android.widget.TextView;
|
||
|
|
||
|
/**
|
||
|
* An activity that shows search results to the user (after a query was supplied by the standard Android search manager)
|
||
|
* and either shows the list of search sites on the left (e.g. on tablets) or allows switching between search sites via
|
||
|
* the action bar spinner.
|
||
|
* @author Eric Kok
|
||
|
*/
|
||
|
@EActivity(resName = "activity_search")
|
||
|
@OptionsMenu(resName = "activity_search")
|
||
10 years ago
|
public class SearchActivity extends Activity implements OnNavigationListener {
|
||
11 years ago
|
|
||
10 years ago
|
@FragmentById(resName = "searchresults_fragment")
|
||
11 years ago
|
protected SearchResultsFragment fragmentResults;
|
||
|
@ViewById
|
||
10 years ago
|
protected ListView searchsitesList;
|
||
11 years ago
|
@ViewById
|
||
|
protected TextView installmoduleText;
|
||
|
@Bean
|
||
|
protected ApplicationSettings applicationSettings;
|
||
|
@Bean
|
||
|
protected NavigationHelper navigationHelper;
|
||
|
@Bean
|
||
|
protected SearchHelper searchHelper;
|
||
|
@SystemService
|
||
|
protected SearchManager searchManager;
|
||
11 years ago
|
private MenuItem searchMenu = null;
|
||
11 years ago
|
private SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this, SearchHistoryProvider.AUTHORITY,
|
||
|
SearchHistoryProvider.MODE);
|
||
11 years ago
|
|
||
|
private List<SearchSetting> searchSites;
|
||
|
private SearchSetting lastUsedSite;
|
||
|
private String lastUsedQuery;
|
||
|
|
||
|
@Override
|
||
|
public void onCreate(Bundle savedInstanceState) {
|
||
|
// Set the theme according to the user preference
|
||
|
if (SystemSettings_.getInstance_(this).useDarkTheme()) {
|
||
|
setTheme(R.style.TransdroidTheme_Dark);
|
||
10 years ago
|
getActionBar().setIcon(R.drawable.ic_activity_torrents);
|
||
11 years ago
|
}
|
||
|
super.onCreate(savedInstanceState);
|
||
|
}
|
||
|
|
||
|
@AfterViews
|
||
|
protected void init() {
|
||
|
|
||
|
// Get the user query, as coming from the standard SearchManager
|
||
|
handleIntent(getIntent());
|
||
|
|
||
|
if (!searchHelper.isTorrentSearchInstalled()) {
|
||
|
// The module install text will be shown instead (in onPrepareOptionsMenu)
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Load sites and find the last used (or set as default) search site
|
||
|
searchSites = applicationSettings.getSearchSettings();
|
||
|
lastUsedSite = applicationSettings.getLastUsedSearchSite();
|
||
|
int lastUsedPosition = -1;
|
||
|
if (lastUsedSite != null) {
|
||
|
for (int i = 0; i < searchSites.size(); i++) {
|
||
11 years ago
|
if (searchSites.get(i).getKey().equals(lastUsedSite.getKey())) {
|
||
11 years ago
|
lastUsedPosition = i;
|
||
11 years ago
|
break;
|
||
|
}
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Allow site selection via list (on large screens) or action bar spinner
|
||
10 years ago
|
getActionBar().setDisplayHomeAsUpEnabled(true);
|
||
11 years ago
|
if (searchsitesList != null) {
|
||
|
// The current layout has a dedicated list view to select the search site
|
||
|
SearchSitesAdapter searchSitesAdapter = SearchSitesAdapter_.getInstance_(this);
|
||
|
searchSitesAdapter.update(searchSites);
|
||
|
searchsitesList.setAdapter(searchSitesAdapter);
|
||
|
searchsitesList.setOnItemClickListener(onSearchSiteClicked);
|
||
|
// Select the last used site; this also starts the search!
|
||
|
if (lastUsedPosition >= 0)
|
||
|
searchsitesList.setItemChecked(lastUsedPosition, true);
|
||
|
} else {
|
||
|
// Use the action bar spinner to select sites
|
||
10 years ago
|
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
|
||
|
getActionBar().setDisplayShowTitleEnabled(false);
|
||
|
getActionBar().setListNavigationCallbacks(new SearchSettingsDropDownAdapter(this, searchSites), this);
|
||
11 years ago
|
// Select the last used site; this also starts the search!
|
||
|
if (lastUsedPosition >= 0)
|
||
10 years ago
|
getActionBar().setSelectedNavigationItem(lastUsedPosition);
|
||
11 years ago
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||
|
super.onCreateOptionsMenu(menu);
|
||
|
if (navigationHelper.enableSearchUi()) {
|
||
10 years ago
|
// Add an expandable SearchView to the action bar
|
||
11 years ago
|
MenuItem item = menu.findItem(R.id.action_search);
|
||
10 years ago
|
final SearchView searchView = new SearchView(this);
|
||
|
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
|
||
|
searchView.setQueryRefinementEnabled(true);
|
||
|
item.setActionView(searchView);
|
||
|
searchMenu = item;
|
||
11 years ago
|
}
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
||
|
super.onPrepareOptionsMenu(menu);
|
||
|
|
||
|
boolean searchInstalled = searchHelper.isTorrentSearchInstalled();
|
||
|
menu.findItem(R.id.action_search).setVisible(searchInstalled);
|
||
|
menu.findItem(R.id.action_refresh).setVisible(searchInstalled);
|
||
|
menu.findItem(R.id.action_downloadsearch).setVisible(!searchInstalled);
|
||
|
if (searchsitesList != null)
|
||
|
searchsitesList.setVisibility(searchInstalled ? View.VISIBLE : View.GONE);
|
||
|
if (searchInstalled)
|
||
10 years ago
|
getFragmentManager().beginTransaction().show(fragmentResults).commit();
|
||
11 years ago
|
else
|
||
10 years ago
|
getFragmentManager().beginTransaction().hide(fragmentResults).commit();
|
||
11 years ago
|
installmoduleText.setVisibility(searchInstalled ? View.GONE : View.VISIBLE);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
protected void onNewIntent(Intent intent) {
|
||
|
handleIntent(intent);
|
||
|
refreshSearch();
|
||
|
}
|
||
|
|
||
|
private void handleIntent(Intent intent) {
|
||
|
|
||
10 years ago
|
lastUsedQuery = parseQuery(intent);
|
||
|
|
||
11 years ago
|
// Is this actually a full HTTP URL? Then redirect this request to add the URL directly
|
||
|
if (lastUsedQuery != null
|
||
|
&& (lastUsedQuery.startsWith("http") || lastUsedQuery.startsWith("https")
|
||
|
|| lastUsedQuery.startsWith("magnet") || lastUsedQuery.startsWith("file"))) {
|
||
10 years ago
|
// Don't broadcast this intent; we can safely assume this is intended for Transdroid only
|
||
|
Intent i = TorrentsActivity_.intent(this).get();
|
||
|
i.setData(Uri.parse(lastUsedQuery));
|
||
|
startActivity(i);
|
||
11 years ago
|
finish();
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
11 years ago
|
@Override
|
||
|
public boolean onSearchRequested() {
|
||
|
if (searchMenu != null) {
|
||
|
searchMenu.expandActionView();
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||
|
@OptionsItem(android.R.id.home)
|
||
|
protected void navigateUp() {
|
||
|
TorrentsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
|
||
|
}
|
||
|
|
||
|
private OnItemClickListener onSearchSiteClicked = new OnItemClickListener() {
|
||
|
|
||
|
@Override
|
||
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||
|
lastUsedSite = searchSites.get(position);
|
||
|
refreshSearch();
|
||
|
}
|
||
|
};
|
||
|
|
||
|
@Override
|
||
|
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
|
||
|
lastUsedSite = searchSites.get(itemPosition);
|
||
|
refreshSearch();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Extracts the query string from the search {@link Intent}
|
||
|
* @return The query string that was entered by the user
|
||
|
*/
|
||
11 years ago
|
private String parseQuery(Intent intent) {
|
||
11 years ago
|
|
||
|
String query = null;
|
||
|
if (intent.getAction().equals(Intent.ACTION_SEARCH)) {
|
||
|
query = intent.getStringExtra(SearchManager.QUERY);
|
||
|
} else if (intent.getAction().equals(Intent.ACTION_SEND)) {
|
||
|
query = SendIntentHelper.cleanUpText(intent);
|
||
|
}
|
||
10 years ago
|
query = query.trim();
|
||
11 years ago
|
if (query != null && query.length() > 0) {
|
||
|
|
||
|
// Remember this search query to later show as a suggestion
|
||
|
suggestions.saveRecentQuery(query, null);
|
||
|
return query;
|
||
|
|
||
|
}
|
||
|
return null;
|
||
|
|
||
|
}
|
||
|
|
||
|
@OptionsItem(resName = "action_refresh")
|
||
|
protected void refreshSearch() {
|
||
11 years ago
|
|
||
|
if (searchMenu != null) {
|
||
|
// Close the search view in the ation bar
|
||
|
searchMenu.collapseActionView();
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
if (lastUsedSite instanceof WebsearchSetting) {
|
||
11 years ago
|
|
||
11 years ago
|
// Start a browser page directly to the requested search results
|
||
|
WebsearchSetting websearch = (WebsearchSetting) lastUsedSite;
|
||
10 years ago
|
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(websearch.getBaseUrl().replace("%s", lastUsedQuery))));
|
||
11 years ago
|
finish();
|
||
11 years ago
|
|
||
11 years ago
|
} else if (lastUsedSite instanceof SearchSite) {
|
||
11 years ago
|
|
||
11 years ago
|
// Save the search site currently used to search for future usage
|
||
|
applicationSettings.setLastUsedSearchSite(lastUsedSite);
|
||
11 years ago
|
// Update the activity title (only shown on large devices)
|
||
10 years ago
|
getActionBar().setTitle(
|
||
11 years ago
|
NavigationHelper.buildCondensedFontString(getString(R.string.search_queryonsite, lastUsedQuery,
|
||
|
lastUsedSite.getName())));
|
||
11 years ago
|
// Ask the results fragment to start a search for the specified query
|
||
11 years ago
|
fragmentResults.startSearch(lastUsedQuery, (SearchSite) lastUsedSite);
|
||
11 years ago
|
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
@OptionsItem(resName = "action_downloadsearch")
|
||
|
protected void downloadSearchModule() {
|
||
|
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.transdroid.org/latest-search")));
|
||
|
}
|
||
|
|
||
|
}
|