aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apg/util/ApgCon.java
blob: 73ee66ad94aabec8d51d80a2668d1aa4e7c91023 (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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
/*
 * Copyright (C) 2011 Markus Doits <markus.doits@googlemail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apg.util;

import org.apg.util.ApgConInterface.OnCallFinishListener;
import org.apg.IApgService;

import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

/**
 * A APG-AIDL-Wrapper
 * 
 * <p>
 * This class can be used by other projects to simplify connecting to the
 * APG-AIDL-Service. Kind of wrapper of for AIDL.
 * </p>
 * 
 * <p>
 * It is not used in this project.
 * </p>
 * 
 * @author Markus Doits <markus.doits@googlemail.com>
 * @version 1.1rc1
 * 
 */
public class ApgCon {
    private static final boolean LOCAL_LOGV = true;
    private static final boolean LOCAL_LOGD = true;

    private final static String TAG = "ApgCon";
    private final static int API_VERSION = 2; // aidl api-version it expects
    private final static String BLOB_URI = "content://org.thialfihar.android.apg.provider.apgserviceblobprovider";
    
    /**
     * How many seconds to wait for a connection to AGP when connecting.
     * Being unsuccessful for this number of seconds, a connection
     * is assumed to be failed.
     */
    public int secondsToWaitForConnection = 15;

    private class CallAsync extends AsyncTask<String, Void, Void> {

        @Override
        protected Void doInBackground(String... arg) {
            if( LOCAL_LOGD ) Log.d(TAG, "Async execution starting");
            call(arg[0]);
            return null;
        }

        protected void onPostExecute(Void res) {
            if( LOCAL_LOGD ) Log.d(TAG, "Async execution finished");
            mAsyncRunning = false;

        }

    }

    private final Context mContext;
    private final error mConnectionStatus;
    private boolean mAsyncRunning = false;
    private OnCallFinishListener mOnCallFinishListener;

    private final Bundle mResult = new Bundle();
    private final Bundle mArgs = new Bundle();
    private final ArrayList<String> mErrorList = new ArrayList<String>();
    private final ArrayList<String> mWarningList = new ArrayList<String>();

    /** Remote service for decrypting and encrypting data */
    private IApgService mApgService = null;

    /** Set apgService accordingly to connection status */
    private ServiceConnection mApgConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            if( LOCAL_LOGD ) Log.d(TAG, "IApgService bound to apgService");
            mApgService = IApgService.Stub.asInterface(service);
        }

        public void onServiceDisconnected(ComponentName className) {
            if( LOCAL_LOGD ) Log.d(TAG, "IApgService disconnected");
            mApgService = null;
        }
    };
    
    /**
     * Different types of local errors
     */
    public static enum error {
        /**
         * no error
         */
        NO_ERROR,
        /**
         * generic error
         */
        GENERIC,
        /**
         * connection to apg service not possible
         */
        CANNOT_BIND_TO_APG,
        /**
         * function to call not provided
         */
        CALL_MISSING,
        /**
         * apg service does not know what to do
         */
        CALL_NOT_KNOWN,
        /**
         * could not find APG being installed
         */
        APG_NOT_FOUND,
        /**
         * found APG but without AIDL interface
         */
        APG_AIDL_MISSING,
        /**
         * found APG but with wrong API
         */
        APG_API_MISSMATCH
    }

    private static enum ret {
        ERROR, // returned from AIDL
        RESULT, // returned from AIDL
        WARNINGS, // mixed AIDL and LOCAL
        ERRORS, // mixed AIDL and LOCAL
    }

    /**
     * Constructor
     * 
     * <p>
     * Creates a new ApgCon object and searches for the right APG version on
     * initialization. If not found, errors are printed to the error log.
     * </p>
     * 
     * @param ctx
     *            the running context
     */
    public ApgCon(Context ctx) {
        if( LOCAL_LOGV ) Log.v(TAG, "EncryptionService created");
        mContext = ctx;

        error tmpError = null;
        try {
            if( LOCAL_LOGV ) Log.v(TAG, "Searching for the right APG version");
            ServiceInfo apgServices[] = ctx.getPackageManager().getPackageInfo("org.thialfihar.android.apg",
                    PackageManager.GET_SERVICES | PackageManager.GET_META_DATA).services;
            if (apgServices == null) {
                Log.e(TAG, "Could not fetch services");
                tmpError = error.GENERIC;
            } else {
                boolean apgServiceFound = false;
                for (ServiceInfo inf : apgServices) {
                    if( LOCAL_LOGV ) Log.v(TAG, "Found service of APG: " + inf.name);
                    if (inf.name.equals("org.thialfihar.android.apg.ApgService")) {
                        apgServiceFound = true;
                        if (inf.metaData == null) {
                            Log.w(TAG, "Could not determine ApgService API");
                            Log.w(TAG, "This probably won't work!");
                            mWarningList.add("(LOCAL) Could not determine ApgService API");
                            tmpError = error.APG_API_MISSMATCH;
                        } else if (inf.metaData.getInt("api_version") != API_VERSION) {
                            Log.w(TAG, "Found ApgService API version " + inf.metaData.getInt("api_version") + " but exspected " + API_VERSION);
                            Log.w(TAG, "This probably won't work!");
                            mWarningList.add("(LOCAL) Found ApgService API version " + inf.metaData.getInt("api_version") + " but exspected " + API_VERSION);
                            tmpError = error.APG_API_MISSMATCH;
                        } else {
                            if( LOCAL_LOGV ) Log.v(TAG, "Found api_version " + API_VERSION + ", everything should work");
                            tmpError = error.NO_ERROR;
                        }
                    }
                }

                if (!apgServiceFound) {
                    Log.e(TAG, "Could not find APG with AIDL interface, this probably won't work");
                    mErrorList.add("(LOCAL) Could not find APG with AIDL interface, this probably won't work");
                    mResult.putInt(ret.ERROR.name(), error.APG_AIDL_MISSING.ordinal());
                    tmpError = error.APG_NOT_FOUND;
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Could not find APG, is it installed?", e);
            mErrorList.add("(LOCAL) Could not find APG, is it installed?");
            mResult.putInt(ret.ERROR.name(), error.APG_NOT_FOUND.ordinal());
            tmpError = error.APG_NOT_FOUND;
        }
        
        mConnectionStatus = tmpError;

    }

    /** try to connect to the apg service */
    private boolean connect() {
        if( LOCAL_LOGV ) Log.v(TAG, "trying to bind the apgService to context");

        if (mApgService != null) {
            if( LOCAL_LOGV ) Log.v(TAG, "allready connected");
            return true;
        }

        try {
            mContext.bindService(new Intent(IApgService.class.getName()), mApgConnection, Context.BIND_AUTO_CREATE);
        } catch (Exception e) {
            Log.e(TAG, "could not bind APG service", e);
            return false;
        }

        int waitCount = 0;
        while (mApgService == null && waitCount++ < secondsToWaitForConnection) {
            if( LOCAL_LOGV ) Log.v(TAG, "sleeping 1 second to wait for apg");
            android.os.SystemClock.sleep(1000);
        }

        if (waitCount >= secondsToWaitForConnection) {
            if( LOCAL_LOGV ) Log.v(TAG, "slept waiting for nothing!");
            return false;
        }

        return true;
    }

    /**
     * Disconnects ApgCon from Apg
     * 
     * <p>
     * This should be called whenever all work with APG is done (e.g. everything
     * you wanted to encrypt is encrypted), since connections with AIDL should
     * not be upheld indefinitely.
     * <p>
     * 
     * <p>
     * Also, if you destroy you end using your ApgCon-instance, this must be
     * called or else the connection to APG is leaked
     * </p>
     */
    public void disconnect() {
        if( LOCAL_LOGV ) Log.v(TAG, "disconnecting apgService");
        if (mApgService != null) {
            mContext.unbindService(mApgConnection);
            mApgService = null;
        }
    }

    private boolean initialize() {
        if (mApgService == null) {
            if (!connect()) {
                if( LOCAL_LOGV ) Log.v(TAG, "connection to apg service failed");
                return false;
            }
        }
        return true;
    }

    /**
     * Calls a function from APG's AIDL-interface
     * 
     * <p>
     * After you have set up everything with {@link #setArg(String, String)}
     * (and variants), you can call a function of the AIDL-interface. This
     * will:
     * <ul>
     * <li>start connection to the remote interface (if not already connected)</li>
     * <li>call the function passed with all parameters synchronously</li>
     * <li>set up everything to retrieve the result and/or warnings/errors</li>
     * <li>call the callback if provided
     * </ul>
     * </p>
     * 
     * <p>
     * Note your thread will be blocked during execution - if you want to call
     * the function asynchronously, see {@link #callAsync(String)}.
     * </p>
     * 
     * @param function
     *            a remote function to call
     * @return true, if call successful (= no errors), else false
     * 
     * @see #callAsync(String)
     * @see #setArg(String, String)
     * @see #setOnCallFinishListener(OnCallFinishListener)
     */
    public boolean call(String function) {
        boolean success = this.call(function, mArgs, mResult);
        if (mOnCallFinishListener != null) {
            try {
                if( LOCAL_LOGD ) Log.d(TAG, "About to execute callback");
                mOnCallFinishListener.onCallFinish(mResult);
                if( LOCAL_LOGD ) Log.d(TAG, "Callback executed");
            } catch (Exception e) {
                Log.w(TAG, "Exception on callback: (" + e.getClass() + ") " + e.getMessage(), e);
                mWarningList.add("(LOCAL) Could not execute callback (" + e.getClass() + "): " + e.getMessage());
            }
        }
        return success;
    }

    /**
     * Calls a function of remote interface asynchronously
     * 
     * <p>
     * This does exactly the same as {@link #call(String)}, but asynchronously.
     * While connection to APG and work are done in background, your thread can
     * go on executing.
     * <p>
     * 
     * <p>
     * To see whether the task is finished, you have two possibilities:
     * <ul>
     * <li>In your thread, poll {@link #isRunning()}</li>
     * <li>Supply a callback with {@link #setOnCallFinishListener(OnCallFinishListener)}</li>
     * </ul>
     * </p>
     * 
     * @param function
     *            a remote function to call
     * 
     * @see #call(String)
     * @see #isRunning()
     * @see #setOnCallFinishListener(OnCallFinishListener)
     */
    public void callAsync(String function) {
        mAsyncRunning = true;
        new CallAsync().execute(function);
    }

    private boolean call(String function, Bundle pArgs, Bundle pReturn) {

        if (!initialize()) {
            mErrorList.add("(LOCAL) Cannot bind to ApgService");
            mResult.putInt(ret.ERROR.name(), error.CANNOT_BIND_TO_APG.ordinal());
            return false;
        }

        if (function == null || function.length() == 0) {
            mErrorList.add("(LOCAL) Function to call missing");
            mResult.putInt(ret.ERROR.name(), error.CALL_MISSING.ordinal());
            return false;
        }

        try {
            Boolean success = (Boolean) IApgService.class.getMethod(function, Bundle.class, Bundle.class).invoke(mApgService, pArgs, pReturn);
            mErrorList.addAll(pReturn.getStringArrayList(ret.ERRORS.name()));
            mWarningList.addAll(pReturn.getStringArrayList(ret.WARNINGS.name()));
            return success;
        } catch (NoSuchMethodException e) {
            Log.e(TAG, "Remote call not known (" + function + "): " + e.getMessage(), e);
            mErrorList.add("(LOCAL) Remote call not known (" + function + "): " + e.getMessage());
            mResult.putInt(ret.ERROR.name(), error.CALL_NOT_KNOWN.ordinal());
            return false;
        } catch (InvocationTargetException e) {
            Throwable orig = e.getTargetException();
            Log.w(TAG, "Exception of type '" + orig.getClass() + "' on AIDL call '" + function + "': " + orig.getMessage(), orig);
            mErrorList.add("(LOCAL) Exception of type '" + orig.getClass() + "' on AIDL call '" + function + "': " + orig.getMessage());
            return false;
        } catch (Exception e) {
            Log.e(TAG, "Generic error (" + e.getClass() + "): " + e.getMessage(), e);
            mErrorList.add("(LOCAL) Generic error (" + e.getClass() + "): " + e.getMessage());
            mResult.putInt(ret.ERROR.name(), error.GENERIC.ordinal());
            return false;
        }

    }

    /**
     * Set a string argument for APG
     * 
     * <p>
     * This defines a string argument for APG's AIDL-interface.
     * </p>
     * 
     * <p>
     * To know what key-value-pairs are possible (or required), take a look into
     * the IApgService.aidl
     * </p>
     * 
     * <p>
     * Note that parameters are not reseted after a call, so you have to
     * reset ({@link #clearArgs()}) them manually if you want to.
     * </p>
     * 
     * 
     * @param key
     *            the key
     * @param val
     *            the value
     * 
     * @see #clearArgs()
     */
    public void setArg(String key, String val) {
        mArgs.putString(key, val);
    }

    /**
     * Set a string-array argument for APG
     * 
     * <p>
     * If the AIDL-parameter is an {@literal ArrayList<String>}, you have to use
     * this function.
     * </p>
     * 
     * <code>
     * <pre>
     * setArg("a key", new String[]{ "entry 1", "entry 2" });
     * </pre>
     * </code>
     * 
     * @param key
     *            the key
     * @param vals
     *            the value
     * 
     * @see #setArg(String, String)
     */
    public void setArg(String key, String vals[]) {
        ArrayList<String> list = new ArrayList<String>();
        for (String val : vals) {
            list.add(val);
        }
        mArgs.putStringArrayList(key, list);
    }

    /**
     * Set up a boolean argument for APG
     * 
     * @param key
     *            the key
     * @param vals
     *            the value
     * 
     * @see #setArg(String, String)
     */
    public void setArg(String key, boolean val) {
        mArgs.putBoolean(key, val);
    }

    /**
     * Set up a int argument for APG
     * 
     * @param key
     *            the key
     * @param vals
     *            the value
     * 
     * @see #setArg(String, String)
     */
    public void setArg(String key, int val) {
        mArgs.putInt(key, val);
    }

    /**
     * Set up a int-array argument for APG
     * <p>
     * If the AIDL-parameter is an {@literal ArrayList<Integer>}, you have to
     * use this function.
     * </p>
     * 
     * @param key
     *            the key
     * @param vals
     *            the value
     * 
     * @see #setArg(String, String)
     */
    public void setArg(String key, int vals[]) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int val : vals) {
            list.add(val);
        }
        mArgs.putIntegerArrayList(key, list);
    }
    
    /**
     * Set up binary data to en/decrypt
     * 
     * @param is
     *          InputStream to get the data from
     */
    public void setBlob(InputStream is) {
        if( LOCAL_LOGD ) Log.d(TAG, "setBlob() called");
        // 1. get the new contentUri
        ContentResolver cr = mContext.getContentResolver();
        Uri contentUri = cr.insert(Uri.parse(BLOB_URI), new ContentValues());
        
        // 2. insert binary data
        OutputStream os = null;
        try {
            os = cr.openOutputStream(contentUri, "w");
        } catch( Exception e ) {
            Log.e(TAG, "... exception on setBlob", e);
        }
        
        byte[] buffer = new byte[8];
        int len = 0;
        try {
            while( (len = is.read(buffer)) != -1) {
                os.write(buffer, 0, len);
            }
            if(LOCAL_LOGD) Log.d(TAG, "... write finished, now closing");
            os.close();
        } catch (Exception e) {
            Log.e(TAG, "... error on writing buffer", e);
        }
        
        mArgs.putString("BLOB", contentUri.toString() );
    }

    /**
     * Clears all arguments
     * 
     * <p>
     * Anything the has been set up with the various
     * {@link #setArg(String, String)} functions is cleared.
     * </p>
     * 
     * <p>
     * Note that any warning, error, callback, result, etc. is NOT cleared with
     * this.
     * </p>
     * 
     * @see #reset()
     */
    public void clearArgs() {
        mArgs.clear();
    }

    /**
     * Return the object associated with the key
     * 
     * @param key
     *            the object's key you want to return
     * @return an object at position key, or null if not set
     */
    public Object getArg(String key) {
        return mArgs.get(key);
    }

    /**
     * Iterates through the errors
     * 
     * <p>
     * With this method you can iterate through all errors. The errors are only
     * returned once and deleted immediately afterwards, so you can only return
     * each error once.
     * </p>
     * 
     * @return a human readable description of a error that happened, or null if
     *         no more errors
     * 
     * @see #hasNextError()
     * @see #clearErrors()
     */
    public String getNextError() {
        if (mErrorList.size() != 0)
            return mErrorList.remove(0);
        else
            return null;
    }

    /**
     * Check if there are any new errors
     * 
     * @return true, if there are unreturned errors, false otherwise
     * 
     * @see #getNextError()
     */
    public boolean hasNextError() {
        return mErrorList.size() != 0;
    }

    /**
     * Get the numeric representation of the last error
     * 
     * <p>
     * Values <100 mean the error happened locally, values >=100 mean the error
     * happened at the remote side (APG). See the IApgService.aidl (or get the
     * human readable description with {@link #getNextError()}) for what
     * errors >=100 mean.
     * </p>
     * 
     * @return the id of the error that happened
     */
    public int getError() {
        if (mResult.containsKey(ret.ERROR.name()))
            return mResult.getInt(ret.ERROR.name());
        else
            return -1;
    }

    /**
     * Iterates through the warnings
     * 
     * <p>
     * With this method you can iterate through all warnings. Warnings are
     * only returned once and deleted immediately afterwards, so you can only
     * return each warning once.
     * </p>
     * 
     * @return a human readable description of a warning that happened, or null
     *         if no more warnings
     * 
     * @see #hasNextWarning()
     * @see #clearWarnings()
     */
    public String getNextWarning() {
        if (mWarningList.size() != 0)
            return mWarningList.remove(0);
        else
            return null;
    }

    /**
     * Check if there are any new warnings
     * 
     * @return true, if there are unreturned warnings, false otherwise
     * 
     * @see #getNextWarning()
     */
    public boolean hasNextWarning() {
        return mWarningList.size() != 0;
    }

    /**
     * Get the result
     * 
     * <p>
     * This gets your result. After doing an encryption or decryption with APG,
     * you get the output with this function.
     * </p>
     * 
     * <p>
     * Note when your last remote call is unsuccessful, the result will
     * still have the same value like the last successful call (or null, if no
     * call was successful). To ensure you do not work with old call's results,
     * either be sure to {@link #reset()} (or at least {@link #clearResult()})
     * your instance before each new call or always check that
     * {@link #hasNextError()} is false.
     * </p>
     * 
     * <p>
     * Note: When handling binary data with {@link #setBlob(InputStream)}, you
     * get your result with {@link #getBlobResult()}.
     * </p>
     * 
     * @return the mResult of the last {@link #call(String)} or
     *         {@link #callAsync(String)}.
     * 
     * @see #reset()
     * @see #clearResult()
     * @see #getResultBundle()
     * @see #getBlobResult()
     */
    public String getResult() {
        return mResult.getString(ret.RESULT.name());
    }

    /**
     * Get the binary result
     * 
     * <p>
     * This gets your binary result. It only works if you called {@link #setBlob(InputStream)} before.
     * 
     * If you did not call encrypt nor decrypt, this will be the same data as you inputed.
     * </p>
     * 
     * @return InputStream of the binary data which was en/decrypted
     * 
     * @see #setBlob(InputStream)
     * @see #getResult()
     */
    public InputStream getBlobResult() {
        if(mArgs.containsKey("BLOB")) {
            ContentResolver cr = mContext.getContentResolver();
            InputStream in = null;
            try {
                in = cr.openInputStream(Uri.parse(mArgs.getString("BLOB")));
            } catch( Exception e ) {
                Log.e(TAG, "Could not return blob in result", e);
            }
            return in;
        } else {
            return null;
        }
    }

    /**
     * Get the result bundle
     * 
     * <p>
     * Unlike {@link #getResult()}, which only returns any en-/decrypted
     * message, this function returns the complete information that was returned
     * by Apg. This also includes the "RESULT", but additionally the warnings,
     * errors and any other information.
     * </p>
     * <p>
     * For warnings and errors it is suggested to use the functions that are
     * provided here, namely {@link #getError()}, {@link #getNextError()},
     * {@link #get_next_Warning()} etc.), but if any call returns something non
     * standard, you have access to the complete result bundle to extract the
     * information.
     * </p>
     * 
     * @return the complete result bundle of the last call to apg
     */
    public Bundle getResultBundle() {
        return mResult;
    }

    public error getConnectionStatus() {
        return mConnectionStatus;
    }

    /**
     * Clears all unfetched errors
     * 
     * @see #getNextError()
     * @see #hasNextError()
     */
    public void clearErrors() {
        mErrorList.clear();
        mResult.remove(ret.ERROR.name());
    }

    /**
     * Clears all unfetched warnings
     * 
     * @see #getNextWarning()
     * @see #hasNextWarning()
     */
    public void clearWarnings() {
        mWarningList.clear();
    }

    /**
     * Clears the last mResult
     * 
     * @see #getResult()
     */
    public void clearResult() {
        mResult.remove(ret.RESULT.name());
    }

    /**
     * Set a callback listener when call to AIDL finishes
     * 
     * @param obj
     *            a object to call back after async execution
     * @see ApgConInterface
     */
    public void setOnCallFinishListener(OnCallFinishListener lis) {
        mOnCallFinishListener = lis;
    }

    /**
     * Clears any callback object
     * 
     * @see #setOnCallFinishListener(OnCallFinishListener)
     */
    public void clearOnCallFinishListener() {
        mOnCallFinishListener = null;
    }

    /**
     * Checks if an async execution is running
     * 
     * <p>
     * If you started something with {@link #callAsync(String)}, this will
     * return true if the task is still running
     * </p>
     * 
     * @return true, if an async task is still running, false otherwise
     * 
     * @see #callAsync(String)
     * 
     */
    public boolean isRunning() {
        return mAsyncRunning;
    }

    /**
     * Completely resets your instance
     * 
     * <p>
     * This currently resets everything in this instance. Errors, warnings,
     * results, callbacks, ... are removed. Any connection to the remote
     * interface is upheld, though.
     * </p>
     * 
     * <p>
     * Note when an async execution ({@link #callAsync(String)}) is
     * running, it's result, warnings etc. will still be evaluated (which might
     * be not what you want). Also mind that any callback you set is also
     * reseted, so when finishing the execution any before defined callback will
     * NOT BE TRIGGERED.
     * </p>
     */
    public void reset() {
        clearErrors();
        clearWarnings();
        clearArgs();
        clearOnCallFinishListener();
        mResult.clear();
    }

}