Search in sources :

Example 11 with PackageMonitor

use of com.android.internal.content.PackageMonitor in project platform_frameworks_base by android.

the class SettingsProvider method registerBroadcastReceivers.

private void registerBroadcastReceivers() {
    IntentFilter userFilter = new IntentFilter();
    userFilter.addAction(Intent.ACTION_USER_REMOVED);
    userFilter.addAction(Intent.ACTION_USER_STOPPED);
    getContext().registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_SYSTEM);
            switch(intent.getAction()) {
                case Intent.ACTION_USER_REMOVED:
                    {
                        synchronized (mLock) {
                            mSettingsRegistry.removeUserStateLocked(userId, true);
                        }
                    }
                    break;
                case Intent.ACTION_USER_STOPPED:
                    {
                        synchronized (mLock) {
                            mSettingsRegistry.removeUserStateLocked(userId, false);
                        }
                    }
                    break;
            }
        }
    }, userFilter);
    PackageMonitor monitor = new PackageMonitor() {

        @Override
        public void onPackageRemoved(String packageName, int uid) {
            synchronized (mLock) {
                mSettingsRegistry.onPackageRemovedLocked(packageName, UserHandle.getUserId(uid));
            }
        }
    };
    // package changes
    monitor.register(getContext(), BackgroundThread.getHandler().getLooper(), UserHandle.ALL, true);
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) PackageMonitor(com.android.internal.content.PackageMonitor)

Example 12 with PackageMonitor

use of com.android.internal.content.PackageMonitor in project android_frameworks_base by AOSPA.

the class AccessibilityManagerService method registerBroadcastReceivers.

