Search in sources :

Example 36 with BroadcastReceiver

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

the class ShadowApplicationTest method canFindAllReceiversForAnIntent.

@Test
public void canFindAllReceiversForAnIntent() throws Exception {
    BroadcastReceiver expectedReceiver = new TestBroadcastReceiver();
    ShadowApplication shadowApplication = shadowOf(RuntimeEnvironment.application);
    assertFalse(shadowApplication.hasReceiverForIntent(new Intent("Foo")));
    RuntimeEnvironment.application.registerReceiver(expectedReceiver, new IntentFilter("Foo"));
    RuntimeEnvironment.application.registerReceiver(expectedReceiver, new IntentFilter("Foo"));
    assertTrue(shadowApplication.getReceiversForIntent(new Intent("Foo")).size() == 2);
}
Also used : IntentFilter(android.content.IntentFilter) TestBroadcastReceiver(org.robolectric.android.TestBroadcastReceiver) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) TestBroadcastReceiver(org.robolectric.android.TestBroadcastReceiver) Test(org.junit.Test)

Example 37 with BroadcastReceiver

use of android.content.BroadcastReceiver in project facebook-android-sdk by facebook.

the class AccessTokenManagerTest method testChangingAccessTokenSendsBroadcast.

@Test
public void testChangingAccessTokenSendsBroadcast() {
    AccessTokenManager accessTokenManager = createAccessTokenManager();
    AccessToken accessToken = createAccessToken();
    accessTokenManager.setCurrentAccessToken(accessToken);
    final Intent[] intents = new Intent[1];
    final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            intents[0] = intent;
        }
    };
    localBroadcastManager.registerReceiver(broadcastReceiver, new IntentFilter(AccessTokenManager.ACTION_CURRENT_ACCESS_TOKEN_CHANGED));
    AccessToken anotherAccessToken = createAccessToken("another string", "1000");
    accessTokenManager.setCurrentAccessToken(anotherAccessToken);
    localBroadcastManager.unregisterReceiver(broadcastReceiver);
    Intent intent = intents[0];
    assertNotNull(intent);
    AccessToken oldAccessToken = intent.getParcelableExtra(AccessTokenManager.EXTRA_OLD_ACCESS_TOKEN);
    AccessToken newAccessToken = intent.getParcelableExtra(AccessTokenManager.EXTRA_NEW_ACCESS_TOKEN);
    assertEquals(accessToken.getToken(), oldAccessToken.getToken());
    assertEquals(anotherAccessToken.getToken(), newAccessToken.getToken());
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 38 with BroadcastReceiver

use of android.content.BroadcastReceiver in project XobotOS by xamarin.

the class LoadedApk method forgetReceiverDispatcher.

public IIntentReceiver forgetReceiverDispatcher(Context context, BroadcastReceiver r) {
    synchronized (mReceivers) {
        HashMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher> map = mReceivers.get(context);
        LoadedApk.ReceiverDispatcher rd = null;
        if (map != null) {
            rd = map.get(r);
            if (rd != null) {
                map.remove(r);
                if (map.size() == 0) {
                    mReceivers.remove(context);
                }
                if (r.getDebugUnregister()) {
                    HashMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher> holder = mUnregisteredReceivers.get(context);
                    if (holder == null) {
                        holder = new HashMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher>();
                        mUnregisteredReceivers.put(context, holder);
                    }
                    RuntimeException ex = new IllegalArgumentException("Originally unregistered here:");
                    ex.fillInStackTrace();
                    rd.setUnregisterLocation(ex);
                    holder.put(r, rd);
                }
                rd.mForgotten = true;
                return rd.getIIntentReceiver();
            }
        }
        HashMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher> holder = mUnregisteredReceivers.get(context);
        if (holder != null) {
            rd = holder.get(r);
            if (rd != null) {
                RuntimeException ex = rd.getUnregisterLocation();
                throw new IllegalArgumentException("Unregistering Receiver " + r + " that was already unregistered", ex);
            }
        }
        if (context == null) {
            throw new IllegalStateException("Unbinding Receiver " + r + " from Context that is no longer in use: " + context);
        } else {
            throw new IllegalArgumentException("Receiver not registered: " + r);
        }
    }
}
Also used : AndroidRuntimeException(android.util.AndroidRuntimeException) BroadcastReceiver(android.content.BroadcastReceiver)

Example 39 with BroadcastReceiver

use of android.content.BroadcastReceiver in project XobotOS by xamarin.

the class ShutdownThread method run.

/**
     * Makes sure we handle the shutdown gracefully.
     * Shuts off power regardless of radio and bluetooth state if the alloted time has passed.
     */
