Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize much #363

Merged
merged 8 commits into from
Dec 28, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Code cleanups
  • Loading branch information
Goooler committed Dec 26, 2021
commit fa4c11a6b832cb5fbe6d00398c2e716da9721a42
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ protected void onCreate(Bundle savedInstanceState) {
R.id.editTextCloneUidToolWriteKey);
mStatusLogContent = findViewById(
R.id.textViewCloneUidToolStatusLogContent);
mShowOptions = (CheckBox) findViewById(
mShowOptions = findViewById(
R.id.checkBoxCloneUidToolOptions);
mRadioButtonKeyB = findViewById(
R.id.radioButtonCloneUidToolKeyB);
Expand Down Expand Up @@ -390,7 +390,7 @@ private void appendToLog(String text) {
* (in this case the "show options" check box).
*/
public void onShowOptions(View view) {
LinearLayout optionsLayout = (LinearLayout) findViewById(
LinearLayout optionsLayout = findViewById(
R.id.linearLayoutOptions);
if (mShowOptions.isChecked()) {
optionsLayout.setVisibility(View.VISIBLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ private void saveFile(final String[] data, final String fileName,
View dialogLayout = getLayoutInflater().inflate(
R.layout.dialog_save_file,
findViewById(android.R.id.content), false);
TextView message = (TextView) dialogLayout.findViewById(
TextView message = dialogLayout.findViewById(
R.id.textViewDialogSaveFileMessage);
final EditText input = (EditText) dialogLayout.findViewById(
final EditText input = dialogLayout.findViewById(
R.id.editTextDialogSaveFileName);
message.setText(messageId);
input.setText(fileName);
Expand Down Expand Up @@ -975,7 +975,7 @@ private void saveKeys() {
if (mDumpName == null) {
mKeysName = "UID_" + mUID;
} else {
mKeysName = new String(mDumpName);
mKeysName = mDumpName;
}
}
mKeysName += ".keys";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ private void onNewFile() {
View dialogLayout = getLayoutInflater().inflate(
R.layout.dialog_save_file,
findViewById(android.R.id.content), false);
TextView message = (TextView) dialogLayout.findViewById(
TextView message = dialogLayout.findViewById(
R.id.textViewDialogSaveFileMessage);
final EditText input = (EditText) dialogLayout.findViewById(
final EditText input = dialogLayout.findViewById(
R.id.editTextDialogSaveFileName);
message.setText(R.string.dialog_new_file);
input.setText(prefill);
Expand All @@ -344,14 +344,9 @@ private void onNewFile() {
// Show keyboard.
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
input.postDelayed(new Runnable()
{
@Override
public void run()
{
input.requestFocus();
imm.showSoftInput(input, 0);
}
input.postDelayed(() -> {
input.requestFocus();
imm.showSoftInput(input, 0);
}, 100);

// Ask user for filename.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public interface IActivityThatReactsToSave {
/**
* This method will be called after a successful save process.
*/
public abstract void onSaveSuccessful();
void onSaveSuccessful();

/**
* This method will be called, if there was an error during the
* save process or it the user hits "cancel" in the "file already exists"
* dialog.
*/
public abstract void onSaveFailure();
void onSaveFailure();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;

import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -97,6 +99,7 @@ private enum FileType {
this.text = text;
}

@NonNull
@Override
public String toString() {
return text;
Expand Down Expand Up @@ -488,7 +491,7 @@ private void saveConvertedDataToContent(String[] convertedContent,
Toast.LENGTH_LONG).show();
return;
}
boolean success = false;
boolean success;
if (mFileType != FileType.BIN) {
success = Common.saveFile(contentDestination, convertedContent, this);
} else {
Expand Down Expand Up @@ -551,7 +554,7 @@ private String[] convertDump(String[] source, FileType srcType,
Common.isValidDumpErrorToast(err, this);
return null;
}
int sectorNumber = 0;
int sectorNumber;
int blockNumber = 0;
for (String line : source) {
if (line.startsWith("+")) {
Expand Down Expand Up @@ -624,7 +627,7 @@ private String[] convertDump(String[] source, FileType srcType,
return null;
}

JSONObject blocks = null;
JSONObject blocks;
try {
JSONObject parsedJson = new JSONObject(TextUtils.join("", json));
blocks = parsedJson.getJSONObject("blocks");
Expand Down Expand Up @@ -886,7 +889,7 @@ private boolean backupDumpsAndKeys(Uri contentDestUri) {
file.getAbsolutePath().substring(commonPathLen));
entry.setTime(file.lastModified());
out.putNextEntry(entry);
int count = 0;
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ private void onSave() {
View dialogLayout = getLayoutInflater().inflate(
R.layout.dialog_save_file,
findViewById(android.R.id.content), false);
TextView message = (TextView) dialogLayout.findViewById(
TextView message = dialogLayout.findViewById(
R.id.textViewDialogSaveFileMessage);
final EditText input = (EditText) dialogLayout.findViewById(
final EditText input = dialogLayout.findViewById(
R.id.editTextDialogSaveFileName);
message.setText(R.string.dialog_save_keys);
input.setText(mFileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;

import de.syss.MifareClassicTool.Common;
import de.syss.MifareClassicTool.R;

Expand Down Expand Up @@ -62,6 +64,7 @@ public enum Preference {
this.text = text;
}

@NonNull
@Override
public String toString() {
return text;
Expand Down Expand Up @@ -370,4 +373,4 @@ public void onSave(View view) {
public void onCancel(View view) {
finish();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,21 @@ public void onCreate(Bundle savedInstanceState) {
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch(requestCode) {
case KEY_MAP_CREATOR:
if (requestCode == KEY_MAP_CREATOR) {
if (resultCode != Activity.RESULT_OK) {
// Error.
if (resultCode == 4) {
// Error. Path from the calling intend was null.
// (This is really strange and should not occur.)
Toast.makeText(this, R.string.info_strange_error,
Toast.LENGTH_LONG).show();
Toast.LENGTH_LONG).show();
}
finish();
return;
} else {
// Read Tag.
readTag();
}
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,10 @@ public void onActivityResult(int requestCode,

// Error handling for the return value of KeyMapCreator.
// So far, only error nr. 4 needs to be handled.
switch (ckmError) {
case 4:
// Error. Path from the calling intend was null.
if (ckmError == 4) {// Error. Path from the calling intend was null.
// (This is really strange and should not occur.)
Toast.makeText(this, R.string.info_strange_error,
Toast.LENGTH_LONG).show();
Toast.LENGTH_LONG).show();
}
}

Expand Down Expand Up @@ -872,7 +870,7 @@ private void initDumpWithPosFromDump(String[] dump) {
// with the static ones.
String newBlock = dump[i].substring(0, 12)
+ mStaticAC.getText().toString()
+ dump[i].substring(18, dump[i].length());
+ dump[i].substring(18);
dump[i] = newBlock;
}
mDumpWithPos.get(sector).put(block++,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public static String[] readFileLineByLine(File file, boolean readAll,
if (file == null || !file.exists()) {
return null;
}
String[] ret = null;
String[] ret;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
Expand Down Expand Up @@ -306,8 +306,8 @@ public static String[] readFileLineByLine(File file, boolean readAll,
* @see #readLineByLine(BufferedReader, boolean, Context)
*/
public static String[] readUriLineByLine(Uri uri, boolean readAll, Context context) {
InputStream contentStream = null;
String[] ret = null;
InputStream contentStream;
String[] ret;
if (uri == null || context == null) {
return null;
}
Expand Down Expand Up @@ -336,7 +336,7 @@ public static String[] readUriLineByLine(Uri uri, boolean readAll, Context conte
* an read error.
*/
public static byte[] readUriRaw(Uri uri, Context context) {
InputStream contentStream = null;
InputStream contentStream;
if (uri == null || context == null) {
return null;
}
Expand Down Expand Up @@ -372,7 +372,7 @@ public static byte[] readUriRaw(Uri uri, Context context) {
*/
private static String[] readLineByLine(BufferedReader reader,
boolean readAll, Context context) {
String[] ret = null;
String[] ret;
String line;
ArrayList<String> linesArray = new ArrayList<>();
try {
Expand Down Expand Up @@ -421,17 +421,14 @@ private static String[] readLineByLine(BufferedReader reader,
public static String getFileName(Uri uri, Context context) {
String result = null;
if (uri.getScheme().equals("content")) {
Cursor cursor = context.getContentResolver().query(
uri, null, null, null, null);
try {
try (Cursor cursor = context.getContentResolver().query(
uri, null, null, null, null)) {
if (cursor != null && cursor.moveToFirst()) {
int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
if (index >= 0) {
result = cursor.getString(index);
}
}
} finally {
cursor.close();
}
}
if (result == null) {
Expand Down Expand Up @@ -580,7 +577,7 @@ public static boolean saveFile(File file, String[] lines, boolean append) {
} else {
error = true;
}
return error == false;
return !error;
}

/**
Expand Down Expand Up @@ -608,7 +605,7 @@ public static boolean saveFile(Uri contentUri, String[] lines, Context context)
* @return True if file writing was successful. False otherwise.
*/
public static boolean saveFile(Uri contentUri, byte[] bytes, Context context) {
OutputStream output = null;
OutputStream output;
if (contentUri == null || bytes == null || context == null || bytes.length == 0) {
return false;
}
Expand Down Expand Up @@ -906,18 +903,7 @@ public static int checkMifareClassicSupport(Tag tag, Context context) {
// MIFARE Plus EV1 2K/4K SL1
return -1;
} else {
if ((sak & 1) == 1) { // SAK bit 1 = 1?
// MIFARE Mini
return -1;
} else {
// MIFARE Classic 1k
// MIFARE SmartMX 1k
// MIFARE Plus S 2K SL1
// MIFARE Plus X 2K SL1
// MIFARE Plus SE 1K
// MIFARE Plus EV1 2K/4K SL1
return -1;
}
return -1;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this to make the code more readable and closer to the datasheet linked in an above comment.
I don't like the optimized version. It is not readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted.

}
} else {
// Some MIFARE tag, but not Classic or Classic compatible.
Expand Down Expand Up @@ -982,7 +968,6 @@ public static boolean openApp(Context context, String packageName) {
* <li>-1 - Can not check because Android version is >= 8.</li>
* </ul>
*/
@SuppressWarnings("deprecation")
public static int isExternalNfcServiceRunning(Context context) {
// getRunningServices() is deprecated since Android 8.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
Expand Down Expand Up @@ -1325,7 +1310,7 @@ public static byte[] acMatrixToACBytes(byte[][] acMatrix) {
* @param hexString The string to check.
* @param context The Context in which the Toast will be shown.
* @return True if sting is hex an 16 Bytes long, False otherwise.
* @see #isHex(String, Context)
* @see #isHex(String, Context)
*/
public static boolean isHexAnd16Byte(String hexString, Context context) {
boolean isHex = isHex(hexString, context);
Expand Down Expand Up @@ -1375,14 +1360,12 @@ public static boolean isValueBlock(String hexString) {
// For better reading (~ = invert operator):
// if (b0=b8 and b0=~b4) and (b1=b9 and b9=~b5) ...
// ... and (b12=b14 and b13=b15 and b12=~b13) then
if ( (b[0] == b[8] && (byte)(b[0]^0xFF) == b[4]) &&
(b[1] == b[9] && (byte)(b[1]^0xFF) == b[5]) &&
(b[2] == b[10] && (byte)(b[2]^0xFF) == b[6]) &&
(b[3] == b[11] && (byte)(b[3]^0xFF) == b[7]) &&
(b[12] == b[14] && b[13] == b[15] &&
(byte)(b[12]^0xFF) == b[13])) {
return true;
}
return (b[0] == b[8] && (byte) (b[0] ^ 0xFF) == b[4]) &&
(b[1] == b[9] && (byte) (b[1] ^ 0xFF) == b[5]) &&
(b[2] == b[10] && (byte) (b[2] ^ 0xFF) == b[6]) &&
(b[3] == b[11] && (byte) (b[3] ^ 0xFF) == b[7]) &&
(b[12] == b[14] && b[13] == b[15] &&
(byte) (b[12] ^ 0xFF) == b[13]);
}
return false;
}
Expand Down Expand Up @@ -1634,7 +1617,7 @@ public static boolean isValidBlock0(String block0, int uidLen, int tagSize,
// Byte3.
if (valid && (uidLen == 7 || uidLen == 10)) {
// The 3rd byte of a double/triple sized UID shall not be 0x88.
valid = !block0.substring(4, 6).equals("88");
valid = !block0.startsWith("88", 4);
}
// ATQA.
// Check if there is a special ATQA tied to MIFARE SmartMX or TNP3xxx.
Expand Down Expand Up @@ -1813,7 +1796,7 @@ public static String hex2Ascii(String hex) {
return null;
}
byte[] bytes = hex2Bytes(hex);
String ret = null;
String ret;
// Replace non printable ASCII with ".".
for(int i = 0; i < bytes.length; i++) {
if (bytes[i] < (byte)0x20 || bytes[i] == (byte)0x7F) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import android.content.Context;
import android.nfc.Tag;
import android.nfc.TagLostException;
import android.nfc.tech.IsoDep;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.NfcA;
import android.os.Bundle;
Expand All @@ -31,8 +30,6 @@
import android.util.SparseArray;
import android.widget.Toast;

import androidx.core.content.res.TypedArrayUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
Expand Down