private void registerBroadcastReceivers() {
    PackageMonitor monitor = new PackageMonitor() {

        @Override
        public void onSomePackagesChanged() {
            synchronized (mLock) {
                // Therefore we ignore packages from linked profiles.
                if (getChangingUserId() != mCurrentUserId) {
                    return;
                }
                // We will update when the automation service dies.
                UserState userState = getCurrentUserStateLocked();
                // We have to reload the installed services since some services may
                // have different attributes, resolve info (does not support equals),
                // etc. Remove them then to force reload.
                userState.mInstalledServices.clear();
                if (!userState.isUiAutomationSuppressingOtherServices()) {
                    if (readConfigurationForUserStateLocked(userState)) {
                        onUserStateChangedLocked(userState);
                    }
                }
            }
        }

        @Override
        public void onPackageUpdateFinished(String packageName, int uid) {
            // re-bind new versions of them.
            synchronized (mLock) {
                final int userId = getChangingUserId();
                if (userId != mCurrentUserId) {
                    return;
                }
                UserState userState = getUserStateLocked(userId);
                boolean unboundAService = false;
                for (int i = userState.mBoundServices.size() - 1; i >= 0; i--) {
                    Service boundService = userState.mBoundServices.get(i);
                    String servicePkg = boundService.mComponentName.getPackageName();
                    if (servicePkg.equals(packageName)) {
                        boundService.unbindLocked();
                        unboundAService = true;
                    }
                }
                if (unboundAService) {
                    onUserStateChangedLocked(userState);
                }
            }
        }

        @Override
        public void onPackageRemoved(String packageName, int uid) {
            synchronized (mLock) {
                final int userId = getChangingUserId();
                // Therefore we ignore packages from linked profiles.
                if (userId != mCurrentUserId) {
                    return;
                }
                UserState userState = getUserStateLocked(userId);
                Iterator<ComponentName> it = userState.mEnabledServices.iterator();
                while (it.hasNext()) {
                    ComponentName comp = it.next();
                    String compPkg = comp.getPackageName();
                    if (compPkg.equals(packageName)) {
                        it.remove();
                        // Update the enabled services setting.
                        persistComponentNamesToSettingLocked(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, userState.mEnabledServices, userId);
                        // Update the touch exploration granted services setting.
                        userState.mTouchExplorationGrantedServices.remove(comp);
                        persistComponentNamesToSettingLocked(Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES, userState.mTouchExplorationGrantedServices, userId);
                        // We will update when the automation service dies.
                        if (!userState.isUiAutomationSuppressingOtherServices()) {
                            onUserStateChangedLocked(userState);
                        }
                        return;
                    }
                }
            }
        }

        @Override
        public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
            synchronized (mLock) {
                final int userId = getChangingUserId();
                // Therefore we ignore packages from linked profiles.
                if (userId != mCurrentUserId) {
                    return false;
                }
                UserState userState = getUserStateLocked(userId);
                Iterator<ComponentName> it = userState.mEnabledServices.iterator();
                while (it.hasNext()) {
                    ComponentName comp = it.next();
                    String compPkg = comp.getPackageName();
                    for (String pkg : packages) {
                        if (compPkg.equals(pkg)) {
                            if (!doit) {
                                return true;
                            }
                            it.remove();
                            persistComponentNamesToSettingLocked(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, userState.mEnabledServices, userId);
                            // We will update when the automation service dies.
                            if (!userState.isUiAutomationSuppressingOtherServices()) {
                                onUserStateChangedLocked(userState);
                            }
                        }
                    }
                }
                return false;
            }
        }
    };
    // package changes
    monitor.register(mContext, null, UserHandle.ALL, true);
    // user change and unlock
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
    intentFilter.addAction(Intent.ACTION_USER_UNLOCKED);
    intentFilter.addAction(Intent.ACTION_USER_REMOVED);
    intentFilter.addAction(Intent.ACTION_USER_PRESENT);
    intentFilter.addAction(Intent.ACTION_SETTING_RESTORED);
    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_USER_UNLOCKED.equals(action)) {
                unlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
            } else if (Intent.ACTION_USER_REMOVED.equals(action)) {
                removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
            } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
                // We will update when the automation service dies.
                synchronized (mLock) {
                    UserState userState = getCurrentUserStateLocked();
                    if (!userState.isUiAutomationSuppressingOtherServices()) {
                        if (readConfigurationForUserStateLocked(userState)) {
                            onUserStateChangedLocked(userState);
                        }
                    }
                }
            } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
                final String which = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
                if (Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES.equals(which)) {
                    synchronized (mLock) {
                        restoreEnabledAccessibilityServicesLocked(intent.getStringExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE), intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE));
                    }
                }
            }
        }
    }, UserHandle.ALL, intentFilter, null, null);
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) AccessibilityService(android.accessibilityservice.AccessibilityService) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) PackageMonitor(com.android.internal.content.PackageMonitor) Point(android.graphics.Point) ComponentName(android.content.ComponentName)

Example 13 with PackageMonitor

use of com.android.internal.content.PackageMonitor in project android_frameworks_base by DirtyUnicorns.

the class AccessibilityManagerService method registerBroadcastReceivers.

