085b5e9223dc1c0300dfbcf880c1b5450c73cbd8
[ouya-romlauncher.git] / src / de / cweiske / ouya / gamelauncher / LaunchGameActivity.java
1 /*
2  * Copyright (C) 2012 OUYA, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package de.cweiske.ouya.gamelauncher;
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24
25 import android.app.Activity;
26 import android.content.ComponentName;
27 import android.content.Intent;
28 import android.content.res.AssetManager;
29 import android.net.Uri;
30 import android.os.Bundle;
31 import android.os.Environment;
32
33 public class LaunchGameActivity extends Activity
34 {
35     String assetFilename = "FIXME_ASSETFILENAME";
36         
37     /**
38      * Called when the activity is first created.
39      */
40     @Override
41     public void onCreate(Bundle savedInstanceState)
42     {
43         super.onCreate(savedInstanceState);
44         
45         File extractedFile = extractFile(this.assetFilename);
46         if (extractedFile == null) {
47                 System.err.println("Failed to extract file " + this.assetFilename);
48                 finish();
49                 return;
50         }
51
52         Intent intent = new Intent();
53         intent.setComponent(new ComponentName("com.explusalpha.Snes9xPlus", "com.imagine.BaseActivity"));
54         intent.setAction("android.intent.action.VIEW");
55         intent.setData(Uri.fromFile(extractedFile));
56         
57                 startActivity(intent);
58                 finish();
59                 return;
60     }
61
62         protected File extractFile(String assetFilename)
63         {
64                 //File cacheDir = this.getExternalCacheDir();
65                 //File cacheDir = getExternalFilesDir(null);
66                 File cacheDir = Environment.getExternalStorageDirectory();
67                 if (cacheDir == null) {
68                         return null;
69                 }
70                 File dir = new File(
71                         cacheDir.toString()
72                         + File.separator
73                         + "gamelauncher"
74                 );
75                 if (!dir.exists()) {
76                         dir.mkdirs();
77                 }
78                 
79                 File targetFile = new File(dir.toString() + File.separator + assetFilename);
80                 if (targetFile.exists()) {
81                         return targetFile;
82                 }
83                 
84                 AssetManager assetManager = this.getAssets();
85                 try {
86                     InputStream in = assetManager.open("game/" + assetFilename);
87                     copyFile(in, new FileOutputStream(targetFile));
88                     return targetFile;
89                 } catch (IOException e) {
90                     e.printStackTrace();
91                 }
92         return null;
93         }
94         
95         protected void copyFile(InputStream in, OutputStream out) throws IOException
96         {
97             byte[] buffer = new byte[1024];
98             int read;
99             while((read = in.read(buffer)) != -1){
100               out.write(buffer, 0, read);
101             }
102         }
103 }