From c8317eb596a867d6513ef78f84a5e620579580ce Mon Sep 17 00:00:00 2001 From: Thomas Martitz Date: Sun, 25 Mar 2012 19:22:39 +0200 Subject: [PATCH] android: clean-up and some refactoring in java code. Change-Id: I78cadb0b71bcb65a55006bf52cfe3e6cda891a38 --- .../org/rockbox/Helper/MediaButtonReceiver.java | 2 +- android/src/org/rockbox/RockboxActivity.java | 4 +- android/src/org/rockbox/RockboxKeyboardInput.java | 4 +- android/src/org/rockbox/RockboxPCM.java | 8 +- android/src/org/rockbox/RockboxService.java | 116 +++++++++------------ android/src/org/rockbox/RockboxYesno.java | 4 +- 6 files changed, 63 insertions(+), 75 deletions(-) diff --git a/android/src/org/rockbox/Helper/MediaButtonReceiver.java b/android/src/org/rockbox/Helper/MediaButtonReceiver.java index e74ec5c8c2..a07e328c13 100644 --- a/android/src/org/rockbox/Helper/MediaButtonReceiver.java +++ b/android/src/org/rockbox/Helper/MediaButtonReceiver.java @@ -88,7 +88,7 @@ public class MediaButtonReceiver KeyEvent key = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); if (key.getAction() == KeyEvent.ACTION_UP) { /* pass the pressed key to Rockbox, starting it if needed */ - RockboxService s = RockboxService.get_instance(); + RockboxService s = RockboxService.getInstance(); if (s == null || !s.isRockboxRunning()) startService(context, intent); else if (RockboxFramebuffer.buttonHandler(key.getKeyCode(), false)) diff --git a/android/src/org/rockbox/RockboxActivity.java b/android/src/org/rockbox/RockboxActivity.java index 372d866849..166d69ebe0 100644 --- a/android/src/org/rockbox/RockboxActivity.java +++ b/android/src/org/rockbox/RockboxActivity.java @@ -91,9 +91,9 @@ public class RockboxActivity extends Activity private void setServiceActivity(boolean set) { - RockboxService s = RockboxService.get_instance(); + RockboxService s = RockboxService.getInstance(); if (s != null) - s.set_activity(set ? this : null); + s.setActivity(set ? this : null); } public void onResume() diff --git a/android/src/org/rockbox/RockboxKeyboardInput.java b/android/src/org/rockbox/RockboxKeyboardInput.java index 107b72ddae..540ba12c22 100644 --- a/android/src/org/rockbox/RockboxKeyboardInput.java +++ b/android/src/org/rockbox/RockboxKeyboardInput.java @@ -35,7 +35,7 @@ public class RockboxKeyboardInput { 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() { public void run() @@ -74,6 +74,6 @@ public class RockboxKeyboardInput public boolean is_usable() { - return RockboxService.get_instance().get_activity() != null; + return RockboxService.getInstance().getActivity() != null; } } diff --git a/android/src/org/rockbox/RockboxPCM.java b/android/src/org/rockbox/RockboxPCM.java index f77bf3bb8f..e77a1b00d9 100644 --- a/android/src/org/rockbox/RockboxPCM.java +++ b/android/src/org/rockbox/RockboxPCM.java @@ -69,7 +69,7 @@ public class RockboxPCM extends AudioTrack Arrays.fill(raw_data, (byte) 0); /* find cleaner way to get context? */ - rbservice = RockboxService.get_instance(); + rbservice = RockboxService.getInstance(); audiomanager = (AudioManager) rbservice.getSystemService(Context.AUDIO_SERVICE); maxstreamvolume = audiomanager.getStreamMaxVolume(streamtype); @@ -147,7 +147,7 @@ public class RockboxPCM extends AudioTrack private void play_pause(boolean pause) { - RockboxService service = RockboxService.get_instance(); + RockboxService service = RockboxService.getInstance(); if (pause) { Intent widgetUpdate = new Intent("org.rockbox.UpdateState"); @@ -192,8 +192,8 @@ public class RockboxPCM extends AudioTrack Intent widgetUpdate = new Intent("org.rockbox.UpdateState"); widgetUpdate.putExtra("state", "stop"); - RockboxService.get_instance().sendBroadcast(widgetUpdate); - RockboxService.get_instance().stopForeground(); + RockboxService.getInstance().sendBroadcast(widgetUpdate); + RockboxService.getInstance().stopForeground(); } public int setStereoVolume(float leftVolume, float rightVolume) diff --git a/android/src/org/rockbox/RockboxService.java b/android/src/org/rockbox/RockboxService.java index 4242726331..5c8f8cfd99 100644 --- a/android/src/org/rockbox/RockboxService.java +++ b/android/src/org/rockbox/RockboxService.java @@ -29,6 +29,7 @@ import java.io.OutputStreamWriter; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; +import org.rockbox.Helper.Logger; import org.rockbox.Helper.MediaButtonReceiver; import org.rockbox.Helper.RunForegroundManager; import android.app.Activity; @@ -38,7 +39,6 @@ import android.os.Bundle; import android.os.Environment; import android.os.IBinder; import android.os.ResultReceiver; -import android.util.Log; import android.view.KeyEvent; /* 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 { - /* 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. - */ + /* this Service is really a singleton class - well almost. */ 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 Activity current_activity = null; - private RunForegroundManager fg_runner; + private Activity mCurrentActivity = null; + private RunForegroundManager mFgRunner; 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_LIB_LOAD_PROGRESS = 1; public static final int RESULT_SERVICE_RUNNING = 3; @@ -72,12 +70,12 @@ public class RockboxService extends Service { instance = 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 */ return instance; } @@ -86,33 +84,44 @@ public class RockboxService extends Service { return rockbox_running; } - public Activity get_activity() + public Activity getActivity() { - return current_activity; + return mCurrentActivity; } - public void set_activity(Activity a) + + public void setActivity(Activity a) + { + mCurrentActivity = a; + } + + private void putResult(int resultCode) { - current_activity = a; + putResult(resultCode, null); } - private void do_start(Intent intent) + private void putResult(int resultCode, Bundle resultData) { - LOG("Start RockboxService (Intent: " + intent.getAction() + ")"); + 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 (rockbox_running) - fg_runner.resendUpdateNotification(); + mFgRunner.resendUpdateNotification(); return; } if (intent.hasExtra("callback")) - resultReceiver = (ResultReceiver) intent.getParcelableExtra("callback"); + mResultReceiver = (ResultReceiver) intent.getParcelableExtra("callback"); if (!rockbox_running) - startservice(); - if (resultReceiver != null) - resultReceiver.send(RESULT_LIB_LOADED, null); + startService(); + putResult(RESULT_LIB_LOADED); 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 */ mMediaButtonReceiver.register(); - if (resultReceiver != null) - resultReceiver.send(RESULT_SERVICE_RUNNING, null); + putResult(RESULT_SERVICE_RUNNING); 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) { - do_start(intent); + doStart(intent); } 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 */ if (intent == null) intent = new Intent("org.rockbox.ServiceRestarted"); - do_start(intent); + doStart(intent); return START_STICKY; } - private void startservice() + private void startService() { final Object lock = new Object(); 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 rockboxCreditsPath = "/data/data/org.rockbox/app_rockbox/rockbox/rocks/viewers"; 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 */ synchronized (lock) { @@ -186,10 +181,10 @@ public class RockboxService extends Service boolean extractToSd = false; if(rockboxInfoFile.exists()) { extractToSd = true; - LOG("extracting resources to SD card"); + Logger.d("extracting resources to SD card"); } else { - LOG("extracting resources to internal memory"); + Logger.d("extracting resources to internal memory"); } if (!arbitraryFile.exists() || (libMisc.lastModified() > arbitraryFile.lastModified())) { @@ -240,20 +235,16 @@ public class RockboxService extends Service is.close(); } - if (resultReceiver != null) { - progressData.putInt("value", progressData.getInt("value", 0) + 1); - resultReceiver.send(RESULT_LIB_LOAD_PROGRESS, progressData); - } + progressData.putInt("value", progressData.getInt("value", 0) + 1); + putResult(RESULT_LIB_LOAD_PROGRESS, progressData); } arbitraryFile.setLastModified(libMisc.lastModified()); } catch(Exception e) { - LOG("Exception when unzipping", e); + Logger.d("Exception when unzipping", e); + Bundle bundle = new Bundle(); e.printStackTrace(); - if (resultReceiver != null) { - Bundle bundle = new Bundle(); - bundle.putString("error", getString(R.string.error_extraction)); - resultReceiver.send(RESULT_ERROR_OCCURED, bundle); - } + bundle.putString("error", getString(R.string.error_extraction)); + putResult(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.close(); } catch(Exception e) { - LOG("Exception when writing default config", e); + Logger.d("Exception when writing default config", e); } } /* Start native code */ - if (resultReceiver != null) - resultReceiver.send(RESULT_INVOKING_MAIN, null); + putResult(RESULT_INVOKING_MAIN); main(); - if (resultReceiver != null) - resultReceiver.send(RESULT_ROCKBOX_EXIT, null); + putResult(RESULT_ROCKBOX_EXIT); - LOG("Stop service: main() returned"); - stopSelf(); /* serivce is of no use anymore */ + Logger.d("Stop service: main() returned"); + stopSelf(); /* service is of no use anymore */ } }, "Rockbox thread"); rb.setDaemon(false); @@ -311,18 +300,17 @@ public class RockboxService extends Service @Override public IBinder onBind(Intent intent) { - // TODO Auto-generated method stub return null; } void startForeground() { - fg_runner.startForeground(); + mFgRunner.startForeground(); } void stopForeground() { - fg_runner.stopForeground(); + mFgRunner.stopForeground(); } @Override @@ -330,7 +318,7 @@ public class RockboxService extends Service { super.onDestroy(); /* 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. * mMediaButtonReceiver.unregister(); */ mMediaButtonReceiver = null; diff --git a/android/src/org/rockbox/RockboxYesno.java b/android/src/org/rockbox/RockboxYesno.java index f770f1c7e2..de8f88ab5f 100644 --- a/android/src/org/rockbox/RockboxYesno.java +++ b/android/src/org/rockbox/RockboxYesno.java @@ -29,7 +29,7 @@ public class RockboxYesno { 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() { public void run() @@ -60,7 +60,7 @@ public class RockboxYesno private boolean is_usable() { - return RockboxService.get_instance().get_activity() != null; + return RockboxService.getInstance().getActivity() != null; } private native void put_result(boolean result); -- 2.11.4.GIT