Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/multi language support #471

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
37 changes: 15 additions & 22 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ buildscript {
mavenLocal()
maven { url "https://jitpack.io" }
}

dependencies {
classpath 'com.google.gms:google-services:4.3.10'
classpath 'com.android.tools.build:gradle:7.4.2'
classpath 'com.google.gms:google-services:4.3.15'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.9.5'
classpath 'de.undercouch:gradle-download-task:4.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// Other dependencies can be added here

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
google()
jcenter()
mavenLocal()
mavenCentral()
maven { url "https://jitpack.io" }
}
}
Expand All @@ -35,40 +35,33 @@ task clean(type: Delete) {
delete rootProject.buildDir
}

task checkStyle(type:JavaExec) {
task checkStyle(type: JavaExec) {
workingDir = "utils"
classpath = files("utils/google-java-format-1.7-all-deps.jar")
FileTree tree = fileTree('app/src/main/java') {
include '**/*.java'
}
args(["-n","--set-exit-if-changed"]) // -n prints filename if using wrong style
tree.each {args += it}
//color codes: "\u001B[33m" - yellow, "\u001B[32m" - green, "\u001B[0m" - reset (Unix)
args(["-n", "--set-exit-if-changed"])
tree.each { args += it }
doFirst {
println("You can fix the style of the files below (if any) with "
+ "\u001B[33m" + "./gradlew applyStyle" + "\u001B[0m")
}
doLast { // will only execute on success (exitcode=0)
println ("\u001B[32m" + " -> All java files follow the correct style." + "\u001B[0m")
doLast {
println("\u001B[32m" + " -> All java files follow the correct style." + "\u001B[0m")
}
}

task checkStyleUnix(type:Exec) {
workingDir 'utils'
commandLine './checkStyle.sh'
}

task applyStyle(type:JavaExec) {
task applyStyle(type: JavaExec) {
workingDir = "utils"
classpath = files("utils/google-java-format-1.7-all-deps.jar")
FileTree tree = fileTree('app/src/main/java') {
include '**/*.java'
}
args("-r") // replace cmd, means write changes back to source file
tree.each {args += it}
args("-r")
tree.each { args += it }
}

task applyStyleUnix(type:Exec) {
workingDir 'utils'
commandLine './applyStyle.sh'
}
apply plugin: 'com.google.gms.google-services'


1 change: 0 additions & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#Thu Sep 19 11:12:47 IST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
Expand Down
2 changes: 1 addition & 1 deletion android/robot/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,4 @@ dependencies {
exclude group: 'org.apache.httpcomponents'
exclude module: 'guava-jdk5'
}
}
}
69 changes: 59 additions & 10 deletions android/robot/src/main/java/org/openbot/OpenBotApplication.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package org.openbot;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;

import org.jetbrains.annotations.NotNull;
import org.openbot.vehicle.Vehicle;

import java.util.Locale;

import timber.log.Timber;

public class OpenBotApplication extends Application {

static Context context;
private static Context context;
public static Vehicle vehicle;

public static Context getContext() {
Expand All @@ -23,21 +32,61 @@ public void onCreate() {
super.onCreate();
context = getApplicationContext();

// Retrieve selected language from SharedPreferences
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String languageCode = sharedPreferences.getString("language", "en"); // Default to English
setLocale(languageCode); // Apply the language setting

int baudRate = Integer.parseInt(sharedPreferences.getString("baud_rate", "115200"));
vehicle = new Vehicle(this, baudRate);
vehicle.initBle();
vehicle.connectUsb();
vehicle.initBle();

// Setup Timber for logging in debug mode
if (BuildConfig.DEBUG) {
Timber.plant(
new Timber.DebugTree() {
@NonNull
@Override
protected String createStackElementTag(@NotNull StackTraceElement element) {
return super.createStackElementTag(element) + ":" + element.getLineNumber();
}
});
Timber.plant(new Timber.DebugTree() {
@NonNull
@Override
protected String createStackElementTag(@NotNull StackTraceElement element) {
return super.createStackElementTag(element) + ":" + element.getLineNumber();
}
});
}
}

// Set the locale for the application
public void setLocale(String languageCode) {
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Resources resources = getResources();
Configuration config = resources.getConfiguration();

// Update the configuration based on the version
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
config.setLocale(locale);
config.setLayoutDirection(locale);
getApplicationContext().createConfigurationContext(config);
} else {
config.locale = locale;
config.setLayoutDirection(locale);
}

resources.updateConfiguration(config, resources.getDisplayMetrics());
}

// Method to dynamically refresh the app's language
public void refreshAppLanguage(String languageCode) {
// Save the new language preference
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("language", languageCode); // Store the new language code
editor.apply();

// Set the locale and refresh the UI
setLocale(languageCode);
if (context instanceof Activity) {
Activity activity = (Activity) context;
activity.recreate(); // Restart the activity to apply the new locale settings
}
}

Expand Down
32 changes: 21 additions & 11 deletions android/robot/src/main/java/org/openbot/model/Category.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
package org.openbot.model;

import android.content.Context;
import java.util.List;

public class Category {

private int titleResId; // Resource ID for the title
private List<SubCategory> subCategories;

// Default constructor
public Category() {}

public Category(String title, List<SubCategory> subCategories) {
this.title = title;
// Constructor accepting a resource ID for the title
public Category(int titleResId, List<SubCategory> subCategories) {
this.titleResId = titleResId;
this.subCategories = subCategories;
}

private String title;
private List<SubCategory> subCategories;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
// Getter method to retrieve the title as a string
public String getTitle(Context context) {
return context.getString(titleResId); // Fetches string resource
}

// Getter and setter for subCategories
public List<SubCategory> getSubCategories() {
return subCategories;
}

public void setSubCategories(List<SubCategory> subCategories) {
this.subCategories = subCategories;
}

// Getter and setter for titleResId
public int getTitleResId() {
return titleResId;
}

public void setTitleResId(int titleResId) {
this.titleResId = titleResId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.openbot.utils;

import android.content.Context;
import org.openbot.R;

public class ControlModeMapping {

private static final String TAG = "ControlModeMapping";

public static Enums.ControlMode getControlMode(Context context, String localizedTitle) {
// Match localized strings to the corresponding enums
if (localizedTitle.equals(context.getString(R.string.control_mode_gamepad))) {
return Enums.ControlMode.GAMEPAD;
} else if (localizedTitle.equals(context.getString(R.string.control_mode_phone))) {
return Enums.ControlMode.PHONE;
} else if (localizedTitle.equals(context.getString(R.string.control_mode_webserver))) {
return Enums.ControlMode.WEBSERVER;
} else if (localizedTitle.equals(context.getString(R.string.control_mode_manette))) {
return Enums.ControlMode.MANETTE;
}

// Fallback case with logging
android.util.Log.e("ControlModeMapping", "Unhandled ControlMode: " + localizedTitle);
return Enums.ControlMode.GAMEPAD; // Provide a safe default
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.openbot.utils;

import android.content.Context;
import org.openbot.R;

public class DriveModeMapping {

private static final String TAG = "DriveModeMapping";

public static Enums.DriveMode getDriveMode(Context context, String localizedTitle) {
// Match localized strings to the corresponding enums
if (localizedTitle.equals(context.getString(R.string.drive_mode_gamepad))) {
return Enums.DriveMode.GAME;
} else if (localizedTitle.equals(context.getString(R.string.drive_mode_joystick))) {
return Enums.DriveMode.JOYSTICK;
} else if (localizedTitle.equals(context.getString(R.string.drive_mode_dual))) {
return Enums.DriveMode.DUAL;
}

// Fallback case with logging
android.util.Log.e("DriveModeMapping", "Unhandled DriveMode: " + localizedTitle);
return Enums.DriveMode.GAME; // Provide a safe default
}
}

Loading