99import android .app .NotificationChannel ;
1010import android .app .NotificationManager ;
1111import android .app .Service ;
12- import android .appwidget .AppWidgetManager ;
13- import android .content .ComponentName ;
1412import android .content .Context ;
1513import android .content .Intent ;
1614import android .content .pm .PackageManager ;
1715import android .os .Build ;
1816import android .os .IBinder ;
1917import android .telephony .PhoneStateListener ;
20- import android .telephony .TelephonyCallback ;
2118import android .telephony .TelephonyManager ;
2219import android .util .Log ;
23-
24- import androidx .annotation .RequiresApi ;
20+ import androidx .annotation .Nullable ;
2521import androidx .core .app .ActivityCompat ;
2622import androidx .core .app .NotificationCompat ;
2723
2824import java .util .concurrent .Executor ;
2925import java .util .concurrent .Executors ;
3026
31- import kotlin .Deprecated ;
32- import kotlin .Suppress ;
27+ import de .kaiserdragon .callforwardingstatus .helper .CallForwardingListener ;
3328
3429public class PhoneStateService extends Service {
3530 private static final String CHANNEL_ID = "CallForwardingServiceID" ;
3631 public static boolean currentState ;
37- Context context ;
32+ Context appcontext ;
3833 static final String TAG = "Service" ;
39- private final Executor executor = Executors . newSingleThreadExecutor ();
34+
4035 // Define NOTIFICATION_ID as a constant
4136 private static final int NOTIFICATION_ID = 1 ;
37+ private CallForwardingListener callForwardingListener ;
4238
4339
4440 /** @noinspection deprecation*/
@@ -47,24 +43,16 @@ public class PhoneStateService extends Service {
4743 @ Override
4844 public void onCallForwardingIndicatorChanged (boolean cfi ) {
4945 if (DEBUG )Log .i (TAG , "onCallForwardingIndicatorChanged CFI Old=" + cfi );
50- // Get the current state of unconditional call forwarding
51- currentState = cfi ;
5246 // Create an Intent with the android.appwidget.action.APPWIDGET_UPDATE action
53- Intent intent = new Intent (context , ForwardingStatusWidget .class );
47+ Intent intent = new Intent (appcontext , ForwardingStatusWidget .class );
5448 intent .setAction ("de.kaiserdragon.callforwardingstatus.APPWIDGET_UPDATE_CFI" );
55- //Toast.makeText(context, "OLD", Toast.LENGTH_SHORT).show();
56- // Add the app widget IDs as an extra
57- int [] ids = AppWidgetManager .getInstance (getApplication ()).getAppWidgetIds (new ComponentName (getApplication (), ForwardingStatusWidget .class ));
58- intent .putExtra (AppWidgetManager .EXTRA_APPWIDGET_IDS , ids );
59-
6049 // Add the CFI value as an extra
61- intent .putExtra ("cfi" , currentState );
62-
50+ intent .putExtra ("cfi" , cfi );
6351 // Send the broadcast
6452 sendBroadcast (intent );
6553
6654 if (Build .VERSION .SDK_INT <= Build .VERSION_CODES .R ) {
67- if (ActivityCompat .checkSelfPermission (context , Manifest .permission .READ_PHONE_STATE ) == PackageManager .PERMISSION_GRANTED ) {
55+ if (ActivityCompat .checkSelfPermission (appcontext , Manifest .permission .READ_PHONE_STATE ) == PackageManager .PERMISSION_GRANTED ) {
6856 super .onCallForwardingIndicatorChanged (cfi );
6957 }
7058 }
@@ -74,10 +62,10 @@ public void onCallForwardingIndicatorChanged(boolean cfi) {
7462 @ Override
7563 public void onCreate () {
7664 super .onCreate ();
77- context = getApplicationContext ();
65+ appcontext = getApplicationContext ();
7866 createNotificationChannel ();
7967 startForegroundService ();
80- registerPhoneStateListener ();
68+
8169 }
8270
8371 private void createNotificationChannel () {
@@ -99,7 +87,7 @@ private void startForegroundService() {
9987 .build ();
10088
10189 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ){
102- if (ActivityCompat .checkSelfPermission (context , Manifest .permission .FOREGROUND_SERVICE_SPECIAL_USE )
90+ if (ActivityCompat .checkSelfPermission (appcontext , Manifest .permission .FOREGROUND_SERVICE_SPECIAL_USE )
10391 == PackageManager .PERMISSION_GRANTED ) {
10492 startForeground (NOTIFICATION_ID , notification , FOREGROUND_SERVICE_TYPE_SPECIAL_USE );
10593 } else {
@@ -114,61 +102,44 @@ private void startForegroundService() {
114102
115103 @ Override
116104 public int onStartCommand (Intent intent , int flags , int startId ) {
105+ registerPhoneStateListener ();
117106 return START_STICKY ;
118107 }
119108
120109 @ SuppressWarnings ("deprecation" )
121110 private void registerPhoneStateListener () {
122111 TelephonyManager telephonyManager = getSystemService (TelephonyManager .class ); // Use class reference for type safety
123- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
124- MyCallForwardingListener listener = new MyCallForwardingListener ();
125- telephonyManager .registerTelephonyCallback (Executors .newSingleThreadExecutor (), listener );
126- if (DEBUG ) Log .i (TAG , "Registered TelephonyCallback" );
112+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .UPSIDE_DOWN_CAKE ) {
113+ if (callForwardingListener == null ) { // Check if already registered
114+ callForwardingListener = new CallForwardingListener (appcontext );
115+ telephonyManager .registerTelephonyCallback (Executors . newSingleThreadExecutor (), callForwardingListener );
116+ if (DEBUG ) Log .i (TAG , "Registered TelephonyCallback" );
117+ }
127118 } else {
128119 telephonyManager .listen (phoneStateListener , PhoneStateListener .LISTEN_CALL_FORWARDING_INDICATOR );
129- if (DEBUG ) Log .i (TAG , "Registered PhoneStateListener" );
120+ // if (DEBUG) Log.i(TAG, "Registered PhoneStateListener");
130121 }
131122 }
132123
133124 @ Override
134125 @ SuppressWarnings ("deprecation" )
135126 public void onDestroy () {
136- Log .d (TAG ,"Destroy" );
137- TelephonyManager telephonyManager = (TelephonyManager ) getSystemService (Context .TELEPHONY_SERVICE );
138- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .S ) telephonyManager .listen (phoneStateListener , PhoneStateListener .LISTEN_NONE );
127+ Log .d (TAG , "Destroy" );
128+ TelephonyManager telephonyManager = getSystemService (TelephonyManager .class );
129+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S &&callForwardingListener != null ) {
130+ telephonyManager .unregisterTelephonyCallback (callForwardingListener ); // Unregister explicitly
131+ callForwardingListener = null ;
132+ if (DEBUG ) Log .i (TAG , "Unregistered TelephonyCallback" );
133+ } else {
134+ telephonyManager .listen (phoneStateListener , PhoneStateListener .LISTEN_NONE );
135+ }
139136 super .onDestroy ();
140- // Unregister MyPhoneStateListener as a phone state listener
141137 }
142138
139+ @ Nullable
143140 @ Override
144141 public IBinder onBind (Intent intent ) {
145- // Return null as this service is not bound to any activity
146142 return null ;
147143 }
148144
149- @ RequiresApi (api = Build .VERSION_CODES .S )
150- public class MyCallForwardingListener extends TelephonyCallback implements TelephonyCallback .CallForwardingIndicatorListener {
151- @ Override
152- public void onCallForwardingIndicatorChanged (boolean cfi ) {
153- // Handle the call forwarding state change here
154- if (DEBUG ) Log .i (TAG , "onCallForwardingIndicatorChanged CFI New=" + cfi );
155- // Get the current state of unconditional call forwarding
156- //Toast.makeText(context, "New", Toast.LENGTH_SHORT).show();
157- currentState = cfi ;
158- // Create an Intent with the android.appwidget.action.APPWIDGET_UPDATE action
159- Intent intent = new Intent (context , ForwardingStatusWidget .class );
160- intent .setAction ("de.kaiserdragon.callforwardingstatus.APPWIDGET_UPDATE_CFI" );
161-
162- // Add the app widget IDs as an extra
163- int [] ids = AppWidgetManager .getInstance (getApplication ()).getAppWidgetIds (new ComponentName (getApplication (), ForwardingStatusWidget .class ));
164- intent .putExtra (AppWidgetManager .EXTRA_APPWIDGET_IDS , ids );
165-
166- // Add the CFI value as an extra
167- intent .putExtra ("cfi" , currentState );
168-
169- // Send the broadcast
170- sendBroadcast (intent );
171-
172- }
173- }
174145}
0 commit comments