Difference between revisions of "Apertium Android"

From Apertium
Jump to navigation Jump to search
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{TOCD}}
This is about the current Apertium Android
 
  +
== CURRENTLY BANNED FROM PLAY STORE ==
  +
This app (and presumably others based on it) is currently banned from the Play Store because it downloads code from non-Play servers.)
   
  +
The goal of the 'official' Apertium Android app is to provide example code on how to integrate Apertium offline translation into an Android app.
For historical versions, please see
 
   
  +
It requires internet permission to enable users to download language pairs (and developers to showcase their work from a phone).
* [[Apertium On Mobile GSOC 2012 project by Arink Verma]]
 
   
  +
If you want a more fully-featured app, try [[Mitzuli]].
 
== Apertium basic offline translator ==
 
 
The goal of the 'official' Apertium Android app is to provide example code on how to integrate Apertium offline translation into an Android app.
 
It requires a minimum set of permissions that enable developers to showcase and test their work from a phone.
 
   
   
Line 18: Line 16:
   
 
The current version can be downloaded [https://play.google.com/store/apps/details?id=org.apertium.android here].
 
The current version can be downloaded [https://play.google.com/store/apps/details?id=org.apertium.android here].
  +
The source code is available at http://apertium.svn.sourceforge.net/viewvc/apertium/trunk/apertium-mobile/apertium-android/.
 
  +
The source code is available at https://github.com/apertium/apertium-android.
   
 
Ideas for further improvements:
 
Ideas for further improvements:
Line 24: Line 23:
 
* theme/colors doesent provide correct visual feedback (you can't see if you press buttons)
 
* theme/colors doesent provide correct visual feedback (you can't see if you press buttons)
 
* language detection - for example using https://code.google.com/p/language-detection/
 
* language detection - for example using https://code.google.com/p/language-detection/
* factor unused stuff into 'Apertium extended' app
+
* factor unused stuff from Arink's original work into 'Apertium extended' app
   
   
Line 30: Line 29:
   
   
== Apertium extended ==
+
=== Developers: Integrating Apertium into your own Android app ===
 
Apertium extended Android app would be a full-feature app with:
 
 
* a cool Android 4 interface using loads of cool visual libraries
 
* OCR,
 
* SMS translation,
 
* TTS, STT
 
* and a zillion of scary permissions (internet + read SMS + read SD card should scare you off unless you really trust the source of the app).
 
 
==History==
 
 
=== Introduction ===
 
It is one of the project, for Google Summer of Code'12. Apertium has a Java port, but it was not working on Android mobile. Lots of people have mobile telephones, and some of them would like to have a translator there.
 
 
 
=== Download ===
 
==== Source Code ====
 
Download source code archive
 
<ul>
 
<li>'''ZIP''' https://github.com/arinkverma/Apertiurm-Androind-app-devlopment/zipball/GSoC_Submit
 
<li>'''TARBALL''' https://github.com/arinkverma/Apertiurm-Androind-app-devlopment/tarball/GSoC_Submit
 
</ul>
 
   
  +
Here are the recommended places to store stuff.
  +
We recommend that you use the ApertiumInstallation utility class included in the demo app.
   
==== Git Repository ====
 
use folloing, to clone git repository
 
 
<pre>
 
<pre>
  +
File packagesDir = new File(getFilesDir(), "packages"); // where packages' data are installed
git clone https://github.com/arinkverma/Apertiurm-Androind-app-devlopment.git
 
  +
File bytecodeDir = new File(getFilesDir(), "bytecode"); // where packages' bytecode are installed. Must be private
  +
File bytecodeCacheDir = new File(getCacheDir(), "bytecodecache"); // where bytecode cache is kept. Must be private
  +
IOUtils.cacheDir = new File(getCacheDir(), "apertium-index-cache"); // where cached transducerindexes are kept
  +
ai = new ApertiumInstallation(packagesDir, bytecodeDir, bytecodeCacheDir);
  +
ai.rescanForPackages();
 
</pre>
 
</pre>
   
To fork, goto https://github.com/arinkverma/Apertiurm-Androind-app-devlopment
 
   
  +
And then use apertiumInstallation.installJar(tmpjarfilelocation, pkg); to install pairs.
   
  +
To do a translation, use
==== Binary ====
 
Download APK
 
<ul>
 
<li>'''Latest version''' [https://github.com/downloads/arinkverma/Apertiurm-Androind-app-devlopment/ApertiumAndroid_Latest.apk ApertiumAndroid_Latest.apk]
 
<li>'''GSoC Final''' [https://github.com/downloads/arinkverma/Apertiurm-Androind-app-devlopment/ApertiumAndroid_GSoC_Submit.apk ApertiumAndroid_GSoC_Submit.apk]
 
<li>'''More versions''' https://github.com/arinkverma/Apertiurm-Androind-app-devlopment/downloads
 
</ul>
 
 
 
 
=== Screenshots ===
 
<gallery>
 
Image:Device-2012-08-07-051235.png|Main screen with option menu showing option of Share,Manage and Inbox
 
Image:Device-2012-08-04-192943.png|Main screen with translation from English to Spanish
 
Image:Device-2012-08-04-220310.png|Mode manage activity, showing deletion of language pair
 
Image:Device-2012-08-04-223217.png|Downloading Spanish-French language pair from internet
 
Image:Device-2012-08-04-222434.png|Showing application configuration setting view
 
Image:Device-2012-08-04-222520.png|Desktop screen with Apertium shortcut widget
 
</gallery>
 
 
 
 
=== Project Structure ===
 
<gallery>
 
Image:Apertium-mobile-flowchart.png|Project structure diagram with Android Activity flow
 
</gallery>
 
 
 
 
=== Implementation Technique ===
 
Some features like process handler, clipboard, styling have to be implemented in special way due to protect memory leakage, different methods in different API, difference in styling between android APIs.
 
 
 
==== Process Handler ====
 
'''Handler class should be static or leaks might occur'''. Handler objects for the same thread all share a common Looper object, which they post messages to and read from.
 
 
As messages contain target Handler, as long as there are messages with target handler in the message queue, the handler cannot be gаrbage collected. If handler is not static, your Service or Activity cannot be gаrbage collected, even after being destroyed.
 
 
Sample code displaying process in two sequential methods
 
 
<pre>
 
<pre>
  +
String mode = ai.titleToMode.get(currentModeTitle);
private static Handler handler = null;
 
  +
String pkg = ai.modeToPackage.get(mode);
@Override
 
  +
Translator.setBase(ai.getBasedirForPackage(pkg), ai.getClassLoaderForPackage(pkg));
public void onCreate(Bundle savedInstanceState) {
 
  +
Translator.setMode(mode);
super.onCreate(savedInstanceState);
 
handler = new Handler();
 
 
}
 
 
private void Run1(){
 
progressDialog.setMessage("Step 1");
 
Thread t = new Thread() {
 
@Override
 
public void run() {
 
//Method code here
 
 
handler.post(new Runnable() {
 
@Override
 
public void run() {
 
//Starting Run1 method after finishing step 1
 
Run2();
 
}
 
});
 
}
 
};
 
t.start();
 
}
 
 
private void Run2(){
 
progressDialog.setMessage("Step 2");
 
Thread t = new Thread() {
 
@Override
 
public void run() {
 
//Method code here
 
 
progressDialog.dismiss();
 
//Process finish
 
}
 
};
 
t.start();
 
}
 
 
</pre>
 
</pre>
   
   
  +
And lastly.... Translator.translate() to translate :-)
==== Clipboard Manger ====
 
Android two entirely different version of clipboardmanger for API Level less than 11 and 11 or more. The build target has been set to Api 7 to 15, preferably 10
 
   
Manifest file to make application compatible to API>=7 and API <=15
 
<pre>
 
<uses-sdk
 
android:maxsdkversion="15"
 
android:minsdkversion="7"
 
android:targetsdkversion="10">
 
</uses-sdk>
 
</pre>
 
   
The API level of android has been fetch on run-time and executing the code accordingly
 
Preview of '''ClipBoardManager.java'''
 
<pre>
 
@SuppressWarnings("deprecation")
 
public void putText(String text){
 
int sdk = android.os.Build.VERSION.SDK_INT;
 
if(sdk < 11) {
 
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
 
clipboard.setText(text);
 
} else {
 
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
 
android.content.ClipData clip = ClipData.newPlainText("simple text",text);
 
clipboard.setPrimaryClip(clip);
 
}
 
}
 
 
@SuppressWarnings("deprecation")
 
public String getText(){
 
String text = null;
 
int sdk = android.os.Build.VERSION.SDK_INT;
 
if(sdk < 11) {
 
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
 
text = clipboard.getText().toString();
 
} else {
 
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
 
text = clipboard.getText().toString();
 
}
 
return text;
 
}
 
</pre>
 
   
  +
== Apertium extended ==
   
  +
Apertium extended Android app would be a full-feature app with:
==== Styling for different API ====
 
  +
* store pairs in SD card (requires permission to read SD card)
There are many difference in style between API < 11 and greater. Hence there is different theme xml file for different API.
 
  +
* load pairs from SD card (for example experimental pairs or pairs not hosted in Apertium regi)
For example
 
  +
* a cool Android 4 interface using loads of cool visual libraries
<ul>
 
  +
* OCR,
<li>''res/'''values'''/themes.xml'' is for API less than 11
 
  +
* SMS translation,
<li>''res/'''values-v11'''/themes.xml'' is for API more and equal to 11
 
  +
* TTS, STT
</ul>
 
  +
* and a zillion of scary permissions (internet + read SMS + read SD card should scare you off unless you really trust the source of the app).
  +
* option to use online translation API(s?)
  +
* mind reading?
   
  +
== History ==
   
  +
* GSOC 2012 [[User:Arinkverma/Apertium on mobile]]
 
  +
* Decemter 2012 Enhancements WRT memory, see https://sourceforge.net/mailarchive/message.php?msg_id=30266147
=== Crash Detection ===
 
Due to device's low RAM for translation or any other reasons, crash can happen. At the time of crash, App report it to app preference and close itself gracefully. So in next run, it can gather report and can be send to developers.
 
 
Code for crash report of '''ApertiumActivity.java'''
 
<pre>
 
//Saving and setting crash happen flag
 
 
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
 
@Override
 
public void uncaughtException(Thread t, Throwable e) {
 
String error = e.getMessage();
 
Log.e("Error", error);
 
appPreference.ReportCrash(error);
 
progressDialog.dismiss();
 
thisActivity.finish();
 
android.os.Process.killProcess(android.os.Process.myPid());
 
}
 
});
 
</pre>
 
   
   
=== See also ===
 
* [[apertium-tinylex|Apertium TinyLex]]
 
* [[Tinylex on a Palm]]
 
* [[Apertium Android]] -- another app, which uses the web service
 
   
 
[[Category:User interfaces]]
 
[[Category:User interfaces]]

Latest revision as of 01:06, 4 June 2020

CURRENTLY BANNED FROM PLAY STORE[edit]

This app (and presumably others based on it) is currently banned from the Play Store because it downloads code from non-Play servers.)

The goal of the 'official' Apertium Android app is to provide example code on how to integrate Apertium offline translation into an Android app.

It requires internet permission to enable users to download language pairs (and developers to showcase their work from a phone).

If you want a more fully-featured app, try Mitzuli.



The current version can be downloaded here.

The source code is available at https://github.com/apertium/apertium-android.

Ideas for further improvements:

  • theme/colors doesent provide correct visual feedback (you can't see if you press buttons)
  • language detection - for example using https://code.google.com/p/language-detection/
  • factor unused stuff from Arink's original work into 'Apertium extended' app


Stuff can go in the 'basic Apertium app' if 1) it doesent require scary permissions and 2) it doesent make the app harder to understand as example code on how to integrate Apertium offline translation into other apps.


Developers: Integrating Apertium into your own Android app[edit]

Here are the recommended places to store stuff. We recommend that you use the ApertiumInstallation utility class included in the demo app.

    File packagesDir = new File(getFilesDir(), "packages"); // where packages' data are installed
    File bytecodeDir = new File(getFilesDir(), "bytecode"); // where packages' bytecode are installed. Must be private
    File bytecodeCacheDir = new File(getCacheDir(), "bytecodecache"); // where bytecode cache is kept. Must be private
    IOUtils.cacheDir = new File(getCacheDir(), "apertium-index-cache"); // where cached transducerindexes are kept
    ai = new ApertiumInstallation(packagesDir, bytecodeDir, bytecodeCacheDir);
    ai.rescanForPackages();


And then use apertiumInstallation.installJar(tmpjarfilelocation, pkg); to install pairs.

To do a translation, use

	String mode = ai.titleToMode.get(currentModeTitle);
	String pkg = ai.modeToPackage.get(mode);
	Translator.setBase(ai.getBasedirForPackage(pkg), ai.getClassLoaderForPackage(pkg));
	Translator.setMode(mode);


And lastly.... Translator.translate() to translate :-)


Apertium extended[edit]

Apertium extended Android app would be a full-feature app with:

  • store pairs in SD card (requires permission to read SD card)
  • load pairs from SD card (for example experimental pairs or pairs not hosted in Apertium regi)
  • a cool Android 4 interface using loads of cool visual libraries
  • OCR,
  • SMS translation,
  • TTS, STT
  • and a zillion of scary permissions (internet + read SMS + read SD card should scare you off unless you really trust the source of the app).
  • option to use online translation API(s?)
  • mind reading?

History[edit]