Search in sources :

Example 26 with BroadcastReceiver

use of android.content.BroadcastReceiver in project platform_frameworks_base by android.

the class BluetoothTestUtils method discoverable.

/**
     * Puts the local device into discoverable mode and checks to make sure that the local device
     * is in discoverable mode and that the correct actions were broadcast.
     *
     * @param adapter The BT adapter.
     */
public void discoverable(BluetoothAdapter adapter) {
    if (!adapter.isEnabled()) {
        fail("discoverable() bluetooth not enabled");
    }
    int scanMode = adapter.getScanMode();
    if (scanMode != BluetoothAdapter.SCAN_MODE_CONNECTABLE) {
        return;
    }
    final Semaphore completionSemaphore = new Semaphore(0);
    final BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (!BluetoothAdapter.ACTION_SCAN_MODE_CHANGED.equals(action)) {
                return;
            }
            final int mode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.SCAN_MODE_NONE);
            if (mode == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
                completionSemaphore.release();
            }
        }
    };
    final IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
    mContext.registerReceiver(receiver, filter);
    assertTrue(adapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE));
    boolean success = false;
    try {
        success = completionSemaphore.tryAcquire(DISCOVERABLE_UNDISCOVERABLE_TIMEOUT, TimeUnit.MILLISECONDS);
        writeOutput(String.format("discoverable() completed in 0 ms"));
    } catch (final InterruptedException e) {
    // This should never happen but just in case it does, the test will fail anyway.
    }
    mContext.unregisterReceiver(receiver);
    if (!success) {
        fail(String.format("discoverable() timeout: scanMode=%d (expected %d)", scanMode, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE));
    }
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent) Semaphore(java.util.concurrent.Semaphore) BroadcastReceiver(android.content.BroadcastReceiver)

Example 27 with BroadcastReceiver

use of android.content.BroadcastReceiver in project platform_frameworks_base by android.

the class BluetoothTestUtils method enable.

/**
     * Enables Bluetooth and checks to make sure that Bluetooth was turned on and that the correct
     * actions were broadcast.
     *
     * @param adapter The BT adapter.
     */
public void enable(BluetoothAdapter adapter) {
    writeOutput("Enabling Bluetooth adapter.");
    assertFalse(adapter.isEnabled());
    int btState = adapter.getState();
    final Semaphore completionSemaphore = new Semaphore(0);
    final BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (!BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
                return;
            }
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
            if (state == BluetoothAdapter.STATE_ON) {
                completionSemaphore.release();
            }
        }
    };
    final IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    mContext.registerReceiver(receiver, filter);
    assertTrue(adapter.enable());
    boolean success = false;
    try {
        success = completionSemaphore.tryAcquire(ENABLE_DISABLE_TIMEOUT, TimeUnit.MILLISECONDS);
        writeOutput(String.format("enable() completed in 0 ms"));
    } catch (final InterruptedException e) {
    // This should never happen but just in case it does, the test will fail anyway.
    }
    mContext.unregisterReceiver(receiver);
    if (!success) {
        fail(String.format("enable() timeout: state=%d (expected %d)", btState, BluetoothAdapter.STATE_ON));
    }
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent) Semaphore(java.util.concurrent.Semaphore) BroadcastReceiver(android.content.BroadcastReceiver)

Example 28 with BroadcastReceiver

use of android.content.BroadcastReceiver in project platform_frameworks_base by android.

the class UserManagerTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    mUserManager = UserManager.get(getContext());
    IntentFilter filter = new IntentFilter(Intent.ACTION_USER_REMOVED);
    getContext().registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            synchronized (mUserLock) {
                mUserLock.notifyAll();
            }
        }
    }, filter);
    removeExistingUsers();
    usersToRemove = new ArrayList<>();
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver)

Example 29 with BroadcastReceiver

use of android.content.BroadcastReceiver in project platform_frameworks_base by android.

the class EventConditionProvider method onBootComplete.

@Override
public void onBootComplete() {
    if (DEBUG)
        Slog.d(TAG, "onBootComplete");
    if (mBootComplete)
        return;
    mBootComplete = true;
    final IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
    filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
    mContext.registerReceiver(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            reloadTrackers();
        }
    }, filter);
    reloadTrackers();
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) BroadcastReceiver(android.content.BroadcastReceiver)

Example 30 with BroadcastReceiver

use of android.content.BroadcastReceiver in project platform_frameworks_base by android.

