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.app.ActivityManager; 20 import android.car.settings.CarSettings; 21 import android.content.ContentResolver; 22 import android.content.Context; 23 import android.database.ContentObserver; 24 import android.net.Uri; 25 import android.os.Handler; 26 import android.provider.Settings; 27 28 import com.android.systemui.Dependency; 29 import com.android.systemui.broadcast.BroadcastDispatcher; 30 import com.android.systemui.dagger.qualifiers.Main; 31 import com.android.systemui.statusbar.policy.DeviceProvisionedControllerImpl; 32 33 import javax.inject.Inject; 34 import javax.inject.Singleton; 35 36 /** 37 * A controller that monitors the status of SUW progress for each user in addition to the 38 * functionality provided by {@link DeviceProvisionedControllerImpl}. 39 */ 40 @Singleton 41 public class CarDeviceProvisionedControllerImpl extends DeviceProvisionedControllerImpl implements 42 CarDeviceProvisionedController { 43 private static final Uri USER_SETUP_IN_PROGRESS_URI = Settings.Secure.getUriFor( 44 CarSettings.Secure.KEY_SETUP_WIZARD_IN_PROGRESS); 45 private final ContentObserver mCarSettingsObserver = new ContentObserver( 46 Dependency.get(Dependency.MAIN_HANDLER)) { 47 48 @Override 49 public void onChange(boolean selfChange, Uri uri, int flags) { 50 if (USER_SETUP_IN_PROGRESS_URI.equals(uri)) { 51 notifyUserSetupInProgressChanged(); 52 } 53 } 54 }; 55 private final ContentResolver mContentResolver; 56 57 @Inject CarDeviceProvisionedControllerImpl(Context context, @Main Handler mainHandler, BroadcastDispatcher broadcastDispatcher)58 public CarDeviceProvisionedControllerImpl(Context context, @Main Handler mainHandler, 59 BroadcastDispatcher broadcastDispatcher) { 60 super(context, mainHandler, broadcastDispatcher); 61 mContentResolver = context.getContentResolver(); 62 } 63 64 @Override isUserSetupInProgress(int user)65 public boolean isUserSetupInProgress(int user) { 66 return Settings.Secure.getIntForUser(mContentResolver, 67 CarSettings.Secure.KEY_SETUP_WIZARD_IN_PROGRESS, /* def= */ 0, user) != 0; 68 } 69 70 @Override isCurrentUserSetupInProgress()71 public boolean isCurrentUserSetupInProgress() { 72 return isUserSetupInProgress(ActivityManager.getCurrentUser()); 73 } 74 75 @Override addCallback(DeviceProvisionedListener listener)76 public void addCallback(DeviceProvisionedListener listener) { 77 super.addCallback(listener); 78 if (listener instanceof CarDeviceProvisionedListener) { 79 ((CarDeviceProvisionedListener) listener).onUserSetupInProgressChanged(); 80 } 81 } 82 83 @Override startListening(int user)84 protected void startListening(int user) { 85 mContentResolver.registerContentObserver( 86 USER_SETUP_IN_PROGRESS_URI, /* notifyForDescendants= */ true, mCarSettingsObserver, 87 user); 88 // The SUW Flag observer is registered before super.startListening() so that the observer is 89 // in place before DeviceProvisionedController starts to track user switches which avoids 90 // an edge case where our observer gets registered twice. 91 super.startListening(user); 92 } 93 94 @Override stopListening()95 protected void stopListening() { 96 super.stopListening(); 97 mContentResolver.unregisterContentObserver(mCarSettingsObserver); 98 } 99 100 @Override onUserSwitched(int newUserId)101 public void onUserSwitched(int newUserId) { 102 super.onUserSwitched(newUserId); 103 mContentResolver.unregisterContentObserver(mCarSettingsObserver); 104 mContentResolver.registerContentObserver( 105 USER_SETUP_IN_PROGRESS_URI, /* notifyForDescendants= */ true, mCarSettingsObserver, 106 newUserId); 107 } 108 notifyUserSetupInProgressChanged()109 private void notifyUserSetupInProgressChanged() { 110 for (int i = mListeners.size() - 1; i >= 0; --i) { 111 DeviceProvisionedListener listener = mListeners.get(i); 112 if (listener instanceof CarDeviceProvisionedListener) { 113 ((CarDeviceProvisionedListener) listener).onUserSetupInProgressChanged(); 114 } 115 } 116 } 117 } 118