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


Generated on Sat May 25 2019 05:00:52 for ResidualVM by doxygen 1.7.1
curved edge   curved edge