Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
camgaertner committed Jun 8, 2016
0 parents commit 8dd1cbf
Show file tree
Hide file tree
Showing 37 changed files with 892 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
27 changes: 27 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "com.example.cameron.spotifyadblocker"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
}
17 changes: 17 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\Cameron\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.cameron.spotifyadblocker;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
27 changes: 27 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.cameron.spotifyadblocker">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".NotificationListener"
android:label="@string/service_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.cameron.spotifyadblocker;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
private boolean enabled;
private Intent serviceIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
enabled = false;
serviceIntent = new Intent(this, NotificationListener.class);
}

public void blockAds(View view) {
if (enabled) {
Log.d("DEBUG", "Stopping Service");
NotificationListener.killService();
stopService(serviceIntent);
enabled = false;
} else {
startService(serviceIntent);
enabled = true;
}
}

public void notificationAccess(View view) {
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.example.cameron.spotifyadblocker;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Timer;
import java.util.TimerTask;

/**
* Created by Cameron on 6/7/2016.
*/
public class NotificationListener extends NotificationListenerService {
boolean muted;
int volume;
static Timer timer;
HashSet<String> blocklist;

public IBinder onBind(Intent intent) {
return super.onBind(intent);
}

public int onStartCommand(Intent intent, int flags, int startID) {
timer = new Timer();
blocklist = new HashSet<String>();
muted = false;
volume = 0;
// Load blocklist
Resources resources = getResources();
InputStream inputStream = resources.openRawResource(R.raw.blocklist);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
try {
while ((line = bufferedReader.readLine()) != null) {
blocklist.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
StatusBarNotification[] notifications = getActiveNotifications();
Notification notification = new Notification();
boolean foundNotification = false;
if (notifications != null) {
// Find which notification is Spotify
for (int i = 0; i < notifications.length; ++i) {
String name = notifications[i].getPackageName();
if (name.contains("spotify")) {
Log.d("DEBUG", name);
notification = notifications[i].getNotification();
foundNotification = true;
break;
}
}
// Check if it is an ad
if (foundNotification) {
Bundle extras = notification.extras;
String title = extras.getString(Notification.EXTRA_TITLE);
if (title != null) {
Log.d("DEBUG", title);
boolean isAdPlaying = blocklist.contains(title);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (isAdPlaying && !muted) {
volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_SHOW_UI);
muted = true;
} else if (!isAdPlaying && muted) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_SHOW_UI);
muted = false;
}
}
}
}
}
}, 10, 250);
return START_NOT_STICKY;
}

public static void killService() {
timer.cancel();
}

@Override
public void onDestroy() {
Log.d("DEBUG", "Destroying Service");
killService();
Log.d("DEBUG", "Timer canceled.");
}

@Override
public void onNotificationPosted(StatusBarNotification notification) {

}

@Override
public void onNotificationRemoved(StatusBarNotification notification) {

}
}
Loading

0 comments on commit 8dd1cbf

Please sign in to comment.