Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoLucidi01 committed Jan 4, 2024
0 parents commit 836794c
Show file tree
Hide file tree
Showing 13 changed files with 289 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
.DS_Store
.cxx
.gradle
.idea
app/build
gradle*
local.properties
36 changes: 36 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
plugins {
id("com.android.application")
}

android {
namespace = "io.github.marcolucidi01.quickmediamute"
compileSdk = 34

defaultConfig {
applicationId = "io.github.marcolucidi01.quickmediamute"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0.0"
base.archivesName = "$applicationId-v$versionName"
}

buildTypes {
debug {
isDebuggable = true
versionNameSuffix = "-debug"
}
release {
isDebuggable = false
isMinifyEnabled = true
isShrinkResources = true
// without this line, my phone thinks that the .apk is invalid, i.e. it won't install unsigned .apks
signingConfig = signingConfigs.getByName("debug")
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
26 changes: 26 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:icon="@drawable/ic_media_audio_off_24"
android:label="@string/app_name">
<service
android:name=".QuickMediaMute"
android:icon="@drawable/ic_media_audio_off_24"
android:label="@string/label_media_audio_off"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>
<activity
android:name=".SoundsSettingsRedirect"
android:exported="true">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES" />
</intent-filter>
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// see license file for copyright and license details
package io.github.marcolucidi01.quickmediamute;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.Icon;
import android.media.AudioManager;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;

public class QuickMediaMute extends TileService
{
private static final String PKG = QuickMediaMute.class.getPackage().getName();
private static final Icon IC_MEDIA_AUDIO_ON = Icon.createWithResource(PKG, R.drawable.ic_media_audio_on_24);
private static final Icon IC_MEDIA_AUDIO_OFF = Icon.createWithResource(PKG, R.drawable.ic_media_audio_off_24);

private final VolumeChangedReceiver volumeChangedReceiver = new VolumeChangedReceiver(this);
// `android.media.VOLUME_CHANGED_ACTION` is an internal "undocumented" broadcast intent, so it
// could break at any time, but works for me (for now)
private final IntentFilter volumeChangedActionFilter = new IntentFilter("android.media.VOLUME_CHANGED_ACTION");

@Override
public void onTileAdded()
{
updateTile();
}

@Override
public void onStartListening()
{
updateTile();
// trigger a tile update if the volume changes *while* the quick settings menu is open
registerReceiver(volumeChangedReceiver, volumeChangedActionFilter);
}

@Override
public void onStopListening()
{
unregisterReceiver(volumeChangedReceiver);
}

@Override
public void onClick()
{
AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_TOGGLE_MUTE, AudioManager.FLAG_SHOW_UI);
updateTile();
}

public void updateTile()
{
AudioManager audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int state = audio.isStreamMute(AudioManager.STREAM_MUSIC) || audio.getStreamVolume(AudioManager.STREAM_MUSIC) <= 0
? Tile.STATE_INACTIVE
: Tile.STATE_ACTIVE;

Tile tile = getQsTile();
if (tile.getState() == state)
return;

tile.setState(state);
if (state == Tile.STATE_ACTIVE) {
tile.setIcon(IC_MEDIA_AUDIO_ON);
tile.setLabel(getString(R.string.label_media_audio_on));
} else {
tile.setIcon(IC_MEDIA_AUDIO_OFF);
tile.setLabel(getString(R.string.label_media_audio_off));
}

tile.updateTile();
}

private static class VolumeChangedReceiver extends BroadcastReceiver
{
private final QuickMediaMute quickMediaMute;

public VolumeChangedReceiver(QuickMediaMute quickMediaMute)
{
this.quickMediaMute = quickMediaMute;
}

@Override
public void onReceive(Context context, Intent intent)
{
quickMediaMute.updateTile();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// see license file for copyright and license details
package io.github.marcolucidi01.quickmediamute;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;

public class SoundsSettingsRedirect extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

String[] settings = new String[] {
Settings.ACTION_SOUND_SETTINGS,
"com.android.settings.SOUND_SETTINGS", // saw it in the source code of another app, probably legacy
Settings.ACTION_SETTINGS,
};
for (String s : settings) {
Intent intent = new Intent(s);
// safeguard since in some cases, a matching Activity may not exist
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
break;
}
}

finish();
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_media_audio_off_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M4.27,3L3,4.27l9,9v0.28c-0.59,-0.34 -1.27,-0.55 -2,-0.55 -2.21,0 -4,1.79 -4,4s1.79,4 4,4 4,-1.79 4,-4v-1.73L19.73,21 21,19.73 4.27,3zM14,7h4V3h-6v5.18l2,2z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_media_audio_on_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,3v10.55c-0.59,-0.34 -1.27,-0.55 -2,-0.55 -2.21,0 -4,1.79 -4,4s1.79,4 4,4 4,-1.79 4,-4V7h4V3h-6z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<string name="app_name">QuickMediaMute</string>
<string name="label_media_audio_on">Sound</string>
<string name="label_media_audio_off">Mute</string>
</resources>
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id("com.android.application") version "8.2.0" apply false
}
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Marco Lucidi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Binary file added quickmediamute.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
QuickMediaMute
==============

an Android quick settings toggle to mute/unmute media audio.

![quickmediamute](quickmediamute.png)

my phone lives constantly in "vibrate" mode and i like the fact that we have two
separate volumes for ringer and media audio, but i wasn't able to find a quick
setting toggle for switching on and off the media audio (like the one we have
for changing ringer mode between mute, vibrate and sound). this "app" adds just
that: a toggle for the media audio which i find very useful when visiting
certain sites or using certain apps.

yes i could just use the volume up and down keys, but i find that a bit clunky
and when i want to unmute the audio, the previous volumes level is *lost*.

install
-------

just grab the latest `.apk` from the releases page and install it on your phone.

i'm not an "Android developer", so i won't bother putting this thing on the
playstore (this was in fact my first Android "app").

otherwise you can build the `.apk` yourself if you don't trust me :)

thanks
------

to this [random stackoverflow question][30] (and linked tutorials) which pointed
me to the Android Quick Settings API.

[30]: https://stackoverflow.com/questions/66367393/android-how-to-add-a-toggle-button-in-the-notification-panel-the-place-wh

---

see [license file][40] for copyright and license details.

[40]: license
17 changes: 17 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}

rootProject.name = "QuickMediaMute"
include(":app")

0 comments on commit 836794c

Please sign in to comment.