Search in sources :

Example 36 with ServiceConnection

use of android.content.ServiceConnection in project cornerstone by Onskreen.

the class PhoneWindowManager method takeScreenshot.

// Assume this is called from the Handler thread.
private void takeScreenshot() {
    synchronized (mScreenshotLock) {
        if (mScreenshotConnection != null) {
            return;
        }
        ComponentName cn = new ComponentName("com.android.systemui", "com.android.systemui.screenshot.TakeScreenshotService");
        Intent intent = new Intent();
        intent.setComponent(cn);
        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, 1);
                    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;
                    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) {
            }
        };
        if (mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE)) {
            mScreenshotConnection = conn;
            mHandler.postDelayed(mScreenshotTimeout, 10000);
        }
    }
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) Message(android.os.Message) Handler(android.os.Handler) ComponentName(android.content.ComponentName) Intent(android.content.Intent) Messenger(android.os.Messenger) RemoteException(android.os.RemoteException)

Example 37 with ServiceConnection

use of android.content.ServiceConnection in project DroidPlugin by DroidPluginTeam.

the class PluginManager method onServiceDisconnected.

@Override
public void onServiceDisconnected(ComponentName componentName) {
    Log.i(TAG, "onServiceDisconnected disconnected!");
    mPluginManager = null;
    Iterator<WeakReference<ServiceConnection>> iterator = sServiceConnection.iterator();
    while (iterator.hasNext()) {
        WeakReference<ServiceConnection> wsc = iterator.next();
        ServiceConnection sc = wsc != null ? wsc.get() : null;
        if (sc != null) {
            sc.onServiceDisconnected(componentName);
        } else {
            iterator.remove();
        }
    }
    //服务连接断开,需要重新连接。
    connectToService();
}
Also used : ServiceConnection(android.content.ServiceConnection) WeakReference(java.lang.ref.WeakReference)

Example 38 with ServiceConnection

use of android.content.ServiceConnection in project AndroidChromium by JackyAndroid.

the class ClientManager method keepAliveForSession.

/** Tries to bind to a client to keep it alive, and returns true for success. */
public synchronized boolean keepAliveForSession(CustomTabsSessionToken session, Intent intent) {
    // service (no calls to this service are made).
    if (intent == null || intent.getComponent() == null)
        return false;
    SessionParams params = mSessionParams.get(session);
    if (params == null)
        return false;
    String packageName = intent.getComponent().getPackageName();
    PackageManager pm = mContext.getApplicationContext().getPackageManager();
    // Only binds to the application associated to this session.
    if (!Arrays.asList(pm.getPackagesForUid(params.uid)).contains(packageName))
        return false;
    Intent serviceIntent = new Intent().setComponent(intent.getComponent());
    // This ServiceConnection doesn't handle disconnects. This is on
    // purpose, as it occurs when the remote process has died. Since the
    // only use of this connection is to keep the application alive,
    // re-connecting would just re-create the process, but the application
    // state has been lost at that point, the callbacks invalidated, etc.
    ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
    boolean ok;
    try {
        ok = mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
    } catch (SecurityException e) {
        return false;
    }
    if (ok)
        params.setKeepAliveConnection(connection);
    return ok;
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) PackageManager(android.content.pm.PackageManager) Intent(android.content.Intent) ComponentName(android.content.ComponentName)

Example 39 with ServiceConnection

use of android.content.ServiceConnection in project wifikeyboard by IvanVolosyuk.

the class WiFiKeyboard method onResume.

