New algorithm. Transliteration

master
ubulem 2016-02-11 16:26:38 +03:00
parent a721088e4d
commit 6b1e289987
5 changed files with 504 additions and 363 deletions

View File

@ -13,6 +13,16 @@ public class MCNativeActivity extends NativeActivity {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
Log.e("WTF", e.getLocalizedMessage());
}
}
}, 2000);
m_MessagReturnCode = -1; m_MessagReturnCode = -1;
m_MessageReturnValue = ""; m_MessageReturnValue = "";
} }

View File

@ -1,18 +1,5 @@
package mobi.MultiCraft; package mobi.MultiCraft;
import static mobi.MultiCraft.PreferencesHelper.TAG_BUILD_NUMBER;
import static mobi.MultiCraft.PreferencesHelper.TAG_SHORTCUT_CREATED;
import static mobi.MultiCraft.PreferencesHelper.getBuildNumber;
import static mobi.MultiCraft.PreferencesHelper.isCreateShortcut;
import static mobi.MultiCraft.PreferencesHelper.loadSettings;
import static mobi.MultiCraft.PreferencesHelper.saveSettings;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.Manifest; import android.Manifest;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
@ -35,7 +22,20 @@ import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.Toast; import android.widget.Toast;
import net.MultiCraft.Official.R;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static mobi.MultiCraft.PreferencesHelper.TAG_BUILD_NUMBER;
import static mobi.MultiCraft.PreferencesHelper.TAG_SHORTCUT_CREATED;
import static mobi.MultiCraft.PreferencesHelper.getBuildNumber;
import static mobi.MultiCraft.PreferencesHelper.isCreateShortcut;
import static mobi.MultiCraft.PreferencesHelper.loadSettings;
import static mobi.MultiCraft.PreferencesHelper.saveSettings;
public class MainActivity extends Activity { public class MainActivity extends Activity {
public final static String TAG = "Error"; public final static String TAG = "Error";
@ -46,10 +46,17 @@ public class MainActivity extends Activity {
public final static String NOMEDIA = ".nomedia"; public final static String NOMEDIA = ".nomedia";
private final static int REQUEST_STORAGE = 0; private final static int REQUEST_STORAGE = 0;
private ProgressDialog mProgressDialog; private ProgressDialog mProgressDialog;
private String dataFolder = "/Android/data/net.MultiCraft.Official/files/"; private String oldDataFolder = "/MultiCraft";
private String dataFolder = "/Android/data/mobi.MultiCraft/files/";
private String unzipLocation = Environment.getExternalStorageDirectory() + dataFolder; private String unzipLocation = Environment.getExternalStorageDirectory() + dataFolder;
private String oldUnzipLocation = Environment.getExternalStorageDirectory() + oldDataFolder;
private String oldWorldLocation = oldUnzipLocation + "/worlds";
private String oldGamesLocation = oldUnzipLocation + "/games/MultiCraft_game";
private String newWorldLocation = unzipLocation + "/worlds";
private String newGamesLocation = unzipLocation + "/games/MultiCraft_game";
private ProgressBar mProgressBar; private ProgressBar mProgressBar;
private Utilities util; private Utilities util;
private boolean isCopyOld = false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -100,14 +107,9 @@ public class MainActivity extends Activity {
draw = getResources().getDrawable(R.drawable.custom_progress_bar); draw = getResources().getDrawable(R.drawable.custom_progress_bar);
mProgressBar.setProgressDrawable(draw); mProgressBar.setProgressDrawable(draw);
util = new Utilities(); util = new Utilities();
util.createDirAndFiles(); util.createDataFolder();
if (getBuildNumber().equals("0")) {
saveSettings(TAG_BUILD_NUMBER, getString(R.string.ver));
util.startDeletion(true);
} else {
util.checkVersion(); util.checkVersion();
} }
}
private void requestPermissionAfterExplain() { private void requestPermissionAfterExplain() {
Toast.makeText(this, R.string.explain, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.explain, Toast.LENGTH_LONG).show();
@ -153,15 +155,12 @@ public class MainActivity extends Activity {
} }
public void runGame() { public void runGame() {
Intent intent = new Intent(MainActivity.this, MCNativeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
if (isFinishing())
return;
dismissProgressDialog();
util.deleteZip(FILES); util.deleteZip(FILES);
util.deleteZip(WORLDS); util.deleteZip(WORLDS);
util.deleteZip(GAMES); util.deleteZip(GAMES);
Intent intent = new Intent(MainActivity.this, MCNativeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
} }
private void startUnzipService(String[] file) throws IOException { private void startUnzipService(String[] file) throws IOException {
@ -180,10 +179,15 @@ public class MainActivity extends Activity {
if (progress >= 0) { if (progress >= 0) {
mProgressBar.setVisibility(View.VISIBLE); mProgressBar.setVisibility(View.VISIBLE);
mProgressBar.setProgress(progress); mProgressBar.setProgress(progress);
} else {
util.createNomedia();
if (isCopyOld) {
new CopyFolderTask().execute(new String[]{oldWorldLocation, newWorldLocation}, new String[]{oldGamesLocation, newGamesLocation});
} else { } else {
runGame(); runGame();
} }
} }
}
}; };
private class DeleteTask extends AsyncTask<String, Void, Void> { private class DeleteTask extends AsyncTask<String, Void, Void> {
@ -199,7 +203,7 @@ public class MainActivity extends Activity {
protected Void doInBackground(String... params) { protected Void doInBackground(String... params) {
location = params[0]; location = params[0];
for (String p : params) { for (String p : params) {
deleteFiles(p); util.deleteFiles(p);
} }
return null; return null;
} }
@ -216,17 +220,29 @@ public class MainActivity extends Activity {
} }
} }
private void deleteFiles(String path) {
File file = new File(path); }
if (file.exists()) {
String deleteCmd = "rm -r " + path; private class CopyFolderTask extends AsyncTask<String[], Void, Void> {
Runtime runtime = Runtime.getRuntime();
@Override
protected Void doInBackground(String[]... params) {
for (String[] p : params) {
File src = new File(p[0]);
File dest = new File(p[1]);
try { try {
runtime.exec(deleteCmd); util.copyDirectory(src, dest);
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, e.getLocalizedMessage()); Log.e(TAG, "copy failed: " + e.getMessage());
} }
} }
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
util.deleteFiles(oldUnzipLocation);
runGame();
} }
} }
@ -254,7 +270,7 @@ public class MainActivity extends Activity {
try { try {
startUnzipService(zips); startUnzipService(zips);
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, e.getMessage()); Log.e(TAG, "unzip failed: " + e.getMessage());
} }
} else } else
Toast.makeText(MainActivity.this, R.string.not_enough_space, Toast.LENGTH_LONG).show(); Toast.makeText(MainActivity.this, R.string.not_enough_space, Toast.LENGTH_LONG).show();
@ -298,18 +314,10 @@ public class MainActivity extends Activity {
* } * }
*/ */
private void createDirAndFiles() { private void createDataFolder() {
try {
File folder = new File(unzipLocation); File folder = new File(unzipLocation);
if (!(folder.exists())) if (!(folder.exists()))
folder.mkdirs(); folder.mkdirs();
File myFile = new File(unzipLocation, NOMEDIA);
if (!myFile.exists())
myFile.createNewFile();
// createLangFile();
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
} }
private void deleteZip(String fileName) { private void deleteZip(String fileName) {
@ -322,10 +330,9 @@ public class MainActivity extends Activity {
if (isAll) { if (isAll) {
new DeleteTask().execute(unzipLocation); new DeleteTask().execute(unzipLocation);
} else { } else {
new DeleteTask().execute(unzipLocation + "textures", unzipLocation + "games", unzipLocation + "builtin", new DeleteTask().execute(unzipLocation + "textures", unzipLocation + "games/MultiCraft", unzipLocation + "builtin",
unzipLocation + "fonts", unzipLocation + "debug.txt"); unzipLocation + "fonts", unzipLocation + "debug.txt");
} }
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@ -343,14 +350,20 @@ public class MainActivity extends Activity {
return availableSpace / SIZE_MB; return availableSpace / SIZE_MB;
} }
public void checkVersion() { private boolean isFolderEmpty(String folder) {
File location = new File(unzipLocation); File location = new File(folder);
File[] contents = location.listFiles(); File[] contents = location.listFiles();
if (contents == null) { return contents == null || contents.length == 0;
Log.e(TAG, "Wrong path to unzip"); }
} else if (contents.length == 1) {
public void checkVersion() {
if (isFolderEmpty(oldUnzipLocation) && isFolderEmpty(unzipLocation)) {
saveSettings(TAG_BUILD_NUMBER, getString(R.string.ver)); saveSettings(TAG_BUILD_NUMBER, getString(R.string.ver));
startDeletion(true); startDeletion(true);
} else if (!isFolderEmpty(oldUnzipLocation)) {
saveSettings(TAG_BUILD_NUMBER, getString(R.string.ver));
isCopyOld = true;
startDeletion(true);
} else if (getBuildNumber().equals(getString(R.string.ver))) { } else if (getBuildNumber().equals(getString(R.string.ver))) {
runGame(); runGame();
} else { } else {
@ -358,5 +371,62 @@ public class MainActivity extends Activity {
startDeletion(false); startDeletion(false);
} }
} }
public void copyDirectory(File sourceLocation, File targetLocation)
throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists() && !targetLocation.mkdirs()) {
throw new IOException("Cannot create dir " + targetLocation.getAbsolutePath());
}
String[] children = sourceLocation.list();
for (String aChildren : children) {
copyDirectory(new File(sourceLocation, aChildren),
new File(targetLocation, aChildren));
}
} else {
// make sure the directory we plan to store the recording in exists
File directory = targetLocation.getParentFile();
if (directory != null && !directory.exists() && !directory.mkdirs()) {
throw new IOException("Cannot create dir " + directory.getAbsolutePath());
}
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from in stream to out stream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
private void deleteFiles(String path) {
File file = new File(path);
if (file.exists()) {
String deleteCmd = "rm -r " + path;
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(deleteCmd);
} catch (IOException e) {
Log.e(TAG, "delete files failed: " + e.getLocalizedMessage());
}
}
}
public void createNomedia() {
File myFile = new File(unzipLocation, NOMEDIA);
if (!myFile.exists())
try {
myFile.createNewFile();
} catch (IOException e) {
Log.e(TAG, "nomedia has not been created: " + e.getMessage());
}
}
} }
} }

