Search in sources :

Example 86 with ServiceConnection

use of android.content.ServiceConnection in project android_packages_apps_DSPManager by CyanogenMod.

the class MyAdapter method onResume.

@Override
public void onResume() {
    super.onResume();
    ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            HeadsetService service = ((HeadsetService.LocalBinder) binder).getService();
            String routing = service.getAudioOutputRouting();
            String[] entries = pagerAdapter.getEntries();
            for (int i = 0; i < entries.length; i++) {
                if (routing.equals(entries[i])) {
                    viewPager.setCurrentItem(i);
                    break;
                }
            }
            unbindService(this);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
    Intent serviceIntent = new Intent(this, HeadsetService.class);
    bindService(serviceIntent, connection, 0);
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) HeadsetService(com.bel.android.dspmanager.service.HeadsetService) ComponentName(android.content.ComponentName) Intent(android.content.Intent)

Example 87 with ServiceConnection

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

the class ManagedServices method registerServiceLocked.

private void registerServiceLocked(final ComponentName name, final int userid, final boolean isSystem) {
    if (DEBUG)
        Slog.v(TAG, "registerService: " + name + " u=" + userid);
    final String servicesBindingTag = name.toString() + "/" + userid;
    if (mServicesBinding.contains(servicesBindingTag)) {
        // stop registering this thing already! we're working on it
        return;
    }
    mServicesBinding.add(servicesBindingTag);
    final int N = mServices.size();
    for (int i = N - 1; i >= 0; i--) {
        final ManagedServiceInfo info = mServices.get(i);
        if (name.equals(info.component) && info.userid == userid) {
            // cut old connections
            if (DEBUG)
                Slog.v(TAG, "    disconnecting old " + getCaption() + ": " + info.service);
            removeServiceLocked(i);
            if (info.connection != null) {
                mContext.unbindService(info.connection);
            }
        }
    }
    Intent intent = new Intent(mConfig.serviceInterface);
    intent.setComponent(name);
    intent.putExtra(Intent.EXTRA_CLIENT_LABEL, mConfig.clientLabel);
    final PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mConfig.settingsAction), 0);
    intent.putExtra(Intent.EXTRA_CLIENT_INTENT, pendingIntent);
    ApplicationInfo appInfo = null;
    try {
        appInfo = mContext.getPackageManager().getApplicationInfo(name.getPackageName(), 0);
    } catch (NameNotFoundException e) {
    // Ignore if the package doesn't exist we won't be able to bind to the service.
    }
    final int targetSdkVersion = appInfo != null ? appInfo.targetSdkVersion : Build.VERSION_CODES.BASE;
    try {
        if (DEBUG)
            Slog.v(TAG, "binding: " + intent);
        ServiceConnection serviceConnection = new ServiceConnection() {

            IInterface mService;

            @Override
            public void onServiceConnected(ComponentName name, IBinder binder) {
                boolean added = false;
                ManagedServiceInfo info = null;
                synchronized (mMutex) {
                    mServicesBinding.remove(servicesBindingTag);
                    try {
                        mService = asInterface(binder);
                        info = newServiceInfo(mService, name, userid, isSystem, this, targetSdkVersion);
                        binder.linkToDeath(info, 0);
                        added = mServices.add(info);
                    } catch (RemoteException e) {
                    // already dead
                    }
                }
                if (added) {
                    onServiceAdded(info);
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Slog.v(TAG, getCaption() + " connection lost: " + name);
            }
        };
        if (!mContext.bindServiceAsUser(intent, serviceConnection, BIND_AUTO_CREATE | BIND_FOREGROUND_SERVICE | BIND_ALLOW_WHITELIST_MANAGEMENT, new UserHandle(userid))) {
            mServicesBinding.remove(servicesBindingTag);
            Slog.w(TAG, "Unable to bind " + getCaption() + " service: " + intent);
            return;
        }
    } catch (SecurityException ex) {
        Slog.e(TAG, "Unable to bind " + getCaption() + " service: " + intent, ex);
        return;
    }
}
Also used : ServiceConnection(android.content.ServiceConnection) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ApplicationInfo(android.content.pm.ApplicationInfo) IInterface(android.os.IInterface) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) IBinder(android.os.IBinder) UserHandle(android.os.UserHandle) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) RemoteException(android.os.RemoteException)

Example 88 with ServiceConnection

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

the class NetworkScoreServiceTest method injectProvider.

