aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/thialfihar/android/apg/utils/ApgCon.java
blob: 4852ad548c75a633c8aed83596a7b99661afa85a (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
package org.thialfihar.android.apg.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

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

import org.thialfihar.android.apg.IApgService;

/**
 * This class can be used by other projects to simplify connecting to the
 * APG-Service. Kind of wrapper of for AIDL.
 * 
 * It is not used in this project.
 */
public class ApgCon {

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

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

        protected void onPostExecute(Void result) {
            Log.d(TAG, "Async execution finished");
            async_running = false;
            if (callback_object != null && callback_method != null) {
                try {
                    Log.d(TAG, "About to execute callback");
                    callback_object.getClass().getMethod(callback_method).invoke(callback_object);
                    Log.d(TAG, "Callback executed");
                } catch (NoSuchMethodException e) {
                    Log.w(TAG, "Exception in callback: Method '" + callback_method + "' not found");
                    warning_list.add("(LOCAL) Could not execute callback, method '" + callback_method + "()' not found");
                } catch (InvocationTargetException e) {
                    Throwable orig = e.getTargetException();
                    Log.w(TAG, "Exception of type '" + orig.getClass() + "' in callback's method '" + callback_method + "()':" + orig.getMessage());
                    warning_list.add("(LOCAL) Exception of type '" + orig.getClass() + "' in callback's method '" + callback_method + "()':"
                            + orig.getMessage());
                } catch (Exception e) {
                    Log.w(TAG, "Exception on callback: (" + e.getClass() + ") " + e.getMessage());
                    warning_list.add("(LOCAL) Could not execute callback (" + e.getClass() + "): " + e.getMessage());
                }
            }
        }

    }

    private final static String TAG = "ApgCon";
    private final static int api_version = 1; // aidl api-version it expects

    private final Context mContext;
    private boolean async_running = false;
    private Object callback_object;
    private String callback_method;

    private final Bundle result = new Bundle();
    private final Bundle args = new Bundle();
    private final ArrayList<String> error_list = new ArrayList<String>();
    private final ArrayList<String> warning_list = new ArrayList<String>();
    private error local_error;

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

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

        public void onServiceDisconnected(ComponentName className) {
            Log.d(TAG, "IApgService disconnected");
            apgService = null;
        }
    };

    public static enum error {
        GENERIC, // no special type
        CANNOT_BIND_TO_APG, // connection to apg service not possible
        CALL_MISSING, // function to call not provided
        CALL_NOT_KNOWN, // apg service does not know what to do
        APG_NOT_FOUND, // could not find APG installed
        APG_AIDL_MISSING, // found APG but without AIDL interface
    }

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

    /**
     * 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) {
        Log.v(TAG, "EncryptionService created");
        mContext = ctx;

        try {
            Log.v(TAG, "Searching for the right APG version");
            ServiceInfo apg_services[] = ctx.getPackageManager().getPackageInfo("org.thialfihar.android.apg",
                    PackageManager.GET_SERVICES | PackageManager.GET_META_DATA).services;
            if (apg_services == null) {
                Log.e(TAG, "Could not fetch services");
            } else {
                boolean apg_service_found = false;
                for (ServiceInfo inf : apg_services) {
                    Log.v(TAG, "Found service of APG: " + inf.name);
                    if (inf.name.equals("org.thialfihar.android.apg.ApgService")) {
                        apg_service_found = true;
                        if (inf.metaData == null) {
                            Log.w(TAG, "Could not determine ApgService API");
                            Log.w(TAG, "This probably won't work!");
                            warning_list.add("(LOCAL) Could not determine ApgService API");
                        } 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!");
                            warning_list.add("(LOCAL) Found ApgService API version" + inf.metaData.getInt("api_version") + " but exspected " + api_version);
                        } else {
                            Log.v(TAG, "Found api_version " + api_version + ", everything should work");
                        }
                    }
                }

                if (!apg_service_found) {
                    Log.e(TAG, "Could not find APG with AIDL interface, this probably won't work");
                    error_list.add("(LOCAL) Could not find APG with AIDL interface, this probably won't work");
                    local_error = error.APG_AIDL_MISSING;
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Could not find APG, is it installed?");
            error_list.add("(LOCAL) Could not find APG, is it installed?");
            local_error = error.APG_NOT_FOUND;
        }
    }

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

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

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

        int wait_count = 0;
        while (apgService == null && wait_count++ < 15) {
            Log.v(TAG, "sleeping 1 second to wait for apg");
            android.os.SystemClock.sleep(1000);
        }

        if (wait_count >= 15) {
            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() {
        Log.v(TAG, "disconnecting apgService");
        if (apgService != null) {
            mContext.unbindService(apgConnection);
            apgService = null;
        }
    }

    private boolean initialize() {
        if (apgService == null) {
            if (!connect()) {
                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 #set_arg(String, String)}
     * (and variants), you can call a function from the AIDL-interface. This
     * will
     * <ul>
     * <li>start connection to the remote interface (if not already connected)</li>
     * <li>call the passed function with all set up parameters synchronously</li>
     * <li>set up everything to retrieve the result and/or warnings/errors</li>
     * </ul>
     * </p>
     * 
     * <p>
     * Note your thread will be blocked during execution - if you want to call
     * the function asynchronously, see {@link #call_async(String)}.
     * </p>
     * 
     * @param function
     *            a remote function to call
     * @return true, if call successful (= no errors), else false
     * 
     * @see #call_async(String)
     * @see #set_arg(String, String)
     */
    public boolean call(String function) {
        return this.call(function, args, result);
    }

    /**
     * Calls a function from 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 to possibilities:
     * <ul>
     * <li>In your thread, poll {@link #is_running()}</li>
     * <li>Supply a callback with {@link #set_callback(Object, String)}</li>
     * </ul>
     * </p>
     * 
     * @param function
     *            a remote function to call
     * 
     * @see #call(String)
     * @see #is_running()
     * @see #set_callback(Object, String)
     */
    public void call_async(String function) {
        async_running = true;
        new call_async().execute(function);
    }

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

        error_list.clear();
        warning_list.clear();

        if (!initialize()) {
            error_list.add("(LOCAL) Cannot bind to ApgService");
            local_error = error.CANNOT_BIND_TO_APG;
            return false;
        }

        if (function == null || function.length() == 0) {
            error_list.add("(LOCAL) Function to call missing");
            local_error = error.CALL_MISSING;
            return false;
        }

        try {
            Boolean success = (Boolean) IApgService.class.getMethod(function, Bundle.class, Bundle.class).invoke(apgService, pArgs, pReturn);
            error_list.addAll(pReturn.getStringArrayList(ret.ERRORS.name()));
            warning_list.addAll(pReturn.getStringArrayList(ret.WARNINGS.name()));
            pReturn.remove(ret.ERRORS.name());
            pReturn.remove(ret.WARNINGS.name());
            return success;
        } catch (NoSuchMethodException e) {
            Log.e(TAG, "Remote call not known (" + function + "): " + e.getMessage());
            error_list.add("(LOCAL) Remote call not known (" + function + "): " + e.getMessage());
            local_error = error.CALL_NOT_KNOWN;
            return false;
        } catch (InvocationTargetException e) {
            Throwable orig = e.getTargetException();
            Log.w(TAG, "Exception of type '" + orig.getClass() + "' on AIDL call '" + function + "':" + orig.getMessage());
            error_list.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());
            error_list.add("(LOCAL) Generic error (" + e.getClass() + "): " + e.getMessage());
            local_error = error.GENERIC;
            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 the parameters are not deleted after a call, so you have to
     * reset ({@link #clear_args()}) them manually if you want to.
     * </p>
     * 
     * 
     * @param key
     *            the key
     * @param val
     *            the value
     * 
     * @see #clear_args()
     */
    public void set_arg(String key, String val) {
        args.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>
     * set_arg("a key", new String[]{ "entry 1", "entry 2" });
     * </pre>
     * </code>
     * 
     * @param key
     *            the key
     * @param vals
     *            the value
     * 
     * @see #set_arg(String, String)
     */
    public void set_arg(String key, String vals[]) {
        ArrayList<String> list = new ArrayList<String>();
        for (String val : vals) {
            list.add(val);
        }
        args.putStringArrayList(key, list);
    }

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

    /**
     * Set up a int argument for APG
     * 
     * @param key
     *            the key
     * @param vals
     *            the value
     * 
     * @see #set_arg(String, String)
     */
    public void set_arg(String key, int val) {
        args.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 #set_arg(String, String)
     */
    public void set_arg(String key, int vals[]) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int val : vals) {
            list.add(val);
        }
        args.putIntegerArrayList(key, list);
    }

    /**
     * Clears all arguments
     * 
     * <p>
     * Anything the has been set up with the various
     * {@link #set_arg(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 clear_args() {
        args.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 get_arg(String key) {
        return args.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 #has_next_error()
     * @see #clear_errors()
     */
    public String get_next_error() {
        if (error_list.size() != 0)
            return error_list.remove(0);
        else
            return null;
    }

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

    /**
     * Returns the type of error happened
     * 
     * <p>
     * Currently, two error types are possible:
     * <ul>
     * <li>ret.LOCAL_ERROR: An error that happened on the caller site. This
     * might be something like connection to AIDL not possible or the funciton
     * call not know by AIDL. This means, the instance is not set up correctly
     * or prerequisites to use APG with AIDL are not met.</li>
     * <li>ret.ERROR: Connection to APG was successful, and the call started but
     * failed. Mostly this is because of wrong or missing parameters for APG.</li>
     * </ul>
     * </p>
     * 
     * @return the type of error that happend: ret.LOCAL_ERROR or ret.ERROR, or
     *         null if none happend
     */
    public ret get_error_type() {
        if (local_error != null) {
            return ret.LOCAL_ERROR;
        } else if (result.containsKey(ret.ERROR.name())) {
            return ret.ERROR;
        } else {
            return null;
        }
    }

    public error get_local_error() {
        return local_error;
    }

    public void clear_local_error() {
        local_error = null;
    }

    public int get_remote_error() {
        if (result.containsKey(ret.ERROR.name())) {
            return result.getInt(ret.ERROR.name());
        } else {
            return -1;
        }
    }

    public void clear_remote_error() {
        result.remove(ret.ERROR.name());
    }

    /**
     * Iterates through the warnings
     * 
     * <p>
     * With this method, you can iterate through all warnings. The 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 #has_next_warning()
     * @see #clear_warnings()
     */
    public String get_next_warning() {
        if (warning_list.size() != 0)
            return warning_list.remove(0);
        else
            return null;
    }

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

    /**
     * Get the result
     * 
     * <p>
     * This gets your result. After doing anything with APG, you get the output
     * with this function
     * </p>
     * <p>
     * Note, that 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 #clear_result()})
     * your instance before each new call or always check that
     * {@link #has_next_error()} is false.
     * </p>
     * 
     * @return the result of the last {@link #call(String)} or
     *         {@link #call_asinc(String)}.
     * 
     * @see #reset()
     * @see #clear_result()
     */
    public String get_result() {
        return result.getString(ret.RESULT.name());
    }

    /**
     * Clears all unfetched errors
     * 
     * @see #get_next_error()
     * @see #has_next_error()
     */
    public void clear_errors() {
        error_list.clear();
        result.remove(ret.ERROR.name());
        clear_local_error();
    }

    /**
     * Clears all unfetched warnings
     * 
     * @see #get_next_warning()
     * @see #has_next_warning()
     */
    public void clear_warnings() {
        warning_list.clear();
    }

    /**
     * Clears the last result
     * 
     * @see #get_result()
     */
    public void clear_result() {
        result.remove(ret.RESULT.name());
    }

    /**
     * Set a callback object and method
     * 
     * <p>
     * After an async execution is finished, obj.meth() will be called. You can
     * use this in order to get notified, when encrypting/decrypting of long
     * data finishes and do not have to poll {@link #is_running()} in your
     * thread. Note, that if the call of the method fails for whatever reason,
     * you won't get notified in any way - so you still should check
     * {@link #is_running()} from time to time.
     * </p>
     * 
     * <p>
     * It produces a warning fetchable with {@link #get_next_warning()} when the
     * callback fails.
     * </p>
     * 
     * <pre>
     * <code>
     * .... your class ...
     * public void callback() {
     *   // do something after encryption finished
     * }
     * 
     * public void encrypt() {
     *   ApgCon mEnc = new ApgCon(context);
     *   // set parameters
     *   mEnc.set_arg(key, value);
     *   ...
     *   
     *   // set callback object and method 
     *   mEnc.set_callback( this, "callback" );
     *   
     *   // start asynchronous call
     *   mEnc.call_async( call );
     *   
     *   // when the call_async finishes, the method "callback()" will be called automatically
     * }
     * </code>
     * </pre>
     * 
     * @param obj
     *            The object, which has the public method meth
     * @param meth
     *            Method to call on the object obj
     */
    public void set_callback(Object obj, String meth) {
        set_callback_object(obj);
        set_callback_method(meth);
    }

    /**
     * Set a callback object
     * 
     * @param obj
     *            a object to call back after async execution
     * @see #set_callback(Object, String)
     */
    public void set_callback_object(Object obj) {
        callback_object = obj;
    }

    /**
     * Set a callback method
     * 
     * @param meth
     *            a method to call on a callback object after async execution
     * @see #set_callback(Object, String)
     */
    public void set_callback_method(String meth) {
        callback_method = meth;
    }

    /**
     * Clears any callback object
     * 
     * @see #set_callback(Object, String)
     */
    public void clear_callback_object() {
        callback_object = null;
    }

    /**
     * Clears any callback method
     * 
     * @see #set_callback(Object, String)
     */
    public void clear_callback_method() {
        callback_method = null;
    }

    /**
     * Clears any callback method and object
     * 
     * @see #set_callback(Object, String)
     */
    public void clear_callback() {
        clear_callback_object();
        clear_callback_method();
    }

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

    /**
     * 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, that when an async execution ({@link #call_async(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 on finishing the async execution any defined callback will
     * NOT BE TRIGGERED.
     * </p>
     */
    public void reset() {
        clear_errors();
        clear_warnings();
        clear_args();
        clear_callback_object();
        clear_callback_method();
        result.clear();
    }

}