1 |
iliev |
2302 |
/* |
2 |
|
|
* JSampler - a java front-end for LinuxSampler |
3 |
|
|
* |
4 |
|
|
* Copyright (C) 2011 Grigor Iliev <grigor@grigoriliev.com> |
5 |
|
|
* |
6 |
|
|
* This file is part of JSampler. |
7 |
|
|
* |
8 |
|
|
* JSampler is free software; you can redistribute it and/or modify |
9 |
|
|
* it under the terms of the GNU General Public License version 2 |
10 |
|
|
* as published by the Free Software Foundation. |
11 |
|
|
* |
12 |
|
|
* JSampler is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
* GNU General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with JSampler; if not, write to the Free Software |
19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
20 |
|
|
* MA 02111-1307 USA |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
package org.jsampler.android; |
24 |
|
|
|
25 |
|
|
import java.util.TreeMap; |
26 |
|
|
|
27 |
|
|
import net.sf.juife.PDUtils; |
28 |
|
|
import net.sf.juife.impl.AndroidPDUtilsImpl; |
29 |
|
|
|
30 |
|
|
import org.jsampler.CC; |
31 |
|
|
import org.jsampler.HF; |
32 |
|
|
import org.jsampler.JSampler; |
33 |
|
|
import org.jsampler.android.view.std.ChooseBackendActivity; |
34 |
|
|
import org.jsampler.android.view.std.JSamplerHomeChooser; |
35 |
|
|
|
36 |
|
|
import android.app.Activity; |
37 |
|
|
import android.app.Dialog; |
38 |
|
|
import android.content.Intent; |
39 |
|
|
import android.os.Bundle; |
40 |
|
|
import android.util.Log; |
41 |
|
|
import android.view.GestureDetector; |
42 |
|
|
import android.view.GestureDetector.SimpleOnGestureListener; |
43 |
|
|
import android.view.Menu; |
44 |
|
|
import android.view.MenuInflater; |
45 |
|
|
import android.view.MenuItem; |
46 |
|
|
import android.view.MotionEvent; |
47 |
|
|
|
48 |
|
|
|
49 |
|
|
public class JSamplerActivity extends Activity { |
50 |
|
|
public static final int DLG_CHOOSE_HOME_DIR = 0; |
51 |
|
|
|
52 |
|
|
public static final int CHOOSE_BACKEND_REQUEST = 1000; |
53 |
|
|
|
54 |
|
|
private boolean started = false; |
55 |
|
|
|
56 |
|
|
private final TreeMap<Integer, Requestor> requestorMap = new TreeMap<Integer, Requestor> (); |
57 |
|
|
|
58 |
|
|
private GestureDetector gestureDetector; |
59 |
|
|
|
60 |
|
|
/** Called when the activity is first created. */ |
61 |
|
|
@Override |
62 |
|
|
public void onCreate(Bundle savedInstanceState) { |
63 |
|
|
super.onCreate(savedInstanceState); |
64 |
|
|
|
65 |
|
|
gestureDetector = new GestureDetector(this, gestureListener); |
66 |
|
|
|
67 |
|
|
//showDialog(DLG_ADD_BACKEND); |
68 |
|
|
|
69 |
|
|
AndroidPDUtilsImpl.activity = this; |
70 |
|
|
AHF.setActivity(this); |
71 |
|
|
|
72 |
|
|
if(!HF.canReadWriteFiles(System.getProperty("user.home"))) { |
73 |
|
|
// FIXME: workaround to make java.util.prefs to work properly |
74 |
|
|
System.setProperty("user.home", getFilesDir().getAbsolutePath()); |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
if(!started) { |
78 |
|
|
JSampler.main(null); |
79 |
|
|
started = true; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
AHF.getMainFrame().onCreate(); |
83 |
|
|
setContentView(AHF.getMainFrame().getView()); |
84 |
|
|
|
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
private SimpleOnGestureListener gestureListener = new SimpleOnGestureListener() { |
88 |
|
|
public boolean |
89 |
|
|
onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { |
90 |
|
|
return AHF.getMainFrame().onFling(e1, e2, velocityX, velocityY); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
public boolean |
94 |
|
|
onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { |
95 |
|
|
return AHF.getMainFrame().onFling(e1, e2, distanceX, distanceY); |
96 |
|
|
} |
97 |
|
|
}; |
98 |
|
|
|
99 |
|
|
/*public boolean |
100 |
|
|
onTouchEvent(MotionEvent event) { |
101 |
|
|
return gestureDetector.onTouchEvent(event); |
102 |
|
|
}*/ |
103 |
|
|
|
104 |
|
|
public boolean |
105 |
|
|
dispatchTouchEvent(MotionEvent event) { |
106 |
|
|
boolean b = gestureDetector.onTouchEvent(event); |
107 |
|
|
if (!b) return super.dispatchTouchEvent(event); |
108 |
|
|
return b; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
@Override |
112 |
|
|
protected void |
113 |
|
|
onActivityResult(int requestCode, int resultCode, Intent data) { |
114 |
|
|
super.onActivityResult(requestCode, resultCode, data); |
115 |
|
|
|
116 |
|
|
if(resultCode != Activity.RESULT_OK) return; |
117 |
|
|
|
118 |
|
|
switch(requestCode) { |
119 |
|
|
case CHOOSE_BACKEND_REQUEST: |
120 |
|
|
Requestor r = requestorMap.get(CHOOSE_BACKEND_REQUEST); |
121 |
|
|
if(r == null) return; |
122 |
|
|
Log.w("JSamplerActivity", "onActivityResult: CHOOSE_BACKEND_REQUEST"); |
123 |
|
|
r.setResult(data); |
124 |
|
|
requestorMap.remove(CHOOSE_BACKEND_REQUEST); |
125 |
|
|
break; |
126 |
|
|
} |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
|
130 |
|
|
@Override |
131 |
|
|
protected void onDestroy() { |
132 |
|
|
super.onDestroy(); |
133 |
|
|
AHF.getMainFrame().uninstall(); |
134 |
|
|
|
135 |
|
|
// postpone |
136 |
|
|
PDUtils.runOnUiThread(new Runnable() { |
137 |
|
|
public void |
138 |
|
|
run() { CC.cleanExit(); } |
139 |
|
|
}); |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
@Override |
143 |
|
|
protected Dialog onCreateDialog(int id) { |
144 |
|
|
Dialog dialog; |
145 |
|
|
|
146 |
|
|
switch(id) { |
147 |
|
|
case DLG_CHOOSE_HOME_DIR: |
148 |
|
|
dialog = new JSamplerHomeChooser(this); |
149 |
|
|
break; |
150 |
|
|
default: |
151 |
|
|
dialog = null; |
152 |
|
|
} |
153 |
|
|
|
154 |
|
|
return dialog; |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
@Override |
158 |
|
|
public boolean |
159 |
|
|
onCreateOptionsMenu(Menu menu) { |
160 |
|
|
MenuInflater inflater = getMenuInflater(); |
161 |
|
|
inflater.inflate(R.menu.jsampler_options_menu, menu); |
162 |
|
|
return true; |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
@Override |
166 |
|
|
public boolean |
167 |
|
|
onOptionsItemSelected(MenuItem item) { |
168 |
|
|
switch (item.getItemId()) { |
169 |
|
|
case R.id.jsampler_options_menu_choose_backend: |
170 |
|
|
CC.changeBackend(); |
171 |
|
|
return true; |
172 |
|
|
default: |
173 |
|
|
return super.onOptionsItemSelected(item); |
174 |
|
|
} |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
public static <R> R |
178 |
|
|
getLocalResult(Intent i) { |
179 |
|
|
try { |
180 |
|
|
if(i == null) return null; |
181 |
|
|
|
182 |
|
|
return (R)i.getSerializableExtra("org.jsampler.SomeSerializableObject"); |
183 |
|
|
} catch(ClassCastException e) { |
184 |
|
|
e.printStackTrace(); |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
return null; |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
public static abstract class Requestor<R> { |
191 |
|
|
private final int requestId; |
192 |
|
|
|
193 |
|
|
/** |
194 |
|
|
* Creates a requestor which is used to start a local activity. |
195 |
|
|
* @param requestId Specifies the activity to be started. |
196 |
|
|
* @see #startLocalActivity |
197 |
|
|
*/ |
198 |
|
|
public Requestor(int requestId) { |
199 |
|
|
this.requestId = requestId; |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
private void |
203 |
|
|
setResult(Intent data) { |
204 |
|
|
onResult(JSamplerActivity.<R>getLocalResult(data)); |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
/** |
208 |
|
|
* Invoked when the activity is finished with result. |
209 |
|
|
* @param res The result obtained from the activity. |
210 |
|
|
*/ |
211 |
|
|
public abstract void onResult(R res); |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
/** |
215 |
|
|
* Creates a local intent (for app internal use) if currently |
216 |
|
|
* there is no registered requestor for the intended activity. |
217 |
|
|
* @param requestId Used to determine the activity which should be started. |
218 |
|
|
* @return The newly created intent or <code>null</code> if the request ID is uknown |
219 |
|
|
* or if there is already registered requestor for the intended activity. |
220 |
|
|
*/ |
221 |
|
|
private Intent |
222 |
|
|
createLocalIntent(int requestId) { |
223 |
|
|
if(requestorMap.containsKey(requestId)) { |
224 |
|
|
Log.w("request ID: " + requestId, "There is a registered requestor for this activity"); |
225 |
|
|
return null; |
226 |
|
|
} |
227 |
|
|
switch(requestId) { |
228 |
|
|
case CHOOSE_BACKEND_REQUEST: |
229 |
|
|
return new Intent(this, ChooseBackendActivity.class); |
230 |
|
|
default: |
231 |
|
|
Log.w("createLocalIntent", "Unknown request ID: " + requestId); |
232 |
|
|
return null; |
233 |
|
|
} |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
/** Starts an app local activity. */ |
237 |
|
|
public void |
238 |
|
|
startLocalActivity(Requestor requestor) { |
239 |
|
|
Intent intent = createLocalIntent(requestor.requestId); |
240 |
|
|
if(intent == null) return; |
241 |
|
|
requestorMap.put(requestor.requestId, requestor); |
242 |
|
|
startActivityForResult(intent, CHOOSE_BACKEND_REQUEST); |
243 |
|
|
} |
244 |
|
|
} |