Universal Acceptance (UA) Micro-Learning Module: Module 10- Processing IDN and EAI in Mobile Apps Using Java.
Instructor Guide
1st Edition.
���© 2024 Creative Commons License - Attribution 4.0 International (CC BY 4.0).�
Universal Acceptance
Processing IDNs and EAI in Mobile Apps UA Micro-Learning Module Objectives:
| 2
Note About the Utilization of Unicode String Literals :
| 3
Unicode Character Set Support in Mobile Apps:
| 4
Unicode Text Layout and Rendering in Mobile Apps:
| 5
Rendering Bidirectional Text and Complex Scripts in Mobile Apps:
| 6
Java Based Mobile Apps Development Frameworks:
| 7
Leveraging ICU for Bidirectional Text and Complex Scripts in Mobile Apps:
| 8
Rendering Bidirectional Text with Mobile Apps Development Frameworks: Android Studio:
| 9
Example: RTL and Complex Scripts Rendering in Mobile Apps using Java Android(1/6):
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.ibm.icu.text.Bidi;
import com.ibm.icu.text.BreakIterator;
import com.ibm.icu.util.ULocale;
| 10
Example: RTL and Complex Scripts Rendering in Mobile Apps using Java Android(2/6):
public class MobileAppTextRendering extends AppCompatActivity {
private TextView renderedLabel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupLayout();
}
private void setupLayout() {
// Create the main layout
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// Create the rendered label
renderedLabel = new TextView(this);
renderedLabel.setId(View.generateViewId());
// Create the render button
Button renderButton = new Button(this);
renderButton.setText("Render Text");
renderButton.setOnClickListener(v -> renderText());
// Add the views to the layout
layout.addView(renderedLabel);
layout.addView(renderButton);
setContentView(layout);
}
}
| 11
Example: RTL and Complex Scripts Rendering in Mobile Apps using Java Android(3/6):
private void renderText() {
String inputText = "ሀሎ مرحبًا Hello 你好,ආයුබෝවන්";
String locale = "ar"; // Arabic locale
String renderedText = renderBidirectionalText(inputText, locale);
renderedLabel.setText(renderedText);
}
private String renderBidirectionalText(String text, String locale) {
// Create an ICU BreakIterator for line breaking
BreakIterator breakIterator = BreakIterator.createLineInstance(new ULocale(locale));
// Perform bidirectional analysis using ICU's Bidi class
Bidi bidiText = new Bidi(text, Bidi.DIRECTION_DEFAULT_LEFT_TO_RIGHT);
// Break the text into lines
breakIterator.setText(bidiText.getDisplayText());
StringBuilder stringBuilder = new StringBuilder();
int start = breakIterator.first();
for (int end = breakIterator.next(); end != BreakIterator.DONE; start = end, end = breakIterator.next()) {
stringBuilder.append(bidiText.getDisplayText(start, end));
}
// Render the lines
return stringBuilder.toString();
}
}
| 12
Example: RTL and Complex Scripts Rendering in Mobile Apps using Java Android(4/6):
The following is a summary of how the above Java code works:
| 13
Example: RTL and Complex Scripts Rendering in Mobile Apps using Java Android(5/6):
The following is an example of the contents of the XML file used in the above java code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/rendered_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_marginBottom="16dp" />
<Button
android:id="@+id/render_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Render Text" />
</RelativeLayout>
| 14
Example: RTL and Complex Scripts Rendering in Mobile Apps using Java Android(6/6):
Generating the Output:
| 15
Mobile IDN Processing: Libraries and Examples(1/3):
| 16
Mobile IDN Processing: Libraries and Examples(2/3):
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.ibm.icu.text.IDNA;
import com.ibm.icu.text.IDNA.Info;
public class IDNProcessing extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String domain = "こんにちは.com";
IDNA idna = IDNA.getUTS46Instance(IDNA.DEFAULT);
| 17
Mobile IDN Processing: Libraries and Examples(3/3):
Info info = new Info();
StringBuilder encodedDomain = new StringBuilder();
StringBuilder decodedDomain = new StringBuilder();
idna.nameToASCII(domain, encodedDomain, info); // Converts IDN to ACE
idna.nameToUnicode(encodedDomain, decodedDomain, info); // Converts ACE to IDN
// Display the results in a TextView
TextView resultTextView = findViewById(R.id.resultTextView);
resultTextView.setText("Encoded Domain: " + encodedDomain.toString() + "\nDecoded Domain: " + decodedDomain.toString());
// Print the results to the log
Log.d("IDNACode", "Encoded Domain: " + encodedDomain.toString());
Log.d("IDNACode", "Decoded Domain: " + decodedDomain.toString());
}
}
| 18
EAI Support in Mobile Apps – Libraries and Examples(1/6):
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class MainActivity extends AppCompatActivity {
// Member variables and methods will be added in subsequent blocks...
}
| 19
EAI Support in Mobile Apps – Libraries and Examples(2/6):
public class MainActivity extends AppCompatActivity {
private static final String TAG = "EmailSender"; // Log tag for debugging
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Replace with your server's email sending logic
new SendEmailTask().execute();
}
}
| 20
EAI Support in Mobile Apps – Libraries and Examples(3/6):
private class SendEmailTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
sendEmail();
return null;
}
private void sendEmail() {
// Set your server's email credentials and properties
String username = "your-server-email@example.com";
String password = "your-server-email-password";
String host = "your-email-server-host";
int port = 587;
// Set the properties for the mail session
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", String.valueOf(port));
}
});
| 21
EAI Support in Mobile Apps – Libraries and Examples(4/6):
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// Create a MimeMessage
MimeMessage message = new MimeMessage(session);
// Set the sender address
String senderEmail = "your-server-email@example.com";
InternetAddress senderAddress = new InternetAddress(senderEmail, true);
message.setFrom(senderAddress);
// Set the recipient address
String recipientEmail = "recipient-email@example.com";
InternetAddress recipientAddress = new InternetAddress(recipientEmail, true);
message.setRecipient(Message.RecipientType.TO, recipientAddress);
| 22
EAI Support in Mobile Apps – Libraries and Examples(5/6):
// Set the email subject and content
message.setSubject("Test Email");
message.setText("This is a test email sent from the mobile app.");
// Send the email
Transport.send(message);
Log.d(TAG, "Email sent successfully.");
} catch (Exception e) {
Log.e(TAG, "Error sending email", e);
}
}
}
| 23
EAI Support in Mobile Apps – Libraries and Examples(6/6):
The following is a summary of what the above code performs:
| 24
Reference:
[1]. Unicode Consortium, (2023, October 5), The Unicode Standard, Version 15.0, Retrieved from https://unicode.org/versions/ on December 14, 2023.
[2]. Android Developer Documentation, (n.d.), Unicode and internationalization, Retrieved from https://stackoverflow.com/questions/16786739/how-to-use-unicode-in-android-resource on December 14, 2023.
[3]. Apple Developer Documentation, (n.d.), Using Unicode in Your App, Retrieved from https://developer.apple.com/documentation/swift/unicode on December 14, 2023.
[4]. Coulthard, M. (2019, May 30), Unicode & Multilingualism in Mobile Apps, Retrieved from https://www.smashingmagazine.com/category/mobile on December 14, 2023.
[5]. Kivy: Kivy Organization, (n.d.), Kivy: Cross-platform Python Framework, Retrieved from https://kivy.org/, on December 14, 2023.
[6]. Google. (n.d.), Android Developer Documentation. Retrieved from https://developer.android.com/docs on December 14, 2023,
[7]. International Components for Unicode (ICU), (n.d.), ICU User Guide, Retrieved from https://unicode-org.github.io/icu/userguide/ on December 14, 2023.
| 25
Reference:
[8]. Stack Overflow. (2015, May 27), Retrieved from https://stackoverflow.com/questions/8126233/how-to-build-icu-so-i-can-use-it-in-an-iphone-app on December 14, 2023.
[9]. International Components for Unicode (ICU), (n.d.), ICU User Guide, Retrieved from https://unicode-org.github.io/icu/userguide/ on December 14, 2023,
[10]. Kivy Team. (n.d.), Kivy Documentation, Retrieved from https://kivy.org/doc/stable/ on December 14, 2023.
[11]. Mendoza, J, (2023, November 25). python-bidi. PyPI. Retrieved from https://pypi.org/project/python-bidi/ on December 14, 2023.
[12]. Mendoza, J. (2023, November 25), idna, PyPI, Retrieved from https://pypi.org/project/idna/ on December 14, 2023,
[13]. International Components for Unicode (ICU). (n.d.). icu4j, GitHub, Retrieved from https://github.com/unicode-org/icu on December 14, 2023.
| 26
Author:
| 27