package de.tudarmstadt.informatik.hostage.logging; import android.app.IntentService; import android.content.Context; import android.content.Intent; import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper; public class Logger extends IntentService { private static final String ACTION_LOG = "de.tudarmstadt.informatik.hostage.action.LOG"; private static final String ACTION_DELETE_BY_BSSID = "de.tudarmstadt.informatik.hostage.action.DELETE_BY_BSSID"; private static final String ACTION_DELETE_BY_DATE = "de.tudarmstadt.informatik.hostage.action.DELETE_BY_DATE"; private static final String ACTION_DELETE_ALL = "de.tudarmstadt.informatik.hostage.action.DELETE_ALL"; private static final String EXTRA_RECORD = "de.tudarmstadt.informatik.hostage.extra.RECORD"; private static final String EXTRA_MISC = "de.tudarmstadt.informatik.hostage.extra.MISC"; public static void log(Context context, Record record) { Intent intent = new Intent(context, Logger.class); intent.setAction(ACTION_LOG); intent.putExtra(EXTRA_RECORD, record); context.startService(intent); } public static void deleteByBssid(Context context, String bssid) { Intent intent = new Intent(context, Logger.class); intent.setAction(ACTION_DELETE_BY_BSSID); intent.putExtra(EXTRA_MISC, bssid); context.startService(intent); } public static void deleteByDate(Context context, long timestamp) { Intent intent = new Intent(context, Logger.class); intent.setAction(ACTION_DELETE_BY_DATE); intent.putExtra(EXTRA_MISC, timestamp); context.startService(intent); } public static void deleteAll(Context context) { Intent intent = new Intent(context, Logger.class); intent.setAction(ACTION_DELETE_ALL); context.startService(intent); } private HostageDBOpenHelper mDbHelper; public Logger() { super("Logger"); } @Override public void onCreate() { super.onCreate(); mDbHelper = new HostageDBOpenHelper(getApplicationContext()); } private void handleActionLog(Record record) { mDbHelper.addRecord(record); } private void handleActionDeleteByBssid(String bssid) { mDbHelper.deleteByBSSID(bssid); } private void handleActionDeleteByDate(long timestamp) { mDbHelper.deleteByDate(timestamp); } private void handleActionDeleteAll() { mDbHelper.clearData(); } @Override protected void onHandleIntent(Intent intent) { if (intent != null) { final String action = intent.getAction(); if (ACTION_LOG.equals(action)) { final Record record = intent.getParcelableExtra(EXTRA_RECORD); handleActionLog(record); } else if (ACTION_DELETE_BY_BSSID.equals(action)) { final String bssid = intent.getStringExtra(EXTRA_MISC); handleActionDeleteByBssid(bssid); } else if (ACTION_DELETE_BY_DATE.equals(action)) { final long timestamp = intent.getLongExtra(EXTRA_MISC, -1L); handleActionDeleteByDate(timestamp); } else if (ACTION_DELETE_ALL.equals(action)) { handleActionDeleteAll(); } } } }