Search in sources :

Example 46 with ContentObserver

use of android.database.ContentObserver in project android_frameworks_base by crdroidandroid.

the class SettingsProviderTest method setSettingAndAssertSuccessfulChange.

private void setSettingAndAssertSuccessfulChange(Runnable setCommand, final int type, final String name, final String value, final int userId) throws Exception {
    ContentResolver contentResolver = getContext().getContentResolver();
    final Uri settingUri = getBaseUriForType(type);
    final AtomicBoolean success = new AtomicBoolean();
    ContentObserver contentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {

        public void onChange(boolean selfChange, Uri changeUri, int changeId) {
            Log.i(LOG_TAG, "onChange(" + selfChange + ", " + changeUri + ", " + changeId + ")");
            assertEquals("Wrong change Uri", changeUri, settingUri);
            assertEquals("Wrong user id", userId, changeId);
            String changeValue = getStringViaFrontEndApiSetting(type, name, userId);
            assertEquals("Wrong setting value", value, changeValue);
            success.set(true);
            synchronized (mLock) {
                mLock.notifyAll();
            }
        }
    };
    contentResolver.registerContentObserver(settingUri, false, contentObserver, userId);
    try {
        setCommand.run();
        final long startTimeMillis = SystemClock.uptimeMillis();
        synchronized (mLock) {
            if (success.get()) {
                return;
            }
            final long elapsedTimeMillis = SystemClock.uptimeMillis() - startTimeMillis;
            if (elapsedTimeMillis > WAIT_FOR_SETTING_URI_CHANGE_TIMEOUT_MILLIS) {
                fail("Could not change setting for " + WAIT_FOR_SETTING_URI_CHANGE_TIMEOUT_MILLIS + " ms");
            }
            final long remainingTimeMillis = WAIT_FOR_SETTING_URI_CHANGE_TIMEOUT_MILLIS - elapsedTimeMillis;
            try {
                mLock.wait(remainingTimeMillis);
            } catch (InterruptedException ie) {
            /* ignore */
            }
        }
    } finally {
        contentResolver.unregisterContentObserver(contentObserver);
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Handler(android.os.Handler) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) ContentObserver(android.database.ContentObserver)

Example 47 with ContentObserver

use of android.database.ContentObserver in project android_frameworks_base by crdroidandroid.

the class LocationManagerService method systemRunning.

public void systemRunning() {
    synchronized (mLock) {
        if (D)
            Log.d(TAG, "systemRunning()");
        // fetch package manager
        mPackageManager = mContext.getPackageManager();
        // fetch power manager
        mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        // prepare worker thread
        mLocationHandler = new LocationWorkerHandler(BackgroundThread.get().getLooper());
        // prepare mLocationHandler's dependents
        mLocationFudger = new LocationFudger(mContext, mLocationHandler);
        mBlacklist = new LocationBlacklist(mContext, mLocationHandler);
        mBlacklist.init();
        mGeofenceManager = new GeofenceManager(mContext, mBlacklist);
        // Monitor for app ops mode changes.
        AppOpsManager.OnOpChangedListener callback = new AppOpsManager.OnOpChangedInternalListener() {

            public void onOpChanged(int op, String packageName) {
                synchronized (mLock) {
                    for (Receiver receiver : mReceivers.values()) {
                        receiver.updateMonitoring(true);
                    }
                    applyAllProviderRequirementsLocked();
                }
            }
        };
        mAppOps.startWatchingMode(AppOpsManager.OP_COARSE_LOCATION, null, callback);
        PackageManager.OnPermissionsChangedListener permissionListener = new PackageManager.OnPermissionsChangedListener() {

            @Override
            public void onPermissionsChanged(final int uid) {
                synchronized (mLock) {
                    applyAllProviderRequirementsLocked();
                }
            }
        };
        mPackageManager.addOnPermissionsChangeListener(permissionListener);
        mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
        updateUserProfiles(mCurrentUserId);
        // prepare providers
        loadProvidersLocked();
        updateProvidersLocked();
    }
    // listen for settings changes
    mContext.getContentResolver().registerContentObserver(Settings.Secure.getUriFor(Settings.Secure.LOCATION_PROVIDERS_ALLOWED), true, new ContentObserver(mLocationHandler) {

        @Override
        public void onChange(boolean selfChange) {
            synchronized (mLock) {
                updateProvidersLocked();
            }
        }
    }, UserHandle.USER_ALL);
    mPackageMonitor.register(mContext, mLocationHandler.getLooper(), true);
    // listen for user change
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
    intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
    intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
    intentFilter.addAction(Intent.ACTION_SHUTDOWN);
    mContext.registerReceiverAsUser(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_USER_SWITCHED.equals(action)) {
                switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
            } else if (Intent.ACTION_MANAGED_PROFILE_ADDED.equals(action) || Intent.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
                updateUserProfiles(mCurrentUserId);
            } else if (Intent.ACTION_SHUTDOWN.equals(action)) {
                // shutdown only if UserId indicates whole system, not just one user
                if (D)
                    Log.d(TAG, "Shutdown received with UserId: " + getSendingUserId());
                if (getSendingUserId() == UserHandle.USER_ALL) {
                    shutdownComponents();
                }
            }
        }
    }, UserHandle.ALL, intentFilter, null, mLocationHandler);
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) LocationBlacklist(com.android.server.location.LocationBlacklist) GeofenceManager(com.android.server.location.GeofenceManager) BroadcastReceiver(android.content.BroadcastReceiver) LocationFudger(com.android.server.location.LocationFudger) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) AppOpsManager(android.app.AppOpsManager) PackageManager(android.content.pm.PackageManager) ContentObserver(android.database.ContentObserver)

