1 /* 2 * Copyright (C) 2021 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.location.LocationManager; 25 26 import androidx.annotation.NonNull; 27 28 import com.android.systemui.R; 29 import com.android.systemui.car.statusicon.StatusIconView; 30 import com.android.systemui.car.statusicon.StatusIconViewController; 31 import com.android.systemui.car.systembar.element.CarSystemBarElementStateController; 32 import com.android.systemui.car.systembar.element.CarSystemBarElementStatusBarDisableController; 33 import com.android.systemui.dagger.qualifiers.Main; 34 import com.android.systemui.settings.UserTracker; 35 36 import dagger.assisted.Assisted; 37 import dagger.assisted.AssistedFactory; 38 import dagger.assisted.AssistedInject; 39 40 /** 41 * A controller for the read-only icon that shows location active status. 42 */ 43 public class LocationStatusIconController extends StatusIconViewController { 44 45 private static final IntentFilter INTENT_FILTER_LOCATION_MODE_CHANGED = new IntentFilter( 46 LocationManager.MODE_CHANGED_ACTION); 47 48 private final Context mContext; 49 private final UserTracker mUserTracker; 50 private final LocationManager mLocationManager; 51 private boolean mIsLocationActive; 52 53 private final BroadcastReceiver mLocationReceiver = new BroadcastReceiver() { 54 @Override 55 public void onReceive(Context context, Intent intent) { 56 updateIconVisibilityForCurrentUser(); 57 } 58 }; 59 60 private final UserTracker.Callback mUserChangedCallback = new UserTracker.Callback() { 61 @Override 62 public void onUserChanged(int newUser, @NonNull Context userContext) { 63 updateIconVisibilityForCurrentUser(); 64 } 65 }; 66 67 @AssistedInject LocationStatusIconController(@ssisted StatusIconView view, CarSystemBarElementStatusBarDisableController disableController, CarSystemBarElementStateController stateController, Context context, @Main Resources resources, UserTracker userTracker)68 protected LocationStatusIconController(@Assisted StatusIconView view, 69 CarSystemBarElementStatusBarDisableController disableController, 70 CarSystemBarElementStateController stateController, 71 Context context, @Main Resources resources, UserTracker userTracker) { 72 super(view, disableController, stateController); 73 mContext = context; 74 mUserTracker = userTracker; 75 mLocationManager = context.getSystemService(LocationManager.class); 76 setIconDrawableToDisplay(resources.getDrawable(R.drawable.ic_location, context.getTheme())); 77 } 78 79 @AssistedFactory 80 public interface Factory extends 81 StatusIconViewController.Factory<LocationStatusIconController> { 82 } 83 84 @Override onViewAttached()85 protected void onViewAttached() { 86 super.onViewAttached(); 87 mContext.registerReceiverForAllUsers(mLocationReceiver, INTENT_FILTER_LOCATION_MODE_CHANGED, 88 /* broadcastPermission= */ null, /* scheduler= */ null); 89 mUserTracker.addCallback(mUserChangedCallback, mContext.getMainExecutor()); 90 updateIconVisibilityForCurrentUser(); 91 } 92 93 @Override onViewDetached()94 protected void onViewDetached() { 95 super.onViewDetached(); 96 mContext.unregisterReceiver(mLocationReceiver); 97 mUserTracker.removeCallback(mUserChangedCallback); 98 } 99 100 @Override updateStatus()101 protected void updateStatus() { 102 setIconVisibility(mIsLocationActive); 103 onStatusUpdated(); 104 } 105 updateIconVisibilityForCurrentUser()106 private void updateIconVisibilityForCurrentUser() { 107 mIsLocationActive = mLocationManager.isLocationEnabledForUser(mUserTracker.getUserHandle()); 108 updateStatus(); 109 } 110 } 111