Apertium Android

From Apertium
Revision as of 11:30, 11 April 2013 by Francis Tyers (talk | contribs)
Jump to navigation Jump to search

This is about the current Apertium Android

For historical versions, please see


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.



The current version can be downloaded here. The source code is available at http://apertium.svn.sourceforge.net/viewvc/apertium/trunk/apertium-mobile/apertium-android/.

Ideas for further improvements:


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.


Apertium extended

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


Git Repository

use folloing, to clone git repository

git clone https://github.com/arinkverma/Apertiurm-Androind-app-devlopment.git

To fork, goto https://github.com/arinkverma/Apertiurm-Androind-app-devlopment


Binary

Download APK


Screenshots


Project Structure


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

private static Handler handler = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    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();
}


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

   
<uses-sdk 
    android:maxsdkversion="15" 
    android:minsdkversion="7" 
    android:targetsdkversion="10">
</uses-sdk>

The API level of android has been fetch on run-time and executing the code accordingly Preview of ClipBoardManager.java

@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;
}


Styling for different API

There are many difference in style between API < 11 and greater. Hence there is different theme xml file for different API. For example

  • res/values/themes.xml is for API less than 11
  • res/values-v11/themes.xml is for API more and equal to 11


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

//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());
    }
});


See also