package com.filemanager.search;

import java.io.File;

import com.intents.FileManagerIntents;

import android.app.IntentService;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

/**
 * Service that asynchronously executes file searches.
 * 
 * @author George Venios.
 * 
 */
public class SearchService extends IntentService {
	/**
	 * Used to inform the SearchableActivity of search start and end.
	 */
	private LocalBroadcastManager lbm;
	private SearchCore searcher;

	public SearchService() {
		super("SearchService");
	}

	@Override
	public void onCreate() {
		super.onCreate();

		lbm = LocalBroadcastManager.getInstance(getApplicationContext());
		
		searcher = new SearchCore(this);
		searcher.setURI(SearchResultsProvider.CONTENT_URI);
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		// The search query
		searcher.setQuery(intent.getStringExtra(FileManagerIntents.EXTRA_SEARCH_QUERY));

		// Set initial path. To be searched first!
		String path = intent
				.getStringExtra(FileManagerIntents.EXTRA_SEARCH_INIT_PATH);
		File root = null;
		if (path != null)
			root = new File(path);
		else
			root = new File("/");

		// Search started, let Receivers know.
		lbm.sendBroadcast(new Intent(FileManagerIntents.ACTION_SEARCH_STARTED));

		// Search in current path.
		searcher.dropPreviousResults();
		searcher.setRoot(root);
		searcher.search(root);

		// Search is over, let Receivers know.
		lbm.sendBroadcast(new Intent(FileManagerIntents.ACTION_SEARCH_FINISHED));
	}
}
@@ -685,18 +685,16 @@
 ext());%0A
-%09%09
 %0A%09%09searc
@@ -838,16 +838,26 @@
 tent) %7B%0A
+%09%09try %7B%0A%0A%09
 %09%09// The
@@ -870,16 +870,17 @@
 h query%0A
+%09
 %09%09search
@@ -897,16 +897,22 @@
 y(intent
+%0A%09%09%09%09%09
 .getStri
@@ -959,24 +959,25 @@
 QUERY));%0A%0A%09%09
+%09
 // Set initi
@@ -1007,16 +1007,17 @@
  first!%0A
+%09
 %09%09String
@@ -1031,24 +1031,25 @@
  intent%0A%09%09%09%09
+%09
 .getStringEx
@@ -1098,16 +1098,17 @@
 ATH);%0A%09%09
+%09
 File roo
@@ -1117,16 +1117,17 @@
 = null;%0A
+%09
 %09%09if (pa
@@ -1137,24 +1137,25 @@
 != null)%0A%09%09%09
+%09
 root = new F
@@ -1171,13 +1171,15 @@
 ;%0A%09%09
+%09
 else%0A
+%09
 %09%09%09r
@@ -1198,24 +1198,25 @@
 le(%22/%22);%0A%0A%09%09
+%09
 // Search st
@@ -1236,32 +1236,33 @@
 ceivers know.%0A%09%09
+%09
 lbm.sendBroadcas
@@ -1266,32 +1266,38 @@
 cast(new Intent(
+%0A%09%09%09%09%09
 FileManagerInten
@@ -1323,24 +1323,25 @@
 ARTED));%0A%0A%09%09
+%09
 // Search in
@@ -1355,16 +1355,17 @@
 t path.%0A
+%09
 %09%09search
@@ -1388,24 +1388,25 @@
 esults();%0A%09%09
+%09
 searcher.set
@@ -1417,16 +1417,17 @@
 (root);%0A
+%09
 %09%09search
@@ -1444,16 +1444,17 @@
 root);%0A%0A
+%09
 %09%09// Sea
@@ -1488,16 +1488,17 @@
 know.%0A%09%09
+%09
 lbm.send
@@ -1518,16 +1518,22 @@
  Intent(
+%0A%09%09%09%09%09
 FileMana
@@ -1568,13 +1568,68 @@
 ISHED));
+%0A%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D
 %0A%09%7D%0A%7D

handle java.lang.NullPointerException