User:Arinkverma/Apertium on mobile

From Apertium
Jump to navigation Jump to search

History[edit]

Introduction[edit]

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[edit]

Source Code[edit]

Download source code archive


Git Repository[edit]

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[edit]

Download APK


Screenshots[edit]


Project Structure[edit]


Implementation Technique[edit]

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[edit]

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[edit]

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[edit]

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[edit]

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[edit]