@Override
public void onResume() {
    super.onResume();
    //        // FIXME: block default port out of use
    //        ServerSocketChannel ch;
    //        try {
    //          ch = ServerSocketChannel.open();
    //          ch.socket().setReuseAddress(true);
    //          ch.socket().bind(new java.net.InetSocketAddress(7777));
    //          ch = ServerSocketChannel.open();
    //          ch.socket().setReuseAddress(true);
    //          ch.socket().bind(new java.net.InetSocketAddress(1111));
    //        } catch (IOException e1) {
    //          // TODO Auto-generated catch block
    //          e1.printStackTrace();
    //        }
    serviceConnection = new ServiceConnection() {

        //@Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Debug.d("WiFiInputMethod connected to HttpService.");
            try {
                Stub listener = new PortUpdateListener.Stub() {

                    @Override
                    public void portUpdated(int newPort) throws RemoteException {
                        if (newPort != port) {
                            port = newPort;
                            setContentView(createView());
                        }
                    }
                };
                RemoteKeyboard.Stub.asInterface(service).setPortUpdateListener(listener);
            } catch (RemoteException e) {
                throw new RuntimeException("WiFiInputMethod failed to connected to HttpService.", e);
            }
        }

        //@Override
        public void onServiceDisconnected(ComponentName name) {
            Debug.d("WiFiInputMethod disconnected from HttpService.");
        }
    };
    if (this.bindService(new Intent(this, HttpService.class), serviceConnection, BIND_AUTO_CREATE) == false) {
        throw new RuntimeException("failed to connect to HttpService");
    }
    setContentView(createView());
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) Stub(com.volosyukivan.PortUpdateListener.Stub) ComponentName(android.content.ComponentName) Intent(android.content.Intent) Stub(com.volosyukivan.PortUpdateListener.Stub) RemoteException(android.os.RemoteException)

Example 40 with ServiceConnection

use of android.content.ServiceConnection in project wifikeyboard by IvanVolosyuk.

the class WiFiInputMethod method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "wifikeyboard");
    //    Debug.d("WiFiInputMethod started");
    serviceConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName name, IBinder service) {
            //        Debug.d("WiFiInputMethod connected to HttpService.");
            try {
                remoteKeyboard = RemoteKeyboard.Stub.asInterface(service);
                keyboardListener = new RemoteKeyListener.Stub() {

                    @Override
                    public void keyEvent(int code, boolean pressed) throws RemoteException {
                        // Debug.d("got key in WiFiInputMethod");
                        receivedKey(code, pressed);
                    }

                    @Override
                    public void charEvent(int code) throws RemoteException {
                        // Debug.d("got key in WiFiInputMethod");
                        receivedChar(code);
                    }

                    @Override
                    public boolean setText(String text) throws RemoteException {
                        return WiFiInputMethod.this.setText(text);
                    }

                    @Override
                    public String getText() throws RemoteException {
                        return WiFiInputMethod.this.getText();
                    }
                };
                RemoteKeyboard.Stub.asInterface(service).registerKeyListener(keyboardListener);
            } catch (RemoteException e) {
                throw new RuntimeException("WiFiInputMethod failed to connected to HttpService.", e);
            }
        }

        //@Override
        public void onServiceDisconnected(ComponentName name) {
        //        Debug.d("WiFiInputMethod disconnected from HttpService.");
        }
    };
    if (this.bindService(new Intent(this, HttpService.class), serviceConnection, BIND_AUTO_CREATE) == false) {
        throw new RuntimeException("failed to connect to HttpService");
    }
}
Also used : PowerManager(android.os.PowerManager) ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) Stub(com.volosyukivan.RemoteKeyListener.Stub) ComponentName(android.content.ComponentName) Intent(android.content.Intent) RemoteException(android.os.RemoteException)

Aggregations

ServiceConnection (android.content.ServiceConnection)122 ComponentName (android.content.ComponentName)95 Intent (android.content.Intent)95 IBinder (android.os.IBinder)94 RemoteException (android.os.RemoteException)75 PendingIntent (android.app.PendingIntent)46 UserHandle (android.os.UserHandle)23 Handler (android.os.Handler)20 Message (android.os.Message)19 Messenger (android.os.Messenger)19 IRemoteViewsFactory (com.android.internal.widget.IRemoteViewsFactory)12 IInterface (android.os.IInterface)9 ResolveInfo (android.content.pm.ResolveInfo)8 IntentFilter (android.content.IntentFilter)7 Point (android.graphics.Point)7 AndroidRuntimeException (android.util.AndroidRuntimeException)7 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)7 Test (org.junit.Test)7 FilterComparison (android.content.Intent.FilterComparison)6 ReceiverCallNotAllowedException (android.content.ReceiverCallNotAllowedException)6