• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.car.statusicon.ui;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.content.res.Resources;
24 import android.telecom.TelecomManager;
25 import android.telephony.TelephonyManager;
26 
27 import androidx.annotation.NonNull;
28 
29 import com.android.systemui.R;
30 import com.android.systemui.car.statusicon.StatusIconView;
31 import com.android.systemui.car.statusicon.StatusIconViewController;
32 import com.android.systemui.car.systembar.element.CarSystemBarElementStateController;
33 import com.android.systemui.car.systembar.element.CarSystemBarElementStatusBarDisableController;
34 import com.android.systemui.dagger.qualifiers.Main;
35 import com.android.systemui.settings.UserTracker;
36 
37 import dagger.assisted.Assisted;
38 import dagger.assisted.AssistedFactory;
39 import dagger.assisted.AssistedInject;
40 
41 /**
42  * A controller for the read-only icon that shows phone call active status.
43  */
44 public class PhoneCallStatusIconController extends StatusIconViewController {
45 
46     private final Context mContext;
47     private final UserTracker mUserTracker;
48     private final TelecomManager mTelecomManager;
49 
50     final BroadcastReceiver mPhoneStateChangeReceiver = new BroadcastReceiver() {
51         @Override
52         public void onReceive(Context context, Intent intent) {
53             if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(intent.getAction())) {
54                 updateStatus();
55             }
56         }
57     };
58 
59     private final UserTracker.Callback mUserChangedCallback = new UserTracker.Callback() {
60         @Override
61         public void onUserChanged(int newUser, @NonNull Context userContext) {
62             updateStatus();
63         }
64     };
65 
66     @AssistedInject
PhoneCallStatusIconController(@ssisted StatusIconView view, CarSystemBarElementStatusBarDisableController disableController, CarSystemBarElementStateController stateController, Context context, @Main Resources resources, UserTracker userTracker)67     protected PhoneCallStatusIconController(@Assisted StatusIconView view,
68             CarSystemBarElementStatusBarDisableController disableController,
69             CarSystemBarElementStateController stateController,
70             Context context, @Main Resources resources, UserTracker userTracker) {
71         super(view, disableController, stateController);
72         mContext = context;
73         mUserTracker = userTracker;
74         mTelecomManager = context.getSystemService(TelecomManager.class);
75         setIconDrawableToDisplay(resources.getDrawable(R.drawable.ic_phone, context.getTheme()));
76     }
77 
78     @AssistedFactory
79     public interface Factory extends
80             StatusIconViewController.Factory<PhoneCallStatusIconController> {
81     }
82 
83     @Override
onViewAttached()84     protected void onViewAttached() {
85         super.onViewAttached();
86         IntentFilter filter = new IntentFilter();
87         filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
88         mContext.registerReceiverForAllUsers(mPhoneStateChangeReceiver,
89                 filter,  /* broadcastPermission= */ null, /* scheduler= */ null);
90         mUserTracker.addCallback(mUserChangedCallback, mContext.getMainExecutor());
91         updateStatus();
92     }
93 
94     @Override
onViewDetached()95     protected void onViewDetached() {
96         super.onViewDetached();
97         mContext.unregisterReceiver(mPhoneStateChangeReceiver);
98         mUserTracker.removeCallback(mUserChangedCallback);
99     }
100 
101     @Override
updateStatus()102     protected void updateStatus() {
103         setIconVisibility(mTelecomManager.isInCall());
104         onStatusUpdated();
105     }
106 }
107