private void registerBroadcastReceivers() {
    PackageMonitor monitor = new PackageMonitor() {

        @Override
        public void onSomePackagesChanged() {
            synchronized (mLock) {
                // Therefore we ignore packages from linked profiles.
                if (getChangingUserId() != mCurrentUserId) {
                    return;
                }
                // We will update when the automation service dies.
                UserState userState = getCurrentUserStateLocked();
                // We have to reload the installed services since some services may
                // have different attributes, resolve info (does not support equals),
                // etc. Remove them then to force reload.
                userState.mInstalledServices.clear();
                if (!userState.isUiAutomationSuppressingOtherServices()) {
                    if (readConfigurationForUserStateLocked(userState)) {
                        onUserStateChangedLocked(userState);
                    }
                }
            }
        }

        @Override
        public void onPackageUpdateFinished(String packageName, int uid) {
            // re-bind new versions of them.
            synchronized (mLock) {
                final int userId = getChangingUserId();
                if (userId != mCurrentUserId) {
                    return;
                }
                UserState userState = getUserStateLocked(userId);
                boolean unboundAService = false;
                for (int i = userState.mBoundServices.size() - 1; i >= 0; i--) {
                    Service boundService = userState.mBoundServices.get(i);
                    String servicePkg = boundService.mComponentName.getPackageName();
                    if (servicePkg.equals(packageName)) {
                        boundService.unbindLocked();
                        unboundAService = true;
                    }
                }
                if (unboundAService) {
                    onUserStateChangedLocked(userState);
                }
            }
        }

        @Override
        public void onPackageRemoved(String packageName, int uid) {
            synchronized (mLock) {
                final int userId = getChangingUserId();
                // Therefore we ignore packages from linked profiles.
                if (userId != mCurrentUserId) {
                    return;
                }
                UserState userState = getUserStateLocked(userId);
                Iterator<ComponentName> it = userState.mEnabledServices.iterator();
                while (it.hasNext()) {
                    ComponentName comp = it.next();
                    String compPkg = comp.getPackageName();
                    if (compPkg.equals(packageName)) {
                        it.remove();
                        // Update the enabled services setting.
                        persistComponentNamesToSettingLocked(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, userState.mEnabledServices, userId);
                        // Update the touch exploration granted services setting.
                        userState.mTouchExplorationGrantedServices.remove(comp);
                        persistComponentNamesToSettingLocked(Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES, userState.mTouchExplorationGrantedServices, userId);
                        // We will update when the automation service dies.
                        if (!userState.isUiAutomationSuppressingOtherServices()) {
                            onUserStateChangedLocked(userState);
                        }
                        return;
                    }
                }
            }
        }

        @Override
        public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
            synchronized (mLock) {
                final int userId = getChangingUserId();
                // Therefore we ignore packages from linked profiles.
                if (userId != mCurrentUserId) {
                    return false;
                }
                UserState userState = getUserStateLocked(userId);
                Iterator<ComponentName> it = userState.mEnabledServices.iterator();
                while (it.hasNext()) {
                    ComponentName comp = it.next();
                    String compPkg = comp.getPackageName();
                    for (String pkg : packages) {
                        if (compPkg.equals(pkg)) {
                            if (!doit) {
                                return true;
                            }
                            it.remove();
                            persistComponentNamesToSettingLocked(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, userState.mEnabledServices, userId);
                            // We will update when the automation service dies.
                            if (!userState.isUiAutomationSuppressingOtherServices()) {
                                onUserStateChangedLocked(userState);
                            }
                        }
                    }
                }
                return false;
            }
        }
    };
    // package changes
    monitor.register(mContext, null, UserHandle.ALL, true);
    // user change and unlock
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
    intentFilter.addAction(Intent.ACTION_USER_UNLOCKED);
    intentFilter.addAction(Intent.ACTION_USER_REMOVED);
    intentFilter.addAction(Intent.ACTION_USER_PRESENT);
    intentFilter.addAction(Intent.ACTION_SETTING_RESTORED);
    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_USER_UNLOCKED.equals(action)) {
                unlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
            } else if (Intent.ACTION_USER_REMOVED.equals(action)) {
                removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
            } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
                // We will update when the automation service dies.
                synchronized (mLock) {
                    UserState userState = getCurrentUserStateLocked();
                    if (!userState.isUiAutomationSuppressingOtherServices()) {
                        if (readConfigurationForUserStateLocked(userState)) {
                            onUserStateChangedLocked(userState);
                        }
                    }
                }
            } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
                final String which = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
                if (Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES.equals(which)) {
                    synchronized (mLock) {
                        restoreEnabledAccessibilityServicesLocked(intent.getStringExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE), intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE));
                    }
                }
            }
        }
    }, UserHandle.ALL, intentFilter, null, null);
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) AccessibilityService(android.accessibilityservice.AccessibilityService) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) PackageMonitor(com.android.internal.content.PackageMonitor) Point(android.graphics.Point) ComponentName(android.content.ComponentName)

Example 14 with PackageMonitor

use of com.android.internal.content.PackageMonitor in project android_frameworks_base by DirtyUnicorns.

the class EnabledComponentsObserver method build.

