Search in sources :

Example 71 with Messenger

use of android.os.Messenger in project android_frameworks_base by DirtyUnicorns.

the class PhoneWindowManager method takeScreenshot.

// Assume this is called from the Handler thread.
private void takeScreenshot(final int screenshotType) {
    synchronized (mScreenshotLock) {
        if (mScreenshotConnection != null) {
            return;
        }
        final ComponentName serviceComponent = new ComponentName(SYSUI_PACKAGE, SYSUI_SCREENSHOT_SERVICE);
        final Intent serviceIntent = new Intent();
        serviceIntent.setComponent(serviceComponent);
        ServiceConnection conn = new ServiceConnection() {

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                synchronized (mScreenshotLock) {
                    if (mScreenshotConnection != this) {
                        return;
                    }
                    Messenger messenger = new Messenger(service);
                    Message msg = Message.obtain(null, screenshotType);
                    final ServiceConnection myConn = this;
                    Handler h = new Handler(mHandler.getLooper()) {

                        @Override
                        public void handleMessage(Message msg) {
                            synchronized (mScreenshotLock) {
                                if (mScreenshotConnection == myConn) {
                                    mContext.unbindService(mScreenshotConnection);
                                    mScreenshotConnection = null;
                                    mHandler.removeCallbacks(mScreenshotTimeout);
                                }
                            }
                        }
                    };
                    msg.replyTo = new Messenger(h);
                    msg.arg1 = msg.arg2 = 0;
                    // Needs delay or else we'll be taking a screenshot of the dialog each time
                    try {
                        Thread.sleep(mScreenshotDelay);
                    } catch (InterruptedException ie) {
                    // Do nothing
                    }
                    if (mStatusBar != null && mStatusBar.isVisibleLw())
                        msg.arg1 = 1;
                    if (mNavigationBar != null && mNavigationBar.isVisibleLw())
                        msg.arg2 = 1;
                    try {
                        messenger.send(msg);
                    } catch (RemoteException e) {
                    }
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                synchronized (mScreenshotLock) {
                    if (mScreenshotConnection != null) {
                        mContext.unbindService(mScreenshotConnection);
                        mScreenshotConnection = null;
                        mHandler.removeCallbacks(mScreenshotTimeout);
                        notifyScreenshotError();
                    }
                }
            }
        };
        if (mContext.bindServiceAsUser(serviceIntent, conn, Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE, UserHandle.CURRENT)) {
            mScreenshotConnection = conn;
            mHandler.postDelayed(mScreenshotTimeout, 10000);
        }
    }
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) Message(android.os.Message) ActionHandler(com.android.internal.utils.du.ActionHandler) Handler(android.os.Handler) DeviceKeyHandler(com.android.internal.os.DeviceKeyHandler) ComponentName(android.content.ComponentName) Intent(android.content.Intent) RecognizerIntent(android.speech.RecognizerIntent) Messenger(android.os.Messenger) RemoteException(android.os.RemoteException)

Example 72 with Messenger

use of android.os.Messenger in project android_frameworks_base by DirtyUnicorns.

the class TestJobService method onStartCommand.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Messenger callback = intent.getParcelableExtra("messenger");
    Message m = Message.obtain();
    m.what = MainActivity.MSG_SERVICE_OBJ;
    m.obj = this;
    try {
        callback.send(m);
    } catch (RemoteException e) {
        Log.e(TAG, "Error passing service object back to activity.");
    }
    return START_NOT_STICKY;
}
Also used : Message(android.os.Message) Messenger(android.os.Messenger) RemoteException(android.os.RemoteException)

Example 73 with Messenger

use of android.os.Messenger in project android_frameworks_base by DirtyUnicorns.

