ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

ResidualVMActivity.java

Go to the documentation of this file.
00001 package org.residualvm.residualvm;
00002 
00003 import android.app.Activity;
00004 import android.app.AlertDialog;
00005 import android.content.Context;
00006 import android.content.DialogInterface;
00007 import android.content.Intent;
00008 import android.media.AudioManager;
00009 import android.net.Uri;
00010 import android.net.wifi.WifiManager;
00011 import android.net.wifi.WifiInfo;
00012 import android.os.Bundle;
00013 import android.os.Environment;
00014 import android.util.DisplayMetrics;
00015 import android.util.Log;
00016 import android.view.View;
00017 import android.view.KeyCharacterMap;
00018 import android.view.KeyEvent;
00019 import android.view.Menu;
00020 import android.view.MenuItem;
00021 import android.view.SurfaceView;
00022 import android.view.SurfaceHolder;
00023 import android.view.MotionEvent;
00024 import android.view.inputmethod.InputMethodManager;
00025 import android.widget.Toast;
00026 import android.widget.Button;
00027 import android.widget.HorizontalScrollView;
00028 import android.widget.ImageView;
00029 
00030 import tv.ouya.console.api.OuyaController;
00031 
00032 import java.io.File;
00033 
00034 public class ResidualVMActivity extends Activity {
00035 
00036 private boolean isBtnsShowing = false;
00037 
00038 public View.OnClickListener optionsBtnOnClickListener = new View.OnClickListener() {
00039         @Override
00040         public void onClick(View v) {
00041 
00042     if(!isBtnsShowing)
00043             ((HorizontalScrollView)findViewById(R.id.btns_scrollview)).setVisibility(View.VISIBLE);
00044     else
00045         ((HorizontalScrollView)findViewById(R.id.btns_scrollview)).setVisibility(View.GONE);
00046 
00047         isBtnsShowing = !isBtnsShowing;
00048 
00049         }
00050     };
00051 
00052     private void emulateKeyPress(int keyCode){
00053         _residualvm.pushEvent(ResidualVMEvents.JE_KEY, KeyEvent.ACTION_DOWN, keyCode, 0, 0, 0, 0);
00054         _residualvm.pushEvent(ResidualVMEvents.JE_KEY, KeyEvent.ACTION_UP, keyCode, 0, 0, 0, 0);
00055     }
00056 
00057 public View.OnClickListener menuBtnOnClickListener = new View.OnClickListener() {
00058         @Override
00059         public void onClick(View v) {
00060             emulateKeyPress(KeyEvent.KEYCODE_F1);
00061         }
00062     };
00063 
00064 public View.OnClickListener inventoryBtnOnClickListener = new View.OnClickListener() {
00065         @Override
00066         public void onClick(View v) {
00067             emulateKeyPress(KeyEvent.KEYCODE_I);
00068         }
00069     };
00070 
00071 public View.OnClickListener lookAtBtnOnClickListener = new View.OnClickListener() {
00072         @Override
00073         public void onClick(View v) {
00074             emulateKeyPress(KeyEvent.KEYCODE_E);
00075         }
00076     };
00077 
00078 public View.OnClickListener useBtnOnClickListener = new View.OnClickListener() {
00079         @Override
00080         public void onClick(View v) {
00081             emulateKeyPress(KeyEvent.KEYCODE_ENTER);
00082         }
00083     };
00084 
00085 public View.OnClickListener pickUpBtnOnClickListener = new View.OnClickListener() {
00086         @Override
00087         public void onClick(View v) {
00088             emulateKeyPress(KeyEvent.KEYCODE_P);
00089         }
00090     };
00091 
00092 
00093     private class MyResidualVM extends ResidualVM {
00094         private boolean usingSmallScreen() {
00095             // Multiple screen sizes came in with Android 1.6.  Have
00096             // to use reflection in order to continue supporting 1.5
00097             // devices :(
00098             DisplayMetrics metrics = new DisplayMetrics();
00099             getWindowManager().getDefaultDisplay().getMetrics(metrics);
00100 
00101             try {
00102                 // This 'density' term is very confusing.
00103                 int DENSITY_LOW = metrics.getClass().getField("DENSITY_LOW").getInt(null);
00104                 int densityDpi = metrics.getClass().getField("densityDpi").getInt(metrics);
00105                 return densityDpi <= DENSITY_LOW;
00106             } catch (Exception e) {
00107                 return false;
00108             }
00109         }
00110 
00111         public MyResidualVM(SurfaceHolder holder) {
00112             super(ResidualVMActivity.this.getAssets(), holder);
00113 
00114             // Enable ResidualVM zoning on 'small' screens.
00115             // FIXME make this optional for the user
00116             // disabled for now since it crops too much
00117             //enableZoning(usingSmallScreen());
00118         }
00119 
00120         @Override
00121         protected void getDPI(float[] values) {
00122             DisplayMetrics metrics = new DisplayMetrics();
00123             getWindowManager().getDefaultDisplay().getMetrics(metrics);
00124 
00125             values[0] = metrics.xdpi;
00126             values[1] = metrics.ydpi;
00127         }
00128 
00129         @Override
00130         protected void displayMessageOnOSD(String msg) {
00131             Log.i(LOG_TAG, "OSD: " + msg);
00132             Toast.makeText(ResidualVMActivity.this, msg, Toast.LENGTH_LONG).show();
00133         }
00134 
00135         @Override
00136         protected void openUrl(String url) {
00137             startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
00138         }
00139 
00140         @Override
00141         protected boolean isConnectionLimited() {
00142             WifiManager wifiMgr = (WifiManager)getSystemService(Context.WIFI_SERVICE);
00143             if (wifiMgr != null && wifiMgr.isWifiEnabled()) {
00144                 WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
00145                 return (wifiInfo == null || wifiInfo.getNetworkId() == -1); //WiFi is on, but it's not connected to any network
00146             }
00147             return true;
00148         }
00149 
00150         @Override
00151         protected void setWindowCaption(final String caption) {
00152             runOnUiThread(new Runnable() {
00153                     public void run() {
00154                         setTitle(caption);
00155                     }
00156                 });
00157         }
00158 
00159         @Override
00160         protected void showVirtualKeyboard(final boolean enable) {
00161             runOnUiThread(new Runnable() {
00162                     public void run() {
00163                         showKeyboard(enable);
00164                     }
00165                 });
00166         }
00167 
00168         @Override
00169         protected String[] getSysArchives() {
00170             return new String[0];
00171         }
00172 
00173     }
00174 
00175     private MyResidualVM _residualvm;
00176     private ResidualVMEvents _events;
00177     private Thread _residualvm_thread;
00178 
00179     @Override
00180     public void onCreate(Bundle savedInstanceState) {
00181         super.onCreate(savedInstanceState);
00182 
00183         setVolumeControlStream(AudioManager.STREAM_MUSIC);
00184 
00185         setContentView(R.layout.main);
00186         OuyaController.init(this);
00187         takeKeyEvents(true);
00188 
00189         // This is a common enough error that we should warn about it
00190         // explicitly.
00191         if (!Environment.getExternalStorageDirectory().canRead()) {
00192             new AlertDialog.Builder(this)
00193                 .setTitle(R.string.no_sdcard_title)
00194                 .setIcon(android.R.drawable.ic_dialog_alert)
00195                 .setMessage(R.string.no_sdcard)
00196                 .setNegativeButton(R.string.quit,
00197                                     new DialogInterface.OnClickListener() {
00198                                         public void onClick(DialogInterface dialog,
00199                                                             int which) {
00200                                             finish();
00201                                         }
00202                                     })
00203                 .show();
00204 
00205             return;
00206         }
00207 
00208         SurfaceView main_surface = (SurfaceView)findViewById(R.id.main_surface);
00209 
00210         main_surface.requestFocus();
00211 
00212         getFilesDir().mkdirs();
00213 
00214         // Store savegames on external storage if we can, which means they're
00215         // world-readable and don't get deleted on uninstall.
00216         String savePath = Environment.getExternalStorageDirectory() + "/ResidualVM/Saves/";
00217         File saveDir = new File(savePath);
00218         saveDir.mkdirs();
00219         if (!saveDir.isDirectory()) {
00220             // If it doesn't work, resort to the internal app path.
00221             savePath = getDir("saves", MODE_WORLD_READABLE).getPath();
00222         }
00223 
00224         // Start ResidualVM
00225         _residualvm = new MyResidualVM(main_surface.getHolder());
00226 
00227         _residualvm.setArgs(new String[] {
00228             "ResidualVM",
00229             "--config=" + getFileStreamPath("residualvmrc").getPath(),
00230             "--path=" + Environment.getExternalStorageDirectory().getPath(),
00231             "--gui-theme=modern",
00232             "--savepath=" + savePath
00233         });
00234 
00235         _events = new ResidualVMEvents(this, _residualvm);
00236 
00237         // On screen buttons listeners
00238         ((ImageView)findViewById(R.id.options)).setOnClickListener(optionsBtnOnClickListener);
00239         ((Button)findViewById(R.id.menu_btn)).setOnClickListener(menuBtnOnClickListener);
00240         ((Button)findViewById(R.id.inventory_btn)).setOnClickListener(inventoryBtnOnClickListener);
00241         ((Button)findViewById(R.id.use_btn)).setOnClickListener(useBtnOnClickListener);
00242         ((Button)findViewById(R.id.pick_up_btn)).setOnClickListener(pickUpBtnOnClickListener);
00243         ((Button)findViewById(R.id.look_at_btn)).setOnClickListener(lookAtBtnOnClickListener);
00244 
00245         main_surface.setOnKeyListener(_events);
00246         main_surface.setOnGenericMotionListener(_events);
00247 
00248         _residualvm_thread = new Thread(_residualvm, "ResidualVM");
00249         _residualvm_thread.start();
00250     }
00251     
00252     @Override
00253     public boolean onCreateOptionsMenu(Menu menu) {
00254         getMenuInflater().inflate(R.menu.game_menu, menu);
00255         return true;
00256     }
00257     
00258     @Override
00259     public boolean onOptionsItemSelected(MenuItem item) {
00260         switch (item.getItemId()) {
00261         case R.id.show_menu:
00262             return true;
00263         }
00264         return super.onOptionsItemSelected(item);
00265     }
00266 
00267     @Override
00268     public void onStart() {
00269         Log.d(ResidualVM.LOG_TAG, "onStart");
00270 
00271         super.onStart();
00272     }
00273 
00274     @Override
00275     public void onResume() {
00276         Log.d(ResidualVM.LOG_TAG, "onResume");
00277 
00278         super.onResume();
00279 
00280         if (_residualvm != null)
00281             _residualvm.setPause(false);
00282     }
00283 
00284     @Override
00285     public void onPause() {
00286         Log.d(ResidualVM.LOG_TAG, "onPause");
00287 
00288         super.onPause();
00289 
00290         if (_residualvm != null)
00291             _residualvm.setPause(true);
00292     }
00293 
00294     @Override
00295     public void onStop() {
00296         Log.d(ResidualVM.LOG_TAG, "onStop");
00297 
00298         super.onStop();
00299     }
00300 
00301     @Override
00302     public void onDestroy() {
00303         Log.d(ResidualVM.LOG_TAG, "onDestroy");
00304 
00305         super.onDestroy();
00306 
00307         if (_events != null) {
00308             _events.sendQuitEvent();
00309 
00310             try {
00311                 // 1s timeout
00312                 _residualvm_thread.join(1000);
00313             } catch (InterruptedException e) {
00314                 Log.i(ResidualVM.LOG_TAG, "Error while joining ResidualVM thread", e);
00315             }
00316 
00317             _residualvm = null;
00318         }
00319     }
00320 
00321     @Override
00322     public boolean onTrackballEvent(MotionEvent e) {
00323         if (_events != null)
00324             return _events.onTrackballEvent(e);
00325 
00326         return false;
00327     }
00328 
00329     @Override
00330     public boolean onTouchEvent(MotionEvent e) {
00331         if (_events != null)
00332             return _events.onTouchEvent(e);
00333 
00334         return false;
00335     }
00336 
00337     private void showKeyboard(boolean show) {
00338         SurfaceView main_surface = (SurfaceView)findViewById(R.id.main_surface);
00339         InputMethodManager imm = (InputMethodManager)
00340             getSystemService(INPUT_METHOD_SERVICE);
00341 
00342         if (show)
00343             imm.showSoftInput(main_surface, InputMethodManager.SHOW_IMPLICIT);
00344         else
00345             imm.hideSoftInputFromWindow(main_surface.getWindowToken(),
00346                                         InputMethodManager.HIDE_IMPLICIT_ONLY);
00347     }
00348 }


Generated on Sat Feb 16 2019 05:01:02 for ResidualVM by doxygen 1.7.1
curved edge   curved edge