summaryrefslogtreecommitdiffstats
path: root/Ministro/src/org/kde/necessitas/ministro/MinistroService.java
blob: fb5df5e17d9b6e8b3c1bad4300760b23d4896d03 (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
    Copyright (c) 2011, BogDan Vatra <bog_dan_ro@yahoo.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.kde.necessitas.ministro;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class MinistroService extends Service
{
    private static final String TAG = "MinistroService";

    private static final String MINISTRO_CHECK_UPDATES_KEY="LASTCHECK";
    private static final String MINISTRO_REPOSITORY_KEY="REPOSITORY";
    private static final String MINISTRO_DEFAULT_REPOSITORY="stable";

    /// Ministro server parameter keys
    private static final String REQUIRED_MODULES_KEY="required.modules";
    private static final String APPLICATION_TITLE_KEY="application.title";
    private static final String QT_PROVIDER_KEY="qt.provider";
    private static final String MINIMUM_MINISTRO_API_KEY="minimum.ministro.api";
    private static final String MINIMUM_QT_VERSION_KEY="minimum.qt.version";
    /// Ministro server parameter keys

    /// loader parameter keys
    private static final String ERROR_CODE_KEY="error.code";
    private static final String ERROR_MESSAGE_KEY="error.message";
    private static final String DEX_PATH_KEY="dex.path";
    private static final String LIB_PATH_KEY="lib.path";
    private static final String LOADER_CLASS_NAME_KEY="loader.class.name";
    private static final String NATIVE_LIBRARIES_KEY="native.libraries";
    private static final String ENVIRONMENT_VARIABLES_KEY="environment.variables";
    private static final String APPLICATION_PARAMETERS_KEY="application.parameters";
    /// loader parameter keys

    /// loader error codes
    private static final int EC_NO_ERROR=0;
    private static final int EC_INCOMPATIBLE=1;
    private static final int EC_NOT_FOUND=2;
    private static final int EC_INVALID_PARAMETERS=3;
    /// loader error codes


    public static String getRepository(Context c)
    {
        SharedPreferences preferences=c.getSharedPreferences("Ministro", MODE_PRIVATE);
        return preferences.getString(MINISTRO_REPOSITORY_KEY,MINISTRO_DEFAULT_REPOSITORY);
    }

    public static void setRepository(Context c, String value)
    {
        SharedPreferences preferences=c.getSharedPreferences("Ministro", MODE_PRIVATE);
        SharedPreferences.Editor editor= preferences.edit();
        editor.putString(MINISTRO_REPOSITORY_KEY,value);
        editor.putLong(MINISTRO_CHECK_UPDATES_KEY,0);
        editor.commit();
    }

    // used to check Ministro Service compatibility
    private static final int MINISTRO_MIN_API_LEVEL=1;
    private static final int MINISTRO_MAX_API_LEVEL=1;

    // MinistroService instance, its used by MinistroActivity to directly access services data (e.g. libraries)
    private static MinistroService m_instance = null;
    private String m_environmentVariables = null;
    private String m_applicationParams = null;
    private String m_loaderClassName = null;
    private String m_pathSeparator = null;
    public static MinistroService instance()
    {
        return m_instance;
    }

    public MinistroService()
    {
        m_instance = this;
    }

    private int m_actionId=0; // last actions id


    // current downloaded libraries
    private ArrayList<Library> m_downloadedLibraries = new ArrayList<Library>();

    ArrayList<Library> getDownloadedLibraries()
    {
        synchronized (this)
        {
            return m_downloadedLibraries;
        }
    }

    // current available libraries
    private ArrayList<Library> m_availableLibraries = new ArrayList<Library>();
    ArrayList<Library> getAvailableLibraries()
    {
        synchronized (this)
        {
            return m_availableLibraries;
        }
    }

    class CheckForUpdates extends AsyncTask<Void, Void, Void>
    {
        @Override
        protected void onPreExecute()
        {
            if (m_version<MinistroActivity.downloadVersionXmlFile(MinistroService.this, true))
            {
                NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                int icon = R.drawable.icon;
                CharSequence tickerText = getResources().getString(R.string.new_qt_libs_msg);       // ticker-text
                long when = System.currentTimeMillis();                                             // notification time
                Context context = getApplicationContext();                                          // application Context
                CharSequence contentTitle = getResources().getString(R.string.ministro_update_msg); // expanded message title
                CharSequence contentText = getResources().getString(R.string.new_qt_libs_tap_msg);  // expanded message text

                Intent notificationIntent = new Intent(MinistroService.this, MinistroActivity.class);
                PendingIntent contentIntent = PendingIntent.getActivity(MinistroService.this, 0, notificationIntent, 0);

                // the next two lines initialize the Notification, using the configurations above
                Notification notification = new Notification(icon, tickerText, when);
                notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
                notification.defaults |= Notification.DEFAULT_SOUND;
                notification.defaults |= Notification.DEFAULT_VIBRATE;
                notification.defaults |= Notification.DEFAULT_LIGHTS;
                try {
                    nm.notify(1, notification);
                } catch(Exception e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        protected Void doInBackground(Void... params) {
            return null;
        }
    }


    // this method reload all downloaded libraries
    synchronized ArrayList<Library> refreshLibraries(boolean checkCrc)
    {
        synchronized (this)
        {
            try
            {
                m_downloadedLibraries.clear();
                m_availableLibraries.clear();
                if (! (new File(m_versionXmlFile)).exists())
                    return m_downloadedLibraries;
                DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
                Document dom = documentBuilder.parse(new FileInputStream(m_versionXmlFile));
                Element root = dom.getDocumentElement();
                m_version = Double.valueOf(root.getAttribute("version"));
                m_loaderClassName=root.getAttribute("loaderClassName");
                m_applicationParams=root.getAttribute("applicationParameters");
                m_applicationParams=m_applicationParams.replaceAll("MINISTRO_PATH", getFilesDir().getAbsolutePath());
                m_environmentVariables=root.getAttribute("environmentVariables");
                m_environmentVariables=m_environmentVariables.replaceAll("MINISTRO_PATH", getFilesDir().getAbsolutePath());
                root.normalize();
                Node node = root.getFirstChild();
                while(node != null)
                {
                    if (node.getNodeType() == Node.ELEMENT_NODE)
                    {
                        Library lib= Library.getLibrary((Element)node, false);
                        File file=new File(m_qtLibsRootPath + lib.filePath);
                        if (file.exists())
                        {
                            if (checkCrc && !Library.checkCRC(file.getAbsolutePath(), lib.sha1))
                                file.delete();
                            else
                                m_downloadedLibraries.add(lib);
                        }
                        m_availableLibraries.add(lib);
                    }
                    // Workaround for an unbelievable bug !!!
                    try {
                        node = node.getNextSibling();
                    } catch (Exception e) {
                        e.printStackTrace();
                        break;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return m_downloadedLibraries;
    }

    // version xml file
    private String m_versionXmlFile;
    public String getVersionXmlFile()
    {
        return m_versionXmlFile;
    }

    private String m_qtLibsRootPath;
    public String getQtLibsRootPath()
    {
        return m_qtLibsRootPath;
    }

    private double m_version = -1;
    public double getVersion()
    {
        return m_version;
    }

    // class used to fire an action, this class is used
    // to start an activity when user needs more libraries to start its application
    class ActionStruct
    {
        ActionStruct(IMinistroCallback cb, String[] m, ArrayList<String> notFoundMoules, String appName)
        {
            id=++m_actionId;
            callback = cb;
            modules = m;
        }
        public int id;
        public IMinistroCallback callback;
        public String[] modules;
    }

    // we can have more then one action
    ArrayList<ActionStruct> m_actions = new ArrayList<ActionStruct>();

    @Override
    public void onCreate()
    {
        m_versionXmlFile = getFilesDir().getAbsolutePath()+"/version.xml";
        m_qtLibsRootPath = getFilesDir().getAbsolutePath()+"/qt/";
        m_pathSeparator = System.getProperty("path.separator", ":");
        SharedPreferences preferences=getSharedPreferences("Ministro", MODE_PRIVATE);
        long lastCheck = preferences.getLong(MINISTRO_CHECK_UPDATES_KEY,0);
        if (MinistroActivity.isOnline(this) && System.currentTimeMillis()-lastCheck>24l*3600*100) // check once/day
        {
            refreshLibraries(true);
            SharedPreferences.Editor editor= preferences.edit();
            editor.putLong(MINISTRO_CHECK_UPDATES_KEY,System.currentTimeMillis());
            editor.commit();
            new CheckForUpdates().execute((Void[])null);
        }
        else
            refreshLibraries(false);
        super.onCreate();
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        return new IMinistro.Stub()
        {
            @Override
            public void requestLoader(IMinistroCallback callback, Bundle parameters) throws RemoteException
            {

                checkModulesImpl(callback, parameters);
            }
        };
    }

    /**
    * Implements the {@link IMinistro.Stub#checkModules(IMinistroCallback, String[], String, int, int)}
    * service method.
    *
    * @param callback
    * @param modules
    * @param appName
    * @param ministroApiLevel
    * @param necessitasApiLevel
    * @throws RemoteException
    */
    final void checkModulesImpl(IMinistroCallback callback, Bundle parameters) throws RemoteException
    {
        if (!parameters.containsKey(REQUIRED_MODULES_KEY)
                || !parameters.containsKey(APPLICATION_TITLE_KEY)
                || !parameters.containsKey(MINIMUM_MINISTRO_API_KEY)
                || !parameters.containsKey(MINIMUM_QT_VERSION_KEY))
        {
            Bundle loaderParams = new Bundle();
            loaderParams.putInt(ERROR_CODE_KEY, EC_INVALID_PARAMETERS);
            loaderParams.putString(ERROR_MESSAGE_KEY, getResources().getString(R.string.invalid_parameters));
            try
            {
                callback.loaderReady(loaderParams);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            Log.e(TAG, "Invalid parameters: " + parameters.toString());
            return;
        }
        int ministroApiLevel = parameters.getInt(MINIMUM_MINISTRO_API_KEY);
        String[] modules = parameters.getStringArray(REQUIRED_MODULES_KEY);
        String appName = parameters.getString(APPLICATION_TITLE_KEY);


        @SuppressWarnings("unused")
        int qtApiLevel = parameters.getInt(MINIMUM_QT_VERSION_KEY); // TODO check if current QT version is compatible with required version
        @SuppressWarnings("unused")
        String qtProvider="necessitas";
        if (parameters.containsKey(QT_PROVIDER_KEY))
            qtProvider=parameters.getString(QT_PROVIDER_KEY); // TODO add the possibility to have more than one provider

        if (ministroApiLevel<MINISTRO_MIN_API_LEVEL || ministroApiLevel>MINISTRO_MAX_API_LEVEL)
        {
            // panic !!! Ministro service is not compatible, user should upgrade Ministro package
            Bundle loaderParams = new Bundle();
            loaderParams.putInt(ERROR_CODE_KEY, EC_INCOMPATIBLE);
            loaderParams.putString(ERROR_MESSAGE_KEY, getResources().getString(R.string.incompatible_ministo_api));
            try
            {
                callback.loaderReady(loaderParams);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            Log.e(TAG, "Ministro cannot satisfy API version: " + ministroApiLevel);
            return;
        }

        // check necessitasApiLevel !!! I'm pretty sure some people will completely ignore my warning
        // and they will deploying apps to Android Market, so let's try to give them a chance.

        // this method is called by the activity client who needs modules.
        ArrayList<String> notFoundModules = new ArrayList<String>();
        Bundle loaderParams = checkModules(modules, notFoundModules);
        if (loaderParams.containsKey(ERROR_CODE_KEY) && EC_NO_ERROR == loaderParams.getInt(ERROR_CODE_KEY))
        {
            try
            {
                callback.loaderReady(loaderParams);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        else
        {
            // Starts a retrieval of the modules which are not readily accessible.
            startRetrieval(callback, modules, notFoundModules, appName);
        }
    }

    /**
    * Creates and sets up a {@link MinistroActivity} to retrieve the modules specified in the
    * <code>notFoundModules</code> argument.
    *
    * @param callback
    * @param modules
    * @param notFoundModules
    * @param appName
    * @throws RemoteException
    */
    private void startRetrieval(IMinistroCallback callback, String[] modules
                                , ArrayList<String> notFoundModules, String appName) throws RemoteException
    {
        ActionStruct as = new ActionStruct(callback, modules, notFoundModules, appName);
        m_actions.add(as); // if not, lets start an activity to do it.

        Intent intent = new Intent(MinistroService.this, MinistroActivity.class);
        intent.putExtra("id", as.id);
        String[] libs = notFoundModules.toArray(new String[notFoundModules.size()]);
        intent.putExtra("modules", libs);
        intent.putExtra("name", appName);

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        boolean failed = false;
        try
        {
            MinistroService.this.startActivity(intent);
        }
        catch(Exception e)
        {
            failed = true;
            throw (RemoteException) new RemoteException().initCause(e);
        }
        finally
        {
            // Removes the dead Activity from our list as it will never finish by itself.
            if (failed)
                m_actions.remove(as);
        }
    }

    /**
    * Called by a finished {@link MinistroActivity} in order to let
    * the service notify the application which caused the activity about
    * the result of the retrieval.
    *
    * @param id
    */
    void retrievalFinished(int id)
    {
        for (int i=0;i<m_actions.size();i++)
        {
            ActionStruct action=m_actions.get(i);
            if (action.id==id)
            {
                postRetrieval(action.callback, action.modules);
                m_actions.remove(i);
                break;
            }
        }
        if (m_actions.size() == 0)
            m_actionId = 0;
    }

    /**
    * Helper method for the last step of the retrieval process.
    *
    * <p>Checks the availability of the requested modules and informs
    * the requesting application about it via the {@link IMinistroCallback}
    * instance.</p>
    *
    * @param callback
    * @param modules
    */
    private void postRetrieval(IMinistroCallback callback, String[] modules)
    {
        // Does a final check whether the libraries are accessible (without caring for
        // the non-accessible ones).
        try
        {
            callback.loaderReady(checkModules(modules, null));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
    * Checks whether a given list of libraries are readily accessible (e.g. usable by a program).
    *
    * <p>If the <code>notFoundModules</code> argument is given, the method fills the list with
    * libraries that need to be retrieved first.</p>
    *
    * @param libs
    * @param notFoundModules
    * @return true if all modules are available
    */
    Bundle checkModules(String[] modules, ArrayList<String> notFoundModules)
    {
        Bundle params = new Bundle();
        boolean res=true;
        ArrayList<Module> libs= new ArrayList<Module>();
        Set<String> jars= new HashSet<String>();
        for (String module: modules)
            res = res & addModules(module, libs, notFoundModules, jars); // don't stop on first error

        ArrayList<String> tempStringArray = new ArrayList<String>();
        // sort all libraries
        Collections.sort(libs, new ModuleCompare());
        for (Module lib: libs)
            tempStringArray.add(m_qtLibsRootPath+lib.path);
        params.putString(NATIVE_LIBRARIES_KEY, Library.join(tempStringArray, m_pathSeparator));

        tempStringArray.clear();
        for (String jar: jars)
            tempStringArray.add(m_qtLibsRootPath+jar);
        params.putString(DEX_PATH_KEY, Library.join(tempStringArray, m_pathSeparator));

        params.putString(LOADER_CLASS_NAME_KEY, m_loaderClassName);
        params.putString(LIB_PATH_KEY, m_qtLibsRootPath);
        params.putString(ENVIRONMENT_VARIABLES_KEY, m_environmentVariables);
        params.putString(APPLICATION_PARAMETERS_KEY, m_applicationParams);
        params.putInt(ERROR_CODE_KEY, res?EC_NO_ERROR:EC_NOT_FOUND);
        if (!res)
            params.putString(ERROR_MESSAGE_KEY, getResources().getString(R.string.dependencies_error));
        return params;
    }

/**
    * Helper method for the module resolution mechanism. It deals with an individual module's
    * resolution request.
    *
    * <p>The method checks whether a given <em>single</em> <code>module</code> is already
    * accessible or needs to be retrieved first. In the latter case the method returns
    * <code>false</code>.</p>
    *
    * <p>The method traverses a <code>module<code>'s dependencies automatically.</p>
    *
    * <p>In order to find out whether a <code>module</code> is accessible the method consults
    * the list of downloaded libraries. If found, an entry to the <code>modules</code> list is
    * added.</p>
    *
    * <p>In case the <code>module</ocde> is not immediately accessible and the <code>notFoundModules</code>
    * argument exists, a list of available libraries is consulted to fill a list of modules which
    * yet need to be retrieved.</p>
    *
    * @param module
    * @param modules
    * @param notFoundModules
    * @param jars
    * @return <code>true</code> if the given module and all its dependencies are readily available.
    */
    private boolean addModules(String module, ArrayList<Module> modules
                            , ArrayList<String> notFoundModules, Set<String> jars)
    {
        // Module argument is not supposed to be null at this point.
        if (modules == null)
            return false; // we are in deep shit if this happens

        // Short-cut: If the module is already in our list of previously found modules then we do not
        // need to consult the list of downloaded modules.
        for (int i=0;i<modules.size();i++)
        {
            if (modules.get(i).name.equals(module))
                return true;
        }

        // Consult the list of downloaded modules. If a matching entry is found, it is added to the
        // list of readily accessible modules and its dependencies are checked via a recursive call.
        for (int i = 0; i< m_downloadedLibraries.size(); i++)
        {
            if (m_downloadedLibraries.get(i).name.equals(module))
            {
                Module m = new Module();
                m.name=m_downloadedLibraries.get(i).name;
                m.path=m_downloadedLibraries.get(i).filePath;
                m.level=m_downloadedLibraries.get(i).level;
                if (m_downloadedLibraries.get(i).needs != null)
                    for(NeedsStruct needed: m_downloadedLibraries.get(i).needs)
                        if (needed.type != null && needed.type.equals("jar"))
                            jars.add(needed.filePath);
                modules.add(m);
                boolean res = true;
                if (m_downloadedLibraries.get(i).depends != null)
                    for (int depIt=0;depIt<m_downloadedLibraries.get(i).depends.length;depIt++)
                        res &= addModules(m_downloadedLibraries.get(i).depends[depIt], modules, notFoundModules, jars);
                return res;
            }
        }

        // Requested module is not readily accessible.
        if (notFoundModules != null)
        {
            // Checks list of modules which are known to not be readily accessible and returns early to
            // prevent double entries.
            for (int i=0;i<notFoundModules.size();i++)
            {
                if (notFoundModules.get(i).equals(module))
                    return false;
            }

            // Deal with not yet readily accessible module's dependencies.
            notFoundModules.add(module);
            for (int i = 0; i< m_availableLibraries.size(); i++)
            {
                if (m_availableLibraries.get(i).name.equals(module))
                {
                    if (m_availableLibraries.get(i).depends != null)
                        for (int depIt=0;depIt<m_availableLibraries.get(i).depends.length;depIt++)
                            addModules(m_availableLibraries.get(i).depends[depIt], modules, notFoundModules, jars);
                    break;
                }
            }
        }
        return false;
    }

    /** Sorter for libraries.
    *
    * Hence the order in which the libraries have to be loaded is important, it is neccessary
    * to sort them.
    */
    static private class ModuleCompare implements Comparator<Module>
    {
        @Override
        public int compare(Module a, Module b)
        {
            return a.level-b.level;
        }
    }

    /** Helper class which allows manipulating libraries.
    *
    * It is similar to the {@link Library} class but has fewer fields.
    */
    static private class Module
    {
        String path;
        String name;
        int level;
    }
}