android: clean-up and some refactoring in java code.

Change-Id: I78cadb0b71bcb65a55006bf52cfe3e6cda891a38
This commit is contained in:
Thomas Martitz 2012-03-25 19:22:39 +02:00
parent 82337dda6a
commit c8317eb596
6 changed files with 65 additions and 77 deletions

View file

@ -88,7 +88,7 @@ public class MediaButtonReceiver
KeyEvent key = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); KeyEvent key = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (key.getAction() == KeyEvent.ACTION_UP) if (key.getAction() == KeyEvent.ACTION_UP)
{ /* pass the pressed key to Rockbox, starting it if needed */ { /* pass the pressed key to Rockbox, starting it if needed */
RockboxService s = RockboxService.get_instance(); RockboxService s = RockboxService.getInstance();
if (s == null || !s.isRockboxRunning()) if (s == null || !s.isRockboxRunning())
startService(context, intent); startService(context, intent);
else if (RockboxFramebuffer.buttonHandler(key.getKeyCode(), false)) else if (RockboxFramebuffer.buttonHandler(key.getKeyCode(), false))

View file

@ -91,9 +91,9 @@ public class RockboxActivity extends Activity
private void setServiceActivity(boolean set) private void setServiceActivity(boolean set)
{ {
RockboxService s = RockboxService.get_instance(); RockboxService s = RockboxService.getInstance();
if (s != null) if (s != null)
s.set_activity(set ? this : null); s.setActivity(set ? this : null);
} }
public void onResume() public void onResume()

View file

@ -35,7 +35,7 @@ public class RockboxKeyboardInput
{ {
public void kbd_input(final String text, final String ok, final String cancel) public void kbd_input(final String text, final String ok, final String cancel)
{ {
final Activity c = RockboxService.get_instance().get_activity(); final Activity c = RockboxService.getInstance().getActivity();
c.runOnUiThread(new Runnable() { c.runOnUiThread(new Runnable() {
public void run() public void run()
@ -74,6 +74,6 @@ public class RockboxKeyboardInput
public boolean is_usable() public boolean is_usable()
{ {
return RockboxService.get_instance().get_activity() != null; return RockboxService.getInstance().getActivity() != null;
} }
} }

View file

@ -69,7 +69,7 @@ public class RockboxPCM extends AudioTrack
Arrays.fill(raw_data, (byte) 0); Arrays.fill(raw_data, (byte) 0);
/* find cleaner way to get context? */ /* find cleaner way to get context? */
rbservice = RockboxService.get_instance(); rbservice = RockboxService.getInstance();
audiomanager = audiomanager =
(AudioManager) rbservice.getSystemService(Context.AUDIO_SERVICE); (AudioManager) rbservice.getSystemService(Context.AUDIO_SERVICE);
maxstreamvolume = audiomanager.getStreamMaxVolume(streamtype); maxstreamvolume = audiomanager.getStreamMaxVolume(streamtype);
@ -147,7 +147,7 @@ public class RockboxPCM extends AudioTrack
private void play_pause(boolean pause) private void play_pause(boolean pause)
{ {
RockboxService service = RockboxService.get_instance(); RockboxService service = RockboxService.getInstance();
if (pause) if (pause)
{ {
Intent widgetUpdate = new Intent("org.rockbox.UpdateState"); Intent widgetUpdate = new Intent("org.rockbox.UpdateState");
@ -192,8 +192,8 @@ public class RockboxPCM extends AudioTrack
Intent widgetUpdate = new Intent("org.rockbox.UpdateState"); Intent widgetUpdate = new Intent("org.rockbox.UpdateState");
widgetUpdate.putExtra("state", "stop"); widgetUpdate.putExtra("state", "stop");
RockboxService.get_instance().sendBroadcast(widgetUpdate); RockboxService.getInstance().sendBroadcast(widgetUpdate);
RockboxService.get_instance().stopForeground(); RockboxService.getInstance().stopForeground();
} }
public int setStereoVolume(float leftVolume, float rightVolume) public int setStereoVolume(float leftVolume, float rightVolume)

View file

@ -29,6 +29,7 @@ import java.io.OutputStreamWriter;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import org.rockbox.Helper.Logger;
import org.rockbox.Helper.MediaButtonReceiver; import org.rockbox.Helper.MediaButtonReceiver;
import org.rockbox.Helper.RunForegroundManager; import org.rockbox.Helper.RunForegroundManager;
import android.app.Activity; import android.app.Activity;
@ -38,7 +39,6 @@ import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.os.IBinder; import android.os.IBinder;
import android.os.ResultReceiver; import android.os.ResultReceiver;
import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
/* This class is used as the main glue between java and c. /* This class is used as the main glue between java and c.
@ -47,19 +47,17 @@ import android.view.KeyEvent;
public class RockboxService extends Service public class RockboxService extends Service
{ {
/* this Service is really a singleton class - well almost. /* this Service is really a singleton class - well almost. */
* To do it properly this line should be instance = new RockboxService()
* but apparently that doesnt work with the way android Services are created.
*/
private static RockboxService instance = null; private static RockboxService instance = null;
/* locals needed for the c code and rockbox state */ /* locals needed for the c code and Rockbox state */
private static volatile boolean rockbox_running; private static volatile boolean rockbox_running;
private Activity current_activity = null; private Activity mCurrentActivity = null;
private RunForegroundManager fg_runner; private RunForegroundManager mFgRunner;
private MediaButtonReceiver mMediaButtonReceiver; private MediaButtonReceiver mMediaButtonReceiver;
private ResultReceiver resultReceiver; private ResultReceiver mResultReceiver;
/* possible result values for intent handling */
public static final int RESULT_INVOKING_MAIN = 0; public static final int RESULT_INVOKING_MAIN = 0;
public static final int RESULT_LIB_LOAD_PROGRESS = 1; public static final int RESULT_LIB_LOAD_PROGRESS = 1;
public static final int RESULT_SERVICE_RUNNING = 3; public static final int RESULT_SERVICE_RUNNING = 3;
@ -72,12 +70,12 @@ public class RockboxService extends Service
{ {
instance = this; instance = this;
mMediaButtonReceiver = new MediaButtonReceiver(this); mMediaButtonReceiver = new MediaButtonReceiver(this);
fg_runner = new RunForegroundManager(this); mFgRunner = new RunForegroundManager(this);
} }
public static RockboxService get_instance() public static RockboxService getInstance()
{ {
/* don't call the construtor here, the instances are managed by /* don't call the constructor here, the instances are managed by
* android, so we can't just create a new one */ * android, so we can't just create a new one */
return instance; return instance;
} }
@ -86,33 +84,44 @@ public class RockboxService extends Service
{ {
return rockbox_running; return rockbox_running;
} }
public Activity get_activity() public Activity getActivity()
{ {
return current_activity; return mCurrentActivity;
}
public void set_activity(Activity a)
{
current_activity = a;
} }
private void do_start(Intent intent) public void setActivity(Activity a)
{ {
LOG("Start RockboxService (Intent: " + intent.getAction() + ")"); mCurrentActivity = a;
}
private void putResult(int resultCode)
{
putResult(resultCode, null);
}
private void putResult(int resultCode, Bundle resultData)
{
if (mResultReceiver != null)
mResultReceiver.send(resultCode, resultData);
}
private void doStart(Intent intent)
{
Logger.d("Start RockboxService (Intent: " + intent.getAction() + ")");
if (intent.getAction().equals("org.rockbox.ResendTrackUpdateInfo")) if (intent.getAction().equals("org.rockbox.ResendTrackUpdateInfo"))
{ {
if (rockbox_running) if (rockbox_running)
fg_runner.resendUpdateNotification(); mFgRunner.resendUpdateNotification();
return; return;
} }
if (intent.hasExtra("callback")) if (intent.hasExtra("callback"))
resultReceiver = (ResultReceiver) intent.getParcelableExtra("callback"); mResultReceiver = (ResultReceiver) intent.getParcelableExtra("callback");
if (!rockbox_running) if (!rockbox_running)
startservice(); startService();
if (resultReceiver != null) putResult(RESULT_LIB_LOADED);
resultReceiver.send(RESULT_LIB_LOADED, null);
if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON))
{ {
@ -123,24 +132,13 @@ public class RockboxService extends Service
/* (Re-)attach the media button receiver, in case it has been lost */ /* (Re-)attach the media button receiver, in case it has been lost */
mMediaButtonReceiver.register(); mMediaButtonReceiver.register();
if (resultReceiver != null) putResult(RESULT_SERVICE_RUNNING);
resultReceiver.send(RESULT_SERVICE_RUNNING, null);
rockbox_running = true; rockbox_running = true;
} }
private void LOG(CharSequence text)
{
Log.d("Rockbox", (String) text);
}
private void LOG(CharSequence text, Throwable tr)
{
Log.d("Rockbox", (String) text, tr);
}
public void onStart(Intent intent, int startId) { public void onStart(Intent intent, int startId) {
do_start(intent); doStart(intent);
} }
public int onStartCommand(Intent intent, int flags, int startId) public int onStartCommand(Intent intent, int flags, int startId)
@ -149,11 +147,11 @@ public class RockboxService extends Service
* after getting killed for memory pressure earlier */ * after getting killed for memory pressure earlier */
if (intent == null) if (intent == null)
intent = new Intent("org.rockbox.ServiceRestarted"); intent = new Intent("org.rockbox.ServiceRestarted");
do_start(intent); doStart(intent);
return START_STICKY; return START_STICKY;
} }
private void startservice() private void startService()
{ {
final Object lock = new Object(); final Object lock = new Object();
Thread rb = new Thread(new Runnable() Thread rb = new Thread(new Runnable()
@ -164,9 +162,6 @@ public class RockboxService extends Service
String rockboxDirPath = "/data/data/org.rockbox/app_rockbox/rockbox"; String rockboxDirPath = "/data/data/org.rockbox/app_rockbox/rockbox";
String rockboxCreditsPath = "/data/data/org.rockbox/app_rockbox/rockbox/rocks/viewers"; String rockboxCreditsPath = "/data/data/org.rockbox/app_rockbox/rockbox/rocks/viewers";
String rockboxSdDirPath = "/sdcard/rockbox"; String rockboxSdDirPath = "/sdcard/rockbox";
File rockboxDir = new File(rockboxDirPath);
File rockboxSdDir = new File(rockboxSdDirPath);
File rockboxCreditsDir = new File(rockboxCreditsPath);
/* load library before unzipping which may take a while */ /* load library before unzipping which may take a while */
synchronized (lock) { synchronized (lock) {
@ -186,10 +181,10 @@ public class RockboxService extends Service
boolean extractToSd = false; boolean extractToSd = false;
if(rockboxInfoFile.exists()) { if(rockboxInfoFile.exists()) {
extractToSd = true; extractToSd = true;
LOG("extracting resources to SD card"); Logger.d("extracting resources to SD card");
} }
else { else {
LOG("extracting resources to internal memory"); Logger.d("extracting resources to internal memory");
} }
if (!arbitraryFile.exists() || (libMisc.lastModified() > arbitraryFile.lastModified())) if (!arbitraryFile.exists() || (libMisc.lastModified() > arbitraryFile.lastModified()))
{ {
@ -240,20 +235,16 @@ public class RockboxService extends Service
is.close(); is.close();
} }
if (resultReceiver != null) { progressData.putInt("value", progressData.getInt("value", 0) + 1);
progressData.putInt("value", progressData.getInt("value", 0) + 1); putResult(RESULT_LIB_LOAD_PROGRESS, progressData);
resultReceiver.send(RESULT_LIB_LOAD_PROGRESS, progressData);
}
} }
arbitraryFile.setLastModified(libMisc.lastModified()); arbitraryFile.setLastModified(libMisc.lastModified());
} catch(Exception e) { } catch(Exception e) {
LOG("Exception when unzipping", e); Logger.d("Exception when unzipping", e);
Bundle bundle = new Bundle();
e.printStackTrace(); e.printStackTrace();
if (resultReceiver != null) { bundle.putString("error", getString(R.string.error_extraction));
Bundle bundle = new Bundle(); putResult(RESULT_ERROR_OCCURED, bundle);
bundle.putString("error", getString(R.string.error_extraction));
resultReceiver.send(RESULT_ERROR_OCCURED, bundle);
}
} }
} }
@ -272,21 +263,19 @@ public class RockboxService extends Service
strm.write("lang: /.rockbox/langs/" + getString(R.string.rockbox_language_file) + "\n"); strm.write("lang: /.rockbox/langs/" + getString(R.string.rockbox_language_file) + "\n");
strm.close(); strm.close();
} catch(Exception e) { } catch(Exception e) {
LOG("Exception when writing default config", e); Logger.d("Exception when writing default config", e);
} }
} }
/* Start native code */ /* Start native code */
if (resultReceiver != null) putResult(RESULT_INVOKING_MAIN);
resultReceiver.send(RESULT_INVOKING_MAIN, null);
main(); main();
if (resultReceiver != null) putResult(RESULT_ROCKBOX_EXIT);
resultReceiver.send(RESULT_ROCKBOX_EXIT, null);
LOG("Stop service: main() returned"); Logger.d("Stop service: main() returned");
stopSelf(); /* serivce is of no use anymore */ stopSelf(); /* service is of no use anymore */
} }
}, "Rockbox thread"); }, "Rockbox thread");
rb.setDaemon(false); rb.setDaemon(false);
@ -311,18 +300,17 @@ public class RockboxService extends Service
@Override @Override
public IBinder onBind(Intent intent) public IBinder onBind(Intent intent)
{ {
// TODO Auto-generated method stub
return null; return null;
} }
void startForeground() void startForeground()
{ {
fg_runner.startForeground(); mFgRunner.startForeground();
} }
void stopForeground() void stopForeground()
{ {
fg_runner.stopForeground(); mFgRunner.stopForeground();
} }
@Override @Override
@ -330,7 +318,7 @@ public class RockboxService extends Service
{ {
super.onDestroy(); super.onDestroy();
/* Don't unregister so we can receive them (and startup the service) /* Don't unregister so we can receive them (and startup the service)
* after idle poweroff. Hopefully it's ok if mMediaButtonReceiver is * after idle power-off. Hopefully it's OK if mMediaButtonReceiver is
* garbage collected. * garbage collected.
* mMediaButtonReceiver.unregister(); */ * mMediaButtonReceiver.unregister(); */
mMediaButtonReceiver = null; mMediaButtonReceiver = null;

View file

@ -29,7 +29,7 @@ public class RockboxYesno
{ {
private void yesno_display(final String text, final String yes, final String no) private void yesno_display(final String text, final String yes, final String no)
{ {
final Activity c = RockboxService.get_instance().get_activity(); final Activity c = RockboxService.getInstance().getActivity();
c.runOnUiThread(new Runnable() { c.runOnUiThread(new Runnable() {
public void run() public void run()
@ -60,7 +60,7 @@ public class RockboxYesno
private boolean is_usable() private boolean is_usable()
{ {
return RockboxService.get_instance().get_activity() != null; return RockboxService.getInstance().getActivity() != null;
} }
private native void put_result(boolean result); private native void put_result(boolean result);