Example 48 with ContentObserver

use of android.database.ContentObserver in project android_frameworks_base by crdroidandroid.

the class NightDisplayService method onUserChanged.

private void onUserChanged(int userHandle) {
    final ContentResolver cr = getContext().getContentResolver();
    if (mCurrentUser != UserHandle.USER_NULL) {
        if (mUserSetupObserver != null) {
            cr.unregisterContentObserver(mUserSetupObserver);
            mUserSetupObserver = null;
        } else if (mBootCompleted) {
            tearDown();
        }
    }
    mCurrentUser = userHandle;
    if (mCurrentUser != UserHandle.USER_NULL) {
        if (!isUserSetupCompleted(cr, mCurrentUser)) {
            mUserSetupObserver = new ContentObserver(mHandler) {

                @Override
                public void onChange(boolean selfChange, Uri uri) {
                    if (isUserSetupCompleted(cr, mCurrentUser)) {
                        cr.unregisterContentObserver(this);
                        mUserSetupObserver = null;
                        if (mBootCompleted) {
                            setUp();
                        }
                    }
                }
            };
            cr.registerContentObserver(Secure.getUriFor(Secure.USER_SETUP_COMPLETE), false, /* notifyForDescendents */
            mUserSetupObserver, mCurrentUser);
        } else if (mBootCompleted) {
            setUp();
        }
    }
}
Also used : Uri(android.net.Uri) ContentResolver(android.content.ContentResolver) ContentObserver(android.database.ContentObserver)

Example 49 with ContentObserver

use of android.database.ContentObserver in project android_frameworks_base by AOSPA.

the class ContentQueryMap method setKeepUpdated.

/**
     * Change whether or not the ContentQueryMap will register with the cursor's ContentProvider 
     * for change notifications. If you use a ContentQueryMap in an activity you should call this
     * with false in onPause(), which means you need to call it with true in onResume()
     * if want it to be kept updated.
     * @param keepUpdated if true the ContentQueryMap should be registered with the cursor's
     * ContentProvider, false otherwise
     */
public void setKeepUpdated(boolean keepUpdated) {
    if (keepUpdated == mKeepUpdated)
        return;
    mKeepUpdated = keepUpdated;
    if (!mKeepUpdated) {
        mCursor.unregisterContentObserver(mContentObserver);
        mContentObserver = null;
    } else {
        if (mHandlerForUpdateNotifications == null) {
            mHandlerForUpdateNotifications = new Handler();
        }
        if (mContentObserver == null) {
            mContentObserver = new ContentObserver(mHandlerForUpdateNotifications) {

                @Override
                public void onChange(boolean selfChange) {
                    // let it query lazily when they ask for the values.
                    if (countObservers() != 0) {
                        requery();
                    } else {
                        mDirty = true;
                    }
                }
            };
        }
        mCursor.registerContentObserver(mContentObserver);
        // mark dirty, since it is possible the cursor's backing data had changed before we 
        // registered for changes
        mDirty = true;
    }
}
Also used : Handler(android.os.Handler) ContentObserver(android.database.ContentObserver)

Example 50 with ContentObserver

use of android.database.ContentObserver in project android_frameworks_base by AOSPA.

the class BluetoothManagerService method registerForBleScanModeChange.

// Monitor change of BLE scan only mode settings.
private void registerForBleScanModeChange() {
    ContentObserver contentObserver = new ContentObserver(null) {

        @Override
        public void onChange(boolean selfChange) {
            if (isBleScanAlwaysAvailable()) {
                // Nothing to do
                return;
            }
            // BLE scan is not available.
            disableBleScanMode();
            clearBleApps();
            try {
                mBluetoothLock.readLock().lock();
                if (mBluetooth != null)
                    mBluetooth.onBrEdrDown();
            } catch (RemoteException e) {
                Slog.e(TAG, "error when disabling bluetooth", e);
            } finally {
                mBluetoothLock.readLock().unlock();
            }
        }
    };
    mContentResolver.registerContentObserver(Settings.Global.getUriFor(Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE), false, contentObserver);
}
Also used : RemoteException(android.os.RemoteException) ContentObserver(android.database.ContentObserver)

Aggregations

ContentObserver (android.database.ContentObserver)96 Handler (android.os.Handler)41 Uri (android.net.Uri)35 ContentResolver (android.content.ContentResolver)28 Intent (android.content.Intent)16 IntentFilter (android.content.IntentFilter)16 BroadcastReceiver (android.content.BroadcastReceiver)9 Context (android.content.Context)9 PendingIntent (android.app.PendingIntent)7 RemoteException (android.os.RemoteException)7 Test (org.junit.Test)7 AppOpsManager (android.app.AppOpsManager)6 GeofenceManager (com.android.server.location.GeofenceManager)6 LocationBlacklist (com.android.server.location.LocationBlacklist)6 LocationFudger (com.android.server.location.LocationFudger)6 HashSet (java.util.HashSet)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Mockito.doAnswer (org.mockito.Mockito.doAnswer)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 Answer (org.mockito.stubbing.Answer)6