/** * Write all the blocks in a Tag to the Mifare tag. * * @param t * @throws IOException */ public void write(Tag t) throws IOException { if (t.getSectorCount() > mKeys.getSectorCount()) throw new IOException("Too few keys"); mTag.connect(); int sectors = mTag.getSectorCount(); try { for (int s = 0; s < sectors; ++s) { // Authenticate for each sector (try B key, then A key) if (!mTag.authenticateSectorWithKeyB(s, mKeys.getKeyB(s)) && !mTag.authenticateSectorWithKeyA(s, mKeys.getKeyA(s))) throw new IOException("Auth error"); // Write to tag. Skip block 0 and the trailer of each sector int blockOffset = mTag.sectorToBlock(s); int lastBlock = blockOffset + mTag.getBlockCountInSector(s); // Skip block 0 blockOffset = blockOffset == 0 ? 1 : blockOffset; for (int b = blockOffset; b < lastBlock; ++b) { mTag.writeBlock(b, t.getBlock(b)); if (mProgressListener != null) mProgressListener.publishProgress((100 * b) / t.getBlockCount()); } } } finally { mTag.close(); } }
@Override public List<DIRECTION> calculate(IProgressListener listener) throws GameException { int j = getHeuristic(); for (ans = j; ; ans++) { iterationCounter = 0; boolean resolved = false; long beginTime = System.currentTimeMillis(); if (solve(startState, blankIndex, 0, -1, j)) { resolved = true; } long consumeTime = System.currentTimeMillis() - beginTime; if (listener != null) { listener.onProgress(new long[] {ans, iterationCounter, consumeTime}); } Log.d(TAG, "running time for ans(" + ans + ") is " + (consumeTime) + "ms"); Log.d(TAG, "iteration count for ans(" + ans + ") is " + iterationCounter); if (resolved) { break; } } List<DIRECTION> solution = new ArrayList<DIRECTION>(); for (int i = 0; i < ans; i++) { switch (moves[i]) { case UP: solution.add(DIRECTION.UP); break; case DOWN: solution.add(DIRECTION.DOWN); break; case LEFT: solution.add(DIRECTION.LEFT); break; case RIGHT: solution.add(DIRECTION.RIGHT); break; } } return solution; }
/** * Read all blocks from the Mifare tag and return the data in a Tag object. * * @return A Tag object containing the data of the Mifare tag. * @throws IOException */ public Tag read() throws IOException { int sectors = mTag.getSectorCount(); Tag t = new Tag(TagType.getType(sectors)); if (t.getSectorCount() > mKeys.getSectorCount()) throw new IOException("Too few keys"); mTag.connect(); try { for (int s = 0; s < sectors; ++s) { byte[] aKey = mKeys.getKeyA(s); byte[] bKey = mKeys.getKeyB(s); // Authenticate for each sector (try A key, then B key) if (!mTag.authenticateSectorWithKeyA(s, aKey) && !mTag.authenticateSectorWithKeyB(s, bKey)) throw new IOException("Auth error"); // Read every block of the sector int blockOffset = mTag.sectorToBlock(s); int lastBlock = blockOffset + mTag.getBlockCountInSector(s); for (int b = blockOffset; b < lastBlock; ++b) { byte[] readBuffer = mTag.readBlock(b); // Manually transfer key data to tag since it is usually not // readable if (b == lastBlock - 1) { for (int i = 0; i < Tag.KEY_SIZE; ++i) { readBuffer[i] = aKey[i]; readBuffer[Tag.BLOCK_SIZE - Tag.KEY_SIZE + i] = bKey[i]; } } t.setBlock(b, readBuffer); if (mProgressListener != null) mProgressListener.publishProgress((100 * b) / t.getBlockCount()); } } return t; } finally { mTag.close(); } }