Skip to content

Commit

Permalink
Fixed memory leaks, except StatisticsFragment.java.
Browse files Browse the repository at this point in the history
  • Loading branch information
irinil committed Jul 21, 2020
1 parent f7cb9f0 commit 34d836a
Show file tree
Hide file tree
Showing 20 changed files with 1,049 additions and 846 deletions.
1 change: 1 addition & 0 deletions src/main/AndroidManifest.xml
Expand Up @@ -58,6 +58,7 @@
android:value="AIzaSyD5vUfuTVKbAmw2jMX9KTTbKGJ9LOhrPdU" />

<activity
android:excludeFromRecents="true"
android:name="de.tudarmstadt.informatik.hostage.ui.activity.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:launchMode="singleInstance"
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/de/tudarmstadt/informatik/hostage/Hostage.java
@@ -1,7 +1,6 @@
package de.tudarmstadt.informatik.hostage;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -47,11 +46,9 @@
import de.tudarmstadt.informatik.hostage.logging.DaoSession;
import de.tudarmstadt.informatik.hostage.persistence.DAO.AttackRecordDAO;
import de.tudarmstadt.informatik.hostage.protocol.Protocol;
import de.tudarmstadt.informatik.hostage.services.MultiStageAlarm;
import de.tudarmstadt.informatik.hostage.system.Device;
import de.tudarmstadt.informatik.hostage.ui.activity.MainActivity;


import static de.tudarmstadt.informatik.hostage.commons.HelperUtils.getBSSID;


Expand All @@ -68,7 +65,6 @@
public class Hostage extends Service {

private HashMap<String, Boolean> mProtocolActiveAttacks;
MultiStageAlarm alarm = new MultiStageAlarm();
private DaoSession dbSession;
public static int prefix;
boolean activeHandlers = false;
Expand Down Expand Up @@ -303,7 +299,7 @@ public void onCreate() {
connectionInfoEditor = connectionInfo.edit();
connectionInfoEditor.apply();

mProtocolActiveAttacks = new HashMap<String, Boolean>();
mProtocolActiveAttacks = new HashMap<>();

Device.checkCapabilities();
Device.executePortRedirectionScript();
Expand Down Expand Up @@ -472,10 +468,8 @@ private void attackNotification() {
}
Notification notification = notificationBuilder.setOngoing(true)
.setPriority(Notification.PRIORITY_DEFAULT)

.build();
startForeground(2, notification);

}
}

Expand Down Expand Up @@ -651,7 +645,7 @@ private int getDefaultPort(String protocolName) {
private LinkedList<Protocol> getImplementedProtocols() {
String[] protocols = getResources().getStringArray(R.array.protocols);
String packageName = Protocol.class.getPackage().getName();
LinkedList<Protocol> implementedProtocols = new LinkedList<Protocol>();
LinkedList<Protocol> implementedProtocols = new LinkedList<>();

for (String protocol : protocols) {
try {
Expand Down Expand Up @@ -777,7 +771,6 @@ public void updateEditor(String ssid, String bssid, int ipAddress, int netmask){
editor.putInt(getString(R.string.connection_info_subnet_mask), netmask);

editor.apply();

}


Expand Down
Expand Up @@ -46,6 +46,8 @@ public class MultiStage extends Service {
String stackbssid;
private DaoSession dbSession;
private DAOHelper daoHelper;
Notification notification;
NotificationManager manager;
private static int offset=0;
private int limit=50;
private int size;
Expand All @@ -69,7 +71,6 @@ public void onCreate() {
startCustomForeground();
fetchData();
}

else {
dbSession = HostageApplication.getInstances().getDaoSession();
daoHelper = new DAOHelper(dbSession,this);
Expand All @@ -87,6 +88,12 @@ public int onStartCommand(Intent intent, int flags, int startId) {
return START_NOT_STICKY;
}

@Override
public void onDestroy(){
manager.cancel(1);
stopForeground(true);
stopSelf();
}

//fetch data of records of last 10 mins
public void fetchData() {
Expand All @@ -101,7 +108,7 @@ public void fetchData() {
filter.setAboveTimestamp(filterTime);
recordArray = daoHelper.getAttackRecordDAO().getRecordsForFilterMutliStage(filter);
sortListIPs();
ArrayList<Stackbean> records = new ArrayList<Stackbean>(addRecordsToStackBean());
ArrayList<Stackbean> records = new ArrayList<>(addRecordsToStackBean());
createMultistageRecord(records);
}

Expand Down Expand Up @@ -165,7 +172,6 @@ private void sortListIPs(){

}


private ArrayList<Stackbean> addRecordsToStackBean(){
ArrayList<Stackbean> b = new ArrayList<>();
String prevRemoteIP = "";
Expand All @@ -191,7 +197,6 @@ private ArrayList<Stackbean> addRecordsToStackBean(){
ssid = tmp.getSsid();
prevLocalIP = tmp.getLocalIP();


}
}

Expand Down Expand Up @@ -229,23 +234,22 @@ public void createMultistageRecord(ArrayList<Stackbean> b){
/**
* Custom foreground for background service
*/

@RequiresApi(api = Build.VERSION_CODES.O)
private void startCustomForeground(){
String NOTIFICATION_CHANNEL_ID = "Try";
String channelName = "BackgroundService";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);

Notification.Builder notificationBuilder = new Notification.Builder(this,NOTIFICATION_CHANNEL_ID);
notificationBuilder.setContentTitle("MutiStage").setContentText("MutiStage running...")
.setSmallIcon(R.drawable.ic_launcher);

Notification notification = notificationBuilder.setOngoing(true)
notification = notificationBuilder.setOngoing(true)
.setPriority(Notification.PRIORITY_DEFAULT)
.build();
startForeground(1, notification);
Expand Down
Expand Up @@ -2,6 +2,7 @@

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
Expand All @@ -11,15 +12,13 @@
* Alarm to call multistage service
*/
public class MultiStageAlarm extends BroadcastReceiver {

/**
* Calls Mutlistage service when receives an attack.
* @param context
* @param intent
* @param context the Context of the service
* @param intent Multistage.class intent.
*/
@Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(MainActivity.getInstance().getApplicationContext(),"Scanning for MultiStage Attacks...",Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, MultiStage.class));
} else {
Expand All @@ -31,18 +30,16 @@ public void SetAlarm(Context context) {
AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MultiStageAlarm.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 1, pi); // Millisec * Second * Minute

am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60, pi); // Millisec * Second * Minute
}

public void CancelAlarm(Context context) {
Intent intent = new Intent(context, MultiStageAlarm.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
context.stopService(intent);
}



}

0 comments on commit 34d836a

Please sign in to comment.