public void run() {
    boolean bluetoothOff;
    boolean radioOff;
    BroadcastReceiver br = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            // We don't allow apps to cancel this, so ignore the result.
            actionDone();
        }
    };
    /*
         * Write a system property in case the system_server reboots before we
         * get to the actual hardware restart. If that happens, we'll retry at
         * the beginning of the SystemServer startup.
         */
    {
        String reason = (mReboot ? "1" : "0") + (mRebootReason != null ? mRebootReason : "");
        SystemProperties.set(SHUTDOWN_ACTION_PROPERTY, reason);
    }
    Log.i(TAG, "Sending shutdown broadcast...");
    // First send the high-level shut down broadcast.
    mActionDone = false;
    mContext.sendOrderedBroadcast(new Intent(Intent.ACTION_SHUTDOWN), null, br, mHandler, 0, null, null);
    final long endTime = SystemClock.elapsedRealtime() + MAX_BROADCAST_TIME;
    synchronized (mActionDoneSync) {
        while (!mActionDone) {
            long delay = endTime - SystemClock.elapsedRealtime();
            if (delay <= 0) {
                Log.w(TAG, "Shutdown broadcast timed out");
                break;
            }
            try {
                mActionDoneSync.wait(delay);
            } catch (InterruptedException e) {
            }
        }
    }
    Log.i(TAG, "Shutting down activity manager...");
    final IActivityManager am = ActivityManagerNative.asInterface(ServiceManager.checkService("activity"));
    if (am != null) {
        try {
            am.shutdown(MAX_BROADCAST_TIME);
        } catch (RemoteException e) {
        }
    }
    final ITelephony phone = ITelephony.Stub.asInterface(ServiceManager.checkService("phone"));
    final IBluetooth bluetooth = IBluetooth.Stub.asInterface(ServiceManager.checkService(BluetoothAdapter.BLUETOOTH_SERVICE));
    final IMountService mount = IMountService.Stub.asInterface(ServiceManager.checkService("mount"));
    try {
        bluetoothOff = bluetooth == null || bluetooth.getBluetoothState() == BluetoothAdapter.STATE_OFF;
        if (!bluetoothOff) {
            Log.w(TAG, "Disabling Bluetooth...");
            // disable but don't persist new state
            bluetooth.disable(false);
        }
    } catch (RemoteException ex) {
        Log.e(TAG, "RemoteException during bluetooth shutdown", ex);
        bluetoothOff = true;
    }
    try {
        radioOff = phone == null || !phone.isRadioOn();
        if (!radioOff) {
            Log.w(TAG, "Turning off radio...");
            phone.setRadio(false);
        }
    } catch (RemoteException ex) {
        Log.e(TAG, "RemoteException during radio shutdown", ex);
        radioOff = true;
    }
    Log.i(TAG, "Waiting for Bluetooth and Radio...");
    // Wait a max of 32 seconds for clean shutdown
    for (int i = 0; i < MAX_NUM_PHONE_STATE_READS; i++) {
        if (!bluetoothOff) {
            try {
                bluetoothOff = bluetooth.getBluetoothState() == BluetoothAdapter.STATE_OFF;
            } catch (RemoteException ex) {
                Log.e(TAG, "RemoteException during bluetooth shutdown", ex);
                bluetoothOff = true;
            }
        }
        if (!radioOff) {
            try {
                radioOff = !phone.isRadioOn();
            } catch (RemoteException ex) {
                Log.e(TAG, "RemoteException during radio shutdown", ex);
                radioOff = true;
            }
        }
        if (radioOff && bluetoothOff) {
            Log.i(TAG, "Radio and Bluetooth shutdown complete.");
            break;
        }
        SystemClock.sleep(PHONE_STATE_POLL_SLEEP_MSEC);
    }
    // Shutdown MountService to ensure media is in a safe state
    IMountShutdownObserver observer = new IMountShutdownObserver.Stub() {

        public void onShutDownComplete(int statusCode) throws RemoteException {
            Log.w(TAG, "Result code " + statusCode + " from MountService.shutdown");
            actionDone();
        }
    };
    Log.i(TAG, "Shutting down MountService");
    // Set initial variables and time out time.
    mActionDone = false;
    final long endShutTime = SystemClock.elapsedRealtime() + MAX_SHUTDOWN_WAIT_TIME;
    synchronized (mActionDoneSync) {
        try {
            if (mount != null) {
                mount.shutdown(observer);
            } else {
                Log.w(TAG, "MountService unavailable for shutdown");
            }
        } catch (Exception e) {
            Log.e(TAG, "Exception during MountService shutdown", e);
        }
        while (!mActionDone) {
            long delay = endShutTime - SystemClock.elapsedRealtime();
            if (delay <= 0) {
                Log.w(TAG, "Shutdown wait timed out");
                break;
            }
            try {
                mActionDoneSync.wait(delay);
            } catch (InterruptedException e) {
            }
        }
    }
    rebootOrShutdown(mReboot, mRebootReason);
}
Also used : Context(android.content.Context) IMountShutdownObserver(android.os.storage.IMountShutdownObserver) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) RemoteException(android.os.RemoteException) IMountService(android.os.storage.IMountService) IBluetooth(android.bluetooth.IBluetooth) RemoteException(android.os.RemoteException) ITelephony(com.android.internal.telephony.ITelephony) IActivityManager(android.app.IActivityManager)

Example 40 with BroadcastReceiver

use of android.content.BroadcastReceiver in project XobotOS by xamarin.

the class WifiWatchdogStateMachine method setupNetworkReceiver.

/**
   *
   */
private void setupNetworkReceiver() {
    mBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
                sendMessage(EVENT_NETWORK_STATE_CHANGE, intent);
            } else if (action.equals(WifiManager.RSSI_CHANGED_ACTION)) {
                obtainMessage(EVENT_RSSI_CHANGE, mNetEventCounter, intent.getIntExtra(WifiManager.EXTRA_NEW_RSSI, -200)).sendToTarget();
            } else if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
                sendMessage(EVENT_SCAN_RESULTS_AVAILABLE);
            } else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
                sendMessage(EVENT_WIFI_RADIO_STATE_CHANGE, intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN));
            }
        }
    };
    mIntentFilter = new IntentFilter();
    mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
    mIntentFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
    mIntentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) BroadcastReceiver(android.content.BroadcastReceiver)

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