1 /* 2 * Copyright (C) 2019 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; 18 19 import android.annotation.NonNull; 20 import android.car.settings.CarSettings; 21 import android.database.ContentObserver; 22 import android.net.Uri; 23 import android.os.Handler; 24 import android.os.UserHandle; 25 26 import com.android.systemui.dagger.SysUISingleton; 27 import com.android.systemui.dagger.qualifiers.Background; 28 import com.android.systemui.dagger.qualifiers.Main; 29 import com.android.systemui.dump.DumpManager; 30 import com.android.systemui.settings.UserTracker; 31 import com.android.systemui.statusbar.policy.DeviceProvisionedControllerImpl; 32 import com.android.systemui.util.settings.GlobalSettings; 33 import com.android.systemui.util.settings.SecureSettings; 34 35 import kotlin.Unit; 36 37 import java.util.concurrent.Executor; 38 import java.util.concurrent.atomic.AtomicBoolean; 39 40 import javax.inject.Inject; 41 42 /** 43 * A controller that monitors the status of SUW progress for each user in addition to the 44 * functionality provided by {@link DeviceProvisionedControllerImpl}. 45 */ 46 @SysUISingleton 47 public class CarDeviceProvisionedControllerImpl extends DeviceProvisionedControllerImpl implements 48 CarDeviceProvisionedController { 49 private final UserTracker mUserTracker; 50 private final Uri mUserSetupInProgressUri; 51 private final ContentObserver mCarSettingsObserver; 52 private final Handler mMainHandler; 53 private final SecureSettings mSecureSettings; 54 private final AtomicBoolean mInitted = new AtomicBoolean(false); 55 56 @Inject CarDeviceProvisionedControllerImpl( SecureSettings secureSettings, GlobalSettings globalSettings, UserTracker userTracker, DumpManager dumpManager, @Background Handler backgroundHandler, @Main Handler mainHandler, @Main Executor mainExecutor)57 public CarDeviceProvisionedControllerImpl( 58 SecureSettings secureSettings, 59 GlobalSettings globalSettings, 60 UserTracker userTracker, 61 DumpManager dumpManager, 62 @Background Handler backgroundHandler, 63 @Main Handler mainHandler, 64 @Main Executor mainExecutor) { 65 super(secureSettings, globalSettings, userTracker, dumpManager, 66 backgroundHandler, mainExecutor); 67 68 mUserTracker = userTracker; 69 mMainHandler = mainHandler; 70 mSecureSettings = secureSettings; 71 mUserSetupInProgressUri = mSecureSettings.getUriFor( 72 CarSettings.Secure.KEY_SETUP_WIZARD_IN_PROGRESS); 73 mCarSettingsObserver = new ContentObserver(mMainHandler) { 74 @Override 75 public void onChange(boolean selfChange, Uri uri, int flags) { 76 if (mUserSetupInProgressUri.equals(uri)) { 77 notifyUserSetupInProgressChanged(); 78 } 79 } 80 }; 81 } 82 83 @Override init()84 public void init() { 85 if (!mInitted.compareAndSet(false, true)) { 86 return; 87 } 88 mSecureSettings.registerContentObserverForUserSync( 89 mUserSetupInProgressUri, /* notifyForDescendants= */ true, 90 mCarSettingsObserver, UserHandle.USER_ALL); 91 super.init(); 92 } 93 94 @Override isUserSetupInProgress(int user)95 public boolean isUserSetupInProgress(int user) { 96 return mSecureSettings.getIntForUser( 97 CarSettings.Secure.KEY_SETUP_WIZARD_IN_PROGRESS, /* def= */ 0, user) != 0; 98 } 99 100 @Override isCurrentUserSetupInProgress()101 public boolean isCurrentUserSetupInProgress() { 102 return isUserSetupInProgress(mUserTracker.getUserId()); 103 } 104 105 @Override addCallback(@onNull DeviceProvisionedListener listener)106 public void addCallback(@NonNull DeviceProvisionedListener listener) { 107 super.addCallback(listener); 108 if (listener instanceof CarDeviceProvisionedListener) { 109 ((CarDeviceProvisionedListener) listener).onUserSetupInProgressChanged(); 110 } 111 } 112 notifyUserSetupInProgressChanged()113 private void notifyUserSetupInProgressChanged() { 114 dispatchChange(listener -> { 115 if (listener instanceof CarDeviceProvisionedListener) { 116 ((CarDeviceProvisionedListener) listener).onUserSetupInProgressChanged(); 117 } 118 return Unit.INSTANCE; 119 }); 120 } 121 } 122