// Buffer service controls
  public String startServerService() {
    try {
      intent.putExtra("port", port);
    } catch (final NumberFormatException e) {
      intent.putExtra("port", 1972);
    }

    try {
      intent.putExtra("nSamples", nSamples);
    } catch (final NumberFormatException e) {
      intent.putExtra("nSamples", 10000);
    }

    try {
      intent.putExtra("nEvents", nEvents);
    } catch (final NumberFormatException e) {
      intent.putExtra("nEvents", 1000);
    }

    Log.i(C.TAG, "Attempting to start Buffer Service");
    ComponentName serviceName = context.startService(intent);
    Log.i(C.TAG, "Managed to start service: " + serviceName);

    String result = "Buffer Service was not found";
    if (serviceName != null) result = serviceName.toString();
    return result;
  }
 private Drawable getActivityIcon(ComponentName componentname)
 {
     Object obj = mContext.getPackageManager();
     ActivityInfo activityinfo;
     int i;
     try
     {
         activityinfo = ((PackageManager) (obj)).getActivityInfo(componentname, 128);
     }
     // Misplaced declaration of an exception variable
     catch (ComponentName componentname)
     {
         componentname.toString();
         return null;
     }
     i = activityinfo.getIconResource();
     if (i == 0)
     {
         return null;
     }
     obj = ((PackageManager) (obj)).getDrawable(componentname.getPackageName(), i, activityinfo.applicationInfo);
     if (obj == null)
     {
         (new StringBuilder()).append("Invalid icon resource ").append(i).append(" for ").append(componentname.flattenToShortString()).toString();
         return null;
     } else
     {
         return ((Drawable) (obj));
     }
 }
 @Override
 public void onServiceConnected(ComponentName className, IBinder binder) {
   Log.d(TAG, className.toString() + " service is bound");
   isServiceBound = true;
   service = ((AbstractGatewayService.AbstractGatewayServiceBinder) binder).getService();
   service.setContext(MainActivity.this);
   Log.d(TAG, "Starting live data");
   try {
     service.startService();
     if (preRequisites)
       btStatusTextView.setText(getString(R.string.status_bluetooth_connected));
   } catch (IOException ioe) {
     Log.e(TAG, "Failure Starting live data");
     btStatusTextView.setText(getString(R.string.status_bluetooth_error_connecting));
     doUnbindService();
   }
 }
  // Interface
  // Clients service controls
  public String startClientsService() {
    try {
      intent.putExtra("port", port);
    } catch (final NumberFormatException e) {
      intent.putExtra("port", 1972);
    }
    // threadInfos = new LinkedHashMap<Integer, ThreadInfo>();
    allArguments = new LinkedHashMap<Integer, Argument[]>();
    threadIDs = new ArrayList<Integer>();

    // Start the client.
    Log.i(TAG, "Attempting to start Clients Service");
    ComponentName serviceName = context.startService(intent);
    Log.i(TAG, "Managed to start service: " + serviceName);

    String result = "Clients Service was not found";
    if (serviceName != null) result = serviceName.toString();
    return result;
  }
  /** Version of registerService that takes the name of a service component to bind to. */
  private void registerService(final ComponentName name, final int userid) {
    if (DEBUG) Slog.v(TAG, "registerService: " + name + " u=" + userid);

    synchronized (mMutex) {
      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 (PackageManager.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);
        if (!mContext.bindServiceAsUser(
            intent,
            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, false /*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);
              }
            },
            Context.BIND_AUTO_CREATE,
            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;
      }
    }
  }
 // This method is *only* called when the connection to the service is lost unexpectedly
 // and *not* when the client unbinds
 // (http://developer.android.com/guide/components/bound-services.html)
 // So the isServiceBound attribute should also be set to false when we unbind from the
 // service.
 @Override
 public void onServiceDisconnected(ComponentName className) {
   Log.d(TAG, className.toString() + " service is unbound");
   isServiceBound = false;
 }