the class TvInputManagerService method registerBroadcastReceivers.

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

        private void buildTvInputList(String[] packages) {
            synchronized (mLock) {
                if (mCurrentUserId == getChangingUserId()) {
                    buildTvInputListLocked(mCurrentUserId, packages);
                    buildTvContentRatingSystemListLocked(mCurrentUserId);
                }
            }
        }

        @Override
        public void onPackageUpdateFinished(String packageName, int uid) {
            if (DEBUG)
                Slog.d(TAG, "onPackageUpdateFinished(packageName=" + packageName + ")");
            // This callback is invoked when the TV input is reinstalled.
            // In this case, isReplacing() always returns true.
            buildTvInputList(new String[] { packageName });
        }

        @Override
        public void onPackagesAvailable(String[] packages) {
            if (DEBUG) {
                Slog.d(TAG, "onPackagesAvailable(packages=" + Arrays.toString(packages) + ")");
            }
            // available.
            if (isReplacing()) {
                buildTvInputList(packages);
            }
        }

        @Override
        public void onPackagesUnavailable(String[] packages) {
            // unavailable.
            if (DEBUG) {
                Slog.d(TAG, "onPackagesUnavailable(packages=" + Arrays.toString(packages) + ")");
            }
            if (isReplacing()) {
                buildTvInputList(packages);
            }
        }

        @Override
        public void onSomePackagesChanged() {
            // the TV inputs.
            if (DEBUG)
                Slog.d(TAG, "onSomePackagesChanged()");
            if (isReplacing()) {
                if (DEBUG)
                    Slog.d(TAG, "Skipped building TV input list due to replacing");
                // methods instead.
                return;
            }
            buildTvInputList(null);
        }

        @Override
        public boolean onPackageChanged(String packageName, int uid, String[] components) {
            // the update can be handled in {@link #onSomePackagesChanged}.
            return true;
        }

        @Override
        public void onPackageRemoved(String packageName, int uid) {
            synchronized (mLock) {
                UserState userState = getOrCreateUserStateLocked(getChangingUserId());
                if (!userState.packageSet.contains(packageName)) {
                    // Not a TV input package.
                    return;
                }
            }
            ArrayList<ContentProviderOperation> operations = new ArrayList<>();
            String selection = TvContract.BaseTvColumns.COLUMN_PACKAGE_NAME + "=?";
            String[] selectionArgs = { packageName };
            operations.add(ContentProviderOperation.newDelete(TvContract.Channels.CONTENT_URI).withSelection(selection, selectionArgs).build());
            operations.add(ContentProviderOperation.newDelete(TvContract.Programs.CONTENT_URI).withSelection(selection, selectionArgs).build());
            operations.add(ContentProviderOperation.newDelete(TvContract.WatchedPrograms.CONTENT_URI).withSelection(selection, selectionArgs).build());
            ContentProviderResult[] results = null;
            try {
                ContentResolver cr = getContentResolverForUser(getChangingUserId());
                results = cr.applyBatch(TvContract.AUTHORITY, operations);
            } catch (RemoteException | OperationApplicationException e) {
                Slog.e(TAG, "error in applyBatch", e);
            }
            if (DEBUG) {
                Slog.d(TAG, "onPackageRemoved(packageName=" + packageName + ", uid=" + uid + ")");
                Slog.d(TAG, "results=" + results);
            }
        }
    };
    monitor.register(mContext, null, UserHandle.ALL, true);
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
    intentFilter.addAction(Intent.ACTION_USER_REMOVED);
    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_REMOVED.equals(action)) {
                removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
            }
        }
    }, UserHandle.ALL, intentFilter, null, null);
}
Also used : Context(android.content.Context) ContentProviderResult(android.content.ContentProviderResult) IntentFilter(android.content.IntentFilter) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) PackageMonitor(com.android.internal.content.PackageMonitor) ContentResolver(android.content.ContentResolver) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Aggregations

BroadcastReceiver (android.content.BroadcastReceiver)381 Intent (android.content.Intent)354 Context (android.content.Context)337 IntentFilter (android.content.IntentFilter)280 PendingIntent (android.app.PendingIntent)91 RemoteException (android.os.RemoteException)26 Test (org.junit.Test)24 Semaphore (java.util.concurrent.Semaphore)20 IOException (java.io.IOException)18 Point (android.graphics.Point)16 PackageMonitor (com.android.internal.content.PackageMonitor)15 ArrayList (java.util.ArrayList)15 AndroidRuntimeException (android.util.AndroidRuntimeException)14 GlobalContext (org.qii.weiciyuan.support.utils.GlobalContext)13 ComponentName (android.content.ComponentName)11 Handler (android.os.Handler)11 ContentObserver (android.database.ContentObserver)9 Bundle (android.os.Bundle)9 ErrnoException (android.system.ErrnoException)9 View (android.view.View)9