the class MainActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Resources res = getResources();
    defaultColor = getColor(R.color.none_received);
    startJobColor = getColor(R.color.start_received);
    stopJobColor = getColor(R.color.stop_received);
    // Set up UI.
    mShowStartView = (TextView) findViewById(R.id.onstart_textview);
    mShowStopView = (TextView) findViewById(R.id.onstop_textview);
    mParamsTextView = (TextView) findViewById(R.id.task_params);
    mDelayEditText = (EditText) findViewById(R.id.delay_time);
    mDeadlineEditText = (EditText) findViewById(R.id.deadline_time);
    mWiFiConnectivityRadioButton = (RadioButton) findViewById(R.id.checkbox_unmetered);
    mAnyConnectivityRadioButton = (RadioButton) findViewById(R.id.checkbox_any);
    mRequiresChargingCheckBox = (CheckBox) findViewById(R.id.checkbox_charging);
    mRequiresIdleCheckbox = (CheckBox) findViewById(R.id.checkbox_idle);
    mIsPersistedCheckbox = (CheckBox) findViewById(R.id.checkbox_persisted);
    mServiceComponent = new ComponentName(this, TestJobService.class);
    // Start service and provide it a way to communicate with us.
    Intent startServiceIntent = new Intent(this, TestJobService.class);
    startServiceIntent.putExtra("messenger", new Messenger(mHandler));
    startService(startServiceIntent);
}
Also used : TestJobService(com.android.demo.jobSchedulerApp.service.TestJobService) ComponentName(android.content.ComponentName) Intent(android.content.Intent) Resources(android.content.res.Resources) Messenger(android.os.Messenger)

Example 74 with Messenger

use of android.os.Messenger in project android_frameworks_base by DirtyUnicorns.

the class NetworkStatsObserversTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    MockitoAnnotations.initMocks(this);
    mObserverHandlerThread = new IdleableHandlerThread("HandlerThread");
    mObserverHandlerThread.start();
    final Looper observerLooper = mObserverHandlerThread.getLooper();
    mStatsObservers = new NetworkStatsObservers() {

        @Override
        protected Looper getHandlerLooperLocked() {
            return observerLooper;
        }
    };
    mCv = new ConditionVariable();
    mHandler = new LatchedHandler(Looper.getMainLooper(), mCv);
    mMessenger = new Messenger(mHandler);
    mActiveIfaces = new ArrayMap<>();
    mActiveUidIfaces = new ArrayMap<>();
}
Also used : ConditionVariable(android.os.ConditionVariable) Looper(android.os.Looper) LatchedHandler(com.android.server.net.NetworkStatsServiceTest.LatchedHandler) Messenger(android.os.Messenger) IdleableHandlerThread(com.android.server.net.NetworkStatsServiceTest.IdleableHandlerThread)

Example 75 with Messenger

use of android.os.Messenger in project android_frameworks_base by DirtyUnicorns.

the class NetworkStatsServiceTest method testRegisterUsageCallback.

