JaRMoS  1.1
Java Reduced Model Simulations
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
MainActivity.java
Go to the documentation of this file.
1 package jarmos.app.activity;
2 
3 import jarmos.app.Const;
4 import jarmos.app.R;
7 
8 import java.lang.reflect.Method;
9 import java.net.MalformedURLException;
10 import java.net.URL;
11 
12 import android.app.Activity;
13 import android.app.AlertDialog;
14 import android.app.Dialog;
15 import android.content.DialogInterface;
16 import android.content.Intent;
17 import android.content.SharedPreferences;
18 import android.os.Bundle;
19 import android.util.Log;
20 import android.view.Menu;
21 import android.view.MenuInflater;
22 import android.view.MenuItem;
23 import android.view.View;
24 import android.widget.Button;
25 import android.widget.EditText;
26 import android.widget.Toast;
27 
39 public class MainActivity extends Activity {
40 
44  public static final int DOWNLOAD_DIALOG_ID = 1;
45 
49  public static final int NO_SD_ID = 2;
50 
54  public static final int OPTIONS_ID = 3;
55 
59  // public static AModelManager modelmng;
60 
61  private Dialog downloadDialog;
62 
64  @Override
65  public void onCreate(Bundle savedInstanceState) {
66  super.onCreate(savedInstanceState);
67  setContentView(R.layout.mainpage);
68 
69  Const.APP_DATA_DIRECTORY = getApplicationInfo().dataDir + "/files";
70 
71  // testwas();
72 
73  // Add listener to the Solve button
74  Button btn = (Button) findViewById(R.id.btnAssets);
75  btn.setOnClickListener(new View.OnClickListener() {
76  public void onClick(View view) {
77  Intent intent = new Intent(MainActivity.this, ModelListActivity.class);
78  intent.putExtra(Const.EXTRA_MODELMANAGER_CLASSNAME, "AssetModelManager");
79  // intent.putExtras(getIntent().getExtras());
80  startActivityForResult(intent, 0);
81  }
82  });
83  btn = (Button) findViewById(R.id.btnSDCard);
84  btn.setOnClickListener(new View.OnClickListener() {
85  public void onClick(View view) {
86 
87  if (!SDModelManager.ensureSDDir()) {
88  showDialog(NO_SD_ID);
89  return;
90  }
91 
92  Intent intent = new Intent(MainActivity.this, ModelListActivity.class);
93  intent.putExtra(Const.EXTRA_MODELMANAGER_CLASSNAME, "SDModelManager");
94  startActivityForResult(intent, 0);
95  }
96  });
97  Button ibtn = (Button) findViewById(R.id.btnDownload);
98  ibtn.setOnClickListener(new View.OnClickListener() {
99  public void onClick(View view) {
100  showDialog(DOWNLOAD_DIALOG_ID);
101  }
102  });
103  }
104 
105  @Override
106  protected Dialog onCreateDialog(int id) {
107  Dialog dialog;
108  AlertDialog.Builder builder = new AlertDialog.Builder(this);
109  switch (id) {
110  case DOWNLOAD_DIALOG_ID:
111  downloadDialog = new Dialog(this);
112  downloadDialog.setContentView(R.layout.download_dialog);
113  downloadDialog.setTitle("Connect to server");
114  downloadDialog.setCancelable(false);
115 
116  // When the download button is pressed, we read the offline_data
117  // files from the specified URL
118  Button downloadButton = (Button) downloadDialog.findViewById(R.id.downloadButton);
119  downloadButton.setOnClickListener(new View.OnClickListener() {
120 
121  public void onClick(View view) {
122 
123  // Get the directory_name from the EditText object
124  EditText urlEntry = (EditText) downloadDialog.findViewById(R.id.urlEntry);
125 
126  URL u = null;
127  try {
128  // Add forwardslash if not entered
129  // if (!url.endsWith("/")) url += "/";
130  u = new URL(urlEntry.getText().toString().trim());
131  } catch (MalformedURLException e) {
132  Toast.makeText(MainActivity.this, R.string.invalidURL, Toast.LENGTH_SHORT);
133  return;
134  }
135 
136  // Dismiss the URL specification dialog
137  dismissDialog(DOWNLOAD_DIALOG_ID);
138 
139  Intent intent = new Intent(MainActivity.this, ModelListActivity.class);
140  intent.putExtra(Const.EXTRA_MODELMANAGER_CLASSNAME, "WebModelManager");
141  intent.putExtra("URL", u);
142  intent.putExtra("modelCaching", true);
143 
144  startActivityForResult(intent, 0);
145  }
146  });
147 
148  // Add listener to cancel download
149  Button quitDownloadButton = (Button) downloadDialog.findViewById(R.id.quitDownloadButton);
150  quitDownloadButton.setOnClickListener(new View.OnClickListener() {
151 
152  public void onClick(View view) {
153  dismissDialog(1);
154  }
155 
156  });
157 
158  dialog = downloadDialog;
159  break;
160  case NO_SD_ID:
161  builder.setMessage("Could not ensure rbappmit-SDcard folder.").setCancelable(false)
162  .setNeutralButton("OK", new DialogInterface.OnClickListener() {
163  public void onClick(DialogInterface dialog, int id) {
164  dialog.dismiss();
165  }
166  });
167  dialog = builder.create();
168  break;
169  case OPTIONS_ID:
170  final SharedPreferences p = getSharedPreferences(Const.PREFERENCES_FILENAME, 0);
171  boolean[] bools = new boolean[] { p.getBoolean(Const.PREF_MODELCACHING, false),
172  p.getBoolean(Const.PREF_MODELCACHING_OVERWRITE, false) };
173 
174  builder = new AlertDialog.Builder(MainActivity.this);
175  builder.setTitle("JaRMoSA options");
176  builder.setMultiChoiceItems(new String[] { "Enable model caching", "Overwrite existing models" }, bools,
177  new DialogInterface.OnMultiChoiceClickListener() {
178  public void onClick(DialogInterface dialog, int item, boolean checked) {
179  if (item == 0) {
180  p.edit().putBoolean(Const.PREF_MODELCACHING, checked).commit();
181  } else {
182  p.edit().putBoolean(Const.PREF_MODELCACHING_OVERWRITE, checked).commit();
183  }
184  }
185  });
186  builder.setNeutralButton("OK", new DialogInterface.OnClickListener() {
187  public void onClick(DialogInterface dialog, int item) {
188  dialog.dismiss();
189  }
190  });
191  dialog = builder.create();
192  break;
193  default:
194  dialog = null;
195  break;
196  }
197  return dialog;
198  }
199 
200  /*
201  * (non-Javadoc)
202  *
203  * @see android.app.Activity#onMenuItemSelected(int, android.view.MenuItem)
204  */
205  @Override
206  public boolean onMenuItemSelected(int featureId, MenuItem item) {
207  if (item.getItemId() == R.id.mm_settings) {
208  showDialog(OPTIONS_ID);
209  return true;
210  } else {
211  return super.onMenuItemSelected(featureId, item);
212  }
213  }
214 
215  // populate main menu
216  @Override
217  public boolean onPrepareOptionsMenu(Menu menu) {
218  menu.clear();
219  MenuInflater inflater = getMenuInflater();
220  inflater.inflate(R.menu.main_menu, menu);
221  return true;
222  }
223 
224  @SuppressWarnings("unused")
225  private void testwas() {
226  AssetModelManager m = new AssetModelManager(getApplicationContext());
227  try {
228  m.useModel("aghdemo");
229 
230  ClassLoader cl = m.getClassLoader();
231  Class<?> c = cl.loadClass("AffineFunctions");
232  c = cl.loadClass("AffineFunctions");
233 
234  Object ci = c.newInstance();
235  int a = 0;
236 
237  Method meth = c.getMethod("get_n_F_functions");
238  Object res = meth.invoke(ci);
239  a = (Integer) res;
240  System.out.println("a=" + a);
241 
242  m.useModel("demo1");
243  ClassLoader cl2 = m.getClassLoader();
244 
245  c = cl.loadClass("AffineFunctions");
246  ci = c.newInstance();
247  res = meth.invoke(ci);
248  a = (Integer) res;
249  System.out.println("a=" + a);
250 
251  c = cl2.loadClass("AffineFunctions");
252  meth = c.getMethod("get_n_F_functions");
253  ci = c.newInstance();
254  res = meth.invoke(ci);
255  a = (Integer) res;
256  System.out.println("a=" + a);
257 
258  } catch (Exception e) {
259  Log.e("testwas", "DOOMED!", e);
260  e.printStackTrace();
261  finish();
262  }
263  }
264 
265  // private void startMMService(String classname, String extra) {
266  // Intent si = new Intent(getApplicationContext(),
267  // ModelManagerService.class);
268  // si.putExtra(ModelManagerService.CLASSNAME_EXTRA, classname);
269  // if (extra != null) {
270  // si.putExtra("URL", extra);
271  // }
272  // if (startService(si) == null) {
273  // Log.e("MainActivity", "Starting the ModelManagerService failed.");
274  // }
275  // }
276 }
static final String PREF_MODELCACHING
The name for the preference storing information about whether models should be cached when loaded fro...
Definition: Const.java:64
Class for model loading from the local SD card of the mobile device running android.
static final String PREFERENCES_FILENAME
The filename for the application preferences.
Definition: Const.java:58
This is the main activity class and entry point of the app.
Class to load models from application assets.
void onCreate(Bundle savedInstanceState)
Called when the activity is first created.
boolean onMenuItemSelected(int featureId, MenuItem item)
This activity generates a simple ListView to allow selection of a model.
boolean onPrepareOptionsMenu(Menu menu)
static final String EXTRA_MODELMANAGER_CLASSNAME
The string describing the class name which denotes the corret AModelManager subclass to be instantiat...
Definition: Const.java:48
static final int NO_SD_ID
Dialog ID for the &quot;no sd card access&quot; dialog.
static final int DOWNLOAD_DIALOG_ID
Dialog ID for the model download url dialog.
Class that contains miscellaneous JaRMoS specific constants and static functions. ...
Definition: Const.java:26
static final String PREF_MODELCACHING_OVERWRITE
The name for the preference storing information about whether existing model data is overwritten when...
Definition: Const.java:70
static final int OPTIONS_ID
Options dialog ID.