Minor bugfixes & renamed 'security' to 'cryptography

This commit is contained in:
2016-01-13 17:28:18 +01:00
parent eb322d94a2
commit abd25f4010
16 changed files with 46 additions and 77 deletions

View File

@ -43,6 +43,7 @@ import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import ch.dissem.apps.abit.listener.ActionBarListener;
import ch.dissem.apps.abit.listener.ListSelectionListener;
@ -438,8 +439,9 @@ public class MainActivity extends AppCompatActivity
@Override
public void updateTitle(CharSequence title) {
//noinspection ConstantConditions
getSupportActionBar().setTitle(title);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(title);
}
}
public Label getSelectedLabel() {

View File

@ -16,7 +16,7 @@ import ch.dissem.bitmessage.entity.payload.Broadcast;
import ch.dissem.bitmessage.entity.valueobject.Label;
import ch.dissem.bitmessage.factory.Factory;
import ch.dissem.bitmessage.ports.ProofOfWorkEngine;
import ch.dissem.bitmessage.security.sc.SpongySecurity;
import ch.dissem.bitmessage.cryptography.sc.SpongyCryptography;
import ch.dissem.bitmessage.utils.UnixTime;
import static ch.dissem.apps.abit.util.Constants.PREFERENCE_SERVER_POW;
@ -27,8 +27,8 @@ import static ch.dissem.bitmessage.utils.UnixTime.DAY;
/**
* @author Christian Basler
*/
public class AndroidSecurity extends SpongySecurity {
public AndroidSecurity() {
public class AndroidCryptography extends SpongyCryptography {
public AndroidCryptography() {
PRNGFixes.apply();
}
}

View File

@ -5,11 +5,6 @@ import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.apps.abit.util.Preferences;
@ -20,20 +15,21 @@ public class WifiReceiver extends BroadcastReceiver {
public void onReceive(Context ctx, Intent intent) {
if (Preferences.isWifiOnly(ctx)) {
BitmessageContext bmc = Singleton.getBitmessageContext(ctx);
ConnectivityManager conMan = (ConnectivityManager) ctx.getSystemService(Context
.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = conMan.getActiveNetworkInfo();
if (netInfo != null && netInfo.getType() != ConnectivityManager.TYPE_WIFI
&& !bmc.isRunning()) {
if (!isConnectedToWifi(ctx) && bmc.isRunning()) {
bmc.shutdown();
}
}
}
public static boolean isConnectedToWifi(Context ctx) {
WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
SupplicantState state = wifiManager.getConnectionInfo().getSupplicantState();
return state == SupplicantState.COMPLETED;
NetworkInfo netInfo = getNetworkInfo(ctx);
return netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI;
}
private static NetworkInfo getNetworkInfo(Context ctx) {
ConnectivityManager conMan = (ConnectivityManager) ctx.getSystemService(Context
.CONNECTIVITY_SERVICE);
return conMan.getActiveNetworkInfo();
}
}

View File

@ -121,12 +121,11 @@ public class AndroidAddressRepository implements AddressRepository {
};
SQLiteDatabase db = sql.getReadableDatabase();
Cursor c = db.query(
try (Cursor c = db.query(
TABLE_NAME, projection,
where,
null, null, null, null
);
try {
)) {
c.moveToFirst();
while (!c.isAfterLast()) {
BitmessageAddress address;
@ -158,8 +157,6 @@ public class AndroidAddressRepository implements AddressRepository {
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
c.close();
}
return result;
}
@ -179,13 +176,10 @@ public class AndroidAddressRepository implements AddressRepository {
private boolean exists(BitmessageAddress address) {
SQLiteDatabase db = sql.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM Address WHERE address='" + address
.getAddress() + "'", null);
try {
try (Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM Address WHERE address='" + address
.getAddress() + "'", null)) {
cursor.moveToFirst();
return cursor.getInt(0) > 0;
} finally {
cursor.close();
}
}

View File

@ -89,6 +89,7 @@ public class AndroidInventory implements Inventory {
} finally {
c.close();
}
LOG.info("Inventory size: " + result.size());
return result;
}

View File

@ -108,20 +108,17 @@ public class AndroidMessageRepository implements MessageRepository, InternalCont
};
SQLiteDatabase db = sql.getReadableDatabase();
Cursor c = db.query(
try (Cursor c = db.query(
LBL_TABLE_NAME, projection,
where,
null, null, null,
LBL_COLUMN_ORDER
);
try {
)) {
c.moveToFirst();
while (!c.isAfterLast()) {
result.add(getLabel(c));
c.moveToNext();
}
} finally {
c.close();
}
return result;
}
@ -174,16 +171,13 @@ public class AndroidMessageRepository implements MessageRepository, InternalCont
where = "";
}
SQLiteDatabase db = sql.getReadableDatabase();
Cursor c = db.query(
try (Cursor c = db.query(
TABLE_NAME, new String[]{COLUMN_ID},
where + "id IN (SELECT message_id FROM Message_Label WHERE label_id IN (" +
"SELECT id FROM Label WHERE type = '" + Label.Type.UNREAD.name() + "'))",
null, null, null, null
);
try {
)) {
return c.getColumnCount();
} finally {
c.close();
}
}
@ -245,13 +239,12 @@ public class AndroidMessageRepository implements MessageRepository, InternalCont
};
SQLiteDatabase db = sql.getReadableDatabase();
Cursor c = db.query(
try (Cursor c = db.query(
TABLE_NAME, projection,
where,
null, null, null,
COLUMN_RECEIVED + " DESC"
);
try {
)) {
c.moveToFirst();
while (!c.isAfterLast()) {
byte[] iv = c.getBlob(c.getColumnIndex(COLUMN_IV));
@ -277,8 +270,6 @@ public class AndroidMessageRepository implements MessageRepository, InternalCont
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
} finally {
c.close();
}
return result;
}

View File

@ -52,12 +52,11 @@ public class AndroidProofOfWorkRepository implements ProofOfWorkRepository {
};
SQLiteDatabase db = sql.getReadableDatabase();
Cursor c = db.query(
try (Cursor c = db.query(
TABLE_NAME, projection,
"initial_hash = X'" + Strings.hex(initialHash) + "'",
null, null, null, null
);
try {
)) {
c.moveToFirst();
if (!c.isAfterLast()) {
int version = c.getInt(c.getColumnIndex(COLUMN_VERSION));
@ -69,8 +68,6 @@ public class AndroidProofOfWorkRepository implements ProofOfWorkRepository {
c.getLong(c.getColumnIndex(COLUMN_EXTRA_BYTES))
);
}
} finally {
c.close();
}
throw new RuntimeException("Object requested that we don't have. Initial hash: " +
Strings.hex(initialHash));
@ -85,20 +82,17 @@ public class AndroidProofOfWorkRepository implements ProofOfWorkRepository {
};
SQLiteDatabase db = sql.getReadableDatabase();
Cursor c = db.query(
List<byte[]> result = new LinkedList<>();
try (Cursor c = db.query(
TABLE_NAME, projection,
null, null, null, null, null
);
List<byte[]> result = new LinkedList<>();
try {
)) {
c.moveToFirst();
while (!c.isAfterLast()) {
byte[] initialHash = c.getBlob(c.getColumnIndex(COLUMN_INITIAL_HASH));
result.add(initialHash);
c.moveToNext();
}
} finally {
c.close();
}
return result;
}

View File

@ -4,7 +4,7 @@ import android.content.Context;
import java.util.List;
import ch.dissem.apps.abit.adapter.AndroidSecurity;
import ch.dissem.apps.abit.adapter.AndroidCryptography;
import ch.dissem.apps.abit.adapter.SwitchingProofOfWorkEngine;
import ch.dissem.apps.abit.listener.MessageListener;
import ch.dissem.apps.abit.pow.ServerPowEngine;
@ -47,7 +47,7 @@ public class Singleton {
new ServerPowEngine(ctx),
new ServicePowEngine(ctx)
))
.security(new AndroidSecurity())
.cryptography(new AndroidCryptography())
.nodeRegistry(new MemoryNodeRegistry())
.inventory(new AndroidInventory(sqlHelper))
.addressRepo(new AndroidAddressRepository(sqlHelper))

View File

@ -7,18 +7,6 @@ import android.os.IBinder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.dissem.apps.abit.listener.MessageListener;
import ch.dissem.apps.abit.notification.NetworkNotification;
import ch.dissem.apps.abit.repository.AndroidInventory;
import ch.dissem.apps.abit.repository.SqlHelper;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.networking.DefaultNetworkHandler;
import ch.dissem.bitmessage.ports.MemoryNodeRegistry;
import ch.dissem.bitmessage.security.sc.SpongySecurity;
import static ch.dissem.apps.abit.notification.NetworkNotification.ONGOING_NOTIFICATION_ID;
/**
* Define a Service that returns an IBinder for the
* sync adapter class, allowing the sync adapter framework to call