aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/services/BarcodeProvider.java
blob: 5ac2ee0163610a2191f4807dd7e2b48755dcd23f (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
package de.pixart.messenger.services;

import android.content.ComponentName;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.CancellationSignal;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.support.annotation.Nullable;
import android.util.Log;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Hashtable;

import de.pixart.messenger.Config;
import de.pixart.messenger.entities.Account;
import de.pixart.messenger.utils.CryptoHelper;
import rocks.xmpp.addr.Jid;

public class BarcodeProvider extends ContentProvider implements ServiceConnection {

    private static final String AUTHORITY = ".barcodes";

    private final Object lock = new Object();

    private XmppConnectionService mXmppConnectionService;
    private boolean mBindingInProcess = false;

    public static Uri getUriForAccount(Context context, Account account) {
        final String packageId = context.getPackageName();
        return Uri.parse("content://" + packageId + AUTHORITY + "/" + account.getJid().asBareJid() + ".png");
    }

    public static Bitmap create2dBarcodeBitmap(String input, int size) {
        try {
            final QRCodeWriter barcodeWriter = new QRCodeWriter();
            final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            final BitMatrix result = barcodeWriter.encode(input, BarcodeFormat.QR_CODE, size, size, hints);
            final int width = result.getWidth();
            final int height = result.getHeight();
            final int[] pixels = new int[width * height];
            for (int y = 0; y < height; y++) {
                final int offset = y * width;
                for (int x = 0; x < width; x++) {
                    pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
                }
            }
            final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;
        } catch (final Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean onCreate() {
        File barcodeDirectory = new File(getContext().getCacheDir().getAbsolutePath() + "/barcodes/");
        if (barcodeDirectory.exists() && barcodeDirectory.isDirectory()) {
            for (File file : barcodeDirectory.listFiles()) {
                if (file.isFile() && !file.isHidden()) {
                    if (file.delete()) {
                        Log.d(Config.LOGTAG, "deleted old barcode file " + file.getAbsolutePath());
                    }
                }
            }
        }
        return true;
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        return null;
    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        return "image/png";
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
        return openFile(uri, mode, null);
    }

    @Override
    public ParcelFileDescriptor openFile(Uri uri, String mode, CancellationSignal signal) throws FileNotFoundException {
        Log.d(Config.LOGTAG, "opening file with uri (normal): " + uri.toString());
        String path = uri.getPath();
        if (path != null && path.endsWith(".png") && path.length() >= 5) {
            String jid = path.substring(1).substring(0, path.length() - 4);
            Log.d(Config.LOGTAG, "account:" + jid);
            if (connectAndWait()) {
                Log.d(Config.LOGTAG, "connected to background service");
                try {
                    Account account = mXmppConnectionService.findAccountByJid(Jid.of(jid));
                    if (account != null) {
                        String shareableUri = account.getShareableUri();
                        String hash = CryptoHelper.getFingerprint(shareableUri);
                        File file = new File(getContext().getCacheDir().getAbsolutePath() + "/barcodes/" + hash);
                        if (!file.exists()) {
                            file.getParentFile().mkdirs();
                            file.createNewFile();
                            Bitmap bitmap = create2dBarcodeBitmap(account.getShareableUri(), 1024);
                            OutputStream outputStream = new FileOutputStream(file);
                            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
                            outputStream.close();
                            outputStream.flush();
                        }
                        return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
                    }
                } catch (Exception e) {
                    throw new FileNotFoundException();
                }
            }
        }
        throw new FileNotFoundException();
    }

    private boolean connectAndWait() {
        Intent intent = new Intent(getContext(), XmppConnectionService.class);
        intent.setAction(this.getClass().getSimpleName());
        Context context = getContext();
        if (context != null) {
            synchronized (this) {
                if (mXmppConnectionService == null && !mBindingInProcess) {
                    Log.d(Config.LOGTAG, "calling to bind service");
                    context.bindService(intent, this, Context.BIND_AUTO_CREATE);
                    this.mBindingInProcess = true;
                }
            }
            try {
                waitForService();
                return true;
            } catch (InterruptedException e) {
                return false;
            }
        } else {
            Log.d(Config.LOGTAG, "context was null");
            return false;
        }
    }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        synchronized (this) {
            XmppConnectionService.XmppConnectionBinder binder = (XmppConnectionService.XmppConnectionBinder) service;
            mXmppConnectionService = binder.getService();
            mBindingInProcess = false;
            synchronized (this.lock) {
                lock.notifyAll();
            }
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        synchronized (this) {
            mXmppConnectionService = null;
        }
    }

    private void waitForService() throws InterruptedException {
        if (mXmppConnectionService == null) {
            synchronized (this.lock) {
                lock.wait();
            }
        } else {
            Log.d(Config.LOGTAG, "not waiting for service because already initialized");
        }
    }
}