DeviceProvisionedControllerImpl.java revision 9c7844cb91b43929d0a86b1c90aa1efb37f5463a
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
15package com.android.systemui.statusbar.policy;
16
17import android.app.ActivityManager;
18import android.content.ContentResolver;
19import android.content.Context;
20import android.database.ContentObserver;
21import android.net.Uri;
22import android.provider.Settings.Global;
23import android.provider.Settings.Secure;
24
25import com.android.systemui.Dependency;
26import com.android.systemui.settings.CurrentUserTracker;
27
28import java.util.ArrayList;
29
30public 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    }
70
71    @Override
72    public void removeCallback(DeviceProvisionedListener listener) {
73        mListeners.remove(listener);
74        if (mListeners.size() == 0) {
75            stopListening();
76        }
77    }
78
79    private void startListening(int user) {
80        mContentResolver.registerContentObserver(mDeviceProvisionedUri, true,
81                mSettingsObserver, 0);
82        mContentResolver.registerContentObserver(mUserSetupUri, true,
83                mSettingsObserver, user);
84        startTracking();
85    }
86
87    private void stopListening() {
88        stopTracking();
89        mContentResolver.unregisterContentObserver(mSettingsObserver);
90    }
91
92    @Override
93    public void onUserSwitched(int newUserId) {
94        stopListening();
95        startListening(newUserId);
96        notifyUserChanged();
97        notifyUserChanged();
98    }
99
100    private void notifyUserChanged() {
101        mListeners.forEach(c -> c.onUserSwitched());
102    }
103
104    private void notifySetupChanged() {
105        mListeners.forEach(c -> c.onUserSetupChanged());
106    }
107
108    private void notifyProvisionedChanged() {
109        mListeners.forEach(c -> c.onDeviceProvisionedChanged());
110    }
111
112    protected final ContentObserver mSettingsObserver = new ContentObserver(Dependency.get(
113            Dependency.MAIN_HANDLER)) {
114
115        @Override
116        public void onChange(boolean selfChange, Uri uri, int userId) {
117            if (mUserSetupUri.equals(uri)) {
118                notifySetupChanged();
119            } else {
120                notifyProvisionedChanged();
121            }
122        }
123    };
124}
125