summaryrefslogtreecommitdiff
path: root/android/src/org/rockbox/RockboxService.java
blob: 5f8329d0c3d913f33147d4accf05b69208b7ca38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package org.rockbox;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class RockboxService extends Service 
{
	/* this Service is really a singleton class */
	public static RockboxFramebuffer fb = null;
	private static RockboxService instance;
	private Notification notification;
	@Override
	public void onCreate()
	{
	    mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		startservice();
		instance = this;
	}

	private void do_start(Intent intent)
	{
		LOG("Start Service");
	}

	private void LOG(CharSequence text)
	{
		Log.d("Rockbox", (String) text);
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		do_start(intent);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		do_start(intent);
	    /* Display a notification about us starting.  We put an icon in the status bar. */
    	create_notification();
		return START_STICKY;
	}

	private void startservice() 
	{
        fb = new RockboxFramebuffer(this);
		final int BUFFER = 2048;
		/* the following block unzips libmisc.so, which contains the files 
		 * we ship, such as themes. It's needed to put it into a .so file
		 * because there's no other way to ship files and have access
		 * to them from native code
		 */
        try 
        {
           BufferedOutputStream dest = null;
           BufferedInputStream is = null;
           ZipEntry entry;
           File file = new File("/data/data/org.rockbox/lib/libmisc.so");
           /* use arbitary file to determine whether extracting is needed */
           File file2 = new File("/data/data/org.rockbox/app_rockbox/rockbox/codecs/mpa.codec");
           if (!file2.exists() || (file.lastModified() > file2.lastModified()))
           {
	           ZipFile zipfile = new ZipFile(file);
	           Enumeration<? extends ZipEntry> e = zipfile.entries();
	           File folder;
	           while(e.hasMoreElements()) {
	              entry = (ZipEntry) e.nextElement();
	              LOG("Extracting: " +entry);
	              if (entry.isDirectory())
	              {
	            	  folder = new File(entry.getName());
	            	  LOG("mkdir "+ entry);
	            	  try {
	            		  folder.mkdirs();
	            	  } catch (SecurityException ex){
	            		  LOG(ex.getMessage());
	            	  }
	            	  continue;
	              }
	              is = new BufferedInputStream(zipfile.getInputStream(entry));
	              int count;
	              byte data[] = new byte[BUFFER];
	              folder = new File(new File(entry.getName()).getParent());
	              LOG("" + folder.getAbsolutePath());
	              if (!folder.exists())
	            	  folder.mkdirs();
	              FileOutputStream fos = new FileOutputStream(entry.getName());
	              dest = new BufferedOutputStream(fos, BUFFER);
	              while ((count = is.read(data, 0, BUFFER)) != -1) {
	                 dest.write(data, 0, count);
	              }
	              dest.flush();
	              dest.close();
	              is.close();
	           }
           }
        } catch(Exception e) {
           e.printStackTrace();
        }

        System.loadLibrary("rockbox");

    	Thread rb = new Thread(new Runnable()
    	{
    		public void run()
    		{
    			main();
    		}
    	},"Rockbox thread");
    	rb.setDaemon(false);
    	rb.start();
	}

    private native void main();
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
    private NotificationManager mNM;

    /**
     * Class for clients to access.  Because we know this service always
     * runs in the same process as its clients, we don't need to deal with
     * IPC.
     */
    public class LocalBinder extends Binder {
        RockboxService getService() {
            return RockboxService.this;
        }
    }

    /* heavily based on the example found on
     * http://developer.android.com/reference/android/app/Service.html
     */
    
    private void create_notification()
    {
		// In this sample, we'll use the same text for the ticker and the expanded notification
        CharSequence text = getText(R.string.notification);

        // Set the icon, scrolling text and timestamp
        notification = new Notification(R.drawable.icon, text,
                System.currentTimeMillis());

        // The PendingIntent to launch our activity if the user selects this notification
        Intent intent = new Intent(this, RockboxActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
        

        // Set the info for the views that show in the notification panel.
        notification.setLatestEventInfo(this, getText(R.string.notification), text, contentIntent);
    }

	public static void startForeground() 
	{
		if (instance != null) 
		{
	        // Send the notification.
	        // We use a layout id because it is a unique number.  We use it later to cancel.
	        instance.mNM.notify(R.string.notification, instance.notification);
	        
	        /*
	         * this call makes the service run as foreground, which
	         * provides enough cpu time to do music decoding in the 
	         * background
	         */
	        instance.startForeground(R.string.notification, instance.notification);
		}
	}
	
	public static void stopForeground() 
	{
		if (instance.notification != null)
		{
			instance.stopForeground(true);
			instance.mNM.cancel(R.string.notification);
		}
	}
}