1616
1717package com .google .android .test .activity ;
1818
19+ import java .util .ArrayList ;
1920import java .util .List ;
2021
2122import android .app .Activity ;
3132import android .os .IBinder ;
3233import android .os .RemoteException ;
3334import android .os .UserHandle ;
35+ import android .os .UserManager ;
3436import android .graphics .Bitmap ;
3537import android .widget .ImageView ;
3638import android .widget .LinearLayout ;
4143import android .view .MenuItem ;
4244import android .view .View ;
4345import android .content .Context ;
46+ import android .content .pm .UserInfo ;
4447import android .content .res .Configuration ;
4548import android .util .Log ;
4649
@@ -51,6 +54,9 @@ public class ActivityTestMain extends Activity {
5154
5255 ActivityManager mAm ;
5356 Configuration mOverrideConfig ;
57+ int mSecondUser ;
58+
59+ ArrayList <ServiceConnection > mConnections = new ArrayList <ServiceConnection >();
5460
5561 class BroadcastResultReceiver extends BroadcastReceiver {
5662 @ Override
@@ -122,6 +128,15 @@ protected void onCreate(Bundle savedInstanceState) {
122128 applyOverrideConfiguration (mOverrideConfig );
123129 }
124130 }
131+
132+ UserManager um = (UserManager )getSystemService (Context .USER_SERVICE );
133+ List <UserInfo > users = um .getUsers ();
134+ mSecondUser = Integer .MAX_VALUE ;
135+ for (UserInfo ui : users ) {
136+ if (ui .id != 0 && mSecondUser > ui .id ) {
137+ mSecondUser = ui .id ;
138+ }
139+ }
125140 }
126141
127142 @ Override
@@ -148,7 +163,12 @@ public void onServiceDisconnected(ComponentName name) {
148163 Log .i (TAG , "Service disconnected " + name );
149164 }
150165 };
151- bindService (intent , conn , Context .BIND_AUTO_CREATE );
166+ if (bindService (intent , conn , Context .BIND_AUTO_CREATE )) {
167+ mConnections .add (conn );
168+ } else {
169+ Toast .makeText (ActivityTestMain .this , "Failed to bind" ,
170+ Toast .LENGTH_LONG ).show ();
171+ }
152172 return true ;
153173 }
154174 });
@@ -185,15 +205,70 @@ public void onServiceDisconnected(ComponentName name) {
185205 return true ;
186206 }
187207 });
188- menu .add ("Send to user 1!" ).setOnMenuItemClickListener (new MenuItem .OnMenuItemClickListener () {
208+ menu .add ("Send to user 0!" ).setOnMenuItemClickListener (new MenuItem .OnMenuItemClickListener () {
209+ @ Override public boolean onMenuItemClick (MenuItem item ) {
210+ Intent intent = new Intent (ActivityTestMain .this , UserTarget .class );
211+ sendOrderedBroadcastAsUser (intent , new UserHandle (0 ), null ,
212+ new BroadcastResultReceiver (),
213+ null , Activity .RESULT_OK , null , null );
214+ return true ;
215+ }
216+ });
217+ menu .add ("Send to user " + mSecondUser + "!" ).setOnMenuItemClickListener (
218+ new MenuItem .OnMenuItemClickListener () {
189219 @ Override public boolean onMenuItemClick (MenuItem item ) {
190220 Intent intent = new Intent (ActivityTestMain .this , UserTarget .class );
191- sendOrderedBroadcastAsUser (intent , new UserHandle (1 ), null ,
221+ sendOrderedBroadcastAsUser (intent , new UserHandle (mSecondUser ), null ,
192222 new BroadcastResultReceiver (),
193223 null , Activity .RESULT_OK , null , null );
194224 return true ;
195225 }
196226 });
227+ menu .add ("Bind to user 0!" ).setOnMenuItemClickListener (new MenuItem .OnMenuItemClickListener () {
228+ @ Override public boolean onMenuItemClick (MenuItem item ) {
229+ Intent intent = new Intent (ActivityTestMain .this , ServiceUserTarget .class );
230+ ServiceConnection conn = new ServiceConnection () {
231+ @ Override
232+ public void onServiceConnected (ComponentName name , IBinder service ) {
233+ Log .i (TAG , "Service connected " + name + " " + service );
234+ }
235+ @ Override
236+ public void onServiceDisconnected (ComponentName name ) {
237+ Log .i (TAG , "Service disconnected " + name );
238+ }
239+ };
240+ if (bindService (intent , conn , Context .BIND_AUTO_CREATE , 0 )) {
241+ mConnections .add (conn );
242+ } else {
243+ Toast .makeText (ActivityTestMain .this , "Failed to bind" ,
244+ Toast .LENGTH_LONG ).show ();
245+ }
246+ return true ;
247+ }
248+ });
249+ menu .add ("Bind to user " + mSecondUser + "!" ).setOnMenuItemClickListener (
250+ new MenuItem .OnMenuItemClickListener () {
251+ @ Override public boolean onMenuItemClick (MenuItem item ) {
252+ Intent intent = new Intent (ActivityTestMain .this , ServiceUserTarget .class );
253+ ServiceConnection conn = new ServiceConnection () {
254+ @ Override
255+ public void onServiceConnected (ComponentName name , IBinder service ) {
256+ Log .i (TAG , "Service connected " + name + " " + service );
257+ }
258+ @ Override
259+ public void onServiceDisconnected (ComponentName name ) {
260+ Log .i (TAG , "Service disconnected " + name );
261+ }
262+ };
263+ if (bindService (intent , conn , Context .BIND_AUTO_CREATE , mSecondUser )) {
264+ mConnections .add (conn );
265+ } else {
266+ Toast .makeText (ActivityTestMain .this , "Failed to bind" ,
267+ Toast .LENGTH_LONG ).show ();
268+ }
269+ return true ;
270+ }
271+ });
197272 menu .add ("Density!" ).setOnMenuItemClickListener (new MenuItem .OnMenuItemClickListener () {
198273 @ Override public boolean onMenuItemClick (MenuItem item ) {
199274 if (mOverrideConfig == null ) {
@@ -226,6 +301,15 @@ protected void onSaveInstanceState(Bundle outState) {
226301 }
227302 }
228303
304+ @ Override
305+ protected void onStop () {
306+ super .onStop ();
307+ for (ServiceConnection conn : mConnections ) {
308+ unbindService (conn );
309+ }
310+ mConnections .clear ();
311+ }
312+
229313 private View scrollWrap (View view ) {
230314 ScrollView scroller = new ScrollView (this );
231315 scroller .addView (view , new ScrollView .LayoutParams (ScrollView .LayoutParams .MATCH_PARENT ,
0 commit comments