-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
- Loading branch information
There are no files selected for viewing
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
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' | ||
} |
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); | ||
} | ||
} |
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) { | ||
|
||
} | ||
} |