use of android.content.ServiceConnection in project materialistic by hidroh.
the class CustomTabsDelegateTest method testBindService.
@Test
public void testBindService() throws RemoteException {
// no chrome installed should not bind service
delegate.bindCustomTabsService(activity);
assertThat(ShadowApplication.getInstance().getBoundServiceConnections()).isEmpty();
// bind service should create connection
RuntimeEnvironment.getRobolectricPackageManager().addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com")), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity"));
RuntimeEnvironment.getRobolectricPackageManager().addResolveInfoForIntent(new Intent("android.support.customtabs.action.CustomTabsService").setPackage("com.android.chrome"), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity"));
delegate.bindCustomTabsService(activity);
List<ServiceConnection> connections = ShadowApplication.getInstance().getBoundServiceConnections();
assertThat(connections).isNotEmpty();
// on service connected should create session and warm up client
verify(service).warmup(anyLong());
assertNotNull(delegate.getSession());
verify(service).newSession(any(ICustomTabsCallback.class));
// may launch url should success
when(service.mayLaunchUrl(any(), any(), any(), any())).thenReturn(true);
assertTrue(delegate.mayLaunchUrl(Uri.parse("http://www.example.com"), null, null));
// on service disconnected should clear session
delegate.unbindCustomTabsService(activity);
assertNull(delegate.getSession());
}
use of android.content.ServiceConnection in project android_frameworks_base by ParanoidAndroid.
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.bindServiceAsUser(intent, conn, Context.BIND_AUTO_CREATE, UserHandle.CURRENT)) {
mScreenshotConnection = conn;
mHandler.postDelayed(mScreenshotTimeout, 10000);
}
}
}
use of android.content.ServiceConnection in project android_frameworks_base by ParanoidAndroid.
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("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.OWNER)) {
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;
}
});
return true;
}
use of android.content.ServiceConnection in project android_frameworks_base by ParanoidAndroid.
the class LoadedApk method forgetServiceDispatcher.
public final IServiceConnection forgetServiceDispatcher(Context context, ServiceConnection c) {
synchronized (mServices) {
HashMap<ServiceConnection, LoadedApk.ServiceDispatcher> map = mServices.get(context);
LoadedApk.ServiceDispatcher sd = null;
if (map != null) {
sd = map.get(c);
if (sd != null) {
map.remove(c);
sd.doForget();
if (map.size() == 0) {
mServices.remove(context);
}
if ((sd.getFlags() & Context.BIND_DEBUG_UNBIND) != 0) {
HashMap<ServiceConnection, LoadedApk.ServiceDispatcher> holder = mUnboundServices.get(context);
if (holder == null) {
holder = new HashMap<ServiceConnection, LoadedApk.ServiceDispatcher>();
mUnboundServices.put(context, holder);
}
RuntimeException ex = new IllegalArgumentException("Originally unbound here:");
ex.fillInStackTrace();
sd.setUnbindLocation(ex);
holder.put(c, sd);
}
return sd.getIServiceConnection();
}
}
HashMap<ServiceConnection, LoadedApk.ServiceDispatcher> holder = mUnboundServices.get(context);
if (holder != null) {
sd = holder.get(c);
if (sd != null) {
RuntimeException ex = sd.getUnbindLocation();
throw new IllegalArgumentException("Unbinding Service " + c + " that was already unbound", ex);
}
}
if (context == null) {
throw new IllegalStateException("Unbinding Service " + c + " from Context that is no longer in use: " + context);
} else {
throw new IllegalArgumentException("Service not registered: " + c);
}
}
}
use of android.content.ServiceConnection in project android_frameworks_base by ParanoidAndroid.
the class KeyChain method bind.
/**
* @hide for reuse by CertInstaller and Settings.
*
* Caller should call unbindService on the result when finished.
*/
public static KeyChainConnection bind(Context context) throws InterruptedException {
if (context == null) {
throw new NullPointerException("context == null");
}
ensureNotOnMainThread(context);
final BlockingQueue<IKeyChainService> q = new LinkedBlockingQueue<IKeyChainService>(1);
ServiceConnection keyChainServiceConnection = new ServiceConnection() {
volatile boolean mConnectedAtLeastOnce = false;
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
if (!mConnectedAtLeastOnce) {
mConnectedAtLeastOnce = true;
try {
q.put(IKeyChainService.Stub.asInterface(service));
} catch (InterruptedException e) {
// will never happen, since the queue starts with one available slot
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
boolean isBound = context.bindService(new Intent(IKeyChainService.class.getName()), keyChainServiceConnection, Context.BIND_AUTO_CREATE);
if (!isBound) {
throw new AssertionError("could not bind to KeyChainService");
}
return new KeyChainConnection(context, keyChainServiceConnection, q.take());
}
Aggregations