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.os.Handler; 23 import android.provider.Settings.Global; 24 import android.provider.Settings.Secure; 25 import android.util.Log; 26 27 import com.android.systemui.broadcast.BroadcastDispatcher; 28 import com.android.systemui.dagger.qualifiers.Main; 29 import com.android.systemui.settings.CurrentUserTracker; 30 31 import java.util.ArrayList; 32 33 import javax.inject.Inject; 34 import javax.inject.Singleton; 35 36 /** 37 */ 38 @Singleton 39 public class DeviceProvisionedControllerImpl extends CurrentUserTracker implements 40 DeviceProvisionedController { 41 42 protected static final String TAG = DeviceProvisionedControllerImpl.class.getSimpleName(); 43 protected final ArrayList<DeviceProvisionedListener> mListeners = new ArrayList<>(); 44 private final ContentResolver mContentResolver; 45 private final Context mContext; 46 private final Uri mDeviceProvisionedUri; 47 private final Uri mUserSetupUri; 48 protected final ContentObserver mSettingsObserver; 49 50 /** 51 */ 52 @Inject DeviceProvisionedControllerImpl(Context context, @Main Handler mainHandler, BroadcastDispatcher broadcastDispatcher)53 public DeviceProvisionedControllerImpl(Context context, @Main Handler mainHandler, 54 BroadcastDispatcher broadcastDispatcher) { 55 super(broadcastDispatcher); 56 mContext = context; 57 mContentResolver = context.getContentResolver(); 58 mDeviceProvisionedUri = Global.getUriFor(Global.DEVICE_PROVISIONED); 59 mUserSetupUri = Secure.getUriFor(Secure.USER_SETUP_COMPLETE); 60 mSettingsObserver = new ContentObserver(mainHandler) { 61 @Override 62 public void onChange(boolean selfChange, Uri uri, int flags) { 63 Log.d(TAG, "Setting change: " + uri); 64 if (mUserSetupUri.equals(uri)) { 65 notifySetupChanged(); 66 } else { 67 notifyProvisionedChanged(); 68 } 69 } 70 }; 71 } 72 73 @Override isDeviceProvisioned()74 public boolean isDeviceProvisioned() { 75 return Global.getInt(mContentResolver, Global.DEVICE_PROVISIONED, 0) != 0; 76 } 77 78 @Override isUserSetup(int currentUser)79 public boolean isUserSetup(int currentUser) { 80 return Secure.getIntForUser(mContentResolver, Secure.USER_SETUP_COMPLETE, 0, currentUser) 81 != 0; 82 } 83 84 @Override getCurrentUser()85 public int getCurrentUser() { 86 return ActivityManager.getCurrentUser(); 87 } 88 89 @Override addCallback(DeviceProvisionedListener listener)90 public void addCallback(DeviceProvisionedListener listener) { 91 mListeners.add(listener); 92 if (mListeners.size() == 1) { 93 startListening(getCurrentUser()); 94 } 95 listener.onUserSetupChanged(); 96 listener.onDeviceProvisionedChanged(); 97 } 98 99 @Override removeCallback(DeviceProvisionedListener listener)100 public void removeCallback(DeviceProvisionedListener listener) { 101 mListeners.remove(listener); 102 if (mListeners.size() == 0) { 103 stopListening(); 104 } 105 } 106 startListening(int user)107 protected void startListening(int user) { 108 mContentResolver.registerContentObserver(mDeviceProvisionedUri, true, 109 mSettingsObserver, 0); 110 mContentResolver.registerContentObserver(mUserSetupUri, true, 111 mSettingsObserver, user); 112 startTracking(); 113 } 114 stopListening()115 protected void stopListening() { 116 stopTracking(); 117 mContentResolver.unregisterContentObserver(mSettingsObserver); 118 } 119 120 @Override onUserSwitched(int newUserId)121 public void onUserSwitched(int newUserId) { 122 mContentResolver.unregisterContentObserver(mSettingsObserver); 123 mContentResolver.registerContentObserver(mDeviceProvisionedUri, true, 124 mSettingsObserver, 0); 125 mContentResolver.registerContentObserver(mUserSetupUri, true, 126 mSettingsObserver, newUserId); 127 notifyUserChanged(); 128 } 129 notifyUserChanged()130 private void notifyUserChanged() { 131 for (int i = mListeners.size() - 1; i >= 0; --i) { 132 mListeners.get(i).onUserSwitched(); 133 } 134 } 135 notifySetupChanged()136 private void notifySetupChanged() { 137 for (int i = mListeners.size() - 1; i >= 0; --i) { 138 mListeners.get(i).onUserSetupChanged(); 139 } 140 } 141 notifyProvisionedChanged()142 private void notifyProvisionedChanged() { 143 for (int i = mListeners.size() - 1; i >= 0; --i) { 144 mListeners.get(i).onDeviceProvisionedChanged(); 145 } 146 } 147 } 148