1 package de.cweiske.headphoneindicator;
3 import android.Manifest;
4 import android.app.Activity;
5 import android.app.NotificationChannel;
6 import android.app.NotificationManager;
7 import android.content.BroadcastReceiver;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.content.IntentFilter;
11 import android.content.pm.PackageManager;
12 import android.hardware.usb.UsbManager;
13 import android.os.Build;
14 import android.os.Bundle;
15 import android.widget.ImageView;
16 import android.widget.TextView;
19 * Shows headphone icon and text, and starts the background service
21 * @author Christian Weiske, cweiske@cweiske.de
23 public class MainActivity extends Activity {
24 BroadcastReceiver headsetPlugReceiver = new BroadcastReceiver() {
26 public void onReceive(Context context, Intent intent) {
27 PlugIntentHelper plugIntentHelper = new PlugIntentHelper(context);
28 PlugInfo plugInfo = plugIntentHelper.getPlugInfo(intent);
29 if (plugInfo.isAudioEvent) {
30 setPlugged(plugInfo.isPlugged, plugInfo.hasMicrophone);
36 protected void onCreate(Bundle savedInstanceState) {
37 super.onCreate(savedInstanceState);
38 registerNotificationChannel();
39 requestNotificationPermission();
41 setContentView(R.layout.activity_main);
42 startService(new Intent(this, BackgroundService.class));
46 protected void onResume() {
48 IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
49 filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
50 filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
52 registerReceiver(this.headsetPlugReceiver, filter);
56 protected void onPause() {
58 this.unregisterReceiver(this.headsetPlugReceiver);
61 public void setPlugged(boolean plugged, boolean microphone)
63 ImageView view = (ImageView) findViewById(R.id.image);
64 TextView label = (TextView) findViewById(R.id.label);
67 view.setImageResource(R.drawable.headset_w);
68 label.setText(R.string.plugged_headset);
70 view.setImageResource(R.drawable.headphones_w);
71 label.setText(R.string.plugged_headphones);
74 view.setImageResource(0);
75 label.setText(R.string.unplugged);
79 private void registerNotificationChannel() {
80 // Create the NotificationChannel, but only on API 26+ because
81 // the NotificationChannel class is not in the Support Library.
82 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
86 CharSequence name = getString(R.string.channel_name);
87 String description = getString(R.string.channel_description);
88 NotificationChannel channel = new NotificationChannel(
89 NotificationReceiver.CHANNEL, name,
91 NotificationManager.IMPORTANCE_LOW
93 channel.setDescription(description);
95 NotificationManager notificationManager = getSystemService(NotificationManager.class);
96 notificationManager.createNotificationChannel(channel);
99 private void requestNotificationPermission() {
100 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
104 if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
107 requestPermissions(new String[] {Manifest.permission.POST_NOTIFICATIONS}, 0);