disable notification setting implemented

This commit is contained in:
danijoo
2016-01-02 13:11:28 +01:00
parent 5c625710a1
commit 33d78ee5f1
3 changed files with 56 additions and 5 deletions

View File

@@ -2,6 +2,7 @@
package="net.headlezz.notificationlogger"> package="net.headlezz.notificationlogger">
<application <application
android:name=".Application"
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"

View File

@@ -0,0 +1,12 @@
package net.headlezz.notificationlogger;
import timber.log.Timber;
public class Application extends android.app.Application {
@Override
public void onCreate() {
super.onCreate();
Timber.plant(new Timber.DebugTree());
}
}

View File

@@ -1,30 +1,61 @@
package net.headlezz.notificationlogger.logger; package net.headlezz.notificationlogger.logger;
import android.app.Notification; import android.app.Notification;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.service.notification.NotificationListenerService; import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification; import android.service.notification.StatusBarNotification;
import android.util.Log;
import net.headlezz.notificationlogger.PackageUtils; import net.headlezz.notificationlogger.PackageUtils;
import java.util.Date; import java.util.Date;
public class NotificationLoggerService extends NotificationListenerService { import timber.log.Timber;
public String TAG = NotificationLoggerService.class.getSimpleName(); public class NotificationLoggerService extends NotificationListenerService implements SharedPreferences.OnSharedPreferenceChangeListener {
public static boolean isRunning = false;
public static boolean isEnabled = true;
SharedPreferences mSharedPrefs;
@Override
public IBinder onBind(Intent intent) {
isRunning = true;
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
isEnabled = mSharedPrefs.getBoolean("pref_logging_enabled", true);
mSharedPrefs.registerOnSharedPreferenceChangeListener(this);
Timber.d(String.format("Logger started (running: %s, enabled: %s)", isRunning, isEnabled));
return super.onBind(intent);
}
@Override
public boolean onUnbind(Intent intent) {
mSharedPrefs.unregisterOnSharedPreferenceChangeListener(this);
Timber.d("Logger stopped");
return super.onUnbind(intent);
}
@Override @Override
public void onNotificationPosted(StatusBarNotification sbn) { public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn); super.onNotificationPosted(sbn);
Log.d(TAG, "Notificaton logged (id: " + sbn.getId() + ")");
if(!isEnabled) {
Timber.d("Notification posted but logger is disabled");
return;
}
LoggedNotification ln = buildLoggedNotification(sbn); LoggedNotification ln = buildLoggedNotification(sbn);
getApplicationContext().getContentResolver().insert( getApplicationContext().getContentResolver().insert(
Logged_notificationTable.CONTENT_URI, Logged_notificationTable.CONTENT_URI,
Logged_notificationTable.getContentValues(ln, false) Logged_notificationTable.getContentValues(ln, false)
); );
Timber.d(String.format("Notificaton logged (id: %d)", ln.notificationId));
} }
private LoggedNotification buildLoggedNotification(StatusBarNotification sbn) { private LoggedNotification buildLoggedNotification(StatusBarNotification sbn) {
@@ -39,7 +70,7 @@ public class NotificationLoggerService extends NotificationListenerService {
try { try {
appName = PackageUtils.getAppName(getApplicationContext(), packageName); appName = PackageUtils.getAppName(getApplicationContext(), packageName);
} catch (PackageManager.NameNotFoundException e) { } catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, "App name not found for " + packageName); Timber.w("App name not found for " + packageName);
appName = ""; appName = "";
} }
@@ -66,4 +97,11 @@ public class NotificationLoggerService extends NotificationListenerService {
return ln; return ln;
} }
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals("pref_logging_enabled")) {
isEnabled = sharedPreferences.getBoolean("pref_logging_enabled", true);
Timber.d("Logging is now " + isEnabled);
}
}
} }