implement search in offline mode

This commit is contained in:
Andrew Dolgov 2012-01-19 15:33:39 +03:00
parent 2f62378980
commit dbf59c0b7d
4 changed files with 136 additions and 8 deletions

View File

@ -27,10 +27,16 @@
<item <item
android:id="@+id/go_online" android:id="@+id/go_online"
android:icon="@drawable/ic_menu_cloud" android:icon="@drawable/ic_menu_cloud"
android:showAsAction="ifRoom|withText"
android:title="@string/go_online" android:title="@string/go_online"
android:visible="false"/> android:visible="false"/>
<item
android:id="@+id/search"
android:actionViewClass="android.widget.SearchView"
android:icon="@android:drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:title="@string/search"/>
<item <item
android:id="@+id/headlines_mark_as_read" android:id="@+id/headlines_mark_as_read"
android:icon="@drawable/ic_menu_tick" android:icon="@drawable/ic_menu_tick"

View File

@ -9,6 +9,7 @@ import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
@ -21,7 +22,6 @@ import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.util.Log; import android.util.Log;
import android.view.ActionMode; import android.view.ActionMode;
import android.view.Display;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -30,7 +30,8 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.animation.AnimationUtils; import android.view.animation.AnimationUtils;
import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.LinearLayout; import android.widget.EditText;
import android.widget.SearchView;
import android.widget.TextView; import android.widget.TextView;
public class OfflineActivity extends FragmentActivity implements public class OfflineActivity extends FragmentActivity implements
@ -328,6 +329,14 @@ public class OfflineActivity extends FragmentActivity implements
findViewById(R.id.feeds_fragment).setVisibility(View.VISIBLE); findViewById(R.id.feeds_fragment).setVisibility(View.VISIBLE);
// } // }
m_activeFeedId = 0; m_activeFeedId = 0;
OfflineFeedsFragment ff = (OfflineFeedsFragment) getSupportFragmentManager()
.findFragmentById(R.id.feeds_fragment);
if (ff != null) {
ff.setSelectedFeedId(0);
}
refreshViews(); refreshViews();
initMainMenu(); initMainMenu();
@ -341,6 +350,13 @@ public class OfflineActivity extends FragmentActivity implements
findViewById(R.id.headlines_fragment).setVisibility(View.INVISIBLE); findViewById(R.id.headlines_fragment).setVisibility(View.INVISIBLE);
m_activeFeedId = 0; m_activeFeedId = 0;
OfflineFeedsFragment ff = (OfflineFeedsFragment) getSupportFragmentManager()
.findFragmentById(R.id.feeds_fragment);
if (ff != null) {
ff.setSelectedFeedId(0);
}
refreshViews(); refreshViews();
initMainMenu(); initMainMenu();
@ -431,12 +447,50 @@ public class OfflineActivity extends FragmentActivity implements
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
OfflineHeadlinesFragment ohf = (OfflineHeadlinesFragment) getSupportFragmentManager() final OfflineHeadlinesFragment ohf = (OfflineHeadlinesFragment) getSupportFragmentManager()
.findFragmentById(R.id.headlines_fragment); .findFragmentById(R.id.headlines_fragment);
switch (item.getItemId()) { switch (item.getItemId()) {
case android.R.id.home: case android.R.id.home:
goBack(false); goBack(false);
return true;
case R.id.search:
if (ohf != null && m_compatMode) {
Dialog dialog = new Dialog(this);
final EditText edit = new EditText(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle(R.string.search)
.setPositiveButton(getString(R.string.search),
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
String query = edit.getText().toString().trim();
ohf.setSearchQuery(query);
}
})
.setNegativeButton(getString(R.string.cancel),
new OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
//
}
}).setView(edit);
dialog = builder.create();
dialog.show();
}
return true; return true;
case R.id.preferences: case R.id.preferences:
Intent intent = new Intent(this, PreferencesActivity.class); Intent intent = new Intent(this, PreferencesActivity.class);
@ -671,8 +725,46 @@ public class OfflineActivity extends FragmentActivity implements
} }
} else if (m_selectedArticleId != 0) { } else if (m_selectedArticleId != 0) {
m_menu.setGroupVisible(R.id.menu_group_article, true); m_menu.setGroupVisible(R.id.menu_group_article, true);
} else if (m_activeFeedId != 0 /*|| m_activeCategory != null */) { } else if (m_activeFeedId != 0) {
m_menu.setGroupVisible(R.id.menu_group_headlines, true); m_menu.setGroupVisible(R.id.menu_group_headlines, true);
MenuItem search = m_menu.findItem(R.id.search);
if (!m_compatMode) {
SearchView searchView = (SearchView) search.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
private String query = "";
@Override
public boolean onQueryTextSubmit(String query) {
OfflineHeadlinesFragment frag = (OfflineHeadlinesFragment) getSupportFragmentManager()
.findFragmentById(R.id.headlines_fragment);
if (frag != null) {
frag.setSearchQuery(query);
this.query = query;
}
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText.equals("") && !newText.equals(this.query)) {
OfflineHeadlinesFragment frag = (OfflineHeadlinesFragment) getSupportFragmentManager()
.findFragmentById(R.id.headlines_fragment);
if (frag != null) {
frag.setSearchQuery(newText);
this.query = newText;
}
}
return false;
}
});
}
} else { } else {
m_menu.setGroupVisible(R.id.menu_group_feeds, true); m_menu.setGroupVisible(R.id.menu_group_feeds, true);
} }
@ -977,6 +1069,15 @@ public class OfflineActivity extends FragmentActivity implements
deselectAllArticles(); deselectAllArticles();
if (m_menu != null) {
MenuItem search = m_menu.findItem(R.id.search);
if (search != null && !m_compatMode) {
SearchView sv = (SearchView) search.getActionView();
sv.setQuery("", false);
}
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
OfflineHeadlinesFragment frag = new OfflineHeadlinesFragment(); OfflineHeadlinesFragment frag = new OfflineHeadlinesFragment();
ft.replace(R.id.headlines_fragment, frag); ft.replace(R.id.headlines_fragment, frag);

View File

@ -284,4 +284,9 @@ public class OfflineFeedsFragment extends Fragment implements OnItemClickListene
return 0; return 0;
} }
public void setSelectedFeedId(int feedId) {
m_selectedFeedId = feedId;
refresh();
}
} }

