Skip to content
This repository has been archived by the owner on Jan 26, 2020. It is now read-only.

Commit

Permalink
Fix crash upload unsupported image to Baidu
Browse files Browse the repository at this point in the history
Add search engine ascii2d.net
Add notice for Baidu
  • Loading branch information
RikkaW committed Jan 21, 2016
1 parent 60a8bf1 commit 668d6db
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 13200
versionCode 13300
}
buildTypes {
release {
Expand All @@ -17,7 +17,7 @@ android {
}

productFlavors {
def String name = "1.3.2"
def String name = "1.3.3"

COOLAPK {
applicationId "rikka.searchbyimage"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Created by Rikka on 2015/12/31.
*/
public class SearchByImageApplication extends Application {
// TODO: change this to save to file
private InputStream imageInputStream;

public InputStream getImageInputStream() {
Expand Down
41 changes: 38 additions & 3 deletions app/src/main/java/rikka/searchbyimage/ui/UploadActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.JsonReader;
import android.widget.PopupWindow;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -43,6 +44,7 @@ public class UploadActivity extends AppCompatActivity {
public final static int SITE_IQDB = 2;
public final static int SITE_TINEYE = 3;
public final static int SITE_SAUCENAO = 4;
public final static int SITE_ASCII2D = 5;

private class Error {
public String title;
Expand Down Expand Up @@ -116,6 +118,10 @@ protected HttpUpload doInBackground(Uri... imageUrl) {
uploadUri = "http://saucenao.com/search.php";
name = "file";
break;
case SITE_ASCII2D:
uploadUri = "http://www.ascii2d.net/search/file";
name = "file";
break;
}

HttpRequestUtils httpRequest = new HttpRequestUtils(uploadUri, "POST");
Expand Down Expand Up @@ -220,20 +226,25 @@ public void onCancel(DialogInterface dialog) {

switch (result.siteId) {
case SITE_BAIDU:
int errno = 0;
int err_no = 0;
String contsign = "";
String obj_url = "";
String simid = "";
String error_msg = "";

JsonReader reader = null;
try {
reader = new JsonReader(new InputStreamReader(new FileInputStream(new File(result.html))));
reader.beginObject();

while (reader.hasNext()) {
String keyName = reader.nextName();
switch (keyName) {
case "errno":
errno = reader.nextInt();
err_no = reader.nextInt();
break;
case "msg":
error_msg = reader.nextString();
break;
case "json_data":
reader.beginObject();
Expand All @@ -253,7 +264,7 @@ public void onCancel(DialogInterface dialog) {
}
}
reader.endObject();
} catch (IOException e) {
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
} finally {
try {
Expand All @@ -265,6 +276,29 @@ public void onCancel(DialogInterface dialog) {
}
}

if (err_no != 0) {
new AlertDialog.Builder(mActivity)
.setTitle("baidu.com")
.setMessage(error_msg)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
finish();
}
})
.show();

mProgressDialog.dismiss();

return;
}

StringBuilder sb = new StringBuilder();
sb.append("http://image.baidu.com/n/mo_search?guess=1&rn=30&appid=0&tag=1&isMobile=0");
sb.append("&queryImageUrl=");
Expand All @@ -276,6 +310,7 @@ public void onCancel(DialogInterface dialog) {
result.url = sb.toString();
case SITE_GOOGLE:
case SITE_TINEYE:
case SITE_ASCII2D:
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(mActivity);
switch (sharedPref.getString("show_result_in", URLUtils.SHOW_IN_WEBVIEW)) {
case URLUtils.SHOW_IN_WEBVIEW:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public class WebViewActivity extends AppCompatActivity {
"", // baidu
"", // iqdb
"", // tineye
"http://saucenao.com/" //saucenao
"http://saucenao.com/", //saucenao
""
};

private WebView mWebView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ public class SettingsFragment extends PreferenceFragment implements
EditTextPreference mCustomGoogleUri;
PreferenceCategory mCategorySauceNAO;

PreferenceCategory mCategoryNotice;
Preference mNotice;

private int click = 0;
private Runnable clearClickCount = new Runnable() {
@Override
Expand Down Expand Up @@ -83,10 +86,12 @@ public void onCreatePreferences(Bundle bundle, String s) {
mCategoryGoogle = (PreferenceCategory) findPreference("category_google");
mCategoryIqdb = (PreferenceCategory) findPreference("category_iqdb");
mCategorySauceNAO = (PreferenceCategory) findPreference("category_saucenao");
mCategoryNotice = (PreferenceCategory) findPreference("category_notice");

mSafeSearch = (SwitchPreference) findPreference("safe_search_preference");
mScreen = (PreferenceScreen) findPreference("screen");
mCustomGoogleUri = (EditTextPreference) findPreference("google_region");
mNotice = (Preference) findPreference("preference_notice");

setCustomGoogleUriHide();
setSearchEngineHide();
Expand Down Expand Up @@ -185,6 +190,7 @@ private void setSearchEngineHide() {
mScreen.removePreference(mCategoryIqdb);
mScreen.addPreference(mCategorySauceNAO);
break;
case UploadActivity.SITE_ASCII2D:
case UploadActivity.SITE_BAIDU:
case UploadActivity.SITE_TINEYE:
mScreen.removePreference(mCategoryGoogle);
Expand All @@ -193,6 +199,20 @@ private void setSearchEngineHide() {

break;
}

switch (siteId) {
case UploadActivity.SITE_BAIDU:
mScreen.addPreference(mCategoryNotice);
mNotice.setSummary(R.string.notice_baidu);
break;
case UploadActivity.SITE_GOOGLE:
case UploadActivity.SITE_IQDB:
case UploadActivity.SITE_SAUCENAO:
case UploadActivity.SITE_ASCII2D:
case UploadActivity.SITE_TINEYE:
mScreen.removePreference(mCategoryNotice);
break;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ public Request initRequest(RequestBody requestBody) throws IOException {
.addHeader("accept-encoding", "deflate")
.addHeader("cache-control", "no-cache")
.addHeader("connection", "Keep-Alive")
.addHeader("transfer-rncoding", "chunked")
.addHeader("user-agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36")
.post(requestBody)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<item>iqdb</item>
<item>TinEye</item>
<item>SauceNAO</item>
<item>ascii2d</item>
</string-array>

<string name="usage">使い方</string>
Expand Down Expand Up @@ -56,6 +57,7 @@
<string name="settings">設定</string>
<string name="setting_each_time">アップロード前に設定を開く</string>
<string name="resize_image">アップロード画像を圧縮</string>
<string name="try_resize_image_if_error">Try this if upload error</string>
<string name="start_upload">アップロード</string>

<string name="permission_require">アクセスが必要</string>
Expand All @@ -82,6 +84,9 @@
<string name="timeout_exception">接続タイムアウト</string>
<string name="unknown_host_exception">ドメイン解決エラー</string>

<string name="notice">Notice</string>
<string name="notice_baidu">WEBP image or large size image is not supported.</string>

<string name="show_result_in">検索結果を表示</string>
<string name="build_in_browser">アプリ内ブラウザ</string>
<string name="chrome_custom_tabs">Chrome custom tabs</string>
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<item>iqdb</item>
<item>TinEye</item>
<item>SauceNAO</item>

<item>ascii2d</item>
</string-array>

<string name="usage">使用方法</string>
Expand Down Expand Up @@ -56,6 +56,7 @@
<string name="settings">设置</string>
<string name="setting_each_time">上传前打开设置</string>
<string name="resize_image">压缩上传图片</string>
<string name="try_resize_image_if_error">当图片不能正常上传时可尝试此选项</string>
<string name="start_upload">开始上传</string>

<string name="permission_require">需要权限</string>
Expand All @@ -82,6 +83,9 @@
<string name="unknown_host_exception">域名解析错误</string>
<string name="socket_exception">网络错误</string>

<string name="notice">注意</string>
<string name="notice_baidu">不支持WebP格式和较大图片</string>

<string name="show_result_in">结果打开方式</string>
<string name="build_in_browser">内置浏览器</string>
<string name="chrome_custom_tabs">Chrome custom tabs</string>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<item>iqdb</item>
<item>TinEye</item>
<item>SauceNAO</item>
<item>ascii2d</item>
</string-array>

<string name="usage">使用方法</string>
Expand Down Expand Up @@ -54,6 +55,7 @@
<string name="settings">設定</string>
<string name="setting_each_time">上載前打開設置</string>
<string name="resize_image">压缩上載圖片</string>
<string name="try_resize_image_if_error">当图片不能正常上传时可尝试此选项</string>
<string name="start_upload">開始上載</string>

<string name="permission_require">需要权限</string>
Expand All @@ -79,6 +81,9 @@
<string name="socket_exception">网络错误</string>
<string name="unknown_host_exception">域名解析错误</string>

<string name="notice">注意</string>
<string name="notice_baidu">不支持webp格式和较大图片</string>

<string name="show_result_in">结果打开方式</string>
<string name="build_in_browser">內建瀏覽器</string>
<string name="chrome_custom_tabs">Chrome custom tabs</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/array.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
</string-array>

<string-array name="google_region_redirect_value" translatable="false">
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<item>iqdb</item>
<item>TinEye</item>
<item>SauceNAO</item>
<item>ascii2d</item>
</string-array>

<string name="usage">Usage</string>
Expand Down Expand Up @@ -56,6 +57,7 @@
<string name="settings">Settings</string>
<string name="setting_each_time">Open settings before upload</string>
<string name="resize_image">Compress upload image</string>
<string name="try_resize_image_if_error">Try this if upload error</string>
<string name="start_upload">Start upload</string>

<string name="permission_require">Permission require</string>
Expand All @@ -82,6 +84,9 @@
<string name="unknown_host_exception">Domain resolution error</string>
<string name="socket_exception">Network error</string>

<string name="notice">Notice</string>
<string name="notice_baidu">WEBP image or large size image is not supported.</string>

<string name="show_result_in">Show result in</string>
<string name="build_in_browser">Build-in browser</string>
<string name="chrome_custom_tabs">Chrome custom tabs</string>
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/xml/preferences_general.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
android:layout="@layout/preference_material"/>

<SwitchPreference
android:defaultValue="false"
android:defaultValue="true"
android:key="resize_image"
android:title="@string/resize_image"
android:summary="@string/try_resize_image_if_error"
android:layout="@layout/preference_material"/>

<!--<SwitchPreference
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/preferences_general_gp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
android:layout="@layout/preference_material"/>

<SwitchPreference
android:defaultValue="false"
android:defaultValue="true"
android:key="resize_image"
android:title="@string/resize_image"
android:layout="@layout/preference_material"/>
Expand Down
17 changes: 14 additions & 3 deletions app/src/main/res/xml/preferences_search_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:key="screen">

<PreferenceCategory
android:title="@string/notice"
android:key="category_notice"
android:order="2"
android:layout="@layout/preference_category_material">
<Preference
android:key="preference_notice"
android:summary=""
android:layout="@layout/preference_material"/>
</PreferenceCategory>

<PreferenceCategory
android:title="@string/google_settings"
android:key="category_google"
android:order="2"
android:order="3"
android:layout="@layout/preference_category_material">

<SwitchPreference
Expand Down Expand Up @@ -36,7 +47,7 @@
<PreferenceCategory
android:title="@string/iqdb_settings"
android:key="category_iqdb"
android:order="2"
android:order="3"
android:layout="@layout/preference_category_material">

<MultiSelectListPreference
Expand All @@ -58,7 +69,7 @@
<PreferenceCategory
android:title="@string/saucenao_settings"
android:key="category_saucenao"
android:order="2"
android:order="3"
android:layout="@layout/preference_category_material">

<rikka.searchbyimage.widget.DropDownPreference
Expand Down

0 comments on commit 668d6db

Please sign in to comment.