Make SynthActivity superclass

New activities can subclass from the SynthActivity, and that will keep
the sound going. We'll actually do something a little different for
preferences, but this will be useful for other activities.
master
Raph Levien 11 years ago
parent 6cff54c18f
commit e90db0e8a4
  1. 4
      android/res/menu/synth_menu.xml
  2. 46
      android/src/com/levien/synthesizer/android/ui/PianoActivity2.java
  3. 68
      android/src/com/levien/synthesizer/android/ui/SynthActivity.java
  4. 2
      android/src/com/levien/synthesizer/android/widgets/keyboard/ScrollStripView.java

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/settings" android:title="@string/settings" />
</menu>

@ -19,20 +19,17 @@ package com.levien.synthesizer.android.ui;
import java.util.List;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
@ -41,7 +38,6 @@ import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.levien.synthesizer.R;
import com.levien.synthesizer.android.service.SynthesizerService;
import com.levien.synthesizer.android.widgets.keyboard.KeyboardSpec;
import com.levien.synthesizer.android.widgets.keyboard.KeyboardView;
import com.levien.synthesizer.android.widgets.keyboard.ScrollStripView;
@ -55,7 +51,7 @@ import com.levien.synthesizer.core.midi.MidiListener;
* This version is hacked up to send MIDI to the C++ engine. This needs to
* be refactored to make it cleaner.
*/
public class PianoActivity2 extends Activity {
public class PianoActivity2 extends SynthActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("synth", "activity onCreate " + getIntent());
@ -79,6 +75,12 @@ public class PianoActivity2 extends Activity {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.synth_menu, menu);
return true;
}
@Override
protected void onDestroy() {
Log.d("synth", "activity onDestroy");
@ -100,19 +102,6 @@ public class PianoActivity2 extends Activity {
super.onResume();
}
@Override
protected void onStart() {
super.onStart();
bindService(new Intent(this, SynthesizerService.class),
synthesizerConnection_, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
unbindService(synthesizerConnection_);
}
@Override
protected void onNewIntent(Intent intent) {
Log.d("synth", "activity onNewIntent " + intent);
@ -173,21 +162,8 @@ public class PianoActivity2 extends Activity {
}
}
private ServiceConnection synthesizerConnection_ = new ServiceConnection() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public void onServiceConnected(ComponentName className, IBinder service) {
SynthesizerService.LocalBinder binder = (SynthesizerService.LocalBinder)service;
synthesizerService_ = binder.getService();
onSynthConnected();
}
public void onServiceDisconnected(ComponentName className) {
onSynthDisconnected();
synthesizerService_ = null;
}
};
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
private void onSynthConnected() {
protected void onSynthConnected() {
final MidiListener synthMidi = synthesizerService_.getMidiListener();
//piano_.bindTo(synthMidi);
keyboard_.setMidiListener(synthMidi);
@ -278,12 +254,10 @@ public class PianoActivity2 extends Activity {
}
}
private void onSynthDisconnected() {
protected void onSynthDisconnected() {
synthesizerService_.setMidiListener(null);
}
private SynthesizerService synthesizerService_;
//private PianoView piano_;
private KeyboardView keyboard_;
private KnobView cutoffKnob_;

@ -0,0 +1,68 @@
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.levien.synthesizer.android.ui;
import com.levien.synthesizer.android.service.SynthesizerService;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
/**
* A base class that handles making a synthesizer connection. All activities should subclass from
* this if sound should keep going during the activity.
*/
public class SynthActivity extends Activity {
@Override
protected void onStart() {
super.onStart();
bindService(new Intent(this, SynthesizerService.class),
synthesizerConnection_, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
unbindService(synthesizerConnection_);
}
protected void onSynthConnected() {
}
protected void onSynthDisconnected() {
}
private ServiceConnection synthesizerConnection_ = new ServiceConnection() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
public void onServiceConnected(ComponentName className, IBinder service) {
SynthesizerService.LocalBinder binder = (SynthesizerService.LocalBinder)service;
synthesizerService_ = binder.getService();
onSynthConnected();
}
public void onServiceDisconnected(ComponentName className) {
onSynthDisconnected();
synthesizerService_ = null;
}
};
protected SynthesizerService synthesizerService_;
}

@ -54,7 +54,7 @@ public class ScrollStripView extends View {
int min = -(int)(offset_ / spacing);
int max = -(int)((offset_ - drawingRect_.width()) / spacing) + 1;
for (int i = min; i <= max; i++) {
float x = offset_ + i * 10f * density * zoom_;
float x = offset_ + i * spacing;
canvas.drawCircle(x, y, 3.0f * density, paint_);
}
}

Loading…
Cancel
Save