Skip to content

Commit

Permalink
Merge pull request #431 from deunlee/master
Browse files Browse the repository at this point in the history
Add Hide Identical Sectors option and show Difference Percentage
  • Loading branch information
ikarus23 authored Sep 15, 2023
2 parents ffd36b9 + 8d528e2 commit dbee8fc
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
Expand Down Expand Up @@ -59,6 +60,7 @@ public class DiffTool extends BasicActivity {
private LinearLayout mDiffContent;
private Button mDumpFileButton1;
private Button mDumpFileButton2;
private CheckBox mDumpHideIdentical;
private SparseArray<String[]> mDump1;
private SparseArray<String[]> mDump2;

Expand All @@ -74,6 +76,7 @@ protected void onCreate(Bundle savedInstanceState) {
mDiffContent = findViewById(R.id.linearLayoutDiffTool);
mDumpFileButton1 = findViewById(R.id.buttonDiffToolDump1);
mDumpFileButton2 = findViewById(R.id.buttonDiffToolDump2);
mDumpHideIdentical = findViewById(R.id.checkBoxDiffToolHideIdentical);

// Check if one or both dumps are already chosen via Intent
// (from DumpEditor).
Expand Down Expand Up @@ -128,14 +131,21 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
* Run diff if there are two dumps and show the result in the GUI.
* @see MCDiffUtils#diffIndices(SparseArray, SparseArray)
*/
@SuppressLint("SetTextI18n")
@SuppressLint({"SetTextI18n", "DefaultLocale"})
private void runDiff() {
// Check if both dumps are there.
if (mDump1 != null && mDump2 != null) {
mDiffContent.removeAllViews();
SparseArray<Integer[][]> diff = MCDiffUtils.diffIndices(
mDump1, mDump2);

// Add the difference between dumps.
int totalLength = 0;
int diffLength = 0;
TextView difference = new TextView(this);
difference.setPadding(0, Common.dpToPx(10), 0, 0);
mDiffContent.addView(difference);

// Walk trough all possible sectors (this way the right
// order will be guaranteed).
for (int sector = 0; sector < 40; sector++) {
Expand All @@ -145,6 +155,34 @@ private void runDiff() {
continue;
}

// A sector that exists only in one dump is counted as differences.
if (blocks.length == 0 || blocks.length == 1) {
String[] bs = blocks.length == 0
? mDump1.get(sector) : mDump2.get(sector);
for (String b : bs) {
totalLength += b.length();
diffLength += b.length();
}
}

// If the Hide Identical Sectors option is checked,
// Do not display the same sector.
if (mDumpHideIdentical.isChecked() && blocks.length > 1) {
int block;
for (block = 0; block < blocks.length; block++) {
if (blocks[block].length != 0) {
break;
}
}
if (block == blocks.length) {
// Identical sector.
for (String b : mDump1.get(sector)) {
totalLength += b.length();
}
continue;
}
}

// Add sector header.
TextView header = new TextView(this);
TextViewCompat.setTextAppearance(header,
Expand Down Expand Up @@ -197,6 +235,8 @@ private void runDiff() {
dump1.setText(mDump1.get(sector)[block]);
dump2.setText(mDump2.get(sector)[block]);

totalLength += mDump1.get(sector)[block].length();

if (blocks[block].length == 0) {
// Set diff line for identical blocks.
diffIndex.setTextColor(Color.GREEN);
Expand All @@ -208,14 +248,23 @@ private void runDiff() {
// Walk through all symbols to populate the diff line.
for (int i : blocks[block]) {
diffString.setCharAt(i, 'X');

// Count the differences.
diffLength++;
}
}
// Add diff entry.
diffIndex.setText(diffString);
mDiffContent.addView(rl);
}
}

// Show the difference percentage between two dumps.
float percentage = 0;
if (totalLength > 0) {
percentage = (float)diffLength / totalLength * 100f;
}
difference.setText(getString(R.string.text_difference_between_dumps) +
": " + String.format("%.2f", percentage) + " %");
}
}

Expand All @@ -241,6 +290,14 @@ public void onChooseDump2(View view) {
startActivityForResult(intent, FILE_CHOOSER_DUMP_FILE_2);
}

/**
* Run diff if the Hide Identical Sectors option is changed.
* @see #runDiff()
*/
public void onHideIdenticalChanged(View view) {
runDiff();
}

/**
* Get the {@link FileChooser#EXTRA_CHOSEN_FILE} from the Intend,
* read the file, check it for errors using
Expand Down
108 changes: 60 additions & 48 deletions Mifare Classic Tool/app/src/main/res/layout/activity_diff_tool.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,73 @@
-->


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayoutDiffTool"
android:id="@+id/linearLayoutDiffToolTop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical"
tools:context="Activities.DiffTool" >

<Button
android:id="@+id/buttonDiffToolDump1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textViewDiffToolDump1"
android:gravity="left|center_vertical"
android:onClick="onChooseDump1"
android:text="@string/action_chose_dump" />
<RelativeLayout
android:id="@+id/relativeLayoutDiffTool"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textViewDiffToolDump1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/buttonDiffToolDump1"
android:layout_alignBottom="@+id/buttonDiffToolDump1"
android:layout_alignParentLeft="true"
android:paddingRight="5dp"
android:text="@string/text_dump_1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/buttonDiffToolDump1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textViewDiffToolDump1"
android:gravity="left|center_vertical"
android:onClick="onChooseDump1"
android:text="@string/action_chose_dump" />

<Button
android:id="@+id/buttonDiffToolDump2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonDiffToolDump1"
android:layout_alignParentRight="true"
android:layout_below="@+id/buttonDiffToolDump1"
android:gravity="left|center_vertical"
android:onClick="onChooseDump2"
android:text="@string/action_chose_dump" />
<TextView
android:id="@+id/textViewDiffToolDump1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/buttonDiffToolDump1"
android:layout_alignBottom="@+id/buttonDiffToolDump1"
android:layout_alignParentLeft="true"
android:paddingRight="5dp"
android:text="@string/text_dump_1"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textViewDiffToolDump2"
android:layout_width="wrap_content"
<Button
android:id="@+id/buttonDiffToolDump2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/buttonDiffToolDump1"
android:layout_alignParentRight="true"
android:layout_below="@+id/buttonDiffToolDump1"
android:gravity="left|center_vertical"
android:onClick="onChooseDump2"
android:text="@string/action_chose_dump" />

<TextView
android:id="@+id/textViewDiffToolDump2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/buttonDiffToolDump2"
android:layout_alignBottom="@+id/buttonDiffToolDump2"
android:layout_alignParentLeft="true"
android:paddingRight="5dp"
android:text="@string/text_dump_2"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

<CheckBox
android:id="@+id/checkBoxDiffToolHideIdentical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/buttonDiffToolDump2"
android:layout_alignBottom="@+id/buttonDiffToolDump2"
android:layout_alignParentLeft="true"
android:paddingRight="5dp"
android:text="@string/text_dump_2"
android:onClick="onHideIdenticalChanged"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="@string/action_hide_identical_sectors"
android:textAppearance="?android:attr/textAppearanceMedium" />

<!-- Separator -->
Expand All @@ -78,17 +95,12 @@
android:layout_height="2dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_below="@+id/buttonDiffToolDump2"
android:background="@color/light_gray" />

<ScrollView
android:id="@+id/scrollViewDiffTool"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/separatorDiffTool" >
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/linearLayoutDiffTool"
Expand All @@ -101,4 +113,4 @@

</ScrollView>

</RelativeLayout>
</LinearLayout>
2 changes: 2 additions & 0 deletions Mifare Classic Tool/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
to 4.0.0? Please read section \"1.2 Data Storage\" of \"Help and Info\".</string>
<string name="text_more_converter">More converter (online)</string>
<string name="text_enter_file_name">Enter a file name</string>
<string name="text_difference_between_dumps">Difference between dumps</string>

<!-- Actions (Buttons, Checkboxs, etc. -->
<string name="action_read_tag">Read Tag</string>
Expand Down Expand Up @@ -310,6 +311,7 @@
<string name="action_multi_purpose_converter">Multi-Purpose Converter</string>
<string name="action_cyber_chef">Cyber Chef</string>
<string name="action_open_clone_uid_tool">Open the Clone UID Tool</string>
<string name="action_hide_identical_sectors">Hide identical sectors</string>

<!-- Toast messages -->
<string name="info_new_tag_found">New tag found</string>
Expand Down

0 comments on commit dbee8fc

Please sign in to comment.