Skip to content

Commit

Permalink
InfoActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
ma1co committed Jun 10, 2016
1 parent 928b900 commit 7f5727d
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".InfoActivity" android:theme="@style/BaseTheme" />
<activity android:name=".VideoActivity" android:theme="@style/BaseTheme" />
<activity android:name=".RegionActivity" android:theme="@style/BaseTheme" />
<activity android:name=".ProtectionActivity" android:theme="@style/BaseTheme" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.ma1co.openmemories.tweak;

public class BackupInfoAdapter<T> implements ItemActivity.InfoItem.Adapter {
private final BackupProperty<T> property;

public BackupInfoAdapter(BackupProperty<T> property) {
this.property = property;
}

public BackupProperty<T> getProperty() {
return property;
}

@Override
public boolean isAvailable() {
return property.exists();
}

@Override
public String getValue() {
try {
return property.getValue().toString();
} catch (BackupProperty.BackupException e) {
return "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ public class BackupKeys {
}

public static final BackupProperty.Byte PAL_NTSC_SELECTOR_ENABLED = new BackupProperty.Byte(0x01070148);

public static final BackupProperty.CString MODEL_NAME = new BackupProperty.CString(0x003e0005, 16);

public static final BackupProperty.ByteArray SERIAL_NUMBER = new BackupProperty.ByteArray(0x00e70003, 4);

public static final BackupProperty.CString PLATFORM_VERSION = new BackupProperty.CString(0x01660024, 8);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ public BackupProtectionException() {
}
}

public static class ByteArray extends BaseProperty<byte[]> {
public ByteArray(int id, int size) {
super(id, byte[].class, size);
}

@Override
protected byte[] toBytes(byte[] value) {
if (value.length != getSize())
throw new IllegalArgumentException("Wrong array length");
return value;
}

@Override
protected byte[] fromBytes(byte[] value) {
return value;
}
}

public static class Byte extends BaseProperty<Integer> {
public Byte(int id) {
super(id, Integer.class, 1);
Expand Down Expand Up @@ -48,6 +66,29 @@ protected Integer fromBytes(byte[] value) {
}
}

public static class CString extends BaseProperty<String> {
public CString(int id, int size) {
super(id, String.class, size);
}

@Override
protected byte[] toBytes(String value) {
byte[] bytes = value.getBytes();
if (bytes.length > getSize())
throw new IllegalArgumentException("String too long");
return Arrays.copyOf(bytes, getSize());
}

@Override
protected String fromBytes(byte[] value) {
String str = new String(value);
int length = str.indexOf('\u0000');
if (length == -1)
length = value.length;
return str.substring(0, length);
}
}

public static abstract class BaseProperty<T> extends BackupProperty<T> {
private final int id;
private final int size;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.github.ma1co.openmemories.tweak;

import android.os.Bundle;

import java.io.IOException;

public class InfoActivity extends ItemActivity {
public static class LoggingInfoAdapter implements InfoItem.Adapter {
private final String key;
private final InfoItem.Adapter parent;

public LoggingInfoAdapter(String key, InfoItem.Adapter parent) {
this.key = key;
this.parent = parent;
}

@Override
public boolean isAvailable() {
return parent.isAvailable();
}

@Override
public String getValue() {
String value = parent.getValue();
Logger.info("InfoAdapter", String.format("%s: %s", key, value));
return value;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

addLoggedInfo("Model", new BackupInfoAdapter<>(BackupKeys.MODEL_NAME));

addInfo("Serial number", new BackupInfoAdapter<byte[]>(BackupKeys.SERIAL_NUMBER) {
@Override
public String getValue() {
try {
byte[] serial = getProperty().getValue();
return String.format("%x%02x%02x%02x", serial[0], serial[1], serial[2], serial[3]);
} catch (BackupProperty.BackupException e) {
return "";
}
}
});

addLoggedInfo("Backup region", new InfoItem.Adapter() {
@Override
public boolean isAvailable() {
return true;
}

@Override
public String getValue() {
try {
return Backup.readData().getRegion();
} catch (IOException | NativeException e) {
return "";
}
}
});

addLoggedInfo("Tweak app version", new InfoItem.Adapter() {
@Override
public boolean isAvailable() {
return true;
}

@Override
public String getValue() {
return BuildConfig.VERSION_NAME;
}
});

addLoggedInfo("Java API version", new BackupInfoAdapter<>(BackupKeys.PLATFORM_VERSION));
}

protected BaseItem addLoggedInfo(String title, InfoItem.Adapter adapter) {
return addInfo(title, new LoggingInfoAdapter(title, adapter));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,36 @@ public Adapter getAdapter() {
}
}

public static class InfoItem extends BaseItem {
public interface Adapter {
boolean isAvailable();
String getValue();
}

private final Adapter adapter;
private final TextView titleView;
private final TextView valueView;

public InfoItem(Context context, String title, Adapter adapter) {
super(context);
inflate(context, R.layout.view_info, this);
titleView = (TextView) findViewById(R.id.title);
valueView = (TextView) findViewById(R.id.value);

this.adapter = adapter;
titleView.setText(title);
}

public Adapter getAdapter() {
return adapter;
}

@Override
public void update() {
valueView.setText(adapter.isAvailable() ? adapter.getValue() : "(not available)");
}
}

private ViewGroup containerView;

@Override
Expand Down Expand Up @@ -143,4 +173,8 @@ protected BaseItem addSwitch(String title, SwitchItem.Adapter adapter) {
protected BaseItem addButton(String text, ButtonItem.Adapter adapter) {
return addItem(new ButtonItem(this, text, adapter));
}

protected BaseItem addInfo(String title, InfoItem.Adapter adapter) {
return addItem(new InfoItem(this, title, adapter));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void uncaughtException(Thread thread, Throwable exp) {
}
});

addTab("info", "Info", android.R.drawable. ic_menu_info_details, InfoActivity.class);
addTab("video", "Video", android.R.drawable.ic_menu_camera, VideoActivity.class);
addTab("region", "Region", android.R.drawable.ic_menu_mapmode, RegionActivity.class);
addTab("protection", "Protection", android.R.drawable.ic_lock_lock, ProtectionActivity.class);
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/layout/view_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:id="@+id/title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:id="@+id/value"/>
</merge>

0 comments on commit 7f5727d

Please sign in to comment.