notificationlistenerservice

This commit is contained in:
danijoo
2016-01-02 12:58:47 +01:00
parent e486bca4fc
commit 5c625710a1
7 changed files with 160 additions and 4 deletions

View File

@@ -9,7 +9,7 @@ android {
defaultConfig {
applicationId "net.headlezz.notificationlogger"
minSdkVersion 18
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"

View File

@@ -14,15 +14,31 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PreferenceActivity"
<activity
android:name=".PreferenceActivity"
android:parentActivityName=".MainActivity" />
<receiver android:name=".createnotification.NotificationAlarmReceiver"
<receiver
android:name=".createnotification.NotificationAlarmReceiver"
android:enabled="true">
<intent-filter>
<action android:name="net.headlezz.notificationbroadcast" />
</intent-filter>
</receiver>
<service
android:name=".logger.NotificationLoggerService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
<provider
android:authorities="net.headlezz.notificationlogger.logger.authority"
android:name="net.headlezz.notificationlogger.logger.NotificationProvider"
android:exported="true"
/>
</application>
</manifest>

View File

@@ -14,6 +14,7 @@ import net.headlezz.notificationlogger.createnotification.NotificationCreationFr
import butterknife.Bind;
import butterknife.ButterKnife;
import timber.log.Timber;
public class MainActivity extends AppCompatActivity {

View File

@@ -0,0 +1,16 @@
package net.headlezz.notificationlogger;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
public class PackageUtils {
public static String getAppName(Context context, String packageName) throws PackageManager.NameNotFoundException {
PackageManager pm = context.getPackageManager();
ApplicationInfo appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
return (String) pm.getApplicationLabel(appInfo);
}
}

View File

@@ -0,0 +1,36 @@
package net.headlezz.notificationlogger.logger;
import java.util.Date;
import ckm.simple.sql_provider.annotation.SimpleSQLColumn;
import ckm.simple.sql_provider.annotation.SimpleSQLTable;
@SimpleSQLTable(table="logged_notification", provider="NotificationProvider")
public class LoggedNotification {
@SimpleSQLColumn("title")
public String title;
@SimpleSQLColumn("message")
public String message;
@SimpleSQLColumn("date")
public Date date;
@SimpleSQLColumn("app_name")
public String appName;
@SimpleSQLColumn("package_name")
public String packageName;
@SimpleSQLColumn("notification_id")
public int notificationId;
@SimpleSQLColumn("user_id")
public int userId;
@SimpleSQLColumn("small_icon_id")
public int smallIconId;
}

View File

@@ -0,0 +1,69 @@
package net.headlezz.notificationlogger.logger;
import android.app.Notification;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
import net.headlezz.notificationlogger.PackageUtils;
import java.util.Date;
public class NotificationLoggerService extends NotificationListenerService {
public String TAG = NotificationLoggerService.class.getSimpleName();
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
Log.d(TAG, "Notificaton logged (id: " + sbn.getId() + ")");
LoggedNotification ln = buildLoggedNotification(sbn);
getApplicationContext().getContentResolver().insert(
Logged_notificationTable.CONTENT_URI,
Logged_notificationTable.getContentValues(ln, false)
);
}
private LoggedNotification buildLoggedNotification(StatusBarNotification sbn) {
int notificationId = sbn.getId();
int userId = sbn.getUserId();
Date date = new Date(sbn.getPostTime());
String packageName = sbn.getPackageName();
int iconId = sbn.getNotification().icon;
String appName;
try {
appName = PackageUtils.getAppName(getApplicationContext(), packageName);
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, "App name not found for " + packageName);
appName = "";
}
Notification notification = sbn.getNotification();
Bundle extras = notification.extras;
String title = extras.getString("android.title", "");
String message = extras.getCharSequence("android.text", "").toString();
if(message.isEmpty() && notification.tickerText != null)
message = notification.tickerText.toString();
if(message.isEmpty() && extras.containsKey("android.infoText"))
message = extras.get("android.infoText").toString();
LoggedNotification ln = new LoggedNotification();
ln.title = title;
ln.message = message;
ln.date = date;
ln.appName = appName;
ln.packageName = packageName;
ln.notificationId = notificationId;
ln.userId = userId;
ln.smallIconId = iconId;
return ln;
}
}

View File

@@ -0,0 +1,18 @@
package net.headlezz.notificationlogger.logger;
import ckm.simple.sql_provider.UpgradeScript;
import ckm.simple.sql_provider.annotation.ProviderConfig;
import ckm.simple.sql_provider.annotation.SimpleSQLConfig;
@SimpleSQLConfig(
name="NotificationProvider",
authority = "net.headlezz.notificationlogger.logger.authority",
database = "notifications.db",
version = 1
)
public class NotificationProviderConfig implements ProviderConfig {
@Override
public UpgradeScript[] getUpdateScripts() {
return new UpgradeScript[0];
}
}