View File

@ -5,14 +5,12 @@ import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType; import android.text.InputType;
import android.text.Spanned;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
import android.view.View.OnKeyListener; import android.view.View.OnKeyListener;
import android.widget.EditText; import android.widget.EditText;
import net.MultiCraft.Official.R;
public class MultiCraftTextEntry extends Activity { public class MultiCraftTextEntry extends Activity {
public AlertDialog mTextInputDialog; public AlertDialog mTextInputDialog;
@ -40,17 +38,6 @@ public class MultiCraftTextEntry extends Activity {
mTextInputWidget.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); mTextInputWidget.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
} else { } else {
mTextInputWidget.setInputType(InputType.TYPE_CLASS_TEXT); mTextInputWidget.setInputType(InputType.TYPE_CLASS_TEXT);
mTextInputWidget.setFilters(new InputFilter[] { new InputFilter() {
public CharSequence filter(CharSequence src, int start, int end, Spanned dst, int dstart, int dend) {
if (src.equals("")) { // for backspace
return src;
}
if (!src.toString().matches("[à-ÿÀ-ß]+")) {
return src;
}
return "";
}
} });
} }
builder.setView(mTextInputWidget); builder.setView(mTextInputWidget);
@ -86,6 +73,9 @@ public class MultiCraftTextEntry extends Activity {
} }
public void pushResult(String text) { public void pushResult(String text) {
if (text.matches(".*[А-я].*")) {
text = Transliteration.toLatin(text);
}
Intent resultData = new Intent(); Intent resultData = new Intent();
resultData.putExtra("text", text); resultData.putExtra("text", text);
setResult(Activity.RESULT_OK, resultData); setResult(Activity.RESULT_OK, resultData);

View File

@ -0,0 +1,72 @@
package mobi.MultiCraft;
public class Transliteration {
private static final String[] CHAR_TABLE = new String[81];
private static final char START_CHAR = 'Ё';
static {
CHAR_TABLE['А' - START_CHAR] = "A";
CHAR_TABLE['Б' - START_CHAR] = "B";
CHAR_TABLE['В' - START_CHAR] = "V";
CHAR_TABLE['Г' - START_CHAR] = "G";
CHAR_TABLE['Д' - START_CHAR] = "D";
CHAR_TABLE['Е' - START_CHAR] = "E";
CHAR_TABLE['Ё' - START_CHAR] = "E";
CHAR_TABLE['Ж' - START_CHAR] = "ZH";
CHAR_TABLE['З' - START_CHAR] = "Z";
CHAR_TABLE['И' - START_CHAR] = "I";
CHAR_TABLE['Й' - START_CHAR] = "J";
CHAR_TABLE['К' - START_CHAR] = "K";
CHAR_TABLE['Л' - START_CHAR] = "L";
CHAR_TABLE['М' - START_CHAR] = "M";
CHAR_TABLE['Н' - START_CHAR] = "N";
CHAR_TABLE['О' - START_CHAR] = "O";
CHAR_TABLE['П' - START_CHAR] = "P";
CHAR_TABLE['Р' - START_CHAR] = "R";
CHAR_TABLE['С' - START_CHAR] = "TS";
CHAR_TABLE['Т' - START_CHAR] = "T";
CHAR_TABLE['У' - START_CHAR] = "U";
CHAR_TABLE['Ф' - START_CHAR] = "F";
CHAR_TABLE['Х' - START_CHAR] = "H";
CHAR_TABLE['Ц' - START_CHAR] = "C";
CHAR_TABLE['Ч' - START_CHAR] = "CH";
CHAR_TABLE['Ш' - START_CHAR] = "SH";
CHAR_TABLE['Щ' - START_CHAR] = "SHCH";
CHAR_TABLE['Ъ' - START_CHAR] = "";
CHAR_TABLE['Ы' - START_CHAR] = "Y";
CHAR_TABLE['Ь' - START_CHAR] = "";
CHAR_TABLE['Э' - START_CHAR] = "E";
CHAR_TABLE['Ю' - START_CHAR] = "U";
CHAR_TABLE['Я' - START_CHAR] = "YA";
for (int i = 0; i < CHAR_TABLE.length; i++) {
char idx = (char) ((char) i + START_CHAR);
char lower = new String(new char[]{idx}).toLowerCase().charAt(0);
if (CHAR_TABLE[i] != null) {
CHAR_TABLE[lower - START_CHAR] = CHAR_TABLE[i].toLowerCase();
}
}
}
/**
* Переводит русский текст в транслит. В результирующей строке
* каждая русская буква будет заменена на соответствующую английскую.
* Не русские символы останутся прежними.
*/
public static String toLatin(String text) {
char charBuffer[] = text.toCharArray();
StringBuilder sb = new StringBuilder(text.length());
for (char symbol : charBuffer) {
int i = symbol - START_CHAR;
if (i >= 0 && i < CHAR_TABLE.length) {
String replace = CHAR_TABLE[i];
sb.append(replace == null ? symbol : replace);
} else {
sb.append(symbol);
}
}
return sb.toString();
}
}

View File

@ -15,7 +15,6 @@ import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.util.Log; import android.util.Log;
import net.MultiCraft.Official.R;
public class UnzipService extends IntentService { public class UnzipService extends IntentService {
public final String TAG = UnzipService.class.getSimpleName(); public final String TAG = UnzipService.class.getSimpleName();