/**
     * Create a EnabledComponentObserver instance.
     *
     * @param context the context to query for changes.
     * @param handler a handler to receive lifecycle events from system services on.
     * @param settingName the name of a setting to monitor for a list of enabled components.
     * @param looper a {@link Looper} to use for receiving package callbacks.
     * @param servicePermission the permission required by the components to be bound.
     * @param serviceName the intent action implemented by the tracked components.
     * @param lock a lock object used to guard instance state in all callbacks and method calls.
     * @return an EnableComponentObserver instance.
     */
public static EnabledComponentsObserver build(@NonNull Context context, @NonNull Handler handler, @NonNull String settingName, @NonNull Looper looper, @NonNull String servicePermission, @NonNull String serviceName, @NonNull final Object lock, @NonNull Collection<EnabledComponentChangeListener> listeners) {
    SettingsObserver s = SettingsObserver.build(context, handler, settingName);
    final EnabledComponentsObserver o = new EnabledComponentsObserver(context, settingName, servicePermission, serviceName, lock, listeners);
    PackageMonitor packageMonitor = new PackageMonitor() {

        @Override
        public void onSomePackagesChanged() {
            o.onPackagesChanged();
        }

        @Override
        public void onPackageDisappeared(String packageName, int reason) {
            o.onPackagesChanged();
        }

        @Override
        public void onPackageModified(String packageName) {
            o.onPackagesChanged();
        }

        @Override
        public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
            o.onPackagesChanged();
            return super.onHandleForceStop(intent, packages, uid, doit);
        }
    };
    packageMonitor.register(context, looper, UserHandle.ALL, true);
    s.addListener(o);
    return o;
}
Also used : Intent(android.content.Intent) PackageMonitor(com.android.internal.content.PackageMonitor)

Example 15 with PackageMonitor

use of com.android.internal.content.PackageMonitor in project android_frameworks_base by ResurrectionRemix.

the class AccessibilityManagerService method registerBroadcastReceivers.

