From 84b8c41173db9a9f77e3f97a93164fdcbabd6fdb Mon Sep 17 00:00:00 2001 From: seven332 Date: Sat, 25 Apr 2015 19:19:22 +0800 Subject: [PATCH] Add hint to search bar --- .../java/com/hippo/ehviewer/Constants.java | 28 ++++++ .../com/hippo/ehviewer/client/EhClient.java | 20 ++++- .../com/hippo/ehviewer/widget/SearchBar.java | 55 +++++++++++- .../main/java/com/hippo/util/AppHandler.java | 35 ++++++++ .../java/com/hippo/util/IntIdGenerator.java | 2 +- .../main/java/com/hippo/util/Messenger.java | 85 +++++++++++++++++++ app/src/main/res/layout/widget_search_bar.xml | 2 +- app/src/main/res/values-zh-rCN/strings.xml | 3 + app/src/main/res/values/strings.xml | 3 + 9 files changed, 228 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/com/hippo/ehviewer/Constants.java create mode 100644 app/src/main/java/com/hippo/util/AppHandler.java create mode 100644 app/src/main/java/com/hippo/util/Messenger.java diff --git a/app/src/main/java/com/hippo/ehviewer/Constants.java b/app/src/main/java/com/hippo/ehviewer/Constants.java new file mode 100644 index 000000000..ef5ecd490 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/Constants.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2015 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 + * 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; + +import com.hippo.util.Messenger; + +public final class Constants { + + public static final int MESSENGER_ID_SOURCE; + + static { + Messenger messenger = Messenger.getInstance(); + MESSENGER_ID_SOURCE = messenger.newId(); + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java index bbcec3767..acda3e7b5 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java @@ -47,6 +47,10 @@ public final class EhClient { "http://forums.e-hentai.org/index.php?act=Login&CODE=01"; private static final String FORUMS_URL = "http://forums.e-hentai.org/index.php"; + public static final String HOST_G = "G.E-Hentai"; + public static final String HOST_EX = "ExHentai"; + public static final String HOST_LOFI = "Lofi.E-Hentai"; + public static final String HEADER_G = "http://g.e-hentai.org/"; public static final String HEADER_EX = "http://exhentai.org/"; public static final String HEADER_LOFI = "http://lofi.e-hentai.org/"; @@ -74,8 +78,20 @@ public final class EhClient { 1L, TimeUnit.SECONDS, requestWorkQueue, threadFactory); } - public static String getUrlHeader(int mode) { - switch (mode) { + public static String getReadableHost(int source) { + switch (source) { + default: + case SOURCE_G: + return HOST_G; + case SOURCE_EX: + return HOST_EX; + case SOURCE_LOFI: + return HOST_LOFI; + } + } + + public static String getUrlHeader(int source) { + switch (source) { default: case SOURCE_G: return HEADER_G; diff --git a/app/src/main/java/com/hippo/ehviewer/widget/SearchBar.java b/app/src/main/java/com/hippo/ehviewer/widget/SearchBar.java index 62475bd2c..7ce1b5611 100644 --- a/app/src/main/java/com/hippo/ehviewer/widget/SearchBar.java +++ b/app/src/main/java/com/hippo/ehviewer/widget/SearchBar.java @@ -18,15 +18,20 @@ package com.hippo.ehviewer.widget; import android.animation.Animator; import android.animation.ObjectAnimator; import android.content.Context; +import android.content.res.Resources; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Path; +import android.graphics.drawable.Drawable; import android.os.Build; import android.support.annotation.NonNull; import android.support.v7.widget.CardView; import android.text.Editable; +import android.text.Spannable; +import android.text.SpannableStringBuilder; import android.text.TextUtils; import android.text.TextWatcher; +import android.text.style.ImageSpan; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.LayoutInflater; @@ -40,8 +45,11 @@ import android.widget.TextView; import com.hippo.animation.SimpleAnimatorListener; import com.hippo.drawable.AddDeleteDrawable; import com.hippo.drawable.DrawerArrowDrawable; +import com.hippo.ehviewer.Constants; import com.hippo.ehviewer.R; +import com.hippo.ehviewer.client.EhClient; import com.hippo.util.MathUtils; +import com.hippo.util.Messenger; import com.hippo.util.UiUtils; import com.hippo.util.ViewUtils; import com.hippo.widget.SimpleImageView; @@ -52,7 +60,7 @@ import java.util.List; public class SearchBar extends CardView implements View.OnClickListener, TextView.OnEditorActionListener, TextWatcher, - SearchEditText.SearchEditTextListener { + SearchEditText.SearchEditTextListener, Messenger.Receiver { private static final long ANIMATION_TIME = 300; @@ -81,6 +89,8 @@ public class SearchBar extends CardView implements View.OnClickListener, private boolean mInAnimation = false; private boolean mFirstLayout = true; + private int mSource; + public SearchBar(Context context) { super(context); init(context); @@ -123,9 +133,25 @@ public class SearchBar extends CardView implements View.OnClickListener, mEditText.addTextChangedListener(this); mSuggestionList = new ArrayList<>(); + // TODO mSuggestionAdapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, mSuggestionList); mList.setAdapter(mSuggestionAdapter); + + // TODO get source from config + setSource(EhClient.SOURCE_EX); + } + + @Override + public void onAttachedToWindow() { + super.onAttachedToWindow(); + Messenger.getInstance().register(Constants.MESSENGER_ID_SOURCE, this); + } + + @Override + public void onDetachedFromWindow() { + super.onDetachedFromWindow(); + Messenger.getInstance().unregister(Constants.MESSENGER_ID_SOURCE, this); } private void updateSuggestions() { @@ -136,6 +162,23 @@ public class SearchBar extends CardView implements View.OnClickListener, mSuggestionAdapter.notifyDataSetChanged(); } + @SuppressWarnings({"deprecation", "ConstantConditions"}) + public void setSource(int source) { + if (mSource != source) { + Resources resources = getContext().getResources(); + Drawable searchImage = resources.getDrawable(R.drawable.ic_search); + SpannableStringBuilder ssb = new SpannableStringBuilder(" "); + ssb.append(String.format(resources.getString(R.string.search_bar_hint), + EhClient.getReadableHost(source))); + int textSize = (int) (mEditText.getTextSize() * 1.25); + searchImage.setBounds(0, 0, textSize, textSize); + ssb.setSpan(new ImageSpan(searchImage), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); + mEditText.setHint(ssb); + + mSource = source; + } + } + public void setHelper(Helper helper) { mHelper = helper; } @@ -357,6 +400,16 @@ public class SearchBar extends CardView implements View.OnClickListener, mHelper.onBackPressed(); } + @Override + public void onReceive(int id, Object obj) { + if (id == Constants.MESSENGER_ID_SOURCE) { + if (obj instanceof Integer) { + int source = (Integer) obj; + setSource(source); + } + } + } + public interface Helper { void onClickTitle(); void onClickMenu(); diff --git a/app/src/main/java/com/hippo/util/AppHandler.java b/app/src/main/java/com/hippo/util/AppHandler.java new file mode 100644 index 000000000..e894c6450 --- /dev/null +++ b/app/src/main/java/com/hippo/util/AppHandler.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2015 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 + * 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.util; + +import android.os.Handler; +import android.os.Looper; + +public class AppHandler extends Handler { + + private static Handler sInstance; + + public static Handler getInstance() { + if (sInstance == null) { + sInstance = new AppHandler(Looper.getMainLooper()); + } + return sInstance; + } + + public AppHandler(Looper mainLooper) { + super(mainLooper); + } +} diff --git a/app/src/main/java/com/hippo/util/IntIdGenerator.java b/app/src/main/java/com/hippo/util/IntIdGenerator.java index 7fc9c3507..bb9a44c03 100644 --- a/app/src/main/java/com/hippo/util/IntIdGenerator.java +++ b/app/src/main/java/com/hippo/util/IntIdGenerator.java @@ -22,7 +22,7 @@ public final class IntIdGenerator { // Empty } - public int nextId() { + public synchronized int nextId() { return mId++; } diff --git a/app/src/main/java/com/hippo/util/Messenger.java b/app/src/main/java/com/hippo/util/Messenger.java new file mode 100644 index 000000000..9e5d4f51b --- /dev/null +++ b/app/src/main/java/com/hippo/util/Messenger.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2015 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 + * 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.util; + +import android.util.SparseArray; + +import java.util.HashSet; +import java.util.Set; + +public class Messenger { + + private IntIdGenerator mIdGenerator; + + private SparseArray> mReceiverSetMap; + + private static Messenger sInstance; + + public static Messenger getInstance() { + if (sInstance == null) { + sInstance = new Messenger(); + } + return sInstance; + } + + private Messenger() { + mIdGenerator = IntIdGenerator.create(); + mReceiverSetMap = new SparseArray<>(); + } + + public int newId() { + return mIdGenerator.nextId(); + } + + public void notify(final int id, final Object obj) { + // Make sure do it in UI thread + AppHandler.getInstance().post(new Runnable() { + @Override + public void run() { + Set receiverSet = mReceiverSetMap.get(id); + if (receiverSet != null) { + for (Receiver receiver : receiverSet) { + receiver.onReceive(id, obj); + } + } + } + }); + } + + public void register(int id, Receiver receiver) { + Set receiverSet = mReceiverSetMap.get(id); + if (receiverSet == null) { + receiverSet = new HashSet<>(); + mReceiverSetMap.put(id, receiverSet); + } + + receiverSet.add(receiver); + } + + public void unregister(int id, Receiver receiver) { + Set receiverSet = mReceiverSetMap.get(id); + if (receiverSet != null) { + receiverSet.remove(receiver); + if (receiverSet.isEmpty()) { + mReceiverSetMap.remove(id); + } + } + } + + public interface Receiver { + void onReceive(final int id, final Object obj); + } +} diff --git a/app/src/main/res/layout/widget_search_bar.xml b/app/src/main/res/layout/widget_search_bar.xml index e7eea1be9..727aaa0be 100644 --- a/app/src/main/res/layout/widget_search_bar.xml +++ b/app/src/main/res/layout/widget_search_bar.xml @@ -65,7 +65,7 @@ android:singleLine="true" android:visibility="gone" android:imeOptions="actionSearch" - android:inputType="textAutoComplete"/> + android:inputType="text"/> diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index e4874bc40..612d702a0 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -15,6 +15,9 @@ --> + + 搜索 %s + 常规选项 高级选项 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fad22be47..fce917c85 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -33,6 +33,9 @@ MISC UNKNOWN + + Search %s + Normal Search Advance Options