1 package org.mamedev.mame;
2 
3 import java.io.*;
4 import android.app.*;
5 import android.os.*;
6 import android.content.res.AssetManager;
7 import android.util.Log;
8 import org.libsdl.app.SDLActivity;
9 import android.view.*;
10 import android.content.pm.ActivityInfo;
11 import android.content.res.Configuration;
12 /**
13     SDL Activity
14 */
15 public class MAME extends SDLActivity {
16     private static final String TAG = "MAME";
17     // Setup
18     @Override
onCreate(Bundle savedInstanceState)19     protected void onCreate(Bundle savedInstanceState) {
20 		super.onCreate(savedInstanceState);
21 		copyAssetAll("mame.ini");
22 		copyAssetAll("ui.ini");
23 		copyAssetAll("roms");
24     }
25 
copyAssetAll(String srcPath)26 	public void copyAssetAll(String srcPath) {
27 		AssetManager assetMgr = this.getAssets();
28 		String assets[] = null;
29 		try {
30 			String destPath = getExternalFilesDir(null) + File.separator + srcPath;
31 			assets = assetMgr.list(srcPath);
32 			if (assets.length == 0) {
33 				copyFile(srcPath, destPath);
34 			} else {
35 				File dir = new File(destPath);
36 				if (!dir.exists())
37 					dir.mkdir();
38 				for (String element : assets) {
39 					copyAssetAll(srcPath + File.separator + element);
40 				}
41 			}
42 		}
43 		catch (IOException e) {
44 		   e.printStackTrace();
45 		}
46 	}
copyFile(String srcFile, String destFile)47 	public void copyFile(String srcFile, String destFile) {
48 		AssetManager assetMgr = this.getAssets();
49 
50 		InputStream is = null;
51 		OutputStream os = null;
52 		try {
53 			is = assetMgr.open(srcFile);
54 			if (new File(destFile).exists() == false)
55 			{
56 				os = new FileOutputStream(destFile);
57 
58 				byte[] buffer = new byte[1024];
59 				int read;
60 				while ((read = is.read(buffer)) != -1) {
61 					os.write(buffer, 0, read);
62 				}
63 				is.close();
64 				os.flush();
65 				os.close();
66 				Log.v(TAG, "copy from Asset:" + destFile);
67 			}
68 		}
69 		catch (IOException e) {
70 			e.printStackTrace();
71 		}
72 	}
73 
74     @Override
dispatchKeyEvent(KeyEvent event)75     public boolean dispatchKeyEvent(KeyEvent event) {
76 
77         int keyCode = event.getKeyCode();
78         // Ignore certain special keys so they're handled by Android
79         if (((event.getSource() & InputDevice.SOURCE_CLASS_BUTTON) != 0) && (keyCode == KeyEvent.KEYCODE_BACK)) {
80 			android.os.Process.killProcess(android.os.Process.myPid());
81         }
82         return super.dispatchKeyEvent(event);
83     }
84 	}
85