private void registerBroadcastReceivers() {
    PackageMonitor monitor = new PackageMonitor() {

        @Override
        public void onSomePackagesChanged() {
            synchronized (mLock) {
                // Therefore we ignore packages from linked profiles.
                if (getChangingUserId() != mCurrentUserId) {
                    return;
                }
                // We will update when the automation service dies.
                UserState userState = getCurrentUserStateLocked();
                // We have to reload the installed services since some services may
                // have different attributes, resolve info (does not support equals),
                // etc. Remove them then to force reload.
                userState.mInstalledServices.clear();
                if (!userState.isUiAutomationSuppressingOtherServices()) {
                    if (readConfigurationForUserStateLocked(userState)) {
                        onUserStateChangedLocked(userState);
                    }
                }
            }
        }

        @Override
        public void onPackageUpdateFinished(String packageName, int uid) {
            // re-bind new versions of them.
            synchronized (mLock) {
                final int userId = getChangingUserId();
                if (userId != mCurrentUserId) {
                    return;
                }
                UserState userState = getUserStateLocked(userId);
                boolean unboundAService = false;
                for (int i = userState.mBoundServices.size() - 1; i >= 0; i--) {
                    Service boundService = userState.mBoundServices.get(i);
                    String servicePkg = boundService.mComponentName.getPackageName();
                    if (servicePkg.equals(packageName)) {
                        boundService.unbindLocked();
                        unboundAService = true;
                    }
                }
                if (unboundAService) {
                    onUserStateChangedLocked(userState);
                }
            }
        }

        @Override
        public void onPackageRemoved(String packageName, int uid) {
            synchronized (mLock) {
                final int userId = getChangingUserId();
                // Therefore we ignore packages from linked profiles.
                if (userId != mCurrentUserId) {
                    return;
                }
                UserState userState = getUserStateLocked(userId);
                Iterator<ComponentName> it = userState.mEnabledServices.iterator();
                while (it.hasNext()) {
                    ComponentName comp = it.next();
                    String compPkg = comp.getPackageName();
                    if (compPkg.equals(packageName)) {
                        it.remove();
                        // Update the enabled services setting.
                        persistComponentNamesToSettingLocked(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, userState.mEnabledServices, userId);
                        // Update the touch exploration granted services setting.
                        userState.mTouchExplorationGrantedServices.remove(comp);
                        persistComponentNamesToSettingLocked(Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES, userState.mTouchExplorationGrantedServices, userId);
                        // We will update when the automation service dies.
                        if (!userState.isUiAutomationSuppressingOtherServices()) {
                            onUserStateChangedLocked(userState);
                        }
                        return;
                    }
                }
            }
        }

        @Override
        public boolean onHandleForceStop(Intent intent, String[] packages, int uid, boolean doit) {
            synchronized (mLock) {
                final int userId = getChangingUserId();
                // Therefore we ignore packages from linked profiles.
                if (userId != mCurrentUserId) {
                    return false;
                }
                UserState userState = getUserStateLocked(userId);
                Iterator<ComponentName> it = userState.mEnabledServices.iterator();
                while (it.hasNext()) {
                    ComponentName comp = it.next();
                    String compPkg = comp.getPackageName();
                    for (String pkg : packages) {
                        if (compPkg.equals(pkg)) {
                            if (!doit) {
                                return true;
                            }
                            it.remove();
                            persistComponentNamesToSettingLocked(Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, userState.mEnabledServices, userId);
                            // We will update when the automation service dies.
                            if (!userState.isUiAutomationSuppressingOtherServices()) {
                                onUserStateChangedLocked(userState);
                            }
                        }
                    }
                }
                return false;
            }
        }
    };
    // package changes
    monitor.register(mContext, null, UserHandle.ALL, true);
    // user change and unlock
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
    intentFilter.addAction(Intent.ACTION_USER_UNLOCKED);
    intentFilter.addAction(Intent.ACTION_USER_REMOVED);
    intentFilter.addAction(Intent.ACTION_USER_PRESENT);
    intentFilter.addAction(Intent.ACTION_SETTING_RESTORED);
    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_USER_UNLOCKED.equals(action)) {
                unlockUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
            } else if (Intent.ACTION_USER_REMOVED.equals(action)) {
                removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
            } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
                // We will update when the automation service dies.
                synchronized (mLock) {
                    UserState userState = getCurrentUserStateLocked();
                    if (!userState.isUiAutomationSuppressingOtherServices()) {
                        if (readConfigurationForUserStateLocked(userState)) {
                            onUserStateChangedLocked(userState);
                        }
                    }
                }
            } else if (Intent.ACTION_SETTING_RESTORED.equals(action)) {
                final String which = intent.getStringExtra(Intent.EXTRA_SETTING_NAME);
                if (Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES.equals(which)) {
                    synchronized (mLock) {
                        restoreEnabledAccessibilityServicesLocked(intent.getStringExtra(Intent.EXTRA_SETTING_PREVIOUS_VALUE), intent.getStringExtra(Intent.EXTRA_SETTING_NEW_VALUE));
                    }
                }
            }
        }
    }, UserHandle.ALL, intentFilter, null, null);
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) AccessibilityService(android.accessibilityservice.AccessibilityService) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) PackageMonitor(com.android.internal.content.PackageMonitor) Point(android.graphics.Point) ComponentName(android.content.ComponentName)

Aggregations

PackageMonitor (com.android.internal.content.PackageMonitor)20 Intent (android.content.Intent)19 BroadcastReceiver (android.content.BroadcastReceiver)15 Context (android.content.Context)15 IntentFilter (android.content.IntentFilter)15 PendingIntent (android.app.PendingIntent)6 ComponentName (android.content.ComponentName)6 Point (android.graphics.Point)6 AccessibilityService (android.accessibilityservice.AccessibilityService)5 ContentProviderOperation (android.content.ContentProviderOperation)4 ContentProviderResult (android.content.ContentProviderResult)4 ContentResolver (android.content.ContentResolver)4 OperationApplicationException (android.content.OperationApplicationException)4 RemoteException (android.os.RemoteException)4 ArrayList (java.util.ArrayList)4