public void testRegisterUsageCallback() throws Exception {
    // pretend that wifi network comes online; service should ask about full
    // network state, and poll any existing interfaces before updating.
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkState(buildWifiState());
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    expectBandwidthControlCheck();
    replay();
    mService.forceUpdateIfaces();
    // verify service has empty history for wifi
    assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
    verifyAndReset();
    String callingPackage = "the.calling.package";
    // very small; should be overriden by framework
    long thresholdInBytes = 1L;
    DataUsageRequest inputRequest = new DataUsageRequest(DataUsageRequest.REQUEST_ID_UNSET, sTemplateWifi, thresholdInBytes);
    // Create a messenger that waits for callback activity
    ConditionVariable cv = new ConditionVariable(false);
    LatchedHandler latchedHandler = new LatchedHandler(Looper.getMainLooper(), cv);
    Messenger messenger = new Messenger(latchedHandler);
    // Allow binder to connect
    IBinder mockBinder = createMock(IBinder.class);
    mockBinder.linkToDeath((IBinder.DeathRecipient) anyObject(), anyInt());
    EasyMock.replay(mockBinder);
    // Force poll
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(buildEmptyStats());
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    replay();
    // Register and verify request and that binder was called
    DataUsageRequest request = mService.registerUsageCallback(callingPackage, inputRequest, messenger, mockBinder);
    assertTrue(request.requestId > 0);
    assertTrue(Objects.equals(sTemplateWifi, request.template));
    // 2 MB
    long minThresholdInBytes = 2 * 1024 * 1024;
    assertEquals(minThresholdInBytes, request.thresholdInBytes);
    // Send dummy message to make sure that any previous message has been handled
    mHandler.sendMessage(mHandler.obtainMessage(-1));
    mHandlerThread.waitForIdle(WAIT_TIMEOUT);
    verifyAndReset();
    // Make sure that the caller binder gets connected
    EasyMock.verify(mockBinder);
    EasyMock.reset(mockBinder);
    // modify some number on wifi, and trigger poll event
    // not enough traffic to call data usage callback
    incrementCurrentTime(HOUR_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    replay();
    forcePollAndWaitForIdle();
    // verify service recorded history
    verifyAndReset();
    assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
    // make sure callback has not being called
    assertEquals(INVALID_TYPE, latchedHandler.mLastMessageType);
    // and bump forward again, with counters going higher. this is
    // important, since it will trigger the data usage callback
    incrementCurrentTime(DAY_IN_MILLIS);
    expectCurrentTime();
    expectDefaultSettings();
    expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).addIfaceValues(TEST_IFACE, 4096000L, 4L, 8192000L, 8L));
    expectNetworkStatsUidDetail(buildEmptyStats());
    expectNetworkStatsPoll();
    replay();
    forcePollAndWaitForIdle();
    // verify service recorded history
    assertNetworkTotal(sTemplateWifi, 4096000L, 4L, 8192000L, 8L, 0);
    verifyAndReset();
    // Wait for the caller to ack receipt of CALLBACK_LIMIT_REACHED
    assertTrue(cv.block(WAIT_TIMEOUT));
    assertEquals(NetworkStatsManager.CALLBACK_LIMIT_REACHED, latchedHandler.mLastMessageType);
    cv.close();
    // Allow binder to disconnect
    expect(mockBinder.unlinkToDeath((IBinder.DeathRecipient) anyObject(), anyInt())).andReturn(true);
    EasyMock.replay(mockBinder);
    // Unregister request
    mService.unregisterUsageRequest(request);
    // Wait for the caller to ack receipt of CALLBACK_RELEASED
    assertTrue(cv.block(WAIT_TIMEOUT));
    assertEquals(NetworkStatsManager.CALLBACK_RELEASED, latchedHandler.mLastMessageType);
    // Make sure that the caller binder gets disconnected
    EasyMock.verify(mockBinder);
}
Also used : ConditionVariable(android.os.ConditionVariable) DataUsageRequest(android.net.DataUsageRequest) IBinder(android.os.IBinder) NetworkStats(android.net.NetworkStats) Messenger(android.os.Messenger)

Aggregations

Messenger (android.os.Messenger)108 RemoteException (android.os.RemoteException)44 Message (android.os.Message)42 Intent (android.content.Intent)38 Handler (android.os.Handler)27 ComponentName (android.content.ComponentName)23 IBinder (android.os.IBinder)23 ServiceConnection (android.content.ServiceConnection)19 DataUsageRequest (android.net.DataUsageRequest)9 Looper (android.os.Looper)9 ConditionVariable (android.os.ConditionVariable)8 AsyncChannel (com.android.internal.util.AsyncChannel)8 PendingIntent (android.app.PendingIntent)7 Bundle (android.os.Bundle)7 HandlerThread (android.os.HandlerThread)7 Binder (android.os.Binder)6 File (java.io.File)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 NetworkTemplate (android.net.NetworkTemplate)5 RecognizerIntent (android.speech.RecognizerIntent)5