View File

@ -45,6 +45,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
private int m_feedId; private int m_feedId;
private int m_activeArticleId; private int m_activeArticleId;
private boolean m_combinedMode = true; private boolean m_combinedMode = true;
private String m_searchQuery = "";
private SharedPreferences m_prefs; private SharedPreferences m_prefs;
@ -120,6 +121,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
m_activeArticleId = savedInstanceState.getInt("activeArticleId"); m_activeArticleId = savedInstanceState.getInt("activeArticleId");
//m_selectedArticles = savedInstanceState.getParcelableArrayList("selectedArticles"); //m_selectedArticles = savedInstanceState.getParcelableArrayList("selectedArticles");
m_combinedMode = savedInstanceState.getBoolean("combinedMode"); m_combinedMode = savedInstanceState.getBoolean("combinedMode");
m_searchQuery = (String) savedInstanceState.getCharSequence("searchQuery");
} }
View view = inflater.inflate(R.layout.headlines_fragment, container, false); View view = inflater.inflate(R.layout.headlines_fragment, container, false);
@ -141,8 +143,14 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
} }
public Cursor createCursor() { public Cursor createCursor() {
return m_offlineServices.getReadableDb().query("articles", if (m_searchQuery.equals("")) {
null, "feed_id = ?", new String[] { String.valueOf(m_feedId) }, null, null, "updated DESC"); return m_offlineServices.getReadableDb().query("articles",
null, "feed_id = ?", new String[] { String.valueOf(m_feedId) }, null, null, "updated DESC");
} else {
return m_offlineServices.getReadableDb().query("articles",
null, "feed_id = ? AND (title LIKE '%' || ? || '%' OR content LIKE '%' || ? || '%')",
new String[] { String.valueOf(m_feedId), m_searchQuery, m_searchQuery }, null, null, "updated DESC");
}
} }
@Override @Override
@ -189,6 +197,7 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
out.putInt("activeArticleId", m_activeArticleId); out.putInt("activeArticleId", m_activeArticleId);
//out.putParcelableArrayList("selectedArticles", m_selectedArticles); //out.putParcelableArrayList("selectedArticles", m_selectedArticles);
out.putBoolean("combinedMode", m_combinedMode); out.putBoolean("combinedMode", m_combinedMode);
out.putCharSequence("searchQuery", m_searchQuery);
} }
public void setLoadingStatus(int status, boolean showProgress) { public void setLoadingStatus(int status, boolean showProgress) {
@ -440,4 +449,11 @@ public class OfflineHeadlinesFragment extends Fragment implements OnItemClickLis
return m_adapter.getCount(); return m_adapter.getCount();
} }
public void setSearchQuery(String query) {
if (!m_searchQuery.equals(query)) {
m_searchQuery = query;
refresh();
}
}
} }