mirror of
https://github.com/ankidroid/Anki-Android.git
synced 2024-09-20 12:02:16 +02:00
Merge pull request #3938 from timrae/remove-google-translate-filter
Remove redundant Google Translate filter and Image search
This commit is contained in:
commit
0f94aca91f
@ -301,11 +301,6 @@
|
||||
android:configChanges="keyboardHidden|orientation|locale|screenSize"
|
||||
android:label="@string/title_activity_load_pronounciation" >
|
||||
</activity>
|
||||
<activity
|
||||
android:name="com.ichi2.anki.multimediacard.activity.SearchImageActivity"
|
||||
android:configChanges="keyboardHidden|orientation|locale|screenSize"
|
||||
android:label="@string/title_activity_search_image" >
|
||||
</activity>
|
||||
<activity
|
||||
android:name="com.ichi2.anki.CardTemplateEditor"
|
||||
android:configChanges="keyboardHidden|orientation|locale|screenSize"
|
||||
|
@ -66,7 +66,6 @@ import com.ichi2.anki.multimediacard.impl.MultimediaEditableNote;
|
||||
import com.ichi2.anki.receiver.SdCardReceiver;
|
||||
import com.ichi2.anki.servicelayer.NoteService;
|
||||
import com.ichi2.async.DeckTask;
|
||||
import com.ichi2.filters.FilterFacade;
|
||||
import com.ichi2.libanki.Card;
|
||||
import com.ichi2.libanki.Collection;
|
||||
import com.ichi2.libanki.Note;
|
||||
@ -574,12 +573,9 @@ public class NoteEditor extends AnkiActivity {
|
||||
}
|
||||
Pair<String, String> messages = new Pair<String, String>(first, second);
|
||||
|
||||
/* Filter garbage information */
|
||||
Pair<String, String> cleanMessages = new FilterFacade(getBaseContext()).filter(messages);
|
||||
|
||||
mSourceText = new String[2];
|
||||
mSourceText[0] = cleanMessages.first;
|
||||
mSourceText[1] = cleanMessages.second;
|
||||
mSourceText[0] = messages.first;
|
||||
mSourceText[1] = messages.second;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,536 +0,0 @@
|
||||
/****************************************************************************************
|
||||
* Copyright (c) 2013 Bibek Shrestha <bibekshrestha@gmail.com> *
|
||||
* Copyright (c) 2013 Zaur Molotnikov <qutorial@gmail.com> *
|
||||
* Copyright (c) 2013 Nicolas Raoul <nicolas.raoul@gmail.com> *
|
||||
* Copyright (c) 2013 Flavio Lerda <flerda@gmail.com> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify it under *
|
||||
* the terms of the GNU General Public License as published by the Free Software *
|
||||
* Foundation; either version 3 of the License, or (at your option) any later *
|
||||
* version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License along with *
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
****************************************************************************************/
|
||||
|
||||
package com.ichi2.anki.multimediacard.activity;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.Menu;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.ichi2.anki.R;
|
||||
import com.ichi2.anki.multimediacard.googleimagesearch.json.ImageSearchResponse;
|
||||
import com.ichi2.anki.multimediacard.googleimagesearch.json.ResponseData;
|
||||
import com.ichi2.anki.multimediacard.googleimagesearch.json.Result;
|
||||
import com.ichi2.anki.web.HttpFetcher;
|
||||
import com.ichi2.anki.web.UrlTools;
|
||||
import com.ichi2.async.Connection;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
|
||||
public class SearchImageActivity extends Activity implements DialogInterface.OnCancelListener {
|
||||
private static final String BUNDLE_KEY_SHUT_OFF = "key.multimedia.shut.off";
|
||||
|
||||
public static final String EXTRA_SOURCE = "search.image.activity.extra.source";
|
||||
// Passed out as a result
|
||||
public static String EXTRA_IMAGE_FILE_PATH = "com.ichi2.anki.search.image.activity.extra.image.file.path";
|
||||
|
||||
private String mSource;
|
||||
private WebView mWebView = null;
|
||||
private Button mPrevButton;
|
||||
private Button mNextButton;
|
||||
private ProgressDialog progressDialog;
|
||||
private ArrayList<String> mImages;
|
||||
private int mCurrentImage;
|
||||
private String mTemplate = null;
|
||||
private Button mPickButton;
|
||||
private DownloadFileTask mDownloadMp3Task;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
|
||||
outState.putBoolean(BUNDLE_KEY_SHUT_OFF, true);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
if (mWebView != null) {
|
||||
// Saving memory
|
||||
mWebView.clearCache(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void finishCancel() {
|
||||
Intent resultData = new Intent();
|
||||
setResult(RESULT_CANCELED, resultData);
|
||||
finish();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
if (savedInstanceState != null) {
|
||||
boolean b = savedInstanceState.getBoolean(BUNDLE_KEY_SHUT_OFF, false);
|
||||
if (b) {
|
||||
finishCancel();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
setContentView(R.layout.activity_search_image);
|
||||
|
||||
try {
|
||||
mSource = getIntent().getExtras().getString(EXTRA_SOURCE);
|
||||
} catch (Exception e) {
|
||||
mSource = "";
|
||||
}
|
||||
|
||||
// If translation fails this is a default - source will be returned.
|
||||
|
||||
mWebView = (WebView) findViewById(R.id.ImageSearchWebView);
|
||||
mWebView.setWebViewClient(new WebViewClient() {
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url) {
|
||||
super.onPageFinished(view, url);
|
||||
processPageLoadFinished();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
||||
super.onPageStarted(view, url, favicon);
|
||||
processPageLoadStarted();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
mPickButton = (Button) findViewById(R.id.ImageSearchPick);
|
||||
mPickButton.setEnabled(false);
|
||||
mPickButton.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
pickImage();
|
||||
}
|
||||
});
|
||||
|
||||
mNextButton = (Button) findViewById(R.id.ImageSearchNext);
|
||||
|
||||
mNextButton.setOnClickListener(new OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
nextClicked();
|
||||
}
|
||||
});
|
||||
|
||||
mPrevButton = (Button) findViewById(R.id.ImageSearchPrev);
|
||||
|
||||
mPrevButton.setOnClickListener(new OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
prevClicked();
|
||||
}
|
||||
});
|
||||
|
||||
mPrevButton.setEnabled(false);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @author zaur This is to load finally the MP3 file with pronunciation.
|
||||
*/
|
||||
private class DownloadFileTask extends AsyncTask<Void, Void, String> {
|
||||
|
||||
private String mAddress;
|
||||
private Context mActivity;
|
||||
|
||||
|
||||
@Override
|
||||
protected String doInBackground(Void... params) {
|
||||
return HttpFetcher.downloadFileToSdCard(mAddress, mActivity, "imgsearch");
|
||||
}
|
||||
|
||||
|
||||
public void setAddress(String address) {
|
||||
mAddress = address;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(String result) {
|
||||
receiveImageFile(result);
|
||||
}
|
||||
|
||||
|
||||
public void setActivity(Context mActivity) {
|
||||
this.mActivity = mActivity;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected void pickImage() {
|
||||
if(!Connection.isOnline()) {
|
||||
returnFailure(gtxt(R.string.network_no_connection));
|
||||
return;
|
||||
}
|
||||
|
||||
String imageUrl = mImages.get(mCurrentImage);
|
||||
|
||||
// And here it is possible to download it... so on,
|
||||
// then return file path.
|
||||
|
||||
// Download MP3 file
|
||||
try {
|
||||
progressDialog = ProgressDialog.show(this, gtxt(R.string.multimedia_editor_progress_wait_title),
|
||||
gtxt(R.string.multimedia_editor_imgs_saving_image), true, false);
|
||||
mDownloadMp3Task = new DownloadFileTask();
|
||||
mDownloadMp3Task.setActivity(this);
|
||||
mDownloadMp3Task.setAddress(imageUrl);
|
||||
mDownloadMp3Task.execute();
|
||||
} catch (Exception e) {
|
||||
progressDialog.dismiss();
|
||||
returnFailure(gtxt(R.string.multimedia_editor_something_wrong));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void receiveImageFile(String result) {
|
||||
dismissCarefullyProgressDialog();
|
||||
|
||||
Intent resultData = new Intent();
|
||||
|
||||
resultData.putExtra(EXTRA_IMAGE_FILE_PATH, result);
|
||||
|
||||
setResult(RESULT_OK, resultData);
|
||||
|
||||
finish();
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected void processPageLoadStarted() {
|
||||
mPickButton.setEnabled(false);
|
||||
progressDialog = ProgressDialog.show(this, getText(R.string.multimedia_editor_progress_wait_title),
|
||||
gtxt(R.string.multimedia_editor_imgs_loading_image), true, false);
|
||||
|
||||
progressDialog.setCancelable(true);
|
||||
}
|
||||
|
||||
|
||||
protected void processPageLoadFinished() {
|
||||
dismissCarefullyProgressDialog();
|
||||
mPickButton.setEnabled(true);
|
||||
}
|
||||
|
||||
|
||||
public String getLocalIpAddress() {
|
||||
try {
|
||||
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
|
||||
NetworkInterface intf = en.nextElement();
|
||||
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
|
||||
InetAddress inetAddress = enumIpAddr.nextElement();
|
||||
if (!inetAddress.isLoopbackAddress()) {
|
||||
return inetAddress.getHostAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private class BackgroundPost extends AsyncTask<Void, Void, ImageSearchResponse> {
|
||||
|
||||
private String mQuery;
|
||||
|
||||
|
||||
@Override
|
||||
protected ImageSearchResponse doInBackground(Void... params) {
|
||||
try {
|
||||
String ip = getLocalIpAddress();
|
||||
|
||||
URL url = new URL("https://ajax.googleapis.com/ajax/services/search/images?"
|
||||
+ "v=1.0&q=Q&userip=IP".replaceAll("Q", getQuery()).replaceAll("IP", UrlTools.encodeUrl(ip)));
|
||||
URLConnection connection = url.openConnection();
|
||||
connection.addRequestProperty("Referer", "anki.ichi2.com");
|
||||
connection.setConnectTimeout(10000);
|
||||
connection.setReadTimeout(60000);
|
||||
|
||||
String line;
|
||||
StringBuilder builder = new StringBuilder();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
while ((line = reader.readLine()) != null) {
|
||||
builder.append(line);
|
||||
}
|
||||
|
||||
Gson gson = new Gson();
|
||||
ImageSearchResponse resp = gson.fromJson(builder.toString(), ImageSearchResponse.class);
|
||||
|
||||
resp.setOk(true);
|
||||
return resp;
|
||||
|
||||
} catch (Exception e) {
|
||||
return new ImageSearchResponse();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(ImageSearchResponse result) {
|
||||
postFinished(result);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param query Used to set the download address
|
||||
*/
|
||||
public void setQuery(String query) {
|
||||
mQuery = query;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Used to know, which of the posts finished, to differentiate.
|
||||
*/
|
||||
public String getQuery() {
|
||||
return UrlTools.encodeUrl(mQuery);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
progressDialog = ProgressDialog.show(this, getText(R.string.multimedia_editor_progress_wait_title),
|
||||
getText(R.string.multimedia_editor_imgs_searching_for_images), true, false);
|
||||
|
||||
progressDialog.setCancelable(true);
|
||||
progressDialog.setOnCancelListener(this);
|
||||
|
||||
if(!Connection.isOnline()) {
|
||||
returnFailure(gtxt(R.string.network_no_connection));
|
||||
return;
|
||||
}
|
||||
|
||||
BackgroundPost p = new BackgroundPost();
|
||||
p.setQuery(mSource);
|
||||
p.execute();
|
||||
}
|
||||
|
||||
|
||||
public void postFinished(ImageSearchResponse response) {
|
||||
|
||||
ArrayList<String> theImages = new ArrayList<String>();
|
||||
|
||||
// No loop, just a good construct to break out from
|
||||
do {
|
||||
if (response == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!response.getOk()) {
|
||||
break;
|
||||
}
|
||||
|
||||
ResponseData rdata = response.getResponseData();
|
||||
|
||||
if (rdata == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
List<Result> results = rdata.getResults();
|
||||
|
||||
if (results == null) {
|
||||
break;
|
||||
}
|
||||
|
||||
for (Result result : results) {
|
||||
if (result == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String url = result.getUrl();
|
||||
|
||||
if (url != null) {
|
||||
theImages.add(url);
|
||||
}
|
||||
}
|
||||
|
||||
if (theImages.size() == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
proceedWithImages(theImages);
|
||||
|
||||
return;
|
||||
|
||||
} while (false);
|
||||
|
||||
returnFailure(gtxt(R.string.multimedia_editor_imgs_no_results));
|
||||
}
|
||||
|
||||
|
||||
private void proceedWithImages(ArrayList<String> theImages) {
|
||||
showToast(gtxt(R.string.multimedia_editor_imgs_images_found));
|
||||
dismissCarefullyProgressDialog();
|
||||
|
||||
mImages = theImages;
|
||||
mCurrentImage = 0;
|
||||
|
||||
showCurrentImage();
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void showCurrentImage() {
|
||||
if (mCurrentImage <= 0) {
|
||||
mCurrentImage = 0;
|
||||
mPrevButton.setEnabled(false);
|
||||
mNextButton.setEnabled(mImages.size() > 0);
|
||||
}
|
||||
|
||||
if (mCurrentImage > 0) {
|
||||
mCurrentImage = Math.min(mImages.size() - 1, mCurrentImage);
|
||||
mPrevButton.setEnabled(true);
|
||||
mNextButton.setEnabled(mCurrentImage < mImages.size() - 1);
|
||||
}
|
||||
|
||||
String source = makeImageCodeTemplate();
|
||||
|
||||
source = source.replaceAll("URL", mImages.get(mCurrentImage));
|
||||
|
||||
mWebView.loadData(source, "text/html", null);
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void returnFailure(String explanation) {
|
||||
showToast(explanation);
|
||||
setResult(RESULT_CANCELED);
|
||||
dismissCarefullyProgressDialog();
|
||||
finish();
|
||||
}
|
||||
|
||||
|
||||
private void dismissCarefullyProgressDialog() {
|
||||
try {
|
||||
if (progressDialog != null) {
|
||||
if (progressDialog.isShowing()) {
|
||||
progressDialog.dismiss();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// nothing is done intentionally
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void showToast(CharSequence text) {
|
||||
int duration = Toast.LENGTH_SHORT;
|
||||
Toast toast = Toast.makeText(this, text, duration);
|
||||
toast.show();
|
||||
}
|
||||
|
||||
|
||||
protected void prevClicked() {
|
||||
--mCurrentImage;
|
||||
showCurrentImage();
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected void nextClicked() {
|
||||
++mCurrentImage;
|
||||
showCurrentImage();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
// Inflate the menu; this adds items to the action bar if it is present.
|
||||
getMenuInflater().inflate(R.menu.activity_search_image, menu);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private String makeImageCodeTemplate() {
|
||||
if (mTemplate != null) {
|
||||
return mTemplate;
|
||||
}
|
||||
|
||||
String source = "<html><body><center>" + gtxt(R.string.multimedia_editor_imgs_pow_by_google)
|
||||
+ "</center><br /><center><img width=\"WIDTH\" src=\"URL\" /> </center></body></html>";
|
||||
|
||||
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
|
||||
if (currentapiVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
|
||||
DisplayMetrics metrics = new DisplayMetrics();
|
||||
getWindowManager().getDefaultDisplay().getMetrics(metrics);
|
||||
|
||||
int height = metrics.heightPixels;
|
||||
int width = metrics.widthPixels;
|
||||
|
||||
int min = Math.min(height, width);
|
||||
|
||||
source = source.replaceAll("WIDTH", (int) Math.round(min * 0.85) + "");
|
||||
} else {
|
||||
source = source.replaceAll("WIDTH", "80%");
|
||||
}
|
||||
|
||||
mTemplate = source;
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCancel(DialogInterface dialog) {
|
||||
// nothing
|
||||
}
|
||||
|
||||
|
||||
private String gtxt(int id) {
|
||||
return getText(id).toString();
|
||||
}
|
||||
|
||||
}
|
@ -37,7 +37,6 @@ import android.widget.Toast;
|
||||
import com.ichi2.anki.R;
|
||||
import com.ichi2.anki.multimediacard.activity.LoadPronounciationActivity;
|
||||
import com.ichi2.anki.multimediacard.activity.PickStringDialogFragment;
|
||||
import com.ichi2.anki.multimediacard.activity.SearchImageActivity;
|
||||
import com.ichi2.anki.multimediacard.activity.TranslationActivity;
|
||||
import com.ichi2.compat.CompatHelper;
|
||||
|
||||
@ -92,8 +91,6 @@ public class BasicTextFieldController extends FieldControllerBase implements IFi
|
||||
layout.addView(layoutTools2);
|
||||
createTranslateButton(layoutTools2, p);
|
||||
createPronounceButton(layoutTools2, p);
|
||||
createSearchImageButton(layoutTools2, p);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -102,35 +99,6 @@ public class BasicTextFieldController extends FieldControllerBase implements IFi
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Google Image Search
|
||||
*
|
||||
* @param layoutTools
|
||||
* @param p
|
||||
*/
|
||||
private void createSearchImageButton(LinearLayout layoutTools, LayoutParams p) {
|
||||
Button clearButton = new Button(mActivity);
|
||||
clearButton.setText(gtxt(R.string.multimedia_editor_text_field_editing_show));
|
||||
layoutTools.addView(clearButton, p);
|
||||
|
||||
clearButton.setOnClickListener(new OnClickListener() {
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
String source = mEditText.getText().toString();
|
||||
|
||||
if (source.length() == 0) {
|
||||
showToast(gtxt(R.string.multimedia_editor_text_field_editing_no_text));
|
||||
return;
|
||||
}
|
||||
|
||||
Intent intent = new Intent(mActivity, SearchImageActivity.class);
|
||||
intent.putExtra(SearchImageActivity.EXTRA_SOURCE, source);
|
||||
mActivity.startActivityForResult(intent, REQUEST_CODE_IMAGE_SEARCH);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
private void createClearButton(LinearLayout layoutTools, LayoutParams p) {
|
||||
Button clearButton = new Button(mActivity);
|
||||
@ -346,17 +314,6 @@ public class BasicTextFieldController extends FieldControllerBase implements IFi
|
||||
|
||||
mEditText.setText(text);
|
||||
|
||||
} else if (requestCode == REQUEST_CODE_IMAGE_SEARCH && resultCode == Activity.RESULT_OK) {
|
||||
String imgPath = data.getExtras().get(SearchImageActivity.EXTRA_IMAGE_FILE_PATH).toString();
|
||||
File f = new File(imgPath);
|
||||
if (!f.exists()) {
|
||||
showToast(gtxt(R.string.multimedia_editor_imgs_failed));
|
||||
}
|
||||
|
||||
ImageField imgField = new ImageField();
|
||||
imgField.setImagePath(imgPath);
|
||||
imgField.setHasTemporaryMedia(true);
|
||||
mActivity.handleFieldChanged(imgField);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,96 +0,0 @@
|
||||
/****************************************************************************************
|
||||
* Copyright (c) 2013 Bibek Shrestha <bibekshrestha@gmail.com> *
|
||||
* Copyright (c) 2013 Zaur Molotnikov <qutorial@gmail.com> *
|
||||
* Copyright (c) 2013 Nicolas Raoul <nicolas.raoul@gmail.com> *
|
||||
* Copyright (c) 2013 Flavio Lerda <flerda@gmail.com> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify it under *
|
||||
* the terms of the GNU General Public License as published by the Free Software *
|
||||
* Foundation; either version 3 of the License, or (at your option) any later *
|
||||
* version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License along with *
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
****************************************************************************************/
|
||||
|
||||
package com.ichi2.anki.multimediacard.googleimagesearch.json;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* One of the JSON classes. generated, corrected by Zaur.
|
||||
* <p>
|
||||
* Google Image search response.
|
||||
*/
|
||||
public class Cursor {
|
||||
private Number currentPageIndex;
|
||||
private String estimatedResultCount;
|
||||
private String moreResultsUrl;
|
||||
private List<Page> pages;
|
||||
private String resultCount;
|
||||
private String searchResultTime;
|
||||
|
||||
|
||||
public Number getCurrentPageIndex() {
|
||||
return this.currentPageIndex;
|
||||
}
|
||||
|
||||
|
||||
public void setCurrentPageIndex(Number currentPageIndex) {
|
||||
this.currentPageIndex = currentPageIndex;
|
||||
}
|
||||
|
||||
|
||||
public String getEstimatedResultCount() {
|
||||
return this.estimatedResultCount;
|
||||
}
|
||||
|
||||
|
||||
public void setEstimatedResultCount(String estimatedResultCount) {
|
||||
this.estimatedResultCount = estimatedResultCount;
|
||||
}
|
||||
|
||||
|
||||
public String getMoreResultsUrl() {
|
||||
return this.moreResultsUrl;
|
||||
}
|
||||
|
||||
|
||||
public void setMoreResultsUrl(String moreResultsUrl) {
|
||||
this.moreResultsUrl = moreResultsUrl;
|
||||
}
|
||||
|
||||
|
||||
public List<Page> getPages() {
|
||||
return this.pages;
|
||||
}
|
||||
|
||||
|
||||
public void setPages(List<Page> pages) {
|
||||
this.pages = pages;
|
||||
}
|
||||
|
||||
|
||||
public String getResultCount() {
|
||||
return this.resultCount;
|
||||
}
|
||||
|
||||
|
||||
public void setResultCount(String resultCount) {
|
||||
this.resultCount = resultCount;
|
||||
}
|
||||
|
||||
|
||||
public String getSearchResultTime() {
|
||||
return this.searchResultTime;
|
||||
}
|
||||
|
||||
|
||||
public void setSearchResultTime(String searchResultTime) {
|
||||
this.searchResultTime = searchResultTime;
|
||||
}
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
/****************************************************************************************
|
||||
* Copyright (c) 2013 Bibek Shrestha <bibekshrestha@gmail.com> *
|
||||
* Copyright (c) 2013 Zaur Molotnikov <qutorial@gmail.com> *
|
||||
* Copyright (c) 2013 Nicolas Raoul <nicolas.raoul@gmail.com> *
|
||||
* Copyright (c) 2013 Flavio Lerda <flerda@gmail.com> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify it under *
|
||||
* the terms of the GNU General Public License as published by the Free Software *
|
||||
* Foundation; either version 3 of the License, or (at your option) any later *
|
||||
* version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License along with *
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
****************************************************************************************/
|
||||
|
||||
package com.ichi2.anki.multimediacard.googleimagesearch.json;
|
||||
|
||||
public class ImageSearchResponse {
|
||||
private ResponseData responseData;
|
||||
private String responseDetails;
|
||||
private Number responseStatus;
|
||||
private boolean Ok = false;
|
||||
|
||||
|
||||
public void setOk(boolean ok) {
|
||||
Ok = ok;
|
||||
}
|
||||
|
||||
|
||||
public boolean getOk() {
|
||||
return Ok;
|
||||
}
|
||||
|
||||
|
||||
public ResponseData getResponseData() {
|
||||
return this.responseData;
|
||||
}
|
||||
|
||||
|
||||
public void setResponseData(ResponseData responseData) {
|
||||
this.responseData = responseData;
|
||||
}
|
||||
|
||||
|
||||
public String getResponseDetails() {
|
||||
return this.responseDetails;
|
||||
}
|
||||
|
||||
|
||||
public void setResponseDetails(String responseDetails) {
|
||||
this.responseDetails = responseDetails;
|
||||
}
|
||||
|
||||
|
||||
public Number getResponseStatus() {
|
||||
return this.responseStatus;
|
||||
}
|
||||
|
||||
|
||||
public void setResponseStatus(Number responseStatus) {
|
||||
this.responseStatus = responseStatus;
|
||||
}
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
/****************************************************************************************
|
||||
* Copyright (c) 2013 Bibek Shrestha <bibekshrestha@gmail.com> *
|
||||
* Copyright (c) 2013 Zaur Molotnikov <qutorial@gmail.com> *
|
||||
* Copyright (c) 2013 Nicolas Raoul <nicolas.raoul@gmail.com> *
|
||||
* Copyright (c) 2013 Flavio Lerda <flerda@gmail.com> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify it under *
|
||||
* the terms of the GNU General Public License as published by the Free Software *
|
||||
* Foundation; either version 3 of the License, or (at your option) any later *
|
||||
* version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License along with *
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
****************************************************************************************/
|
||||
|
||||
package com.ichi2.anki.multimediacard.googleimagesearch.json;
|
||||
|
||||
/**
|
||||
* One of the JSON classes. generated, corrected by Zaur.
|
||||
* <p>
|
||||
* Google Image search response.
|
||||
*/
|
||||
public class Page {
|
||||
private Number label;
|
||||
private String start;
|
||||
|
||||
|
||||
public Number getLabel() {
|
||||
return this.label;
|
||||
}
|
||||
|
||||
|
||||
public void setLabel(Number label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
|
||||
public String getStart() {
|
||||
return this.start;
|
||||
}
|
||||
|
||||
|
||||
public void setStart(String start) {
|
||||
this.start = start;
|
||||
}
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
/****************************************************************************************
|
||||
* Copyright (c) 2013 Bibek Shrestha <bibekshrestha@gmail.com> *
|
||||
* Copyright (c) 2013 Zaur Molotnikov <qutorial@gmail.com> *
|
||||
* Copyright (c) 2013 Nicolas Raoul <nicolas.raoul@gmail.com> *
|
||||
* Copyright (c) 2013 Flavio Lerda <flerda@gmail.com> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify it under *
|
||||
* the terms of the GNU General Public License as published by the Free Software *
|
||||
* Foundation; either version 3 of the License, or (at your option) any later *
|
||||
* version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License along with *
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
****************************************************************************************/
|
||||
|
||||
package com.ichi2.anki.multimediacard.googleimagesearch.json;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* One of the JSON classes. generated, corrected by Zaur.
|
||||
* <p>
|
||||
* Google Image search response.
|
||||
*/
|
||||
public class ResponseData {
|
||||
private Cursor cursor;
|
||||
private List<Result> results;
|
||||
|
||||
|
||||
public Cursor getCursor() {
|
||||
return this.cursor;
|
||||
}
|
||||
|
||||
|
||||
public void setCursor(Cursor cursor) {
|
||||
this.cursor = cursor;
|
||||
}
|
||||
|
||||
|
||||
public List<Result> getResults() {
|
||||
return this.results;
|
||||
}
|
||||
|
||||
|
||||
public void setResults(List<Result> results) {
|
||||
this.results = results;
|
||||
}
|
||||
}
|
@ -1,193 +0,0 @@
|
||||
/****************************************************************************************
|
||||
* Copyright (c) 2013 Bibek Shrestha <bibekshrestha@gmail.com> *
|
||||
* Copyright (c) 2013 Zaur Molotnikov <qutorial@gmail.com> *
|
||||
* Copyright (c) 2013 Nicolas Raoul <nicolas.raoul@gmail.com> *
|
||||
* Copyright (c) 2013 Flavio Lerda <flerda@gmail.com> *
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify it under *
|
||||
* the terms of the GNU General Public License as published by the Free Software *
|
||||
* Foundation; either version 3 of the License, or (at your option) any later *
|
||||
* version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License along with *
|
||||
* this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
****************************************************************************************/
|
||||
|
||||
package com.ichi2.anki.multimediacard.googleimagesearch.json;
|
||||
|
||||
/**
|
||||
* One of the JSON classes. generated, corrected by Zaur.
|
||||
* <p>
|
||||
* Google Image search response.
|
||||
*/
|
||||
public class Result {
|
||||
private String gsearchResultClass;
|
||||
private String content;
|
||||
private String contentNoFormatting;
|
||||
private String height;
|
||||
private String imageId;
|
||||
private String originalContextUrl;
|
||||
private String tbHeight;
|
||||
private String tbUrl;
|
||||
private String tbWidth;
|
||||
private String title;
|
||||
private String titleNoFormatting;
|
||||
private String unescapedUrl;
|
||||
private String url;
|
||||
private String visibleUrl;
|
||||
private String width;
|
||||
|
||||
|
||||
public String getGsearchResultClass() {
|
||||
return this.gsearchResultClass;
|
||||
}
|
||||
|
||||
|
||||
public void setGsearchResultClass(String gsearchResultClass) {
|
||||
this.gsearchResultClass = gsearchResultClass;
|
||||
}
|
||||
|
||||
|
||||
public String getContent() {
|
||||
return this.content;
|
||||
}
|
||||
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
|
||||
public String getContentNoFormatting() {
|
||||
return this.contentNoFormatting;
|
||||
}
|
||||
|
||||
|
||||
public void setContentNoFormatting(String contentNoFormatting) {
|
||||
this.contentNoFormatting = contentNoFormatting;
|
||||
}
|
||||
|
||||
|
||||
public String getHeight() {
|
||||
return this.height;
|
||||
}
|
||||
|
||||
|
||||
public void setHeight(String height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
|
||||
public String getImageId() {
|
||||
return this.imageId;
|
||||
}
|
||||
|
||||
|
||||
public void setImageId(String imageId) {
|
||||
this.imageId = imageId;
|
||||
}
|
||||
|
||||
|
||||
public String getOriginalContextUrl() {
|
||||
return this.originalContextUrl;
|
||||
}
|
||||
|
||||
|
||||
public void setOriginalContextUrl(String originalContextUrl) {
|
||||
this.originalContextUrl = originalContextUrl;
|
||||
}
|
||||
|
||||
|
||||
public String getTbHeight() {
|
||||
return this.tbHeight;
|
||||
}
|
||||
|
||||
|
||||
public void setTbHeight(String tbHeight) {
|
||||
this.tbHeight = tbHeight;
|
||||
}
|
||||
|
||||
|
||||
public String getTbUrl() {
|
||||
return this.tbUrl;
|
||||
}
|
||||
|
||||
|
||||
public void setTbUrl(String tbUrl) {
|
||||
this.tbUrl = tbUrl;
|
||||
}
|
||||
|
||||
|
||||
public String getTbWidth() {
|
||||
return this.tbWidth;
|
||||
}
|
||||
|
||||
|
||||
public void setTbWidth(String tbWidth) {
|
||||
this.tbWidth = tbWidth;
|
||||
}
|
||||
|
||||
|
||||
public String getTitle() {
|
||||
return this.title;
|
||||
}
|
||||
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
|
||||
public String getTitleNoFormatting() {
|
||||
return this.titleNoFormatting;
|
||||
}
|
||||
|
||||
|
||||
public void setTitleNoFormatting(String titleNoFormatting) {
|
||||
this.titleNoFormatting = titleNoFormatting;
|
||||
}
|
||||
|
||||
|
||||
public String getUnescapedUrl() {
|
||||
return this.unescapedUrl;
|
||||
}
|
||||
|
||||
|
||||
public void setUnescapedUrl(String unescapedUrl) {
|
||||
this.unescapedUrl = unescapedUrl;
|
||||
}
|
||||
|
||||
|
||||
public String getUrl() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
|
||||
public String getVisibleUrl() {
|
||||
return this.visibleUrl;
|
||||
}
|
||||
|
||||
|
||||
public void setVisibleUrl(String visibleUrl) {
|
||||
this.visibleUrl = visibleUrl;
|
||||
}
|
||||
|
||||
|
||||
public String getWidth() {
|
||||
return this.width;
|
||||
}
|
||||
|
||||
|
||||
public void setWidth(String width) {
|
||||
this.width = width;
|
||||
}
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
|
||||
package com.ichi2.filters;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Pair;
|
||||
|
||||
import com.ichi2.anki.AnkiDroidApp;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Filter-facade execute all filtering operations.
|
||||
*
|
||||
* @author evgenij.kozhevnikov@gmail.com
|
||||
*/
|
||||
public class FilterFacade {
|
||||
|
||||
/* Context for checking preferences */
|
||||
private Context context;
|
||||
|
||||
/* All filters, that are will running */
|
||||
private final List<CardFilter> filters = new ArrayList<CardFilter>() {
|
||||
{
|
||||
add(new GoogleTranslaterFilter());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
public FilterFacade(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Run all filters processes. Messages in params will be updated.
|
||||
*
|
||||
* @param messages data, received from external application, where first attribute is the SUBJECT information and
|
||||
* second attribute is the TEXT information.
|
||||
*/
|
||||
public Pair<String, String> filter(Pair<String, String> messages) {
|
||||
Pair<String, String> result = new Pair<String, String>(messages.first, messages.second);
|
||||
for (CardFilter cardFilter : filters) {
|
||||
result = cardFilter.filter(result, AnkiDroidApp.getSharedPrefs(context));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
|
||||
package com.ichi2.filters;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Pair;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Remove unnecessary information from Google Translate.
|
||||
*
|
||||
* @author evgenij.kozhevnikov@gmail.com
|
||||
*/
|
||||
public class GoogleTranslaterFilter extends AbstractCardFilter {
|
||||
|
||||
public static final String CHECK_PATTERN = "Google";
|
||||
|
||||
private static final String SEARCH_PATTERN = "(?:\\):\\s*)(.*)(?:\\s|\\b)";
|
||||
|
||||
|
||||
public Pair<String, String> filter(Pair<String, String> messages, SharedPreferences preferences) {
|
||||
Pair<String, String> result = new Pair<String, String>(messages.first, messages.second);
|
||||
Pattern pattern = Pattern.compile(SEARCH_PATTERN);
|
||||
Matcher matcher = pattern.matcher(getSearchText(messages));
|
||||
if (isCanBeExecuted(messages, preferences) && matcher.find()) {
|
||||
String translate = matcher.group(1);
|
||||
if (matcher.find()) {
|
||||
result = new Pair<String, String>(matcher.group(1), translate);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check conditions to running current filter.
|
||||
*
|
||||
* @param messages original messages.
|
||||
* @param preferences program settings.
|
||||
* @return true, if filter could be run, otherwise false.
|
||||
*/
|
||||
private boolean isCanBeExecuted(Pair<String, String> messages, SharedPreferences preferences) {
|
||||
return useFilter(preferences) && messages.first.contains(CHECK_PATTERN);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Forming full text message for search.
|
||||
*
|
||||
* @param messages original messages.
|
||||
* @return full text message for search.
|
||||
*/
|
||||
private String getSearchText(Pair<String, String> messages) {
|
||||
return new StringBuilder(messages.first).append(messages.second).append(' ').toString();
|
||||
}
|
||||
|
||||
}
|
@ -136,8 +136,6 @@
|
||||
<string name="pref_keep_screen_on">Keep screen on</string>
|
||||
<string name="pref_keep_screen_on_summ">Disable screen timeout</string>
|
||||
<string name="convert_fen_text">Chess notation support</string>
|
||||
<string name="filter_google_translate">From Google Translate</string>
|
||||
<string name="filter_google_translate_summ">Just save source word and translation</string>
|
||||
<string name="convert_fen_text_summ">Draw chessboard from Forsyth–Edwards Notation. The notation must be enclosed in [fen][/fen] tags.</string>
|
||||
<string name="enable_api_title">Enable AnkiDroid API</string>
|
||||
<string name="enable_api_summary">Let other apps connect to AnkiDroid and read / write data without your intervention</string>
|
||||
|
@ -108,11 +108,6 @@
|
||||
android:positiveButtonText="@string/dialog_ok"
|
||||
android:summary="@string/reset_languages_summ"
|
||||
android:title="@string/reset_languages" />
|
||||
<CheckBoxPreference
|
||||
android:defaultValue="false"
|
||||
android:key="com.ichi2.filters.GoogleTranslaterFilter"
|
||||
android:summary="@string/filter_google_translate_summ"
|
||||
android:title="@string/filter_google_translate" />
|
||||
<CheckBoxPreference
|
||||
android:defaultValue="false"
|
||||
android:key="convertFenText"
|
||||
|
Loading…
Reference in New Issue
Block a user