Notification should make no sound
[headphoneindicator.git] / src / de / cweiske / headphoneindicator / MainActivity.java
1 package de.cweiske.headphoneindicator;
2
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;
17
18 /**
19  * Shows headphone icon and text, and starts the background service
20  *
21  * @author Christian Weiske, cweiske@cweiske.de
22  */
23 public class MainActivity extends Activity {
24     BroadcastReceiver headsetPlugReceiver = new BroadcastReceiver() {
25         @Override
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);
31             }
32         }
33     };
34
35     @Override
36     protected void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38         registerNotificationChannel();
39         requestNotificationPermission();
40
41         setContentView(R.layout.activity_main);
42         startService(new Intent(this, BackgroundService.class));
43     }
44
45     @Override
46     protected void onResume() {
47         super.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);
51
52         registerReceiver(this.headsetPlugReceiver, filter);
53     }
54
55     @Override
56     protected void onPause() {
57         super.onPause();
58         this.unregisterReceiver(this.headsetPlugReceiver);
59     }
60
61     public void setPlugged(boolean plugged, boolean microphone)
62     {
63         ImageView view = (ImageView) findViewById(R.id.image);
64         TextView label = (TextView) findViewById(R.id.label);
65         if (plugged) {
66             if (microphone) {
67                 view.setImageResource(R.drawable.headset_w);
68                 label.setText(R.string.plugged_headset);
69             } else {
70                 view.setImageResource(R.drawable.headphones_w);
71                 label.setText(R.string.plugged_headphones);
72             }
73         } else {
74             view.setImageResource(0);
75             label.setText(R.string.unplugged);
76         }
77     }
78
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) {
83             return;
84         }
85
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,
90             //low = no sound
91             NotificationManager.IMPORTANCE_LOW
92         );
93         channel.setDescription(description);
94
95         NotificationManager notificationManager = getSystemService(NotificationManager.class);
96         notificationManager.createNotificationChannel(channel);
97     }
98
99     private void requestNotificationPermission() {
100         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
101             return;
102         }
103
104         if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED) {
105             return;
106         }
107         requestPermissions(new String[] {Manifest.permission.POST_NOTIFICATIONS}, 0);
108     }
109 }