BarcodeProvider.java

  1package eu.siacs.conversations.services;
  2
  3import android.content.ComponentName;
  4import android.content.ContentProvider;
  5import android.content.ContentValues;
  6import android.content.Context;
  7import android.content.Intent;
  8import android.content.ServiceConnection;
  9import android.database.Cursor;
 10import android.graphics.Bitmap;
 11import android.graphics.Color;
 12import android.net.Uri;
 13import android.os.CancellationSignal;
 14import android.os.IBinder;
 15import android.os.ParcelFileDescriptor;
 16import android.support.annotation.Nullable;
 17import android.util.Log;
 18
 19import com.google.zxing.BarcodeFormat;
 20import com.google.zxing.EncodeHintType;
 21import com.google.zxing.common.BitMatrix;
 22import com.google.zxing.qrcode.QRCodeWriter;
 23import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 24
 25import java.io.File;
 26import java.io.FileNotFoundException;
 27import java.io.FileOutputStream;
 28import java.io.OutputStream;
 29import java.util.Hashtable;
 30
 31import eu.siacs.conversations.Config;
 32import eu.siacs.conversations.entities.Account;
 33import eu.siacs.conversations.utils.CryptoHelper;
 34import rocks.xmpp.addr.Jid;
 35
 36public class BarcodeProvider extends ContentProvider implements ServiceConnection {
 37
 38	private static final String AUTHORITY = ".barcodes";
 39
 40	private final Object lock = new Object();
 41
 42	private XmppConnectionService mXmppConnectionService;
 43	private boolean mBindingInProcess = false;
 44
 45	public static Uri getUriForAccount(Context context, Account account) {
 46		final String packageId = context.getPackageName();
 47		return Uri.parse("content://" + packageId + AUTHORITY + "/" + account.getJid().asBareJid() + ".png");
 48	}
 49
 50	public static Bitmap create2dBarcodeBitmap(String input, int size) {
 51		try {
 52			final QRCodeWriter barcodeWriter = new QRCodeWriter();
 53			final Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
 54			hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
 55			hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
 56			final BitMatrix result = barcodeWriter.encode(input, BarcodeFormat.QR_CODE, size, size, hints);
 57			final int width = result.getWidth();
 58			final int height = result.getHeight();
 59			final int[] pixels = new int[width * height];
 60			for (int y = 0; y < height; y++) {
 61				final int offset = y * width;
 62				for (int x = 0; x < width; x++) {
 63					pixels[offset + x] = result.get(x, y) ? Color.BLACK : Color.WHITE;
 64				}
 65			}
 66			final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
 67			bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
 68			return bitmap;
 69		} catch (final Exception e) {
 70			e.printStackTrace();
 71			return null;
 72		}
 73	}
 74
 75	@Override
 76	public boolean onCreate() {
 77		File barcodeDirectory = new File(getContext().getCacheDir().getAbsolutePath() + "/barcodes/");
 78		if (barcodeDirectory.exists() && barcodeDirectory.isDirectory()) {
 79			for (File file : barcodeDirectory.listFiles()) {
 80				if (file.isFile() && !file.isHidden()) {
 81					if (file.delete()) {
 82						Log.d(Config.LOGTAG, "deleted old barcode file " + file.getAbsolutePath());
 83					}
 84				}
 85			}
 86		}
 87		return true;
 88	}
 89
 90	@Nullable
 91	@Override
 92	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
 93		return null;
 94	}
 95
 96	@Nullable
 97	@Override
 98	public String getType(Uri uri) {
 99		return "image/png";
100	}
101
102	@Nullable
103	@Override
104	public Uri insert(Uri uri, ContentValues values) {
105		return null;
106	}
107
108	@Override
109	public int delete(Uri uri, String selection, String[] selectionArgs) {
110		return 0;
111	}
112
113	@Override
114	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
115		return 0;
116	}
117
118	@Override
119	public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
120		return openFile(uri, mode, null);
121	}
122
123	@Override
124	public ParcelFileDescriptor openFile(Uri uri, String mode, CancellationSignal signal) throws FileNotFoundException {
125		Log.d(Config.LOGTAG, "opening file with uri (normal): " + uri.toString());
126		String path = uri.getPath();
127		if (path != null && path.endsWith(".png") && path.length() >= 5) {
128			String jid = path.substring(1).substring(0, path.length() - 4);
129			Log.d(Config.LOGTAG, "account:" + jid);
130			if (connectAndWait()) {
131				Log.d(Config.LOGTAG, "connected to background service");
132				try {
133					Account account = mXmppConnectionService.findAccountByJid(Jid.of(jid));
134					if (account != null) {
135						String shareableUri = account.getShareableUri();
136						String hash = CryptoHelper.getFingerprint(shareableUri);
137						File file = new File(getContext().getCacheDir().getAbsolutePath() + "/barcodes/" + hash);
138						if (!file.exists()) {
139							file.getParentFile().mkdirs();
140							file.createNewFile();
141							Bitmap bitmap = create2dBarcodeBitmap(account.getShareableUri(), 1024);
142							OutputStream outputStream = new FileOutputStream(file);
143							bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
144							outputStream.close();
145							outputStream.flush();
146						}
147						return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
148					}
149				} catch (Exception e) {
150					throw new FileNotFoundException();
151				}
152			}
153		}
154		throw new FileNotFoundException();
155	}
156
157	private boolean connectAndWait() {
158		Intent intent = new Intent(getContext(), XmppConnectionService.class);
159		intent.setAction(this.getClass().getSimpleName());
160		Context context = getContext();
161		if (context != null) {
162			synchronized (this) {
163				if (mXmppConnectionService == null && !mBindingInProcess) {
164					Log.d(Config.LOGTAG, "calling to bind service");
165					context.startService(intent);
166					context.bindService(intent, this, Context.BIND_AUTO_CREATE);
167					this.mBindingInProcess = true;
168				}
169			}
170			try {
171				waitForService();
172				return true;
173			} catch (InterruptedException e) {
174				return false;
175			}
176		} else {
177			Log.d(Config.LOGTAG, "context was null");
178			return false;
179		}
180	}
181
182	@Override
183	public void onServiceConnected(ComponentName name, IBinder service) {
184		synchronized (this) {
185			XmppConnectionService.XmppConnectionBinder binder = (XmppConnectionService.XmppConnectionBinder) service;
186			mXmppConnectionService = binder.getService();
187			mBindingInProcess = false;
188			synchronized (this.lock) {
189				lock.notifyAll();
190			}
191		}
192	}
193
194	@Override
195	public void onServiceDisconnected(ComponentName name) {
196		synchronized (this) {
197			mXmppConnectionService = null;
198		}
199	}
200
201	private void waitForService() throws InterruptedException {
202		if (mXmppConnectionService == null) {
203			synchronized (this.lock) {
204				lock.wait();
205			}
206		} else {
207			Log.d(Config.LOGTAG, "not waiting for service because already initialized");
208		}
209	}
210}