// "injects" the mock INetworkRecommendationProvider into the NetworkScoreService.
private void injectProvider() {
    final ComponentName componentName = new ComponentName(NEW_SCORER.packageName, NEW_SCORER.recommendationServiceClassName);
    when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(NEW_SCORER);
    when(mContext.bindServiceAsUser(isA(Intent.class), isA(ServiceConnection.class), anyInt(), isA(UserHandle.class))).thenAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            IBinder mockBinder = mock(IBinder.class);
            when(mockBinder.queryLocalInterface(anyString())).thenReturn(mRecommendationProvider);
            invocation.<ServiceConnection>getArgument(1).onServiceConnected(componentName, mockBinder);
            return true;
        }
    });
    mNetworkScoreService.systemRunning();
}
Also used : ServiceConnection(android.content.ServiceConnection) IBinder(android.os.IBinder) InvocationOnMock(org.mockito.invocation.InvocationOnMock) UserHandle(android.os.UserHandle) ComponentName(android.content.ComponentName) Intent(android.content.Intent)

Example 89 with ServiceConnection

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

the class ActivityTestMain method onCreateOptionsMenu.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Animate!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            AlertDialog.Builder builder = new AlertDialog.Builder(ActivityTestMain.this, R.style.SlowDialog);
            builder.setTitle("This is a title");
            builder.show();
            return true;
        }
    });
    menu.add("Bind!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, SingleUserService.class);
            ServiceConnection conn = new ServiceConnection() {

                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.i(TAG, "Service connected " + name + " " + service);
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.i(TAG, "Service disconnected " + name);
                }
            };
            if (bindService(intent, conn, Context.BIND_AUTO_CREATE)) {
                mConnections.add(conn);
            } else {
                Toast.makeText(ActivityTestMain.this, "Failed to bind", Toast.LENGTH_LONG).show();
            }
            return true;
        }
    });
    menu.add("Start!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, SingleUserService.class);
            startService(intent);
            return true;
        }
    });
    menu.add("Rebind Isolated!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, IsolatedService.class);
            ServiceConnection conn = new ServiceConnection() {

                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.i(TAG, "Isolated service connected " + name + " " + service);
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.i(TAG, "Isolated service disconnected " + name);
                }
            };
            if (mIsolatedConnection != null) {
                Log.i(TAG, "Unbinding existing service: " + mIsolatedConnection);
                unbindService(mIsolatedConnection);
                mIsolatedConnection = null;
            }
            Log.i(TAG, "Binding new service: " + conn);
            if (bindService(intent, conn, Context.BIND_AUTO_CREATE)) {
                mIsolatedConnection = conn;
            } else {
                Toast.makeText(ActivityTestMain.this, "Failed to bind", Toast.LENGTH_LONG).show();
            }
            return true;
        }
    });
    menu.add("Send!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, SingleUserReceiver.class);
            sendOrderedBroadcast(intent, null, new BroadcastResultReceiver(), null, Activity.RESULT_OK, null, null);
            return true;
        }
    });
    menu.add("Call!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            ContentProviderClient cpl = getContentResolver().acquireContentProviderClient(SingleUserProvider.AUTHORITY);
            Bundle res = null;
            try {
                res = cpl.call("getuser", null, null);
            } catch (RemoteException e) {
            }
            int user = res != null ? res.getInt("user", -1) : -1;
            Toast.makeText(ActivityTestMain.this, "Provider executed as user " + (user >= 0 ? Integer.toString(user) : "unknown"), Toast.LENGTH_LONG).show();
            cpl.release();
            return true;
        }
    });
    menu.add("Send to user 0!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, UserTarget.class);
            sendOrderedBroadcastAsUser(intent, new UserHandle(0), null, new BroadcastResultReceiver(), null, Activity.RESULT_OK, null, null);
            return true;
        }
    });
    menu.add("Send to user " + mSecondUser + "!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, UserTarget.class);
            sendOrderedBroadcastAsUser(intent, new UserHandle(mSecondUser), null, new BroadcastResultReceiver(), null, Activity.RESULT_OK, null, null);
            return true;
        }
    });
    menu.add("Bind to user 0!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, ServiceUserTarget.class);
            ServiceConnection conn = new ServiceConnection() {

                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.i(TAG, "Service connected " + name + " " + service);
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.i(TAG, "Service disconnected " + name);
                }
            };
            if (bindServiceAsUser(intent, conn, Context.BIND_AUTO_CREATE, UserHandle.SYSTEM)) {
                mConnections.add(conn);
            } else {
                Toast.makeText(ActivityTestMain.this, "Failed to bind", Toast.LENGTH_LONG).show();
            }
            return true;
        }
    });
    menu.add("Bind to user " + mSecondUser + "!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(ActivityTestMain.this, ServiceUserTarget.class);
            ServiceConnection conn = new ServiceConnection() {

                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    Log.i(TAG, "Service connected " + name + " " + service);
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    Log.i(TAG, "Service disconnected " + name);
                }
            };
            if (bindServiceAsUser(intent, conn, Context.BIND_AUTO_CREATE, new UserHandle(mSecondUser))) {
                mConnections.add(conn);
            } else {
                Toast.makeText(ActivityTestMain.this, "Failed to bind", Toast.LENGTH_LONG).show();
            }
            return true;
        }
    });
    menu.add("Density!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            if (mOverrideConfig == null) {
                mOverrideConfig = new Configuration();
            }
            if (mOverrideConfig.densityDpi == Configuration.DENSITY_DPI_UNDEFINED) {
                mOverrideConfig.densityDpi = (getApplicationContext().getResources().getConfiguration().densityDpi * 2) / 3;
            } else {
                mOverrideConfig.densityDpi = Configuration.DENSITY_DPI_UNDEFINED;
            }
            recreate();
            return true;
        }
    });
    menu.add("HashArray").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            ArrayMapTests.run();
            return true;
        }
    });
    menu.add("Add App Recent").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            addAppRecents(1);
            return true;
        }
    });
    menu.add("Add App 10x Recent").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            addAppRecents(10);
            return true;
        }
    });
    menu.add("Exclude!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            setExclude(true);
            return true;
        }
    });
    menu.add("Include!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            setExclude(false);
            return true;
        }
    });
    menu.add("Open Doc").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            ActivityManager.AppTask task = findDocTask();
            if (task == null) {
                Intent intent = new Intent(ActivityTestMain.this, DocActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT | Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS);
                startActivity(intent);
            } else {
                task.moveToFront();
            }
            return true;
        }
    });
    menu.add("Stack Doc").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            ActivityManager.AppTask task = findDocTask();
            if (task != null) {
                ActivityManager.RecentTaskInfo recent = task.getTaskInfo();
                Intent intent = new Intent(ActivityTestMain.this, DocActivity.class);
                if (recent.id >= 0) {
                    // Stack on top.
                    intent.putExtra(DocActivity.LABEL, "Stacked");
                } else {
                    // Start root activity.
                    intent.putExtra(DocActivity.LABEL, "New Root");
                }
                task.startActivity(ActivityTestMain.this, intent, null);
            }
            return true;
        }
    });
    menu.add("Spam!").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            scheduleSpam(false);
            return true;
        }
    });
    menu.add("Track time").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, "We are sharing this with you!");
            ActivityOptions options = ActivityOptions.makeBasic();
            Intent receiveIntent = new Intent(ActivityTestMain.this, TrackTimeReceiver.class);
            receiveIntent.putExtra("something", "yeah, this is us!");
            options.requestUsageTimeReport(PendingIntent.getBroadcast(ActivityTestMain.this, 0, receiveIntent, PendingIntent.FLAG_CANCEL_CURRENT));
            startActivity(Intent.createChooser(intent, "Who do you love?"), options.toBundle());
            return true;
        }
    });
    menu.add("Transaction fail").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.putExtra("gulp", new int[1024 * 1024]);
            startActivity(intent);
            return true;
        }
    });
    menu.add("Spam idle alarm").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            scheduleSpamAlarm(0);
            return true;
        }
    });
    menu.add("Ignore battery optimizations").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Intent intent;
            if (!mPower.isIgnoringBatteryOptimizations(getPackageName())) {
                intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.fromParts("package", getPackageName(), null));
            } else {
                intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
            }
            startActivity(intent);
            return true;
        }
    });
    return true;
}
Also used : AlertDialog(android.app.AlertDialog) ServiceConnection(android.content.ServiceConnection) Configuration(android.content.res.Configuration) IBinder(android.os.IBinder) UserHandle(android.os.UserHandle) ComponentName(android.content.ComponentName) ActivityOptions(android.app.ActivityOptions) Bundle(android.os.Bundle) MenuItem(android.view.MenuItem) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RemoteException(android.os.RemoteException) ContentProviderClient(android.content.ContentProviderClient)

Example 90 with ServiceConnection

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

the class ActivityTestMain method onStop.

@Override
protected void onStop() {
    super.onStop();
    mHandler.removeMessages(MSG_SPAM_ALARM);
    for (ServiceConnection conn : mConnections) {
        unbindService(conn);
    }
    mConnections.clear();
    if (mIsolatedConnection != null) {
        unbindService(mIsolatedConnection);
        mIsolatedConnection = null;
    }
}
Also used : ServiceConnection(android.content.ServiceConnection)

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