From 23225bdb5652e4a2c7ed4c5215c111d2c688c41d Mon Sep 17 00:00:00 2001 From: seven332 Date: Tue, 23 Sep 2014 14:03:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=98=BE=E7=A4=BA=20suggesti?= =?UTF-8?q?on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- res/layout/search_normal.xml | 6 +- .../hippo/ehviewer/network/HttpHelper.java | 12 ++- .../ehviewer/ui/GalleryListActivity.java | 30 +++---- .../hippo/ehviewer/ui/SettingsActivity.java | 6 +- .../hippo/ehviewer/widget/PrefixEditText.java | 26 +++--- .../ehviewer/widget/SuggestionHelper.java | 89 +++++++++++++++++++ .../ehviewer/widget/SuggestionTextView.java | 85 ++++++++++++++++++ 7 files changed, 216 insertions(+), 38 deletions(-) create mode 100644 src/com/hippo/ehviewer/widget/SuggestionHelper.java create mode 100644 src/com/hippo/ehviewer/widget/SuggestionTextView.java diff --git a/res/layout/search_normal.xml b/res/layout/search_normal.xml index 195db6615..307fe0edc 100644 --- a/res/layout/search_normal.xml +++ b/res/layout/search_normal.xml @@ -38,7 +38,7 @@ android:layout_marginLeft="16dp" android:text="@string/checkbox_uploader" /> - - + android:singleLine="true" + android:dropDownVerticalOffset="38dp" /> reads = mData.getAllReads(); - for (GalleryInfo item : reads) { - File folder = new File(Config.getDownloadPath(), - StringEscapeUtils.escapeHtml4(item.title)); - try { - Util.deleteContents(folder); - folder.delete(); - } catch (IOException e) { - e.printStackTrace(); - } - }*/ mWindowsAnimate.free(); } diff --git a/src/com/hippo/ehviewer/ui/SettingsActivity.java b/src/com/hippo/ehviewer/ui/SettingsActivity.java index 954df82c5..78205d437 100644 --- a/src/com/hippo/ehviewer/ui/SettingsActivity.java +++ b/src/com/hippo/ehviewer/ui/SettingsActivity.java @@ -75,6 +75,7 @@ import com.hippo.ehviewer.widget.DialogBuilder; import com.hippo.ehviewer.widget.FileExplorerView; import com.hippo.ehviewer.widget.MaterialToast; import com.hippo.ehviewer.widget.ProgressDialogBulider; +import com.hippo.ehviewer.widget.SuggestionHelper; import com.hippo.ehviewer.widget.SuperDialogUtil; public class SettingsActivity extends AbsPreferenceActivity { @@ -410,8 +411,9 @@ public class SettingsActivity extends AbsPreferenceActivity { }).create().show(); } else if (KEY_CLEAR_SUGGESTIONS.equals(key)) { - SearchRecentSuggestions suggestions = new SearchRecentSuggestions(mActivity, - SimpleSuggestionProvider.AUTHORITY, SimpleSuggestionProvider.MODE); + SearchRecentSuggestions suggestions = SuggestionHelper.getInstance ( + mActivity, SimpleSuggestionProvider.AUTHORITY, + SimpleSuggestionProvider.MODE); suggestions.clearHistory(); } return true; diff --git a/src/com/hippo/ehviewer/widget/PrefixEditText.java b/src/com/hippo/ehviewer/widget/PrefixEditText.java index f75d1d170..5513cb8c2 100644 --- a/src/com/hippo/ehviewer/widget/PrefixEditText.java +++ b/src/com/hippo/ehviewer/widget/PrefixEditText.java @@ -21,14 +21,14 @@ import android.text.Editable; import android.text.Selection; import android.text.TextWatcher; import android.util.AttributeSet; -import android.widget.EditText; +import android.widget.AutoCompleteTextView; -public class PrefixEditText extends EditText +public class PrefixEditText extends AutoCompleteTextView implements TextWatcher { - + private Context mContext; private String mPrefix; - + public PrefixEditText(Context context) { super(context); init(context); @@ -41,24 +41,24 @@ public class PrefixEditText extends EditText super(context, attrs, defStyle); init(context); } - + private void init(Context context) { mContext = context; addTextChangedListener(this); } - + public void setPrefix(int resId) { setPrefix(mContext.getString(resId)); } - + public void setPrefix(String prefix) { mPrefix = prefix; setText(mPrefix); if (mPrefix != null) Selection.setSelection(getText(), mPrefix.length()); } - - @Override + + @Override protected void onSelectionChanged(int selStart, int selEnd) { super.onSelectionChanged(selStart, selEnd); if(mPrefix != null) { @@ -72,15 +72,15 @@ public class PrefixEditText extends EditText setSelection(newSelStart, newSelEnd); } } - + @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} - + @Override public void onTextChanged(CharSequence s, int start, int before, int count) {} - + @Override public void afterTextChanged(Editable s) { if(mPrefix != null && @@ -89,5 +89,5 @@ public class PrefixEditText extends EditText setSelection(mPrefix.length()); } } - + } diff --git a/src/com/hippo/ehviewer/widget/SuggestionHelper.java b/src/com/hippo/ehviewer/widget/SuggestionHelper.java new file mode 100644 index 000000000..e15691eda --- /dev/null +++ b/src/com/hippo/ehviewer/widget/SuggestionHelper.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2014 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hippo.ehviewer.widget; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import android.content.ContentResolver; +import android.content.Context; +import android.database.Cursor; +import android.net.Uri; +import android.provider.SearchRecentSuggestions; + +public class SuggestionHelper extends SearchRecentSuggestions { + + private static final String QUERY = "query"; + private static final String ORDER_BY = "date DESC"; + + private static final int MAX_HISTORY_COUNT = 250; + + private final List mQueryList = new ArrayList(); + + private static Map sInstances = new HashMap(); + + public static SuggestionHelper getInstance(Context context, String authority, int mode) { + SuggestionHelper s = sInstances.get(authority); + if (s == null) { + s = new SuggestionHelper(context, authority, mode); + sInstances.put(authority, s); + } + return s; + } + + private SuggestionHelper(Context context, String authority, int mode) { + super(context, authority, mode); + + Uri suggestionsUri = Uri.parse("content://" + authority + "/suggestions"); + + // Get all suggestion + ContentResolver cr = context.getContentResolver(); + Cursor cursor = cr.query(suggestionsUri, new String[]{QUERY}, null, null, ORDER_BY); + while(cursor.moveToNext()) + mQueryList.add(cursor.getString(cursor.getColumnIndex(QUERY))); + cursor.close(); + } + + public List getQueries() { + return mQueryList; + } + + @Override + public void saveRecentQuery(final String queryString, final String line2) { + super.saveRecentQuery(queryString, line2); + mQueryList.add(0, queryString); + truncateHistory(MAX_HISTORY_COUNT); + } + + @Override + public void clearHistory() { + super.clearHistory(); + truncateHistory(0); + } + + private void truncateHistory(int maxEntries) { + if (maxEntries == 0) { + mQueryList.clear(); + } else { + for (int i = mQueryList.size() - 1; i >= maxEntries; i--) { + mQueryList.remove(i); + } + } + } +} diff --git a/src/com/hippo/ehviewer/widget/SuggestionTextView.java b/src/com/hippo/ehviewer/widget/SuggestionTextView.java new file mode 100644 index 000000000..c9f6b6bd3 --- /dev/null +++ b/src/com/hippo/ehviewer/widget/SuggestionTextView.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2014 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hippo.ehviewer.widget; + +import java.util.List; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.KeyEvent; +import android.widget.ArrayAdapter; +import android.widget.AutoCompleteTextView; + +public class SuggestionTextView extends PrefixEditText { + + private static final String TAG = SuggestionTextView.class.getSimpleName(); + + private List mAllSuggestions; + private ArrayAdapter mAdapte; + + public SuggestionTextView(Context context) { + super(context); + } + + public SuggestionTextView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public SuggestionTextView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + /** + * We override this method to avoid replacing the query box text when a + * suggestion is clicked. + */ + @Override + protected void replaceText(CharSequence text) { + } + + /** + * We override this method to avoid an extra onItemClick being called on + * the drop-down's OnItemClickListener by + * {@link AutoCompleteTextView#onKeyUp(int, KeyEvent)} when an item is + * clicked with the trackball. + */ + @Override + public void performCompletion() { + } + + /** + * We override this method so that we can allow a threshold of zero, + * which ACTV does not. + */ + @Override + public boolean enoughToFilter() { + return true; + } + + public void setSuggestionHelper(SuggestionHelper helper) { + if (helper != null) { + mAllSuggestions = helper.getQueries(); + mAdapte = new ArrayAdapter(getContext(), + android.R.layout.simple_list_item_1, mAllSuggestions); + setAdapter(mAdapte); + } else { + mAllSuggestions = null; + mAdapte = null; + setAdapter(null); + } + } +}