Fix extracting libmisc.so
[kugel-rb.git] / android / src / org / rockbox / RockboxService.java
blob033ece320c73d12f69ef98cfcfe9b93e2736dc93
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Thomas Martitz
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 package org.rockbox;
24 import java.io.BufferedInputStream;
25 import java.io.BufferedOutputStream;
26 import java.io.File;
27 import java.io.FileOutputStream;
28 import java.util.Enumeration;
29 import java.util.Timer;
30 import java.util.TimerTask;
31 import java.util.zip.ZipEntry;
32 import java.util.zip.ZipFile;
34 import org.rockbox.Helper.RunForegroundManager;
36 import android.app.Activity;
37 import android.app.Service;
38 import android.content.BroadcastReceiver;
39 import android.content.Context;
40 import android.content.Intent;
41 import android.content.IntentFilter;
42 import android.os.Bundle;
43 import android.os.IBinder;
44 import android.os.ResultReceiver;
45 import android.util.Log;
47 /* This class is used as the main glue between java and c.
48 * All access should be done through RockboxService.get_instance() for safety.
51 public class RockboxService extends Service
53 /* this Service is really a singleton class - well almost.
54 * To do it properly this line should be instance = new RockboxService()
55 * but apparently that doesnt work with the way android Services are created.
57 private static RockboxService instance = null;
59 /* locals needed for the c code and rockbox state */
60 private RockboxFramebuffer fb = null;
61 private boolean mRockboxRunning = false;
62 private volatile boolean rbLibLoaded;
63 private Activity current_activity = null;
64 private IntentFilter itf;
65 private BroadcastReceiver batt_monitor;
66 private RunForegroundManager fg_runner;
67 @SuppressWarnings("unused")
68 private int battery_level;
69 private ResultReceiver resultReceiver;
71 public static final int RESULT_LIB_LOADED = 0;
72 public static final int RESULT_LIB_LOAD_PROGRESS = 1;
73 public static final int RESULT_FB_INITIALIZED = 2;
74 public static final int RESULT_ERROR_OCCURED = 3;
76 @Override
77 public void onCreate()
79 instance = this;
82 public static RockboxService get_instance()
84 return instance;
87 public RockboxFramebuffer get_fb()
89 return fb;
91 /* framebuffer is initialised by the native code(!) so this is needed */
92 public void set_fb(RockboxFramebuffer newfb)
94 fb = newfb;
95 mRockboxRunning = true;
96 if (resultReceiver != null)
97 resultReceiver.send(RESULT_FB_INITIALIZED, null);
100 public Activity get_activity()
102 return current_activity;
104 public void set_activity(Activity a)
106 current_activity = a;
109 private void do_start(Intent intent)
111 LOG("Start Service");
113 if (intent != null && intent.hasExtra("callback"))
114 resultReceiver = (ResultReceiver) intent.getParcelableExtra("callback");
115 if (!rbLibLoaded)
116 startservice();
118 /* Display a notification about us starting.
119 * We put an icon in the status bar. */
120 try {
121 fg_runner = new RunForegroundManager(this);
122 } catch (Exception e) {
123 e.printStackTrace();
127 private void LOG(CharSequence text)
129 Log.d("Rockbox", (String) text);
132 private void LOG(CharSequence text, Throwable tr)
134 Log.d("Rockbox", (String) text, tr);
137 public void onStart(Intent intent, int startId) {
138 do_start(intent);
141 public int onStartCommand(Intent intent, int flags, int startId)
143 do_start(intent);
144 return 1; /* old API compatibility: 1 == START_STICKY */
147 private void startservice()
149 final int BUFFER = 8*1024;
150 Thread rb = new Thread(new Runnable()
152 public void run()
154 String rockboxDirPath = "/data/data/org.rockbox/app_rockbox/rockbox";
155 File rockboxDir = new File(rockboxDirPath);
157 /* the following block unzips libmisc.so, which contains the files
158 * we ship, such as themes. It's needed to put it into a .so file
159 * because there's no other way to ship files and have access
160 * to them from native code
162 File libMisc = new File("/data/data/org.rockbox/lib/libmisc.so");
163 /* use arbitrary file to determine whether extracting is needed */
164 File arbitraryFile = new File(rockboxDir, "viewers.config");
165 if (!arbitraryFile.exists() || (libMisc.lastModified() > arbitraryFile.lastModified()))
169 Bundle progressData = new Bundle();
170 byte data[] = new byte[BUFFER];
171 ZipFile zipfile = new ZipFile(libMisc);
172 Enumeration<? extends ZipEntry> e = zipfile.entries();
173 progressData.putInt("max", zipfile.size());
175 while(e.hasMoreElements())
177 ZipEntry entry = (ZipEntry) e.nextElement();
178 File file;
179 /* strip off /.rockbox when extracting */
180 String fileName = entry.getName();
181 int slashIndex = fileName.indexOf('/', 1);
182 file = new File(rockboxDirPath + fileName.substring(slashIndex));
184 if (!entry.isDirectory())
186 /* Create the parent folders if necessary */
187 File folder = new File(file.getParent());
188 if (!folder.exists())
189 folder.mkdirs();
191 /* Extract file */
192 BufferedInputStream is = new BufferedInputStream(zipfile.getInputStream(entry), BUFFER);
193 FileOutputStream fos = new FileOutputStream(file);
194 BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
196 int count;
197 while ((count = is.read(data, 0, BUFFER)) != -1)
198 dest.write(data, 0, count);
200 dest.flush();
201 dest.close();
202 is.close();
205 if (resultReceiver != null) {
206 progressData.putInt("value", progressData.getInt("value", 0) + 1);
207 resultReceiver.send(RESULT_LIB_LOAD_PROGRESS, progressData);
210 } catch(Exception e) {
211 LOG("Exception when unzipping", e);
212 e.printStackTrace();
213 if (resultReceiver != null) {
214 Bundle bundle = new Bundle();
215 bundle.putString("error", getString(R.string.error_extraction));
216 resultReceiver.send(RESULT_ERROR_OCCURED, bundle);
221 System.loadLibrary("rockbox");
222 rbLibLoaded = true;
223 if (resultReceiver != null)
224 resultReceiver.send(RESULT_LIB_LOADED, null);
226 main();
227 throw new IllegalStateException("native main() returned!");
229 }, "Rockbox thread");
230 rb.setDaemon(false);
231 rb.start();
233 private native void main();
235 /* returns true once rockbox is up and running.
236 * This is considered done once the framebuffer is initialised
238 public boolean isRockboxRunning()
240 return mRockboxRunning;
243 @Override
244 public IBinder onBind(Intent intent)
246 // TODO Auto-generated method stub
247 return null;
251 @SuppressWarnings("unused")
253 * Sets up the battery monitor which receives the battery level
254 * about each 30 seconds
256 private void initBatteryMonitor()
258 itf = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
259 batt_monitor = new BroadcastReceiver()
261 @Override
262 public void onReceive(Context context, Intent intent)
264 /* we get literally spammed with battery statuses
265 * if we don't delay the re-attaching
267 TimerTask tk = new TimerTask()
269 public void run()
271 registerReceiver(batt_monitor, itf);
274 Timer t = new Timer();
275 context.unregisterReceiver(this);
276 int rawlevel = intent.getIntExtra("level", -1);
277 int scale = intent.getIntExtra("scale", -1);
278 if (rawlevel >= 0 && scale > 0)
279 battery_level = (rawlevel * 100) / scale;
280 else
281 battery_level = -1;
282 /* query every 30s should be sufficient */
283 t.schedule(tk, 30000);
286 registerReceiver(batt_monitor, itf);
289 public void startForeground()
291 fg_runner.startForeground();
294 public void stopForeground()
296 fg_runner.stopForeground();
299 @Override
300 public void onDestroy()
302 super.onDestroy();
303 fb.destroy();
304 /* Make sure our notification is gone. */
305 stopForeground();