diff --git a/projects/mtg/Android/src/net/wagic/utils/DeckImporter.java b/projects/mtg/Android/src/net/wagic/utils/DeckImporter.java new file mode 100644 index 000000000..9cde32cd1 --- /dev/null +++ b/projects/mtg/Android/src/net/wagic/utils/DeckImporter.java @@ -0,0 +1,276 @@ +package net.wagic.utils; + +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.File; +import java.io.IOException; +import java.util.Scanner; + +import android.util.Log; + +public class DeckImporter +{ + + public static String importDeck( File f, String mypath, String activePath ) + { + String message = ""; + String deck = ""; + String deckname = ""; + if(f.exists() && !f.isDirectory()) + { + deckname = f.getName(); + int pos = deckname.lastIndexOf("."); + if (pos > 0) + { + deckname = deckname.substring(0, pos); + } + deck += "#NAME:"+deckname+"\n"; + try + { + Scanner scanner = new Scanner(new File(mypath)); + if (scanner.hasNext()) + { + while (scanner.hasNext()) + { + String line = scanner.nextLine(); + String[] slines = line.split("\\s+"); + String arranged = ""; + for(int idx = 1; idx < slines.length; idx++) + { + arranged += slines[idx] + " "; + } + if (isNumeric(slines[0]) && arranged != null) + { + if (slines[1] != null && slines[1].startsWith("[")) + { + arranged = arranged.substring(5); + slines[1] = slines[1].replaceAll("\\[", "").replaceAll("\\]",""); + deck += arranged + " (" + renameSet(slines[1]) + ") * " + slines[0] + "\n"; + } + else + { + deck += arranged + "(*) * " + slines[0] + "\n"; + } + } + } + File profile = new File(activePath+"/Res/settings/options.txt"); + if(profile.exists() && !profile.isDirectory()) + { + String profileName = getActiveProfile(profile); + if(profileName != "Missing!") + { + File rootProfiles = new File(activePath+"/Res/profiles/"+profileName); + if(rootProfiles.exists() && rootProfiles.isDirectory()) + { + //save deck + int countdeck = 1; + File[] files = rootProfiles.listFiles(); + for (int i = 0; i < files.length; i++) + {//check if there is available deck... + if(files[i].getName().startsWith("deck")) + countdeck++; + } + File toSave = new File(rootProfiles+"/deck"+countdeck+".txt"); + try + { + FileOutputStream fop = new FileOutputStream(toSave); + + // if file doesn't exists, then create it + if (!toSave.exists()) { + toSave.createNewFile(); + } + + // get the content in bytes + byte[] contentInBytes = deck.getBytes(); + + fop.write(contentInBytes); + fop.flush(); + fop.close(); + + message = "Import Deck Success!\n\n"+deck; + } + catch (IOException e) + { + message = e.getMessage(); + } + } + else + { + message = "Missing Folder!"; + } + } + } + else + { + message = "Invalid Profile!"; + } + } + else + { + message = "No errors, and file EMPTY"; + } + } + catch(IOException e) + { + message = e.getMessage(); + } + } + return message; + } + + private static boolean isNumeric(String input) + { + try + { + Integer.parseInt(input); + } + catch(NumberFormatException ex) + { + return false; + } + return true; + } + + private static String getActiveProfile(File mypath) + { + String name = ""; + try + { + Scanner scanner = new Scanner(new File(mypath.toString())); + if (scanner.hasNext()) + { + String line = scanner.nextLine(); + name = line.substring(8); + } + else + { + return "Missing!"; + } + } + catch(IOException e) + { + return "Missing!"; + } + return name; + } + + private static String renameSet(String set) + { + if (set == "AL") + return "ALL"; + if (set == "AQ") + return "ATQ"; + if (set == "AP") + return "APC"; + if (set == "AN") + return "ARN"; + if (set == "AE") + return "ARC"; + if (set == "BR") + return "BRB"; + if (set == "BD") + return "BTD"; + if (set == "CH") + return "CHR"; + if (set == "6E") + return "6ED"; + if (set == "CS") + return "CSP"; + if (set == "DS") + return "DST"; + if (set == "D2") + return "DD2"; + if (set == "8E") + return "8ED"; + if (set == "EX") + return "EXO"; + if (set == "FE") + return "FEM"; + if (set == "FD") + return "5DN"; + if (set == "5E") + return "5ED"; + if (set == "4E") + return "4ED"; + if (set == "GP") + return "GPT"; + if (set == "HL") + return "HML"; + if (set == "IA") + return "ICE"; + if (set == "IN") + return "INV"; + if (set == "JU") + return "JUD"; + if (set == "LG") + return "LEG"; + if (set == "LE") + return "LGN"; + if (set == "A") + return "LEA"; + if (set == "B") + return "LEB"; + if (set == "MM") + return "MMQ"; + if (set == "MI") + return "MIR"; + if (set == "MR") + return "MRD"; + if (set == "NE") + return "NEM"; + if (set == "9E") + return "9ED"; + if (set == "OD") + return "ODY"; + if (set == "ON") + return "ONS"; + if (set == "PS") + return "PLS"; + if (set == "PT") + return "POR"; + if (set == "P2") + return "P02"; + if (set == "P3") + return "PTK"; + if (set == "PR") + return "PPR"; + if (set == "PY") + return "PCY"; + if (set == "R") + return "RV"; + if (set == "SC") + return "SCG"; + if (set == "7E") + return "7ED"; + if (set == "ST") + return "S99"; + if (set == "ST2K") + return "S00"; + if (set == "SH") + return "STH"; + if (set == "TE") + return "TMP"; + if (set == "DK") + return "DRK"; + if (set == "TO") + return "TOR"; + if (set == "UG") + return "UGL"; + if (set == "U") + return "2ED"; + if (set == "UD") + return "UDS"; + if (set == "UL") + return "ULG"; + if (set == "US") + return "USG"; + if (set == "VI") + return "VIS"; + if (set == "WL") + return "WTH"; + else + return set; + } + +} diff --git a/projects/mtg/Android/src/net/wagic/utils/StorageOptions.java b/projects/mtg/Android/src/net/wagic/utils/StorageOptions.java index d462567ef..e97ac68f1 100644 --- a/projects/mtg/Android/src/net/wagic/utils/StorageOptions.java +++ b/projects/mtg/Android/src/net/wagic/utils/StorageOptions.java @@ -5,6 +5,10 @@ import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; +import java.util.HashSet; +import java.util.Set; +import android.os.Build; + import android.os.Environment; import android.util.Log; @@ -21,8 +25,10 @@ public class StorageOptions public static void determineStorageOptions() { initializeMountPoints(); + readMountsFileTest(); readMountsFile(); readVoldFile(); + removeDuplicates(mMounts); compareMountsWithVold(); testAndCleanMountsList(); setProperties(); @@ -40,6 +46,42 @@ public class StorageOptions } } + private static void readMountsFileTest() + { + /* + * Test mountpoints storage -kevlahnota + */ + + try + { + Scanner scanner = new Scanner(new File("/proc/mounts")); + while (scanner.hasNext()) + { + String line = scanner.nextLine(); + if (line.startsWith("/")) + { + String[] lineElements = line.split("\\s+"); + if ("vfat".equals(lineElements[2]) || "fuse".equals(lineElements[2])) + { + File mountPoint = new File(lineElements[1]); + if (!lineElements[1].equals(defaultMountPoint)) + if (mountPoint.isDirectory() && mountPoint.canRead()) + mMounts.add(lineElements[1]); + } + } + } + } catch (FileNotFoundException fnfex) + { + // if proc/mount doesn't exist we just use + Log.i(StorageOptions.class.getCanonicalName(), fnfex.getMessage() + ": assuming " + defaultMountPoint + " is the only mount point"); + mMounts.add(defaultMountPoint); + } catch (Exception e) + { + Log.e(StorageOptions.class.getCanonicalName(), e.getMessage() + ": unknown exception while reading mounts file"); + mMounts.add(defaultMountPoint); + } + } + private static void readMountsFile() { /* @@ -106,6 +148,23 @@ public class StorageOptions mMounts.add(defaultMountPoint); } } + + private static ArrayList removeDuplicates(ArrayList list) + { + ArrayList result = new ArrayList(); + + HashSet set = new HashSet(); + + for (String item : list) + { + if (!set.contains(item)) + { + result.add(item); + set.add(item); + } + } + return result; + } private static void compareMountsWithVold() { @@ -132,14 +191,24 @@ public class StorageOptions /* * Now that we have a cleaned list of mount paths Test each one to make sure it's a valid and available path. If it is not, remove it from the list. */ - + int t = 0; for (int i = 0; i < mMounts.size(); i++) { + t++; String mount = mMounts.get(i); File root = new File(mount); if (!root.exists() || !root.isDirectory() || !root.canWrite()) mMounts.remove(i--); } + + if (t == 0 && Build.VERSION.SDK_INT >= 19) + {//If none is found and build version is kitkat or higher + File root = new File("/storage/sdcard0"); + if (root.exists() && root.isDirectory() && root.canWrite()) + mMounts.add("/storage/sdcard0"); + if (isExternalStorageAvailable() && !isExternalStorageReadOnly()) + mMounts.add("/storage/sdcard1"); + } } private static void setProperties() @@ -153,10 +222,10 @@ public class StorageOptions int i = 1; for (String path : mMounts) { // TODO: /mnt/sdcard is assumed to always mean internal storage. Use this comparison until there is a better way to do this - if ("/mnt/sdcard".equalsIgnoreCase(path)) - mLabels.add("Built-in Storage"); + if ("/mnt/sdcard".equalsIgnoreCase(path) || "/storage/sdcard0".equalsIgnoreCase(path)) + mLabels.add("Internal SD " + "[" + path + "]"); else - mLabels.add("External SD Card " + i++); + mLabels.add("External SD " + " [" + path + "]"); } labels = new String[mLabels.size()]; @@ -171,4 +240,20 @@ public class StorageOptions // use and to prepare it for the next time it's needed. mMounts.clear(); } -} \ No newline at end of file + + private static boolean isExternalStorageReadOnly() { + String extStorageState = Environment.getExternalStorageState(); + if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) { + return true; + } + return false; + } + + private static boolean isExternalStorageAvailable() { + String extStorageState = Environment.getExternalStorageState(); + if (Environment.MEDIA_MOUNTED.equals(extStorageState)) { + return true; + } + return false; + } +} diff --git a/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java b/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java index 847475a48..1651d87d1 100644 --- a/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java +++ b/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java @@ -1,12 +1,20 @@ package org.libsdl.app; + import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; import java.io.InputStream; +import java.io.IOException; import java.io.OutputStream; import java.net.URL; import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Scanner; + + import javax.microedition.khronos.egl.EGL10; import javax.microedition.khronos.egl.EGLConfig; @@ -16,6 +24,7 @@ import javax.microedition.khronos.egl.EGLSurface; import net.wagic.app.R; import net.wagic.utils.StorageOptions; +import net.wagic.utils.DeckImporter; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; @@ -59,7 +68,10 @@ import android.widget.FrameLayout.LayoutParams; */ public class SDLActivity extends Activity implements OnKeyListener { - + //import deck globals + public ArrayList myresult = new ArrayList(); + public String myclickedItem = ""; + // TAG used for debugging in DDMS public static String TAG = Activity.class.getCanonicalName(); @@ -200,7 +212,61 @@ public class SDLActivity extends Activity implements OnKeyListener setStorage.create().show(); } + + private void importDeckOptions() + { + AlertDialog.Builder importDeck = new AlertDialog.Builder(this); + int index=internalPath.lastIndexOf('/'); + importDeck.setTitle("Choose Deck to Import:"); + File root = new File(internalPath.substring(0,index)+"/Download"); + File[] files = root.listFiles(); + for( File f : files) + { + if( !myresult.contains(f.toString()) && (f.toString().contains(".txt")||f.toString().contains(".dec"))) + myresult.add(f.toString()); + } + + //get first item? + if(!myresult.isEmpty()) + myclickedItem = myresult.get(0).toString(); + + importDeck.setSingleChoiceItems(myresult.toArray(new String[myresult.size()]), 0, new DialogInterface.OnClickListener() + { + public void onClick(DialogInterface dialog, int item) + { + myclickedItem = myresult.get(item).toString(); + } + }); + importDeck.setPositiveButton("Import Deck", new DialogInterface.OnClickListener() + { + public void onClick(DialogInterface dialog, int which) + { + processSelectedDeck( myclickedItem ); + if (mSurface == null) + mSingleton.initializeGame(); + } + }); + + importDeck.create().show(); + } + + private void processSelectedDeck(String mypath) + { + AlertDialog.Builder infoDialog = new AlertDialog.Builder(this); + infoDialog.setTitle("Imported Deck:"); + String activePath = sdcardPath; + if(activePath == ""){ + activePath = internalPath; + } + + File f = new File(mypath); + //Call the deck importer.... + String state = DeckImporter.importDeck(f, mypath, activePath); + infoDialog.setMessage(state); + infoDialog.show(); + } + private void checkStorageLocationPreference() { SharedPreferences settings = getSharedPreferences(kWagicSharedPreferencesKey, MODE_PRIVATE); @@ -334,7 +400,8 @@ public class SDLActivity extends Activity implements OnKeyListener public boolean onCreateOptionsMenu(Menu menu) { SubMenu settingsMenu = menu.addSubMenu(Menu.NONE, 1, 1, "Settings"); - menu.add(Menu.NONE, 2, 2, "About"); + menu.add(Menu.NONE, 2, 2, "Import"); + menu.add(Menu.NONE, 3, 3, "About"); settingsMenu.add(kStorageDataOptionsMenuId, kStorageDataOptionsMenuId, Menu.NONE, "Storage Data Options"); // buildStorageOptionsMenu(settingsMenu); @@ -350,6 +417,9 @@ public class SDLActivity extends Activity implements OnKeyListener { displayStorageOptions(); } else if (itemId == 2) + { + importDeckOptions(); + } else if (itemId == 3) { // display some info about the app AlertDialog.Builder infoDialog = new AlertDialog.Builder(this);