1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui.statusbar.policy; 16 17 import android.app.ActivityManager; 18 import android.content.ContentResolver; 19 import android.content.Context; 20 import android.database.ContentObserver; 21 import android.net.Uri; 22 import android.provider.Settings.Global; 23 import android.provider.Settings.Secure; 24 25 import com.android.systemui.Dependency; 26 import com.android.systemui.settings.CurrentUserTracker; 27 28 import java.util.ArrayList; 29 30 public class DeviceProvisionedControllerImpl extends CurrentUserTracker implements 31 DeviceProvisionedController { 32 33 private final ArrayList<DeviceProvisionedListener> mListeners = new ArrayList<>(); 34 private final ContentResolver mContentResolver; 35 private final Context mContext; 36 private final Uri mDeviceProvisionedUri; 37 private final Uri mUserSetupUri; 38 39 public DeviceProvisionedControllerImpl(Context context) { 40 super(context); 41 mContext = context; 42 mContentResolver = context.getContentResolver(); 43 mDeviceProvisionedUri = Global.getUriFor(Global.DEVICE_PROVISIONED); 44 mUserSetupUri = Secure.getUriFor(Secure.USER_SETUP_COMPLETE); 45 } 46 47 @Override 48 public boolean isDeviceProvisioned() { 49 return Global.getInt(mContentResolver, Global.DEVICE_PROVISIONED, 0) != 0; 50 } 51 52 @Override 53 public boolean isUserSetup(int currentUser) { 54 return Secure.getIntForUser(mContentResolver, Secure.USER_SETUP_COMPLETE, 0, currentUser) 55 != 0; 56 } 57 58 @Override 59 public int getCurrentUser() { 60 return ActivityManager.getCurrentUser(); 61 } 62 63 @Override 64 public void addCallback(DeviceProvisionedListener listener) { 65 mListeners.add(listener); 66 if (mListeners.size() == 1) { 67 startListening(getCurrentUser()); 68 } 69 listener.onUserSetupChanged(); 70 listener.onDeviceProvisionedChanged(); 71 } 72 73 @Override 74 public void removeCallback(DeviceProvisionedListener listener) { 75 mListeners.remove(listener); 76 if (mListeners.size() == 0) { 77 stopListening(); 78 } 79 } 80 81 private void startListening(int user) { 82 mContentResolver.registerContentObserver(mDeviceProvisionedUri, true, 83 mSettingsObserver, 0); 84 mContentResolver.registerContentObserver(mUserSetupUri, true, 85 mSettingsObserver, user); 86 startTracking(); 87 } 88 89 private void stopListening() { 90 stopTracking(); 91 mContentResolver.unregisterContentObserver(mSettingsObserver); 92 } 93 94 @Override 95 public void onUserSwitched(int newUserId) { 96 mContentResolver.unregisterContentObserver(mSettingsObserver); 97 mContentResolver.registerContentObserver(mDeviceProvisionedUri, true, 98 mSettingsObserver, 0); 99 mContentResolver.registerContentObserver(mUserSetupUri, true, 100 mSettingsObserver, newUserId); 101 notifyUserChanged(); 102 } 103 104 private void notifyUserChanged() { 105 mListeners.forEach(c -> c.onUserSwitched()); 106 } 107 108 private void notifySetupChanged() { 109 mListeners.forEach(c -> c.onUserSetupChanged()); 110 } 111 112 private void notifyProvisionedChanged() { 113 mListeners.forEach(c -> c.onDeviceProvisionedChanged()); 114 } 115 116 protected final ContentObserver mSettingsObserver = new ContentObserver(Dependency.get( 117 Dependency.MAIN_HANDLER)) { 118 119 @Override 120 public void onChange(boolean selfChange, Uri uri, int userId) { 121 if (mUserSetupUri.equals(uri)) { 122 notifySetupChanged(); 123 } else { 124 notifyProvisionedChanged(); 125 } 126 } 127 }; 128 } 129