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

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 {

    public 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 {
                    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 (Exception e) {
                    Log.w(TAG, "Exception on callback: " + e.getMessage());
                    warning_list.add("LOCAL: Could not execute callback");
                }
            }
        }

    }

    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>();

    /** 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
    }

    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!");
                        } 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!");
                        } 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");
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG, "Could not find APG, is it installed?");
        }
    }

    /** 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;
    }

    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;
    }

    public boolean call(String function) {
        return this.call(function, args, result);
    }

    public void call_async(String function) {
        async_running = true;
        new call_async().execute(function);
    }

    public boolean call(String function, Bundle pArgs) {
        return this.call(function, pArgs, result);
    }

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

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

        if (!initialize()) {
            error_list.add("LOCAL: Cannot bind to ApgService");
            pReturn.putInt("LOCAL_ERROR", error.CANNOT_BIND_TO_APG.ordinal());
            return false;
        }

        if (function == null || function.length() == 0) {
            error_list.add("LOCAL: Function to call missing");
            pReturn.putInt("LOCAL_ERROR", error.CALL_MISSING.ordinal());
            return false;
        }

        try {
            Boolean ret = (Boolean) IApgService.class.getMethod(function, Bundle.class, Bundle.class).invoke(apgService, pArgs, pReturn);
            error_list.addAll(pReturn.getStringArrayList("ERRORS"));
            warning_list.addAll(pReturn.getStringArrayList("WARNINGS"));
            return ret;
        } catch (NoSuchMethodException e) {
            Log.e(TAG, e.getMessage());
            error_list.add("LOCAL: " + e.getMessage());
            pReturn.putInt("LOCAL_ERROR", error.CALL_NOT_KNOWN.ordinal());
            return false;
        } catch (Exception e) {
            Log.e(TAG, "" + e.getMessage());
            error_list.add("LOCAL: " + e.getMessage());
            pReturn.putInt("LOCAL_ERROR", error.GENERIC.ordinal());
            return false;
        }

    }

    public void set_arg(String key, String val) {
        args.putString(key, val);
    }

    public void set_arg(String key, String vals[]) {
        ArrayList<String> list = new ArrayList<String>();
        for (String val : vals) {
            list.add(val);
        }
        set_arg(key, list);
    }

    public void set_arg(String key, ArrayList<String> vals) {
        args.putStringArrayList(key, vals);
    }

    public void set_arg(String key, boolean val) {
        args.putBoolean(key, val);
    }

    public void set_arg(String key, int val) {
        args.putInt(key, val);
    }

    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);
    }

    public void clear_args() {
        args.clear();
    }

    public Object get_arg(String key) {
        return args.get(key);
    }

    public String get_next_error() {
        if (error_list.size() != 0)
            return error_list.remove(0);
        else
            return null;
    }

    public boolean has_next_error() {
        return error_list.size() != 0;
    }

    public String get_next_warning() {
        if (warning_list.size() != 0)
            return warning_list.remove(0);
        else
            return null;
    }

    public boolean has_next_warning() {
        return warning_list.size() != 0;
    }

    public String get_result() {
        return result.getString("RESULT");
    }

    public void clear_errors() {
        error_list.clear();
    }

    public void clear_warnings() {
        warning_list.clear();
    }

    public void clear_result() {
        result.clear();
    }

    /**
     * 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;
    }

    public void clear_callback_object() {
        callback_object = null;
    }

    public void clear_callback_method() {
        callback_method = null;
    }

    public boolean is_running() {
        return async_running;
    }

    public void reset() {
        clear_errors();
        clear_warnings();
        clear_args();
        clear_result();
        clear_callback_object();
        clear_callback_method();
    }

}