Skip to content

Commit

Permalink
LanguageActivity: Reset to default
Browse files Browse the repository at this point in the history
  • Loading branch information
ma1co committed Apr 30, 2016
1 parent dacc7cb commit 48edb2e
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class Backup {
private static native void nativeWrite(int id, byte[] data) throws NativeException;
private static native void nativeSync() throws NativeException;
private static native void nativeSetId1(int value) throws NativeException;
private static native byte[] nativeReadPresetData() throws NativeException;

private static File settingDir = new File("/setting");
private static File backupBinFile = new File(settingDir, "Backup.bin");
Expand All @@ -37,6 +38,10 @@ public static void cleanup() throws IOException {
throw new IOException("Cannot delete setting dir");
}

public static BackupFile readData() throws IOException, NativeException {
return new BackupFile(nativeReadPresetData());
}

public static void setProtection(boolean protect) throws IOException, NativeException {
nativeSetId1(protect ? 1 : 0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.ma1co.openmemories.tweak;

import java.io.File;
import java.io.IOException;

public class BackupFile extends Binary {
Expand All @@ -9,11 +8,6 @@ public BackupFile(byte[] data) throws IOException {
checkData();
}

public BackupFile(File file) throws IOException {
super(file);
checkData();
}

private void checkData() throws IOException {
if (getLength() < 0x100)
throw new IOException("Data too short");
Expand All @@ -25,8 +19,4 @@ private void checkData() throws IOException {
public String getRegion() {
return readCString(0xC0);
}

public void setRegion(String region) {
writeCString(0xC0, region);
}
}
18 changes: 0 additions & 18 deletions app/src/main/java/com/github/ma1co/openmemories/tweak/Binary.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.github.ma1co.openmemories.tweak;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

public class Binary {
Expand All @@ -12,13 +9,6 @@ public Binary(byte[] data) {
this.data = data;
}

public Binary(File file) throws IOException {
FileInputStream f = new FileInputStream(file);
data = new byte[(int) file.length()];
f.read(data);
f.close();
}

public byte[] getData() {
return data;
}
Expand All @@ -34,15 +24,7 @@ public String readCString(int offset) {
return new String(data, offset, length);
}

public void writeCString(int offset, String str) {
writeBytes(offset, (str + "\u0000").getBytes());
}

public byte[] readBytes(int offset, int length) {
return Arrays.copyOfRange(data, offset, offset + length);
}

public void writeBytes(int offset, byte[] bytes) {
System.arraycopy(bytes, 0, data, offset, bytes.length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@
import android.widget.TextView;

public class LanguageActivity extends BaseActivity {
public static final int[] LANG_ALL = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
public static final int[] LANG_APAC = new int[] { 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 2, 2, 1, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
public static final int[] LANG_CN = new int[] { 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
public static final int[] LANG_EU = new int[] { 1, 2, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 2, 1, 2, 2, 1, 2, 2, 2, 2 };
public static final int[] LANG_JP = new int[] { 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
public static final int[] LANG_US = new int[] { 1, 2, 1, 2, 1, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };

public static int[] getLangsForRegion(String r) {
if ("ALLLANG".equals(r))
return LANG_ALL;
else if ("AP2".equals(r) || "IN5".equals(r))
return LANG_APAC;
else if ("CEC".equals(r) || "CE".equals(r) || "RU2".equals(r))
return LANG_EU;
else if ("CN2".equals(r) || "E38".equals(r) || "JE3".equals(r) || "KR2".equals(r) || "TW6".equals(r))
return LANG_CN;
else if ("J1".equals(r))
return LANG_JP;
else if ("UC2".equals(r))
return LANG_US;
else
return null;
}

public static final int LANGUAGE_ENABLED = 1;
public static final int LANGUAGE_DISABLED = 2;

Expand All @@ -24,7 +48,7 @@ protected void onResume() {
}

protected void readCheck() throws BackupCheckException {
checkBackupByteValues(BackupKeys.LANGUAGE_ACTIVE_LIST, 1, 2);
checkBackupByteValues(BackupKeys.LANGUAGE_ACTIVE_LIST, LANGUAGE_ENABLED, LANGUAGE_DISABLED);
}

protected void writeCheck() throws BackupCheckException {
Expand All @@ -38,11 +62,9 @@ protected boolean[] readActiveLanguages() throws NativeException {
return langs;
}

protected void writeActiveLanguages(boolean[] langs) throws NativeException {
for (int i = 0; i < BackupKeys.LANGUAGE_ACTIVE_LIST.length; i++) {
int value = langs[i] ? LANGUAGE_ENABLED : LANGUAGE_DISABLED;
Backup.setValue(BackupKeys.LANGUAGE_ACTIVE_LIST[i], new byte[]{(byte) value});
}
protected void writeActiveLanguages(int[] langs) throws NativeException {
for (int i = 0; i < BackupKeys.LANGUAGE_ACTIVE_LIST.length; i++)
Backup.setValue(BackupKeys.LANGUAGE_ACTIVE_LIST[i], new byte[]{(byte) langs[i]});
}

protected void showActiveLanguages() {
Expand All @@ -62,7 +84,7 @@ protected void showActiveLanguages() {
}
}

protected void setActiveLanguages(boolean[] langs) {
protected void setActiveLanguages(int[] langs) {
try {
Logger.info("setActiveLanguages", "attempting to write active languages");
readCheck();
Expand All @@ -76,22 +98,27 @@ protected void setActiveLanguages(boolean[] langs) {
}
}

public void onActivateJapaneseButtonClicked(View view) {
boolean[] langs = new boolean[BackupKeys.LANGUAGE_ACTIVE_LIST.length];
langs[1] = true;
setActiveLanguages(langs);
protected void resetActiveLanguages() {
try {
Logger.info("resetActiveLanguages", "attempting to read region");
String region = Backup.readData().getRegion();
Logger.info("resetActiveLanguages", "region is " + region);
int[] langs = getLangsForRegion(region.split("_")[1]);
if (langs == null)
throw new BackupCheckException("Unknown region: " + region);
setActiveLanguages(langs);
Logger.info("resetActiveLanguages", "done");
} catch (Exception e) {
Logger.error("resetActiveLanguages", e);
showError(e);
}
}

public void onActivateEnglishButtonClicked(View view) {
boolean[] langs = new boolean[BackupKeys.LANGUAGE_ACTIVE_LIST.length];
langs[0] = true;
setActiveLanguages(langs);
public void onActivateAllButtonClicked(View view) {
setActiveLanguages(LANG_ALL);
}

public void onActivateAllButtonClicked(View view) {
boolean[] langs = new boolean[BackupKeys.LANGUAGE_ACTIVE_LIST.length];
for (int i = 0; i < langs.length; i++)
langs[i] = true;
setActiveLanguages(langs);
public void onResetButtonClicked(View view) {
resetActiveLanguages();
}
}
29 changes: 24 additions & 5 deletions app/src/main/jni/backup.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,39 @@ int32_t Backup_protect(uint32_t mode, void *overwrite_data, uint32_t size)
return backup_sync_msg(25, 3, mode, overwrite_data, size);
}

int32_t Backup_senser_cmd_ID1(uint32_t value)
uint32_t Backup_senser_sync_msg(uint16_t function, uint32_t arg0, uint32_t arg1, uint32_t *ret1, uint32_t *ret2)
{
const uint32_t type = 0x3E0166;
const uint32_t len = 0x24;
const uint32_t len = 0x140;

uint32_t *msg;
osal_valloc_msg_wait(type, (void **) &msg, len, 1);
memset(msg, 0, len);
msg[6] = 0xF0603;
msg[7] = value;
msg[0] = arg0;
msg[6] = (function << 16) | 0x603;
msg[7] = arg1;

osal_snd_sync_msg(type, msg);
uint32_t result = msg[0];
if (ret1) *ret1 = msg[2];
if (ret2) *ret2 = msg[3];

osal_free_msg(type, msg);
return result == 0x108 ? 0 : -1;
return result;
}

int32_t Backup_senser_cmd_ID1(uint32_t value)
{
return Backup_senser_sync_msg(0xF, 0, value, NULL, NULL) == 0x108 ? 0 : -1;
}

int32_t Backup_senser_cmd_preset_data_read(void **ptr, uint32_t *len)
{
if (ptr && !*ptr) {
// alloc & read
return Backup_senser_sync_msg(5, 1, 1, (void *) ptr, len) == 0x108 ? 0 : -1;
} else {
// free
return Backup_senser_sync_msg(5, 0x10, 0, (void *) ptr, len) == 0x120 ? 0 : -1;
}
}
1 change: 1 addition & 0 deletions app/src/main/jni/backup.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ int32_t Backup_write(uint32_t id, void *addr);
void Backup_sync_all();
int32_t Backup_protect(uint32_t mode, void *overwrite_data, uint32_t size);
int32_t Backup_senser_cmd_ID1(uint32_t value);
int32_t Backup_senser_cmd_preset_data_read(void **ptr, uint32_t *len);
25 changes: 25 additions & 0 deletions app/src/main/jni/jni.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,31 @@ void Java_com_github_ma1co_openmemories_tweak_Backup_nativeSetId1(JNIEnv *env, j
throw_exception(env, "Backup_senser_cmd_ID1 failed");
}

jbyteArray Java_com_github_ma1co_openmemories_tweak_Backup_nativeReadPresetData(JNIEnv *env, jclass clazz)
{
void *data = NULL;
uint32_t len = 0;
int32_t err = Backup_senser_cmd_preset_data_read(&data, &len);
if (err)
throw_exception(env, "Backup_senser_cmd_preset_data_read (read) failed");

jbyteArray res = (*env)->NewByteArray(env, len);
jbyte *res_ptr = (*env)->GetByteArrayElements(env, res, NULL);

FILE *f = fopen("/dev/mem", "rb");
fseek(f, (long) data, SEEK_SET);
fread(res_ptr, 1, len, f);
fclose(f);

(*env)->ReleaseByteArrayElements(env, res, res_ptr, 0);

err = Backup_senser_cmd_preset_data_read(&data, &len);
if (err)
throw_exception(env, "Backup_senser_cmd_preset_data_read (free) failed");

return res;
}

void Java_com_github_ma1co_openmemories_tweak_Shell_nativeExec(JNIEnv *env, jclass clazz, jstring command)
{
const char *command_ptr = (*env)->GetStringUTFChars(env, command, NULL);
Expand Down
19 changes: 6 additions & 13 deletions app/src/main/res/layout/activity_language.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,17 @@
android:id="@+id/langTextView"/>

<Button
android:layout_width="250sp"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Activate Japanese only (hide menu)"
android:onClick="onActivateJapaneseButtonClicked"/>

<Button
android:layout_width="250sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Activate English only (for testing)"
android:onClick="onActivateEnglishButtonClicked"/>
android:text="Activate all"
android:onClick="onActivateAllButtonClicked"/>

<Button
android:layout_width="250sp"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Activate all"
android:onClick="onActivateAllButtonClicked"/>
android:text="Reset to default"
android:onClick="onResetButtonClicked"/>

</LinearLayout>

0 comments on commit 48edb2e

Please sign in to comment.