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);
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))

View file

@ -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()

View file

@ -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;
}
}

View file

@ -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)

View file

@ -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;
}
public void set_activity(Activity a)
{
current_activity = a;
return mCurrentActivity;
}
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 (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;

View file

@ -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);